1

I am an R beginner and even newer to lmer and mixed effects modelling.

If I have an lmer model of:

model <- lmer(C ~ Group * Time + (1|ID), REML = FALSE, data=data)

How do I access details of the model in order to plot them?

I know I can plot the residuals against the fitted values using either:

plot(model)

OR

plot(predict(model), resid(model))

But is there a way to access other elements/details of the model? What other model elements can be accessed? I am aware that I can use the function 'summary()' to look up the random and fixed effect. But what about plotting the residuals against other elements of the model e.g. residuals against fixed effects or random effect?

rff234
  • 15
  • 4
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What details do you want specifically? – MrFlick Apr 17 '23 at 13:45
  • Hi MrFlick. Basically, my 'Time' fixed effect in my model is a factor of 7 levels. My residual vs fitted plot looks a bit strange so I would like to plot my residuals against my 'Time' factor in its 7 levels – rff234 Apr 17 '23 at 15:21

3 Answers3

3

When you have doubts like this you shoud check the Vignette of the package, in this case vignette(lme4). Only with a superficial search I found this table that maybe is of use:

Method Description
anova Decomposition of fixed-effects contributions or model comparison.
as.function Function returning profiled deviance or REML criterion.
coef Sum of the random and fixed effects for each level.
confint Confidence intervals on linear mixed-model parameters.
deviance Minus twice maximum log-likelihood. (Use REMLcrit for the REML criterion.)
df.residual Residual degrees of freedom.
drop1 Drop allowable single terms from the model.
extractAIC Generalized Akaike information criterion
fitted Fitted values given conditional modes.
fixef Estimates of the fixed-effects coefficients
formula Mixed-model formula of fitted model.
logLik Maximum log-likelihood.
model.frame Data required to fit the model.
model.matrix Fixed-effects model matrix
ngrps Number of levels in each grouping factor.
nobs Number of observations.
plot Diagnostic plots for mixed-model fits.
predict Various types of predicted values.
print Basic printout of mixed-model objects.
profile Profiled likelihood over various model parameters.
ranef Conditional modes of the random effects.
refit A model (re)fitted to a new set of observations of the response variable.
refitML A model (re)fitted by maximum likelihood.
residuals Various types of residual values.
sigma Residual standard deviation.
simulate Simulated data from a fitted mixed model.
summary Summary of a mixed model.
terms Terms representation of a mixed model.
update An updated model using a revised formula or other arguments.
VarCorr Estimated random-effects variances, standard deviations, and correlations.
vcov Covariance matrix of the fixed-effect estimates.
weights Prior weights used in model fitting.
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
2

Here's one way to plot residuals vs your time factor:

library(broom.mixed)
aa <- augment(model)
boxplot(.resid ~ Time, data = aa)

You could also use the built-in plot method (see help("plot.merMod")):

plot(model, resid(.) ~ Time)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

Yes you can use summary() function just like with any other lm or glm object to get an assessment of the actual model.

Similarly you can treat these objects like you would a dataframe.

The only difference is instead of using the $ you should use the @ symbol.

For example:

lm <- lmer(Petal.Length ~ Sepal.Length * Sepal.Width + (1|Species), data = iris)

lm@frame %>% head()

 Petal.Length Sepal.Length Sepal.Width Species
1          1.4          5.1         3.5  setosa
2          1.4          4.9         3.0  setosa
3          1.3          4.7         3.2  setosa
4          1.5          4.6         3.1  setosa
5          1.4          5.0         3.6  setosa
6          1.7          5.4         3.9  setosa

You can from that point explore deeper in the object.

Hansel Palencia
  • 1,006
  • 9
  • 17
  • 1
    I don't recommend this - elements of S4 objects (accessed via `@`) are generally intended to be used by developers rather than end-users. Accessor functions like `model.frame()` are better (see Míriam Muñoz Lapeira's answer) – Ben Bolker Apr 17 '23 at 14:30
  • Good point, I learned something new! Though in defence on this, sometimes accessing elements is an extremely powerful way of understanding how a model is working. Probably not super relevant to a beginner who is trying to plot some residuals, but useful nonetheless. – Hansel Palencia Apr 18 '23 at 08:58