R:nls()公式中的多项式快捷符号

JD *_*ong 3 r formula nls lm

使用线性模型函数lm()多项式公式可以包含如下的快捷符号:

m <- lm(y ~ poly(x,3))
Run Code Online (Sandbox Code Playgroud)

这是一个快捷方式,使用户不必创建x ^ 2和x ^ 3变量或在公式中键入它们I(x^2) + I(x^3).是否有非线性函数的可比表示法nls()?

had*_*ley 6

poly(x, 3)它不仅仅是一个快捷方式x + I(x ^ 2) + I(x ^ 3)- 它实际上产生了具有不相关的良好属性的legendre多项式:

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
Run Code Online (Sandbox Code Playgroud)


Dir*_*tel 5

简短回答:是的.

稍微长一点的答案:测试这个很便宜.我刚刚运行example(nls)以获取模型和数据,然后插入一个术语poly().

甚至更长的答案:lm()实际上并不知道poly(),公式在拟合发生之前得到解决.因此,在nls()具有公式接口的意义上......它必然会接受poly().

偏离主题而不是要求:你是否根据Harrell的RMS书来调查样条线?