gpt4 book ai didi

r - 线性回归 - 将预测值附加到同一数据集

转载 作者:行者123 更新时间:2023-12-02 03:55:04 25 4
gpt4 key购买 nike

我最近开始使用 R 编程。这是我的数据集

WeekOfYear  Production
1 202612
2 245633
3 299653
4 252612
5 299633
6 288993
7 254653
8 288612
9 277733
10 245633

我需要预测一年中剩余几周的“生产”值

relation<-lm(Production~WeekOfYear,dataset)

predict(relation,data.frame(WeekOfYear=c(11)))

如何将第 11 周到第 52 周(年底)的预测值附加到如下所示的同一数据集中

WeekOfYear  Production
1 202612
2 245633
3 299653
4 252612
5 299633
6 288993
7 254653
8 288612
9 277733
10 245633
11 predicted value
12 predicted value

等等

-或-

WeekOfYear  Production  Regression
1 202612 fitted value
2 245633 fitted value
3 299653 fitted value
4 252612 fitted value
5 299633 fitted value
6 288993 fitted value
7 254653 fitted value
8 288612 fitted value
9 277733 fitted value
10 245633 fitted value
11 predicted value
12 predicted value
13 predicted value
14 predicted value
.
.
52 predicted value

最佳答案

你可以这样做:

relation <- lm(Production ~ WeekOfYear, dat)

WeekOfYear <- 1:52
predict(relation, data.frame(WeekOfYear))

dat2 <- data.frame(WeekOfYear, regression = predict(relation, data.frame(WeekOfYear)))

merge(dat, dat2, by = 'WeekOfYear', all.y = TRUE)

结果:

   WeekOfYear Production regression
1 1 202612 250517.6
2 2 245633 253864.1
3 3 299653 257210.5
4 4 252612 260557.0
5 5 299633 263903.5
6 6 288993 267249.9
7 7 254653 270596.4
8 8 288612 273942.9
9 9 277733 277289.3
10 10 245633 280635.8
11 11 NA 283982.3
12 12 NA 287328.7

----

51 51 NA 417840.9
52 52 NA 421187.4

关于r - 线性回归 - 将预测值附加到同一数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44264099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com