I would like to compute the marginal R squared for the whole factors from an anova table. This post shows how to get the effect size for each fixed effect based on the r2glmm package but it does not seem to work to get the whole factor.
Here is an example:
library(lmerTest)
library(r2glmm)
set.seed(1)
df <- data.frame(id = rep(1:3, 5),
score = sample(15),
gender = rep(c("male", "female", "other"), 5),
age = rep(c("teenager", "adult", "old"), each = 5))
mdl <- lmer(score ~ gender + age + (1|id), data = df)
df
id score gender age
1 1 9 male teenager
2 2 4 female teenager
3 3 7 other teenager
4 1 1 male teenager
5 2 2 female teenager
6 3 13 other adult
7 1 11 male adult
8 2 3 female adult
9 3 8 other adult
10 1 12 male adult
11 2 5 female old
12 3 6 other old
13 1 15 male old
14 2 10 female old
15 3 14 other old
Compute the anova table:
anova(mdl)
# Type III Analysis of Variance Table with Satterthwaite's method
# Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
# gender 24.75 12.375 2 10 1.0081 0.39916
# age 80.45 40.225 2 10 3.2770 0.08044 .
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I would like to compute the effect sizes for gender and age factors from the above anova table.
I tried:
r2beta(mdl, partial = TRUE)
# Effect Rsq upper.CL lower.CL
# 1 Model 0.407 0.761 0.172
# 2 gendermale 0.215 0.601 0.003
# 3 genderother 0.143 0.537 0.001
# 5 ageteenager 0.143 0.537 0.001
# 4 ageold 0.027 0.377 0.000
But none is giving me what I am looking for.
Is there any solution?
Edit
So with the development version from GitHub it does compute the effect sizes for the whole factor:
r2beta(mdl, partial = TRUE)
# Effect Rsq upper.CL lower.CL
# 1 Model 0.407 0.761 0.172
# 3 age 0.260 0.656 0.034
# 2 gender 0.233 0.638 0.026
The problem now is that I need both: for the whole factor and for the break down with dummy codes (or any other code). Is there a way to achieve this or do I need to manually create dummy codes? it would be nice to have an option in the function to specify which output we wish.
library(lmerTest)instead oflibrary(lmer)anddata = dfinstead ofdata = DTin your secondlmer()call. If so, I do get the effect sizes for the (whole) factorsgenderandagewith the latestr2glmmversion from GitHub. What happens if you run restartR, then runremove.packages(c("lmerTest", "r2glmm")), theninstall.packages("lmerTest"); devtools::install_github("bcjaeger/r2glmm"), and then execute your code with the adaptions I mentioned? – statmerkur May 12 '22 at 15:38