我花了很多时间试图弄清楚如何生成一个理想的情节,并想知道是否有人可以提供帮助 . 该图用于说明二元响应变量上“时间”和“组”之间的相互作用,“组2”的“时间”和“组”之间的相互作用比“组1”更快 . 以下是使用模拟数据所需的简化版本 . 我已经正确地为每个小组绘制了一个斜率(我认为),但是现在想要在每个斜坡周围绘制一个置信度 .
#simulate 41 individuals, each occurring over a random number of days:
days.each<-sapply(sample(2:40,41,T),function(a)1:a)
thedata<-data.frame(day=unlist(days.each))
#add the individual id in:
thedata$id<-rep(1:41,sapply(days.each,length))
#9 of these individuals are in group 1, and 10 and in group 2:
thedata$group<-ifelse(thedata$id<20,1,2)
#with increasing 'day', the y increases. This relationship is stronger in individuals from group 2:
thedata$y<-(thedata$day*thedata$group)
#Also, some individuals have generally higher 'y' than others:
thedata$y<-thedata$y+rnorm(41,10,3)[thedata$id]
#the y is binomial:
thedata$y<-rbinom(nrow(thedata),1,thedata$y/max(thedata$y))
#change the group and id to class "factor"
thedata$id<-as.factor(thedata$id);thedata$group<-as.factor(thedata$group)
#run the full model:
library(lme4)
model <- glmer(formula=y ~ day*group+(1|id),family=binomial,data=thedata)
#to illustrate the interaction, plot one line for each group:
vals<-predict(model,re.form=NA,type ="response")
plot(y~day,data=thedata,type="n")
lines(sort(vals[thedata$group==1])~sort(thedata$day[thedata$group==1]),col="black")
lines(sort(vals[thedata$group==2])~sort(thedata$day[thedata$group==2]),col="purple")
#now add confidence around these slopes?
任何帮助都会受到大力赞赏
编辑:以前的问题是基于线性混合模型与单个预测器的拟合度 . 这个问题涉及来自具有二项式误差结构的广义线性混合模型的x1的每个值的置信度,二项式误差结构包括两个独立变量以及它们之间的相互作用 . 如果有人能证明如何将类似于Extract prediction band from lme fit中的蓝色乐队的东西添加到我的情节中,那将是非常好的 .