# Use and calculation of conditional and marginal R2 in generalized additive mixed models

In Nakagawa et al. the conditional and marginal R2 are defined for linear mixed models to disentangle the ~variance explained by the fixed and random effects .

Nakagawa, S., Johnson, P. C. D., & Schielzeth, H. (2017). The coefficient of determination R2 and intra-class correlation coefficient from generalized linear mixed-effects models revisited and expanded. Journal of The Royal Society Interface, 14(134), 20170213. doi: 10.1098/rsif.2017.0213

When fitting a generalized additive mixed model (GAMM) via the mgcv package in R with normal errors and identity link, the fit is done via the nlme package here. Is it possible to estimate marginal and conditional R2 for this non linear regression in the same way as for lme, e.g. via the r.squaredGLMM() function from the MuMIn package and if yes why?

Update: What if I rewrite my gam model and use polynomials and lme instead, say

lme(Y~ poly(x1,2):fac+poly(x2,2):fac, random=~1|a/b/d)


does this reduce to:

lme(Y~ I(x1^2):fac+I(x1):fac+I(x2^2):fac+I(x2):fac+fac, random=~1|a/b/d)


so that this reduces to a linear mixed model instead?

• Since your question is about a specific function in a package,, why don't you just try it and see what happens? Nov 5, 2021 at 10:41
• Sure, I can calculate them and I know that the calculation works or better said I get estimated from it, but I do not know if those are mathematically meaningul, because I have not a linear mixed model but estimated nonlinear effects in a gamm and so if I can actually calculate the variances of the fixed effects in that way? Nov 10, 2021 at 4:00
• good question ... Nov 10, 2021 at 17:40
• Be careful about $R^2$ when the model is nonlinear. The “proportion of variance explained” interpretation does not apply.
– Dave
Nov 11, 2021 at 23:11
• Can I (in the gaussian error identity link case) calculate : 1- ((deviance(model)/deviance(nullmodel)) where nullmodel is a model including only my random effect structure? Should this give me a deviance explained for my fixed effects at least? Nov 11, 2021 at 23:19

You can use the r.squaredGLMM function in the R package MuMIn as it follows:
r.squaredGLMM(your_GAMM_object$lme)  The R squared for a non-linear function should be the same as that for a linear function. The reason is that it explains the amount of variance explained by the fitted line of best fit. When you have GAM model, this is the same case. You can think of this as the functional summation of each of the spline basis function terms in the model. The combined summation of best fit line can then be analysed using the exact same variance explained principle as a linear function. • thanks for your help,. From GAMM (rdocumentation.org/packages/mgcv/versions/1.8-38/topics/gamm) the wiggly parts of the smooth functions are treated as random effects, so I don´t know how to disentangle the variance of the fixed effects then in accordance to the formula in Nakagawa et al.... Nov 11, 2021 at 23:22 • To disentangle the fixed and random effect R squared, fit the model with fixed effects only variables, calculate the corresponding R squared for this model fit. Then fit the the model with the fixed variable plus random effect variable(s) e.g. y ~ fixed vars + (1 | randomVar), and calculate the corresponding R squared coefficient. The increase in R square is the amount of variance explained by adding the random effects variable(s); this can be converted to a percentage. Similarly for icc @MriRo. Nov 12, 2021 at 21:43 • -1 It is false that$R^2\$ describes the explained variance in nonlinear regression models, of which GAM is one example.