#
NOTE that you should probably not try to match a random effect for a grouping variable with only two levels - this will almost always lead to an estimate of the variance of the random zero effect, which in turn will put your predicted lines directly on top of each other - I switch from gl(2,50) to gl(10,10) ...
RF1<-gl(10,10) d <- data.frame(resp,pred1,pred2,RF1)
The development version of lme4 has a predict() function which makes this a little easier ...
- Predict for the range
pred1 with pred2 equal to its average value, and vice versa. This is all a little smarter than it needs to be, as it generates all the values ββfor both focal predictors and speaks to ggplot at a time ...
()
nd <- with(d, rbind(data.frame(expand.grid(RF1=levels(RF1), pred1=seq(min(pred1),max(pred1),length=51)), pred2=mean(pred2),focus="pred1"), data.frame(expand.grid(RF1=levels(RF1), pred2=seq(min(pred2),max(pred2),length=51)), pred1=mean(pred1),focus="pred2"))) nd$val <- with(nd,pred1[focus=="pred1"],pred2[focus=="pred2"]) pframe <- data.frame(nd,resp=predict(mod,newdata=nd)) library(ggplot2) ggplot(pframe,aes(x=val,y=resp,colour=RF1))+geom_line()+ facet_wrap(~focus,scale="free")
- Alternatively, focusing only on
pred1 and generating predictions for the (small / discrete) range of pred2 values ββ...
()
nd <- with(d, data.frame(expand.grid(RF1=levels(RF1), pred1=seq(min(pred1),max(pred1),length=51), pred2=seq(-20,100,by=40)))) pframe <- data.frame(nd,resp=predict(mod,newdata=nd)) ggplot(pframe,aes(x=pred1,y=resp,colour=RF1))+geom_line()+ facet_wrap(~pred2,nrow=1)
You might want to set scale="free" in the last facet_wrap() ... or use facet_grid(~pred2,labeller=label_both)
For a presentation, you can replace the colour aesthetics with group if all you want to do is to distinguish between groups (i.e. split separate lines), rather than identify them ...