I'm working with a growth curve model (generated with lmer) and would like to add confidence intervals to the model plot using geom_ribbon(). (The full R code can be downloaded here and the data set is here.)
#Fit Model
GCA.elogit<-lmer(elog~(ot1+ot2+ot3)*Treatment*Experiment + (ot1|Subject),
control=lmerControl(optimizer="bobyqa"), data=GCA, weights=1/wts, REML=F)
#Add the fitted values to the data frame
GCA2<-data.frame(GCA, GCA_Full=fitted(GCA.elogit))
I got lower and upper bounds for the confidence interval using library(merTools) and the predictInterval() function and added these bounds to the data frame.
#Use Predict to get confidence intervals (upr and lwr), add to new data frame
library(merTools)
prediction<-predictInterval(GCA.elogit, GCA2, level=0.8)
GCA3 <- cbind(GCA2, prediction)
But when I try to use these lower and upper bounds in a model plot with geom_ribbon(), I get some funny behavior. I can use stat_summary() to draw the bounds using dashed lines, but geom_ribbon doesn't fill in this space like it should. Instead, it creates a shaded period with spikes for each of the data points. 1: https://i.stack.imgur.com/ae8Ma.jpg:
#plot model -- Filtered for Example -- Creates jagged lines
mplot2<-filter(GCA3, Experiment == "Blocking", Time <= 10)%>%
ggplot(aes(Time, GCA_Full, group=Treatment, fill=Treatment, color=Treatment))+
stat_summary(aes(y=GCA_Full), fun=mean, geom="line")+ #fit
stat_summary(aes(y=upr), fun=mean, geom="line", linetype="dashed")+ #dashed upper bounds
stat_summary(aes(y=lwr), fun=mean, geom="line", linetype="dashed")+ #dashed lower bounds
geom_ribbon(aes(ymin=lwr, ymax=upr), na.rm=TRUE, color=NA , alpha = .1) + #ribbon should fit between upr and lwr dashed lines
theme_bw(base_size=10)+facet_wrap(~Experiment)
labels<-labs(x="Item", y="Accuracy (elogit)")
mplot2+labels+scale_x_continuous(breaks=seq(0,10, 1))
The funny thing is that if I look at an individual subject (or group by subject like I did below) it works as expected. https://i.stack.imgur.com/2m8mS.jpg
#plot model color -- Filter includes subject -- Ribbon works as expected
mplot2<-filter(GCA3, Experiment == "Blocking", Time <= 10, as.numeric(Subject)<= 1002) %>%
ggplot(aes(Time, GCA_Full, group=Subject, fill=Subject, color=Subject))+
stat_summary(aes(y=GCA_Full), fun=mean, geom="line")+ #fit
stat_summary(aes(y=upr), fun=mean, geom="line", linetype="dashed")+ #dashed upper bounds
stat_summary(aes(y=lwr), fun=mean, geom="line", linetype="dashed")+ #dashed lower bounds
geom_ribbon(aes(ymin=lwr, ymax=upr), na.rm=TRUE, color=NA , alpha = .1) + #ribbon should fit between upr and lwr dashed lines
theme_bw(base_size=10)+facet_wrap(~Experiment)
labels<-labs(x="Item", y="Accuracy (elogit)")
mplot2+labels+scale_x_continuous(breaks=seq(0,10, 1))
I'm at a complete loss here and I feel like I've tried 1000 small fixes. Any help would be greatly appreciated!