# Mixed effect model validation

I fit a mixed effect model to one set of data and now I want to check its performance when applied to another data set. I want to plot a figure of "predicted value" and "observed value" and calculate $R^2$ to evaluate model performance.

Is there any code or command for this in R software?

If you fitted your model using the lme4 library, the documentation (listed under the function plot.merMod) suggests the following:

data(Orthodont,package="nlme")
fm1 <- lmer(distance ~ age + (age|Subject), data=Orthodont)

## standardized residuals versus fitted values by gender
plot(fm1, resid(., scaled=TRUE) ~ fitted(.) | Sex, abline = 0)


plot(fm1) also works with the lme function. If you calculate R^2 as

cor(Orthodont\$age, predict(fm1))^2


keep in mind that it is in fact only a pseudo R^2 as it does not account for the random structure. Alternatively you can calculate marginal and conditional R^2 as described here:

This is the publication that describes it in detail

Nakagawa, S., and H. Schielzeth. 2013. A general and simple method for obtaining R2 from generalized linear mixed-effects models. Methods in Ecology and Evolution 4(2): 133-142.

There is also a package called lmmfit although I haven't used it, yet