Model Formula Objects


These are objects of class formula. This class of objects represents the structural models in all model-fitting functions and is also used in other functions, particularly for plots.


Formulas are their own value. That is, they represent an expression calling the operator ~, but evaluating this expression just returns the expression itself. The purpose of formula objects is to supply the essential information to fit models, produce plots, and so on, in a readable form that can be passed around, stored in other objects, and manipulated to determine the terms and response of a model.
Names in the formula eventually must be interpreted as objects, often as variables in a data frame. However, this interpretation takes place only when the related subexpressions have been removed from the formula object.
Operators in formulas have the same precedence as anywhere else in the engine. Operators that are especially significant in formulas are: The following functions are also used:
These objects are created by a call to the ~ operator. Typically, this call is used inside a call to a model fitting function, but need not be.
Generic functions that have methods specific to "formula" include:
A formula is a call (that is, of mode "call") to the ~ operator. It is considered as a list and usually has three components: It is possible to build a formula without a response, in which case the formula must be processed before being used in the typical fashion.
See Also
terms.object, poly.
##### General rules for formulas 
# T ~ F      T is modeled as F, where F may include other terms 
# Fa + Fb    Include both Fa and Fb in the model 
# Fa - Fb    Include all of Fa except what is in Fb in the model 
# Fa : Fb    The interaction between Fa and Fb 
# Fa * Fb    Fa + Fb + Fa : Fb 
# Fb %in% Fa Fb is nested within Fa 
# Fa / Fb    Fa + Fb %in% Fa 
# F^m        All terms in F crossed to order m 
#            in update(), the previous set, 
#            in other formulas, all variables (except those on the 
#            other side of ~) 
# In these rules, F may be the name of a variable, 
# an expression such as bs(x, df=4) that returns a variable or matrix, 
# or a formula subexpression built using the same syntax. 

# Examples data(fuel.frame, package="Sdatasets") lm(Fuel ~ Weight, data=fuel.frame) lm(Fuel ~ Weight + Disp., data=fuel.frame)

# no intercept lm(Fuel ~ Weight - 1, data=fuel.frame)

# Type is a factor; this gives a single slope,

# and separate intercepts for each level of Type lm(Fuel ~ Weight + Type, data=fuel.frame)

# Separate intercepts and slopes for each level lm(Fuel ~ Weight * Type, data=fuel.frame)

# equivalent to previous lm(Fuel ~ Weight + Type + Weight:Type, data=fuel.frame)

# equivalent to Weight + Type lm(Fuel ~ Weight * Type - Weight:Type, data=fuel.frame)

# force use of ordinary arithmetic lm(Fuel ~ I(Weight + Disp.), data=fuel.frame)

# . = all variables except Fuel fit1 <- lm(Fuel ~ ., data=fuel.frame)

# remove Mileage as a variable fit2 <- update(fit1, . ~ . - Mileage)

# Formulas may be saved and used later data(wafer, package="Sdatasets") my.form <- pre.mean ~ spinsp + devtime aov(my.form, wafer)

# The arithmetic operators have special meanings in formulae: # use I() to use them in arithmetic or use poly() to make # quadratic and higher terms. A <- 1:5 B <- c(1,2,3,5,8)

# (Intercept) A:B (interaction term only) model.matrix(~A:B)

# (Intercept) A B A:B (main effects + interaction) model.matrix(~A*B)

# (Intercept) A B A:B (ditto) model.matrix(~(A+B)^2)

# (Intercept) A (interaction of A and A is just A) model.matrix(~A*A)

# (Intercept) A (ditto) model.matrix(~A^2)

# (Intercept) I(A^2) (constant + quadratic terms only) model.matrix(~I(A^2))

# (Intercept) A I(A^2) (constant + linear + quadratic terms) model.matrix(~A + I(A^2))

# (Intercept) poly(A, 2)1 poly(A, 2)2 # constant, linear, and quadratic terms - orthogonal model.matrix(~poly(A,2))

Package base version 6.0.0-69
Package Index