尝试在 r 中使用 lm() 和年份
Trying to use lm() in r with the year
我的数据集:
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 列进行了线性回归。当我 运行 这两列的摘要(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
斜率截距方程不正确。但是当我 运行 lm 使用 ye 列时,它是正确的。
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 年等等。我需要更改什么才能使年列在等式中起作用?我也在 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)
)。
我的数据集:
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 列进行了线性回归。当我 运行 这两列的摘要(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
斜率截距方程不正确。但是当我 运行 lm 使用 ye 列时,它是正确的。
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 年等等。我需要更改什么才能使年列在等式中起作用?我也在 excel 中尝试过这个,它是一回事。
我们可以用 predict
来做到这一点。
model <- lm(pop~year, data = this)
predict(model,data.frame(year=c(2020,2021)))
1 2
10572162 10674701
This answer 非常详细地涵盖了您的问题,但简短的回答是两者是等价的,除了截距项。
为了便于解释,您可能需要设置一个参考年,然后根据该参考年设置回归年数据(例如 2010 = 参考年 0,2015 = 年 5),就像您对ye
列。
另一位评论者建议使用 predict()
来预测 2020 年和 2021 年,这两种方法都适用(分别使用 c(2020, 2021)
或 c(20, 21)
)。