代码来自动化多个glmer模型和比较不同的因变量

tzcvj98z  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(105)

我想:
1.为了自动运行以下3个模型,再次用于yone、ytwo、ythree、zone、ztwo等的不同结果(在模型中,第一变量应该从yone0变为ytwo0,以此类推)。
2)最后,我想将模型摘要和比较导出到Word

我尝试阅读以下内容,但不知道如何根据我的要求获得摘要和修改:Unexpected error when looping the glmer function with the effects package

dat3_long <- structure(list(
  ID = c("1", "1", "1", "1", "2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4"),
  visit = c("0", "1", "3", "4", "0", "1", "3", "4", "0", "1", "3", "4", "0", "1", "3","4"),
  x1 = c("5.6", "1.5", "0.5", NA, "6", NA, NA, NA, "3.4","2.4", "2.5", "1", NA, 0, NA, "3.3"),
  x2 = c("0", "0", "0", "0", "1", "1", "1", "1", "2","2", "2", "2", "1", "1", "1", "1"),
  yone = c("0", NA, NA, NA, "1", "0", "0", "0", "0", "0", "0", "1", 1, NA, NA, "0"),
  yone0 = c("0", 1, NA, NA, "1", "0", "0", "0", "0", "0", "0", "1", NA, NA, 0, "0"),
  ytwo = c("0", NA, 1, NA, "1", "0", "0", "0", "0", "0", "0", "1", NA, 0, NA, "0"),
  ytwo0 = c("0", NA, NA, 1, "1", "0", "0", "0", "0", "0", "0", "1", NA, NA, NA, "0"),
  ythree = c("0", NA, 1, NA, "1", "0", "0", "0", "0", "0", "0", "1", NA, 0, NA, "0"),
  ythree0 = c("0", 1, NA, NA, "1", "0", "0", "0", "0", "0", "0", "1", NA, NA, NA, "0"),
  zone = c("0", NA, NA, 1, "1", "0", "0", "0", "0", "0", "0", "1", NA, 1, NA, "0"),
  zone0 = c("0", NA, NA, NA, "1", "0", "0", "0", "0", "0", "0", "1", NA, NA, 1, "0"),
  ztwo = c("0", NA, 1, NA, "1", "0", "0", "0", "0", "0", "0", "1", NA, NA, NA, "0"),
  ztwo0 = c("0", NA, NA, 1, "1", "0", "0", "0", "0", "0", "0", "1", NA, 1, NA, "0")),
row.names = 2:17, class = "data.frame")

#
#character to factor
names <- c(4:14)
dat3_long[,names] <- lapply(dat3_long[,names] , factor)
str(dat3_long)

# I want 1) to automate following 3 models again for different outcomes from yone, ytwo, ythree, zone, ztwo etc. (in model 1st variab changed from yone0 to ytwo 0 and so on)
#models
#1
yone_1 <- glmer(yone ~ yone0 + visit + x1+ x2 + (1 | ID), data=dat3_long, family = binomial, control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=100000)))
summary(yone_1)
#2 + x2*visit
yone_2 <- glmer(yone ~ yone0 + visit + x1 + x2 + x2*visit +(1 | ID), data=dat3_long, family = binomial, control=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=100000)))
summary(yone_2)
#3 
yone_3 <- glmer(yone ~ yone0 + visit + x1 + x2*visit + (1 | ID), data=dat3_long, family = binomial)#, 
summary(yone_3)

#
anova(yone_1,yone_2)
#
anova(yone_3,yone_1)

#2)lastly I want to export model summary and comparison to word


#Thanking you!
pqwbnv8z

pqwbnv8z1#

update()reformulate()是你的朋友。
假设您已经拟合了模型一次:

base_model <- glmer(yone ~ yone0 + visit + x1+ x2 + (1 | ID), 
                data=dat3_long, family = binomial,  ...)

设置要尝试的响应和预测变量的名称:

responses <- grep("(one|two|three)$", names(dat3_long), value = TRUE)
preds <- c("x2", "x2+x2:visit", "x2:visit")
results <- list()
for (r in responses) {
   for (p in preds) {
      form <- reformulate(response = r,
            c(paste0(r, "0"), "x1", p, "(1 | ID)"))
      results[[paste(p, r, sep = ":")]] <- update(base_model, formula = form)
   }
}

这将给予你一个适合的模型列表;我会让其他人解释如何将它们导出到Word。(如果我这样做,我可能会写一个夸托或RMarkdown文件,循环/打印各种摘要,并将其编译为.docx文件,但我猜officer包也适合这些东西?
PS你应该知道x2*visit扩展到x2 + x2:visit,所以x2 + x2*visitx2*visit就R而言是等价的。

  • 如果你想要包含固定效应的p值的摘要,你应该在加载lme4之后加载library(lmerTest)而不是/。
  • lapply(results, summary) * 或 * library(broom.mixed); lapply(results, tidy, effects = "fixed")将获得包含p值的摘要
  • 要获得anova()结果,可以在内部for循环中使用anova()(或运行单独的for循环)来比较相应的拟合模型
  • 运行gtsummary的警告方法表明您需要更新dplyr的版本;install.packages("dplyr")应该可以工作。

相关问题