I am trying to visualize the results from a glmm that I ran with the lme4 package.

M8<-glmer(abundance~ Mom+ Mom*settlment2 + (1|Pop) + (1|obs), family=poisson, data=glm)

My response variable are count data, therefore I have chosen a poisson distribution. Also, my data are overdispersed, so I have added in observation as a random effect to help account for this. However, my data still seem to be violating independence, which is why I am using a generalised linear mixed model. However, the built-in R predict function breaks down for lmer type objects, so I using the predictSE.mer command from the package (AICcmodavg).

plot(abundance, settlment2, xlab="settlment", ylab="abundance", data=glm) #plot my raw data
MyData<-data.frame(settlment2=seq(from=0, to = 350, by=15.3), glm$Mom) #make a new data frame 
G<-predictSE.mer(M8, newdata=MyData, type="link", se.fit=TRUE, level=0, print.matrix=FALSE) #now to calculate fitted means
lines(MyData$settlment2, F, lty=1)

plot with glmer model This seems to work somewhat well, but I am having trouble producing data.frames correctly as I increase the number of fixed factors and interactions.

So, I guess what I am looking for is a way to do this in ggplot2. It works well with lme objects from nlme but not glmer objects. For example using lme:

m11<-lme(abundance~settlment2*Pop + Pop + settlment2, random=~1|Mom, data=glm,  method="ML", na.action=na.exclude)

g0 <-ggplot(glm,aes(x=settlment2,y=abundance,colour=Pop))+geom_point(aes(group=Pop),alpha=0.3)+

g0 + geom_line(data=transform(glm,abundance=predict(m11)), lty=1)

plot from lme model Essentially, what I would like to do is use ggplot2 so that I can break up the results like the second graph, into my two populations (Pop) but using the code above for glmer and not with lme.

I have been reading Mixed Effects Models and Extension in Ecology in R (Zuur et al.) trying to reproduce their deer data (pg. 326) or (pg. 329) but instead of probabilities on the Y-axis, I would like just predicted values. I have looked into the function plotLMER.fnc in the package LMERConvienienceFunctions as well, but I would really like to know if this is possible to do in ggplot2 as I am much more familiar with that package.

Thanks in advance for the advice!

  • $\begingroup$ I don't follow the logic. You say "I am having trouble producing data frames correctly" and then continue "I am looking tor a way to do this in gig plot". Seems there are two questions here. The first is "how do I make the data frame" and the second is "how do I do this in ggplot". $\endgroup$ – David LeBauer Jun 3 '14 at 4:42
  • $\begingroup$ What happens when you just change m11 to a glmer instead of lme model? I'm rather new to these functions, but I know predict works with glmer too, so I don't understand why this isn't as straightforward as it seems. Your code seems to work with my data (though I don't have a discrete grouping factor)... $\endgroup$ – Nick Stauner Jun 3 '14 at 4:46
  • $\begingroup$ Re David: My issues with producing data frames is the reason I would like to switch to ggplot2 away from the first section of script. $\endgroup$ – LiveLongandProsper Jun 3 '14 at 6:30
  • 1
    $\begingroup$ A reproducible example would be very helpful. The predict() method should work just fine in recent versions (>1.0) of lme4 ... you might find something useful in rpubs.com/bbolker/glmmchapter $\endgroup$ – Ben Bolker Jun 3 '14 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.