Learn R Programming

stats (version 3.3.3)

expand.model.frame: Add new variables to a model frame

Description

Evaluates new variables as if they had been part of the formula of the specified model. This ensures that the same na.action and subset arguments are applied and allows, for example, x to be recovered for a model using sin(x) as a predictor.

Usage

expand.model.frame(model, extras,
                   envir = environment(formula(model)),
                   na.expand = FALSE)

Arguments

model
a fitted model
extras
one-sided formula or vector of character strings describing new variables to be added
envir
an environment to evaluate things in
na.expand
logical; see below

Value

A data frame.

Details

If na.expand = FALSE then NA values in the extra variables will be passed to the na.action function used in model. This may result in a shorter data frame (with na.omit) or an error (with na.fail). If na.expand = TRUE the returned data frame will have precisely the same rows as model.frame(model), but the columns corresponding to the extra variables may contain NA.

See Also

model.frame, predict

Examples

Run this code
model <- lm(log(Volume) ~ log(Girth) + log(Height), data = trees)
expand.model.frame(model, ~ Girth) # prints data.frame like

dd <- data.frame(x = 1:5, y = rnorm(5), z = c(1,2,NA,4,5))
model <- glm(y ~ x, data = dd, subset = 1:4, na.action = na.omit)
expand.model.frame(model, "z", na.expand = FALSE) # = default
expand.model.frame(model, "z", na.expand = TRUE)

Run the code above in your browser using DataLab