【发布时间】:2019-11-30 07:23:57
【问题描述】:
n = 50
set.seed(100)
x = matrix(runif(n, -2, 2), nrow=n)
y = 2 + 0.75*sin(x) - 0.75*cos(x) + rnorm(n, 0, 0.2)
.
在 R 中, 我想用最小二乘法估计上面的多项式函数。
这意味着我想知道 γ0、γ1、γ2 和 γ3 的估计值。
而且我也想知道这个估计函数下的MSE。
我用过这个
summary(lm(y ~ x+ x^2+ x^3))
但只要得到this output:
Call:
lm(formula = y ~ x + (x^2) + (x^3))
Residuals:
Min 1Q Median 3Q Max
-0.66448 -0.22251 -0.07694 0.20647 0.79429
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.54972 0.04761 32.55 <2e-16 ***
x 0.65279 0.04633 14.09 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3319 on 48 degrees of freedom
Multiple R-squared: 0.8053, Adjusted R-squared: 0.8013
F-statistic: 198.6 on 1 and 48 DF, p-value: < 2.2e-16
请告诉我在 R 中,我可以使用什么函数或包来做到这一点。
谢谢。
【问题讨论】:
-
试试
summary(lm(y ~ x + I(x^2)+ I(x^3)))或summary(lm(y ~ poly(x, 3, raw=TRUE))。 -
谢谢,为什么只在 (x^2) 和 (x^3) 上加上 ''I'' 就可以工作?
标签: r statistics least-squares