[R-sig-ME] gamm4: predict to reflect random effects?

Mark Miller mark.gr.miller at gmail.com
Tue Oct 7 04:27:34 CEST 2014


Mollie Brooks <mbrooks at ...> writes:

> 
> Dear mixed modelers,
> 
> Can anyone confirm that there is no way to make predictions from a 
gamm4 model including the random effects?
> I assume it is the same issue as with mgcv:gamm as discussed here 
> http://r.789695.n4.nabble.com/mgcv-gamm-predict-to-reflect-random-s-
effects-td3622738.html
> 
> I believe predict.merMod would include the random effects by default, 
but it doesn’t recognize the
> variable names given by gamm4.
> 
> Here is code in case you want to try something
> x <- runif(100)
> fac <- sample(1:20,100,replace=TRUE)
> eta <- x^2*3 + fac/20; fac <- as.factor(fac)
> y <- rpois(100,exp(eta))
> dat=data.frame(x=x, fac=fac, eta=eta, y=y)
> ## fit model and examine it...
> bp <- gamm4(y~s(x),family=poisson,random=~(1|fac), data=dat)
> 
> pred=predict(bp$gam, newdata=dat, type="response")
> pred2=predict(bp$mer, newdata=dat, type="response")
> #Error in eval(expr, envir, enclos) : object 'X' not found
> 
> Is the best alternative still to use gam?
> bp2=gam(y~s(x)+s(fac, bs="re"), method="REML", family=poisson, 
data=dat)
> 
> Thanks,
> Mollie
> 
> ------------------------
> Mollie Brooks, PhD
> Postdoctoral Researcher, Population Ecology Research Group 
http://www.popecol.org
> Institute of Evolutionary Biology & Environmental Studies, University 
of Zürich
> 
> 	[[alternative HTML version deleted]]
> 
> 
> 
> Dear mixed modelers,
> 
> Can anyone confirm that there is no way to make predictions from a 
gamm4 model including the random effects?
> I assume it is the same issue as with mgcv:gamm as discussed here 
> http://r.789695.n4.nabble.com/mgcv-gamm-predict-to-reflect-random-s-
effects-td3622738.html
> 
> I believe predict.merMod would include the random effects by default, 
but it doesn’t recognize the
> variable names given by gamm4.
> 
> Here is code in case you want to try something
> x <- runif(100)
> fac <- sample(1:20,100,replace=TRUE)
> eta <- x^2*3 + fac/20; fac <- as.factor(fac)
> y <- rpois(100,exp(eta))
> dat=data.frame(x=x, fac=fac, eta=eta, y=y)
> ## fit model and examine it...
> bp <- gamm4(y~s(x),family=poisson,random=~(1|fac), data=dat)
> 
> pred=predict(bp$gam, newdata=dat, type="response")
> pred2=predict(bp$mer, newdata=dat, type="response")
> #Error in eval(expr, envir, enclos) : object 'X' not found
> 
> Is the best alternative still to use gam?
> bp2=gam(y~s(x)+s(fac, bs="re"), method="REML", family=poisson, 
data=dat)
> 
> Thanks,
> Mollie
> 
> ------------------------
> Mollie Brooks, PhD
> Postdoctoral Researcher, Population Ecology Research Group 
http://www.popecol.org
> Institute of Evolutionary Biology & Environmental Studies, University 
of Zürich
> 
> 	[[alternative HTML version deleted]]
> 
> 
Hi Mollie, 

I am also in the same position as you (although a couple of months 
behind). Have you found a solution for predicting fixed and random 
effects from GAMMs? I am going to try manually adding the fixed and 
random effects from the gamm4$mer object using model.matrix. If anyone 
has any other suggestions I'd be very keen to know.

Cheers,

Mark



More information about the R-sig-mixed-models mailing list