r - 在 ggplot 中绘制二项式 GLMER 的随机效应

标签 r ggplot2 glm lme4

我一直在使用ggplot2使用geom_smooth(method="glm")通过连续预测器绘制生存数据(1,0)的二项式拟合,但是我不知道是否可以使用 geom_smooth(method="glmer") 合并随机效果。当我尝试时,我收到以下警告消息:

Warning message: Computation failed in stat_smooth(): No random effects terms specified in formula

是否可以在stat_smooth()中指定特定的随机效果,如果可以,这是如何完成的?

ggplot GLM

下面的示例代码和虚拟数据:

library(ggplot2)
library(lme4)

# simulate dummy dataframe

x <- data.frame(time = c(1, 1, 1, 1, 1, 1,1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                         3, 3, 3, 3, 3, 3, 3, 3, 3,4, 4, 4, 4, 4, 4, 4, 4, 4),
                type = c('a', 'a', 'a', 'b', 'b', 'b','c','c','c','a', 'a', 'a', 
                         'b', 'b', 'b','c','c','c','a', 'a', 'a', 'b', 'b', 'b',
                         'c','c','c','a', 'a', 'a', 'b', 'b', 'b','c','c','c'), 
                randef = c('aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc',
                           'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc', 
                           'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc', 
                           'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'), 
                surv = sample(x = 1:200, size = 36, replace = TRUE), 
                nonsurv= sample(x = 1:200, size = 36, replace = TRUE))


# convert to survival and non survival into individuals following 
https://stackoverflow.com/questions/51519690/convert-cbind-format-for- binomial-glm-in-r-to-a-dataframe-with-individual-rows

x_long <- x %>%
  gather(code, count, surv, nonsurv)

# function to repeat a data.frame
x_df <- function(x, n){
  do.call('rbind', replicate(n, x, simplify = FALSE))
        }

# loop through each row and then rbind together
x_full <- do.call('rbind', 
                  lapply(1:nrow(x_long), 
                         FUN = function(i) x_df(x_long[i,], x_long[i, ]$count)))

# create binary_code
x_full$binary <- as.numeric(x_full$code == 'surv')

### binomial glm with interaction between time and type:
summary(fm2<-glm(binary ~ time*type, data = x_full, family = "binomial"))

### plot glm in ggplot2
ggplot(x_full, 
       aes(x = time, y = as.numeric(x_full$binary), fill= x_full$type)) +
   geom_smooth(method="glm", aes(color = factor(x_full$type)), 
               method.args = list(family = "binomial"))

### add randef to glmer
summary(fm3 <- glmer(binary ~ time * type + (1|randef), data = x_full, family = "binomial"))

### incorporate glmer in ggplot?
ggplot(x_full, aes(x = time, y = as.numeric(x_full$binary), fill= x_full$type)) +
  geom_smooth(method = "glmer", aes(color = factor(x_full$type)), 
              method.args = list(family = "binomial"))

或者,我如何使用预测来解决这个问题并将拟合/误差合并到 ggplot 中?

非常感谢任何帮助!

更新

Daniel 在这里使用 sjPlot 和 ggeffects 提供了一个非常有用的解决方案 here 。我使用下面的预测附加了一个更冗长的解决方案,我一直打算在本周末更新。希望这对其处于同样困境的其他人有用!

newdata <- with(fm3, 
                expand.grid(type=levels(x$type),
                            time = seq(min(x$time), max(x$time), len = 100)))

Xmat <- model.matrix(~ time * type, newdata)
fixest <- fixef(fm3)
fit <- as.vector(fixest %*% t(Xmat))
SE <- sqrt(diag(Xmat %*% vcov(fm3) %*% t(Xmat)))
q <- qt(0.975, df = df.residual(fm3))

linkinv <- binomial()$linkinv
newdata <- cbind(newdata, fit = linkinv(fit), 
                 lower = linkinv(fit - q * SE),
                 upper = linkinv(fit + q * SE))

ggplot(newdata, aes(y=fit, x=time , col=type)) +
  geom_line() +       
  geom_ribbon(aes(ymin=lower, ymax=upper, fill=type), color=NA, alpha=0.4)

最佳答案

我不确定您的更新是否产生了正确的图,因为“回归线”几乎是一条直线,而相关的 CI 与该线不对称。

但是,我认为您可以使用 sjPlot 生成您想要的图。或ggeffects .

plot_model(fm3, type = "pred", terms = c("time", "type"), pred.type = "re")

pr <- ggpredict(fm3, c("time", "type"), type = "re")
plot(pr)

enter image description here

如果您不想根据随机效应来调整预测,只需省略 pred.type 即可。 类型参数:

plot_model(fm3, type = "pred", terms = c("time", "type"))

pr <- ggpredict(fm3, c("time", "type"))
plot(pr)

enter image description here

您还可以根据不同水平的随机效应绘制预测,只需将随机效应项添加到 terms 参数即可:

pr <- ggpredict(fm3, c("time", "type", "randef"))
plot(pr)

enter image description here

...或者反过来:

# NOTE! predictions are almost identical for each random
# effect group, so lines are overlapping!
pr <- ggpredict(fm3, c("time", "randef", "type"))
plot(pr)

enter image description here

您可以找到更多详细信息in this package-vignette .

关于r - 在 ggplot 中绘制二项式 GLMER 的随机效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53255211/

相关文章:

r - 在小节上方注释值(ggplot多面)

r - 修剪 ggplot2 中的第一个和最后一个标签

R:GLMM glmer 与 glmmPQL

r - 将 `glm` 生成的对象作为 R 中的列表查看

r - 使用 ggplot2,什么代码创建由单个单词及其计数组成的条形图?

拨浪鼓安装错误: Invalid root element:

r - 如何使 flextable 适合 word_document 输出中侧边框的宽度? Markdown

r - 为什么这个 CSV 数据与 ggplot2 晶须图复杂化?

r - Shiny : showing one message for all errors

r - 拟合和比较 R 中的多条 sigmoid 曲线