Download introduction to the R Project for Statistical Computing

Transcript
> model <- lm(Volume ~ Height)
but even if not, the variable names can be referred to a frame with the
data= named argument:
> model <- lm(Volume ~ Height, data=trees)
Additive effects More complicated models include additive effects, using
the + formula operator:
> model <- lm(Volume ~ Height + Girth, data=trees)
Note this is not an arithmetic addition, but rather a special use in the
model notation. Here the tree volume is explained by by both its height
and girth, considered as independent predictors.
Interactions The : formula operator is used to indicate interactions; usually these are used in addition to additive terms:
> model <- lm(Volume ~ Height + Girth + Height:Girth, data=trees)
Here the tree volume is explained by both its height and girth, as well as
their interaction, i.e. that the effect of girth is different at different heights.
The * formula operator is shorthand for all linear terms and interactions
of the named independent variables, so that the previous example could
have been more simply written as:
> model <- lm(Volume ~ Height * Girth, data=trees)
The ^ formula operator is used to indicate predictor crossing to the specified degree:
> model <- lm(Volume ~ (Height + Girth)^2, data=trees)
Here the ^2 expands to all interactions between the named predictors,
since there are only two; this is equivlent to Height + Girth + Height:Girth,
which in this two-predictor case is also the same as Height * Girth.
Removing terms Sometimes it is convenient to specify a model and then
remove a term from it with the - formula operator. As a somewhat artificial example, to model tree volume by only tree girth and its interaction
with height:
> model <- lm(Volume ~ Height*Girth - Height, data=trees)
This is equivalent to:
53