Constructs or extracts a nested model (fit0) from a full model (fit1) using flexible input: a model object, formula, character string, or matrix.

This function is useful for preparing models for comparison, e.g., via likelihood ratio test.

get_nested_model_info(fit1, fit0)

Arguments

fit1

A fitted model object (e.g., from lm, lmer, etc.).

fit0

A nested model specification: a model object, a formula (e.g., ~ . - x), a character vector of term names to remove, or a restriction matrix.

Value

A list with:

formula_large

Formula for fit1.

formula_small

Formula for resolved fit0.

large_model

The full model fit1.

small_model

The nested model fit0.

L

Restriction matrix defining the nested model.

Examples

if (requireNamespace("lme4", quietly = TRUE)) {
  library(lme4)
  data(sleepstudy)
  fit1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
  fit0 <- lmer(Reaction ~ (Days | Subject), sleepstudy)

  get_nested_model_info(fit1, fit0)               # as model object
  get_nested_model_info(fit1, ~ . - Days)         # as formula
  get_nested_model_info(fit1, "Days")             # as string
  ## get_nested_model_info(fit1, c(0, 1))         # numeric (converted to matrix)
}
#> $formula.large
#> Reaction ~ Days + (Days | Subject)
#> 
#> $formula.small
#> Reaction ~ (Days | Subject)
#> 
#> $largeModel
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ Days + (Days | Subject)
#>    Data: sleepstudy
#> REML criterion at convergence: 1743.628
#> Random effects:
#>  Groups   Name        Std.Dev. Corr
#>  Subject  (Intercept) 24.741       
#>           Days         5.922   0.07
#>  Residual             25.592       
#> Number of obs: 180, groups:  Subject, 18
#> Fixed Effects:
#> (Intercept)         Days  
#>      251.41        10.47  
#> 
#> $smallModel
#> Linear mixed model fit by REML ['lmerMod']
#> Formula: Reaction ~ (Days | Subject)
#>    Data: sleepstudy
#> REML criterion at convergence: 1769.845
#> Random effects:
#>  Groups   Name        Std.Dev. Corr 
#>  Subject  (Intercept) 25.53         
#>           Days        11.93    -0.18
#>  Residual             25.59         
#> Number of obs: 180, groups:  Subject, 18
#> Fixed Effects:
#> (Intercept)  
#>       257.8  
#> 
#> $L
#>      [,1] [,2]
#> [1,]    0    1
#>