我的数据集:
structure(list(year = 2010:2019, pop = c(9574323, 9657592, 9749476,
9843336, 9932887, 10031646, 10154788, 10268233, 10381615, 10488084
), ye = 1:10), row.names = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10"), class = "data.frame")
我仅是Year和Pop列的线性回归。当我为这两列运行summary(lm)时,我得到的是:
> summary(lm(pop~year, data = this))
Call:
lm(formula = pop ~ year, data = this)
Residuals:
Min 1Q Median 3Q Max
-27821.4 -10094.9 656.5 12968.3 27549.8
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -196556312 4240960 -46.35 5.19e-11 ***
year 102539 2105 48.71 3.49e-11 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 19120 on 8 degrees of freedom
Multiple R-squared: 0.9966, Adjusted R-squared: 0.9962
F-statistic: 2372 on 1 and 8 DF, p-value: 3.493e-11
斜率截距方程不正确。但是当我使用ye列运行lm时,它是正确的。
summary(lm(pop〜ye,data = this))
Call:
lm(formula = pop ~ ye, data = this)
Residuals:
Min 1Q Median 3Q Max
-27821.4 -10094.9 656.5 12968.3 27549.8
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 9444234 13062 723.00 < 2e-16 ***
ye 102539 2105 48.71 3.49e-11 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 19120 on 8 degrees of freedom
Multiple R-squared: 0.9966, Adjusted R-squared: 0.9962
F-statistic: 2372 on 1 and 8 DF, p-value: 3.493e-11
这不是我想要的,因为我想预测2020年,2021年等。为了使Year列在等式中起作用,我需要更改什么?我也在excel中尝试过,它也是一样。
我们可以用predict
来做。
model <- lm(pop~year, data = this)
predict(model,data.frame(year=c(2020,2021)))
1 2
10572162 10674701
Cross Validated中的[This answer]非常详细地介绍了您的问题,但简短的答案是,除了拦截项外,两者是等效的。
为了便于解释,您可能要设置一个参考年,然后根据该参考年设置回归的年数据(例如2010 =参考年0,2015 =年5),就像您对ye
所做的那样列。
[另一位评论者建议使用predict()
来预测2020年和2021年,这两种方法都适用(分别使用c(2020, 2021)
或c(20, 21)
。]