Skip to contents

Hypothesis testing on a selected fixed or random effect in a fitted lmer model using the Likelihood Ratio Test. Currently only support models fitted with lme4::lmer() that contain fixed effects and a single random structure (i.e., no nested /, double vertical ||, or multi-level random effects).

Usage

lrt_lmer(model, target, type, data, has_interaction = FALSE, verbose = TRUE)

Arguments

model

A fitted model object from lme4::lmer(). Currently not supporting formula that contains shorthand ., such as y ~ ., or more than one random structure.

target

A character value specifying the variable to be tested. Must match either a fixed effect or a variable within a random effect structure.

type

A character string indicating whether target is a "fixed" or "random" effect.

data

A data frame used to fit model.

has_interaction

A logical value, TRUE or FALSE (the default), indicating whether model contains interaction between fixed effects. If TRUE, all terms containing target will be removed to form the reduced model.

verbose

A logical value, TRUE (the default) or FALSE, controlling whether detailed test summary are printed to the console.

Value

A named list containing key test statistics:

  • logLik: Numeric. The log-likelihood difference between the full and reduced model.

  • df: Integer. The degrees of freedom difference between the two models, representing the number of parameters removed.

  • pvalue: Numeric. The p-value from the Chi-square test.

Methods

The hypothesis test evaluates whether the selected effect provides meaningful variance explanation in the linear mixed-effects model:

  • Null hypothesis H0: The selected variable has no valid effect (i.e., its coefficient is 0 for fixed effects, or its variance component is 0 for random effects)

  • Alternative hypothesis H1: The selected variable significantly contributes to model variability

To test this, the function removes the target variable, refits a suitable, reduced model without it, and compares the fit of the full and reduced models using a Chi-square test based on log-likelihood differences and calculated degrees of freedom (depending on the target type).

Reduced Model

The reduced model is constructed differently based on the type of effect being tested:

  • Fixed effect: The target fixed variable is removed from the original lmer() formula, and the model is refitted.

  • Random intercept: The full model is reconstructed by modifying the original lmer() call to retain only the random intercept (ignoring any random slopes). The reduced model is a standard lm() model containing only the fixed effects, and the likelihood ratio test follows a mixture chi-square distribution for p-value computation.

  • Random slope: The target variable is only removed from the random slope structure. The reduced model remains an lmer() model, and the likelihood ratio test follows a mixture chi-square distribution for p-value computation.

Any lmer() models fitted within this function, including the input model provided by the user, will be refitted with REML = FALSE to ensure accurate hypothesis testing.

Examples

library(lme4)
model <- lmer(math ~ math_old + cltype + (cltype | school_id), data = star)

# Fixed effect testing
fix_result <- lrt_lmer(model, target = "math_old", type = "fixed", data = star)
#> Likelihood Ratio Test for 'math_old' (fixed effect)
#> Full Model: math ~ math_old + cltype + (cltype | school_id)
#> Reduced Model: math ~ cltype + (cltype | school_id)
#> Test Statistics:
#> - Log-Likelihood Difference: 3202.807
#> - Degrees of Freedom: 1 (Difference in estimated parameters)
#> - P-Value: < 2.2e-16 (Chi-square test)
#> Since p = < 2.2e-16, under alpha level of 0.05, we reject the null hypothesis. The test suggests that the fixed effect of math_old is statistically significant and contributes to the model.

fix_result
#> $logLik
#> [1] 3202.807
#> 
#> $df
#> [1] 1
#> 
#> $pvalue
#> [1] 0
#> 

# Random intercept testing
random_result1 <- lrt_lmer(model, target = "school_id", type = "random", data = star)
#> Likelihood Ratio Test for 'school_id' (random intercept effect)
#> Full Model: math ~ math_old + cltype + (1 | school_id)
#> Reduced Model: math ~ math_old + cltype
#> Test Statistics:
#> - Log-Likelihood Difference: 603.7954
#> - Degrees of Freedom: 1 (Difference in estimated parameters)
#> - P-Value: < 2.2e-16 (Chi-square test)
#> Since p = < 2.2e-16, under alpha level of 0.05, we reject the null hypothesis. The test suggests that the random intercept effect of school_id is statistically significant and contributes to the model.

# Random slope testing
random_result2 <- lrt_lmer(model, target = "cltype", type = "random", data = star)
#> Likelihood Ratio Test for 'cltype' (random slope effect)
#> Full Model: math ~ math_old + cltype + (cltype | school_id)
#> Reduced Model: math ~ math_old + cltype + (1 | school_id)
#> Test Statistics:
#> - Log-Likelihood Difference: 326.1249
#> - Degrees of Freedom: 5 (Difference in estimated parameters)
#> - P-Value: < 2.2e-16 (Chi-square test)
#> Since p = < 2.2e-16, under alpha level of 0.05, we reject the null hypothesis. The test suggests that the random slope effect of cltype is statistically significant and contributes to the model.