I am using lme4 package to run a Mixed-Effects Model followed by the predict
function ot obtain fitting lines per invidual level and group level. Yet, I am struggling to get the confidence interval of the fitting line per group level to represent them in a ggplot. My code is as follows:
The data: myData is a data frame containing:
totalVol: numeric vector // Years: numeric vector // TIV: numeric vector // Group: ordered factor with 5 levels:0<1<2<3<4 // subject: factor with 46 levels
#The model
ModelLME <- lmer(totalVol ~ Years*Group + TIV + (1|subject),data=myData)
#New data frame to keep one of the variables fixed for the group prediction lines
newdat = data.frame(totalvol=myData$totalVol, Years=myData$Years, Group=myData$Group, TIV=median(myData$TIV))
#Prediction lines
pred1a <-predict(ModelLME, newdata=newdat, re.form=NA) #predict at group level
pred1b <-predict(ModelLME) #predict at individual level
#Plot
plot <- ggplot(myData, aes(x=Years, y=totalVol)) + facet_grid(~Group) +
geom_line(aes(y=pred1b, group=subject)) +
geom_smooth(aes(y=pred1a), method = "glm", size=1.5, color="orange") + theme_light() + geom_point(size=1, shape=22) +
xlab("- Years") + ylab("Total Volume (mm3)") + theme(strip.text.x = element_text(size = 12, color ="black", face = "bold"), strip.background = element_rect(
color="black", fill="light grey",linetype="solid"))
To add the confidence interval of the group prediction line, I have tried:
myData <- cbind(myData,predictInterval(ModelLME,which="fixed")
#add the interval to the plot
plotCI <- plot + geom_ribbon(aes(Years,ymin=lwr, ymax=upr,group=Group),alpha =.2)
But in this way, I get the confidence interval adjusted to each subject, not to each group:
.
I expected that restricting predictInterval
to fixed effects only would give the interval per group, as occurs in predict
. But my understanding of how these functions work is quite limited, so I am probably missing something. I have also tested the confint
function but this gives a confidence interval per estimate of the model, so not sure if that is useful for the plot.
I will greatly appreciate any advice on how to represent the CI properly or other suggestions on better ways to approach this prediction! Thank you so much in advance!
Diana.
Best Answer
You may want to double-check this is correct... You could extract the means and CI of each group at a reference grid and plot the results. The package
emmeans
should do most of the job.I use here the Orthodont dataset with a model similar to yours:
I haven't done this before but it seems to me that to get the means at points other than the original ones, you can use the argument
at
inref_grid
and proceed in the same way as above. For example, we want age from 0 to 24: