KevC
KevC

Reputation: 33

With a lmer model, can I extract the fitted values of 'y' for the whole model

I am a novice with R and this is a very basic question. I am using lmer to fit a mixed model to a data frame, as follows:

model1=lmer(Mass~Season + Area + Month + (1|Season:Month), data=Transdata)

and then using ggplot2 to plot the fitted data and various diagnostics. For example, for fitted values:

ggplot(model1, aes(x = Season, y = Mass)) + geom_point()

gives me a plot of Mass per Season, shown separately for each of the 3 different areas and 4 different months. Is there a way in which I can get a single estimate of the mean Mass per Season integrated across the different Areas and Months (i.e. from the fixed effects), and e.g. the SEs for each estimate?

Upvotes: 2

Views: 2620

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226162

Probably the easiest way to do this is

library(emmeans)
emmeans(model1, ~ Season)

I think you could reparameterize/modify the formula so that the parameters corresponded to means per season (rather than the default, which is to fit an intercept corresponding to the first season and then parameterize the model in terms of differences between seasons), but using emmeans is easier.

Upvotes: 3

Robert Long
Robert Long

Reputation: 6812

Is there a way in which I can get a single estimate of the mean Mass per Season integrated across the different Areas and Months (i.e. from the fixed effects), and e.g. the SEs for each estimate?

If I have understood the question properly, surely just the output from summary(model1) will provide this. It will give a separate estimate for each level of Season, apart from the reference level, and each estimate is then the expected difference in Mass for each Season relative to the reference level, keeping the other fixed effects constant, which would seem to answer your question.


Edit: After re-reading the question, the title seems to ask a different question to the body. As for the title:

With a lmer model, can I extract the fitted values of 'y' for the whole model

Yes, you can simply run

fitted(model1)

Upvotes: 1

Related Questions