Home > Software design >  How to get a Rquared and F-ratio for a mixed effects model in R?
How to get a Rquared and F-ratio for a mixed effects model in R?

Time:06-29

Data fits a mixed effects model with nested random effects. How do I get the r-square and F-ratio for this model?

set.seed(111)
df <- data.frame(level = rep(c("A","B"), times = 8),
                 time = rep(c("1","2","3","4"), each = 4),
                 x1 =  rnorm(16,3,1),
                 x2 = rnorm(16,3,1))

mod <- lmer(x1 ~ x2   I(x2^2)   (1|time/level), df)
summary(mod)

Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest'] Formula: x1 ~ x2   I(x2^2)   (1 | time/level)    Data: df

REML criterion at convergence: 47.9

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-1.72702 -0.41979  0.00653  0.43709  2.36393 

Random effects:  Groups     Name        Variance Std.Dev.  level:time (Intercept) 0.00     0.00      time       (Intercept) 0.00     0.00    Residual               1.02     1.01     Number of obs: 16, groups:  level:time, 8; time, 4

Fixed effects:
            Estimate Std. Error       df t value Pr(>|t|)     (Intercept)  3.58299    0.81911 13.00000   4.374 0.000753 *** x2      
-0.59777    0.54562 13.00000  -1.096 0.293147     I(x2^2)      0.07686    0.09356 13.00000   0.822 0.426136    
--- Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
        (Intr) x2     x2      -0.868        I(x2^2)  0.660 -0.928 optimizer (nloptwrap) convergence code: 0 (OK) boundary (singular) fit: see ?isSingular

CodePudding user response:

For the R-squared you can use the r.squaredLR function from the MuMIn package:

library(MuMIn)
r.squaredLR(mod)

Output:

[1] 0.1017782
attr(,"adj.r.squared")
[1] 0.1086741

For the F-ratio, maybe you want this:

anova(mod)

Output:

Analysis of Variance Table
        npar  Sum Sq Mean Sq F value
x2         1 0.81407 0.81407  0.7981
I(x2^2)    1 0.68850 0.68850  0.6750
  • Related