我想:
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!
1条答案
按热度按时间pqwbnv8z1#
update()
和reformulate()
是你的朋友。假设您已经拟合了模型一次:
设置要尝试的响应和预测变量的名称:
这将给予你一个适合的模型列表;我会让其他人解释如何将它们导出到Word。(如果我这样做,我可能会写一个夸托或RMarkdown文件,循环/打印各种摘要,并将其编译为
.docx
文件,但我猜officer
包也适合这些东西?PS你应该知道
x2*visit
扩展到x2 + x2:visit
,所以x2 + x2*visit
和x2*visit
就R而言是等价的。lme4
之后加载library(lmerTest)
而不是/。lapply(results, summary)
* 或 *library(broom.mixed); lapply(results, tidy, effects = "fixed")
将获得包含p值的摘要anova()
结果,可以在内部for
循环中使用anova()
(或运行单独的for
循环)来比较相应的拟合模型gtsummary
的警告方法表明您需要更新dplyr
的版本;install.packages("dplyr")
应该可以工作。