compare_models.lm: Compare two fitted linear models

View source: R/anova.R

compare_models.lmR Documentation

Compare two fitted linear models

Description

Debiased score test of the null fit.0 against the alternative fit.1. Both are internally refit as Gaussian-family GLMs and the call is dispatched to compare_models.glm.

Usage

## S3 method for class 'lm'
compare_models(fit.0, fit.1, ...)

Arguments

fit.0

The null model as a fitted lm object.

fit.1

The alternative model as a fitted lm object. Must be a supermodel of fit.0; see compare_models.glm.

...

Additional arguments passed to compare_models.glm.

Value

An object of class "dScoreTest": a list whose key elements are the debiased test statistic t.stat and the one-sided p-value p.val (right tail of the standard normal), along with the test-set score residuals, the hunted direction, and the call. It has print, summary and plot methods.

Examples

 set.seed(42)
 n <- 500
 dat <- data.frame(x1 = rnorm(n), x2 = rnorm(n), x3 = rnorm(n))
 dat$x3 <- dat$x3 + (dat$x1 + dat$x2) / 3
 dat$y  <- 1 + dat$x1 + 2 * dat$x3 + rnorm(n)
 fit.0 <- lm(y ~ x1 + x3,        data = dat)
 fit.1 <- lm(y ~ x1 + x2 + x3,   data = dat)
 
 # test fit.0 against fit.1: should not be rejected
 compare_models(fit.0, fit.1)
 anova(fit.0, fit.1)

 # misspecified model: should be rejected
 fit.00 <- lm(y ~ x1 + x2, data = dat)
 compare_models(fit.00, fit.1)
 plot(compare_models(fit.00, fit.1))
 compare_models(fit.00, fit.1, hunt.style="wls")
 anova(fit.00, fit.1)


dScoreTest documentation built on Sept. 3, 2026, 1:06 a.m.