My model is
lm(formula = medv ~ crim + indus + rm + dis + crim * indus)
I have to add the polynomial term rm*rm
of order 2 to the above model. How do we do this in R?
My model is
lm(formula = medv ~ crim + indus + rm + dis + crim * indus)
I have to add the polynomial term rm*rm
of order 2 to the above model. How do we do this in R?
Here's two options.
lm(formula = medv ~ crim + indus + poly(rm, 2, raw = TRUE) + dis + crim * indus)
lm(formula = medv ~ crim + indus + rm + dis + crim * indus + I(rm^2))
The first uses poly(..., raw = TRUE)
where raw = TRUE
ensures the coefficients can be intepreted as usual. The latter uses the as is operator I(...)
which evaluates whats within it before using it in a the formula context. Note that x^2
isn't translated into the normal polynomial in R within a formula context.