Re-estimates a model using the same formula and design but with a new response vector.

The refit generic allows simulation-based workflows (e.g., parametric bootstrap) where new synthetic responses are drawn from a fitted model and the model is re-fitted with the same design structure.

# S3 method for class 'lm'
refit(object, newresp = NULL, ...)

# S3 method for class 'lme'
refit(object, newresp = NULL, ...)

# S3 method for class 'gls'
refit(object, newresp = NULL, ...)

Arguments

object

A fitted model object. Supported classes include lm, lme (from nlme), and gls (from nlme).

newresp

A numeric vector of new response values of the same length as the original data.

...

Additional arguments (currently ignored).

Value

A new fitted model object of the same class as the original.

Methods

refit.lm

Refits a linear model with the same formula but a new response vector.

refit.lme

Refits a linear mixed-effects model (from nlme) with new response data.

refit.gls

Refits a generalized least squares model (from nlme) with new response data.

Examples

if (require(nlme) && require(lme4)) {
  data(Orthodont, package = "nlme")

  # Fit models
  fit_lm  <- lm(distance ~ age, data = Orthodont)
  fit_gls <- gls(distance ~ age, data = Orthodont)
  fit_lme <- lme(distance ~ age, random = ~ 1 | Subject, data = Orthodont)

  # Simulate new response vectors
  set.seed(123)
  new_y <- rnorm(nrow(Orthodont), mean = mean(Orthodont$distance), sd = sd(Orthodont$distance))

  # Refit models with new response
  refit(fit_lm, newresp = new_y)
  refit(fit_gls, newresp = new_y)
  refit(fit_lme, newresp = new_y)
}
#> Error in UseMethod("refit"): no applicable method for 'refit' applied to an object of class "lm"