##### 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))