[R] How to plot 3-D surface graph from lmer mixed models?

Duncan Murdoch murdoch at stats.uwo.ca
Thu Aug 13 14:00:01 CEST 2009

willow1980 wrote:
> Dear R users,
> I have a problem in plotting 3 dimensional graph using mixed models. 
> My model is
> sur_prop ~
> afr_c+I(afr_c^2)+I(afr_c^3)+byear_c+I(byear_c^2)+I(byear_c^3)+I(byear_c^4)+(1|Studyparish)+afr_c:byear_c
> +afr_c:I(byear_c^2)+afr_c:I(byear_c^3)+afr_c:I(byear_c^4)+I(afr_c^2):byear_c+I(afr_c^2):I(byear_c^2)+I(afr_c^2):I(byear_c^3)+I(afr_c^2):I(byear_c^4) 
> This is a study on the effect of mothers' age and cohort year on children's
> survival.
> I  can extract predicted value using a suggested method from website:
> model at X%*%fixef(model). To my knowledge, this method is reasonable. It is a
> bit alike matrix representation in multiple regression.
> However, I cannot use such predicted values together with age and cohort
> values to plot a 3-D surface plot. Since my dataset is very large, it is
> certainly useless to plot 3-D points scatter plot, which is very difficult
> to discern the pattern. By the way, I have tried "plot3d", "scatterplot" and
> "regr2.plot" to give scatter plot. 
> So, do you have any suggestion on how to plot a surface plot given above
> information? For example, how to use "persp" or "wireframe" in case of lmer
> with more than two explanatory variables?
> Thank you very much for helping!

I only see two explanatory variables:  afr_c, byear_c.  If you have more 
than two, you can't use a surface plot:  surfaces are two dimensional.

All of the 3D surface functions want basically the same thing:  a matrix 
giving evaluations of a function at locations of the explanatory 
variables.  So you need to calculate that.  I'd do it by using 
expand.grid to create a large set of combinations of values of the 
explanatory variables, ask your model to do predictions at all of those 
locations, and then reshape the result into a matrix.

Duncan Murdoch

More information about the R-help mailing list