我正在尝试使用 lm(poly) 来获得某些点的多项式回归,但对它返回的回归公式系数有一些疑问。
像这样的样本:
x=seq(1,100) y=x^2+3*x+7 适合=lm(y~poly(x,2))
结果是:
lm(formula = y ~ poly(x, 2))
系数:
(截取)poly(x, 2)1 poly(x, 2)2 3542 30021 7452
为什么系数不是 7,3,2?
非常感谢!
我正在尝试使用 lm(poly) 来获得某些点的多项式回归,但对它返回的回归公式系数有一些疑问。
像这样的样本:
x=seq(1,100) y=x^2+3*x+7 适合=lm(y~poly(x,2))
结果是:
lm(formula = y ~ poly(x, 2))
系数:
(截取)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.
和
raw:如果为真,则使用原始而不是正交多项式。
但是你也可以使用费迪南德建议的,它有效。
coef(lm(y ~ x + I(x^2)))
## (Intercept) x I(x^2)
## 7.8104 2.7538 1.0150