With the linear model function lm() polynomial formulas can contain a shortcut notation like this:
m <- lm(y ~ poly(x,3))
this is a shortcut that keeps the user from having to create x^2 and x^3 variables or typing them in the formula like I(x^2) + I(x^3)
. Is there comparable notation for the nonlinear function nls()
?
Short answer: yes.
Slightly longer answer: It is pretty cheap to test this. I just ran
example(nls)
to get a model and data loaded and then inserted a term withpoly()
.Even longer answer:
lm()
doesn't actually know aboutpoly()
, the formulae get resolved before the fitting happens. So in the sense thatnls()
has a formula interface ... it was bound to acceptpoly()
.Off-topic and not asked for: Did you look into splines as well as per Harrell's RMS book?
poly(x, 3)
is rather more than just a shortcut forx + I(x ^ 2) + I(x ^ 3)
- it actually produces legendre polynomials which have the nice property of being uncorrelated: