Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

refit

Refit a (merMod) Model with a Different Response


Description

Refit a model, possibly after modifying the response vector. This makes use of the model representation and directly goes to the optimization.

Usage

refit(object, newresp, ...)

## S3 method for class 'merMod'
refit(object, newresp = NULL, rename.response = FALSE,
      maxit = 100, ...)

Arguments

object

a fitted model, usually of class lmerMod, to be refit with a new response.

newresp

an (optional) numeric vector providing the new response, of the same length as the original response (see Details for information on NA handling). May also be a data frame with a single numeric column, e.g. as produced by simulate(object).

rename.response

when refitting the model, should the name of the response variable in the formula and model frame be replaced with the name of newresp?

maxit

scalar integer, currently only for GLMMs: the maximal number of Pwrss update iterations.

...

optional additional parameters. For the merMod method, control.

Details

Refit a model, possibly after modifying the response vector. This could be done using update(), but the refit() approach should be faster because it bypasses the creation of the model representation and goes directly to the optimization step.

Setting rename.response = TRUE may be necessary if one wants to do further operations (such as update) on the fitted model. However, the refitted model will still be slightly different from the equivalent model fitted via update; in particular, the terms component is not updated to reflect the new response variable, if it has a different name from the original.

If newresp has an na.action attribute, then it is assumed that NA values have already been removed from the numeric vector; this allows the results of simulate(object) to be used even if the original response vector contained NA values. Otherwise, the length of newresp must be the same as the original length of the response.

Value

an object like x, but fit to a different response vector Y.

See Also

update.merMod for more flexible and extensive model refitting; refitML for refitting a REML fitted model with maximum likelihood (‘ML’).

Examples

## Ex. 1: using refit() to fit each column in a matrix of responses -------
set.seed(101)
Y <- matrix(rnorm(1000),ncol=10)
## combine first column of responses with predictor variables
d <- data.frame(y=Y[,1],x=rnorm(100),f=rep(1:10,10))
## (use check.conv.grad="ignore" to disable convergence checks because we
##  are using a fake example)
## fit first response
fit1 <- lmer(y ~ x+(1|f), data = d,
             control= lmerControl(check.conv.grad="ignore",
                                  check.conv.hess="ignore"))
## combine fit to first response with fits to remaining responses
res <- c(fit1,lapply(as.data.frame(Y[,-1]), refit, object=fit1))

## Ex. 2: refitting simulated data using data that contain NA values ------
sleepstudyNA <- sleepstudy
sleepstudyNA$Reaction[1:3] <- NA
fm0 <- lmer(Reaction ~ Days + (1|Subject), sleepstudyNA)
## the special case of refitting with a single simulation works ...
ss0 <- refit(fm0, simulate(fm0))
## ... but if simulating multiple responses (for efficiency),
## need to use na.action=na.exclude in order to have proper length of data
fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudyNA, na.action=na.exclude)
ss <- simulate(fm1, 5)
res2 <- refit(fm1, ss[[5]])

lme4

Linear Mixed-Effects Models using 'Eigen' and S4

v1.1-26
GPL (>= 2)
Authors
Douglas Bates [aut] (<https://orcid.org/0000-0001-8316-9503>), Martin Maechler [aut] (<https://orcid.org/0000-0002-8685-9910>), Ben Bolker [aut, cre] (<https://orcid.org/0000-0002-2127-0443>), Steven Walker [aut] (<https://orcid.org/0000-0002-4394-9078>), Rune Haubo Bojesen Christensen [ctb] (<https://orcid.org/0000-0002-4494-3399>), Henrik Singmann [ctb] (<https://orcid.org/0000-0002-4842-3657>), Bin Dai [ctb], Fabian Scheipl [ctb] (<https://orcid.org/0000-0001-8172-3603>), Gabor Grothendieck [ctb], Peter Green [ctb] (<https://orcid.org/0000-0002-0238-9852>), John Fox [ctb], Alexander Bauer [ctb], Pavel N. Krivitsky [ctb, cph] (<https://orcid.org/0000-0002-9101-3362>, shared copyright on simulate.formula)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.