runehaubo / old-lmerTest

**OLD** lmerTest version - see runehaubo/lmerTestR for the new one
2 stars 0 forks source link

Problems with rand() #4

Open runehaubo opened 6 years ago

runehaubo commented 6 years ago
data("TVbo", package="lmerTest")
> fm <- lmer(Coloursaturation ~ TVset*Picture +
+              (-1 + TVset|Assessor), data=TVbo)
> lmerTest:::rand(fm) # bug
Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:0: unexpected end of input
1: Coloursaturation~-1+TVset - 
   ^

This notation seems to test the entire RE-term despite fm and fm2 being identical:

> fm2 <- lmer(Coloursaturation ~ TVset*Picture +
+              (0 + TVset|Assessor), data=TVbo)
> fm3 <- lm(Coloursaturation ~ TVset*Picture, data=TVbo)
> lmerTest:::rand(fm2)
Analysis of Random effects Table:
               Chi.sq Chi.DF p.value    
TVset:Assessor   64.3      1   1e-15 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> -2*c(logLik(fm2, REML=TRUE) - logLik(fm3, REML=TRUE))
[1] -64.30212
> anova(fm, fm2)
refitting model(s) with ML (instead of REML)
Data: TVbo
Models:
object: Coloursaturation ~ TVset * Picture + (-1 + TVset | Assessor)
..1: Coloursaturation ~ TVset * Picture + (0 + TVset | Assessor)
       Df    AIC    BIC  logLik deviance Chisq Chi Df Pr(>Chisq)
object 19 590.88 652.77 -276.44   552.88                        
..1    19 590.88 652.77 -276.44   552.88     0      0          1

Test is incorrectly labelled:

> fm4 <- lmer(Coloursaturation ~ TVset * Picture + (TVset | Assessor), data=TVbo)
> fm5 <- lmer(Coloursaturation ~ TVset * Picture + (1 | Assessor), data=TVbo)
> anova(fm4, fm5, refit=FALSE)
Data: TVbo
Models:
..1: Coloursaturation ~ TVset * Picture + (1 | Assessor)
object: Coloursaturation ~ TVset * Picture + (TVset | Assessor)
       Df    AIC    BIC  logLik deviance  Chisq Chi Df Pr(>Chisq)    
..1    14 633.22 678.83 -302.61   605.22                             
object 19 598.17 660.06 -280.09   560.17 45.048      5  1.418e-08 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> lmerTest:::rand(fm4) # wrong test!!!!
Analysis of Random effects Table:
               Chi.sq Chi.DF p.value    
TVset:Assessor     45      5   1e-08 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Also note that fm, fm2 and fm4 are just reparameterizations of the same model.

rand() fails to throw an error when models (of which one is lm) are fitted in different datasets due to missing values:

> m <- lmer(Preference ~ sens2 + Homesize + (1 |Consumer:Income), data=carrots)
> lmerTest:::rand(m)
Analysis of Random effects Table:
                Chi.sq Chi.DF p.value    
Consumer:Income   51.9      1   6e-13 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> m2 <- lm(Preference ~ sens2 + Homesize, data=carrots)
> nrow(model.frame(m))
[1] 1161
> nrow(model.frame(m2))
[1] 1233

Unable to handle models without parentheses around random terms:

> fm <- lmer(Reaction ~ 1 + Days|Subject, sleepstudy)
> lmerTest:::rand(fm) ## Bug in lmerTest::rand
Error: Invalid grouping factor specification, Subject - (1 + Days | Subject) + (1 | Subject)
In addition: There were 12 warnings (use warnings() to see them)