用Lm(Polyy)求公式系数 [英] using lm(poly) to get formula coeff

查看:22
本文介绍了用Lm(Polyy)求公式系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用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.

raw:如果为true,则使用RAW而不是正交多项式。

但是你也可以使用费迪南德提出的方法,它是有效的。

coef(lm(y ~ x + I(x^2)))
## (Intercept)           x      I(x^2) 
##      7.8104      2.7538      1.0150 

这篇关于用Lm(Polyy)求公式系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆