我正在尝试使用 lm(poly) 获得某些点的多项式回归,但对它返回的回归公式系数有一些疑问。
像这样的样本:
x=seq(1,100)
y=x^2+3*x+7
fit=lm(y~poly(x,2))
结果是:
lm(formula = y ~ poly(x, 2))
系数:
(Intercept) poly(x, 2)1 poly(x, 2)2
3542 30021 7452
为什么系数不是 7、3、2?
非常感谢!
您需要设置raw
TRUE 的参数表示您不想使用正交多项式
这是默认的
set.seed(101)
N <- 100
x <- rnorm(N, 10, 3)
epsilon <- rnorm(N)
y <- 7 + 3 * x + x^2 + epsilon
coef(lm(y ~ poly(x, 2, raw = TRUE)))
## (Intercept) poly(x, 2, raw = TRUE)1
## 7.8104 2.7538
## poly(x, 2, raw = TRUE)2
## 1.0150
来自的帮助poly
你有的功能
描述:
Returns or evaluates orthogonal polynomials of degree 1 to
‘degree’ over the specified set of points ‘x’. These are all
orthogonal to the constant polynomial of degree 0. Alternatively,
evaluate raw polynomials.
And
raw:如果为 true,则使用原始多项式且非正交多项式。
但你也可以使用费迪南德提出的建议,它有效。
coef(lm(y ~ x + I(x^2)))
## (Intercept) x I(x^2)
## 7.8104 2.7538 1.0150
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)