1

I fit a mixed model using lmer of the lme4 package in R. Using lmerTest::anova I can get the sums of squares for fixed effects I have included in my model. Unfortunately, the total sums of squares (SSTO) and the error sums of squares (SSE) are not provided. Is there a way to obtain these values? I am including a contrived dataset below for checking solutions.

# libraries
library(lme4)
library(lmerTest)

# factors
fixed <- c(rep('a', 4), rep('b', 4))
random <- c('r1', 'r1', 'r2', 'r2', 'r3', 'r3', 'r4', 'r4')

# constructing response variable
fixed_effect <- ifelse(fixed == 'a', 1, 2)
random_effect <- as.numeric(gsub('r', '', random))
error <- rnorm(length(fixed))
y <- fixed_effect + random_effect + error

# modeling    
mod <- lmer(y ~ fixed + (1|random))
anova(mod) # only provides sums of squares for fixed effects
BioBroo
  • 613
  • 1
  • 7
  • 21
  • 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. – MrFlick Mar 11 '21 at 01:10
  • Thanks for the tip—I added one. – BioBroo Mar 11 '21 at 01:31
  • 1
    This is a little harder than you might think because the package can handle very general cases (non-nested, unbalanced) where the sums of squares are not completely partitionable ... https://stats.stackexchange.com/questions/71914/what-does-the-anova-command-do-with-a-lmer-model-object https://stats.stackexchange.com/questions/31118/how-to-estimate-variance-components-with-lmer-for-models-with-random-effects-and – Ben Bolker Mar 11 '21 at 01:38
  • Doesn't `lmerTest` need to calculate MSE for many of the F-tests that it performs though? – BioBroo Mar 11 '21 at 01:49
  • No ............. – Ben Bolker Mar 11 '21 at 02:13
  • I don't know how it would do F-tests of random effects if it didn't. – BioBroo Mar 11 '21 at 02:24
  • Ah, looks like it does LRTs. – BioBroo Mar 11 '21 at 02:26
  • Just to close this up: It does an approximate test. Linear mixed effect models fit using REML and performing tests under REML is complicated, but pretty good approximation exist. One of these types of tests are used for `summary(merMod)` (with `lmerTest` loaded). For `anova(merMod)` the model is refit using non-reml methods and then a test is performed using likelihood ratio. Similar if you use `anova(merMod1, merMod2)`. If you want to know more, it would require reading up on mixed effect model tests. – Oliver Mar 17 '21 at 20:29
  • @Oliver, I wasn't looking for help with any kind of testing. I specifically wanted to obtain SSE for a mixed model I'd fit for an unusual use. – BioBroo Mar 18 '21 at 05:22
  • That was somewhat my point. SSE, MSE, SST etc. are usually used for either model performance or model fit tests. And since the tests on mixed effect models have to either refit under maximum likelihood and use LRT, or alternatively use approximative tests, these are never actually calculated. The fact that one is showing in `anova` is more surprising because "why bother" (it is not used), while the lack of another is understandable :-) – Oliver Mar 18 '21 at 09:46

0 Answers0