easystats / easystats/performance

test_vuong for mixed models

Open
#186 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement :boom:
Dominant language
R
Stars
1.2k
Forks
109
Avg merge
6h 34m
Merged PRs (30d)
8

Description

As far as I understand, two things are required for mixed models to work with that test:

  • Individual loglikelihoods: This is very strange. Because the .glm method seems to work natively for .glmer (https://github.com/easystats/insight/blob/master/R/loglikelihood.R#L159), but the .lm method doesn't for lmer. Moreover, I simply cannot find the function definition of logLik to see what it does! I've looked in base R and in lme4, don't know where it's hidden.
x <- lme4::lmer(Sepal.Length ~ Sepal.Width + (1|Species), data = iris)
stats::logLik(x)
#> 'log Lik.' -97.31807 (df=4)

Created on 2021-01-11 by the reprex package (v0.3.0)

This shouldn't be too hard to reimplement to get the non-summed LLs, once we find how logLik is currently implemented.

  • estfun: In vuong test one of the two dependency currently required is sandwich::estfun(), which computes Empirical Estimating Functions of models. From my (overly)quick reading, it seems to be some sort of empirical approximation of something loosely related to loglik that is especially useful in some cases (e.g., for complex models). 🤷

In any case, it seems that for most of cases, estfun are equivalent to the residuals multiplied by the models' matrix:

x <- lm(mpg ~ disp * wt, data=mtcars)

ref <- sandwich::estfun(x)
head(ref)
#>                   (Intercept)       disp         wt    disp:wt
#> Mazda RX4         -1.95292297 -312.46768 -5.1166582 -818.66531
#> Mazda RX4 Wag     -0.77410571 -123.85691 -2.2255539 -356.08863
#> Datsun 710        -3.05824938 -330.29093 -7.0951386 -766.27497
#> Hornet 4 Drive     3.03272404  782.44280  9.7502078 2515.55361
#> Hornet Sportabout  2.75608748  992.19149  9.4809409 3413.13873
#> Valiant            0.06097151   13.71859  0.2109614   47.46632


rez <- insight::get_residuals(x) * model.matrix(x)
head(rez == ref)
#>                   (Intercept) disp   wt disp:wt
#> Mazda RX4                TRUE TRUE TRUE    TRUE
#> Mazda RX4 Wag            TRUE TRUE TRUE    TRUE
#> Datsun 710               TRUE TRUE TRUE    TRUE
#> Hornet 4 Drive           TRUE TRUE TRUE    TRUE
#> Hornet Sportabout        TRUE TRUE TRUE    TRUE
#> Valiant                  TRUE TRUE TRUE    TRUE

Created on 2021-01-11 by the reprex package (v0.3.0)

While pretty straightforward, sandwich doesn't support lme4 models. So one possibility could be to reimplement "estfun", as the code seems pretty simple: https://github.com/cran/sandwich/blob/master/R/estfun.R (and we could probably make it even more concise and robust thanks to the power of insight). This would allow us to drop the sandwich dependency and support more models.

What do you think?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the test_vuong implementation and inspect insight/R/loglikelihood.R, then compare the relevant behavior with sandwich/R/estfun.R for the lme4::lmer example in the issue. Trace how individual loglikelihoods and estimating functions are obtained for existing models. Done means mixed models work with test_vuong and the required dependency behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
analytics, data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.