R: polynomial shortcut notation in nls() formula

馋奶兔 提交于 2019-12-21 05:35:54

问题


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()?


回答1:


poly(x, 3) is rather more than just a shortcut for x + I(x ^ 2) + I(x ^ 3)- it actually produces legendre polynomials which have the nice property of being uncorrelated:

options(digits = 2)
x <- runif(100)
var(cbind(x, x ^ 2, x ^ 3))
#       x            
# x 0.074 0.073 0.064
#   0.073 0.077 0.071
#   0.064 0.071 0.067
zapsmall(var(poly(x, 3)))
#      1    2    3
# 1 0.01 0.00 0.00
# 2 0.00 0.01 0.00
# 3 0.00 0.00 0.01



回答2:


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 with poly().

Even longer answer: lm() doesn't actually know about poly(), the formulae get resolved before the fitting happens. So in the sense that nls() has a formula interface ... it was bound to accept poly().

Off-topic and not asked for: Did you look into splines as well as per Harrell's RMS book?



来源:https://stackoverflow.com/questions/3643606/r-polynomial-shortcut-notation-in-nls-formula

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!