[英]How to apply a rolling window with linear regression and multiple variables in R( or python)?
I have date over 708 days.我有超过 708 天的约会。 There are 6 products columns with sell numbers in the row.
行中有 6 个带有销售编号的产品列。
There are also 13 variables.还有13个变量。 Most of them are weather date like sun hours or temperature, others are binary (no event=
0
, event= 1
).其中大部分是天气日期,如太阳时数或温度,其他是二进制的(没有 event=
0
, event= 1
)。
The goal is to predict the sell numbers of a product.目标是预测产品的销售数量。
The data in the picture is generalized.图片中的数据是通用的。
What I want to do is a rolling window.我想做的是滚动 window。
So that I can take 77days to predict the following week (predict day 78 to 84 with 1:77) and then so on (take 8:84 to predict 85:93).这样我就可以用 77 天来预测下一周(用 1:77 预测第 78 到 84 天),然后依此类推(用 8:84 预测 85:93)。
And also I want to compare the combined predictions (78:708) with the actual date.而且我还想将组合预测 (78:708) 与实际日期进行比较。
I didn't find a way to apply a rolling ln function in R which worked.我没有找到在 R 中应用滚动 ln function 的有效方法。
My python skills are very basic, but a solution in python would also be appreciated.我的 python 技能非常基础,但 python 中的解决方案也将不胜感激。
Model: Model:
model_A<-lm(formula=Produkt_A~weekday+Variable_1+Variable_2+Variable_3+Variable_4+Variable_5+Variable_6+Variable_7+Variable_8+Variable_9+Variable_10+Variable_11+Variable_12+Variable_13,data=df) model_A<-lm(公式=Produkt_A~weekday+Variable_1+Variable_2+Variable_3+Variable_4+Variable_5+Variable_6+Variable_7+Variable_8+Variable_9+Variable_10+Variable_11+Variable_12+Variable_13,data=df)
First 100 data points:前 100 个数据点:
data <- structure(list(weekday = c(7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4,
5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4,
5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4,
5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4,
5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4,
5, 6, 7, 1), Produkt_A = c(204, 187, 189, 203, 217, 211, 222,
194, 219, 240, 161, 202, 193, 236, 164, 204, 170, 190, 213, 235,
199, 195, 182, 184, 189, 209, 188, 176, 209, 192, 203, 201, 209,
256, 216, 198, 209, 169, 192, 173, 170, 201, 170, 186, 166, 200,
206, 192, 153, 171, 170, 190, 216, 208, 202, 188, 179, 177, 149,
192, 179, 223, 198, 192, 184, 154, 172, 186, 215, 207, 179, 141,
167, 181, 182, 181, 202, 168, 216, 197, 211, 191, 210, 200, 171,
196, 214, 200, 206, 203, 188, 141, 127, 181, 195, 233, 191, 227,
186, 134), Prdukt_B = c(213, 237, 219, 228, 198, 247, 246, 230,
229, 192, 236, 182, 205, 202, 167, 206, 195, 179, 209, 211, 232,
192, 191, 174, 182, 232, 229, 207, 212, 236, 237, 195, 241, 286,
247, 229, 230, 255, 211, 247, 288, 226, 226, 184, 192, 200, 258,
238, 270, 224, 194, 197, 228, 244, 230, 241, 215, 218, 218, 196,
275, 246, 247, 215, 250, 207, 243, 253, 264, 221, 185, 216, 211,
218, 218, 267, 261, 212, 242, 246, 254, 269, 308, 278, 234, 230,
259, 225, 232, 257, 209, 193, 192, 193, 240, 229, 220, 242, 210,
159), Produkt_C = c(18, 27, 37, 21, 27, 20, 35, 15, 27, 20, 23,
22, 20, 19, 20, 21, 23, 15, 14, 27, 21, 14, 22, 28, 23, 22, 34,
27, 20, 20, 22, 33, 40, 35, 42, 44, 38, 31, 37, 32, 37, 30, 24,
20, 28, 29, 26, 36, 29, 24, 15, 31, 22, 31, 39, 35, 39, 35, 31,
30, 34, 51, 31, 30, 33, 19, 26, 39, 32, 51, 28, 19, 33, 32, 26,
33, 41, 48, 33, 29, 42, 33, 60, 51, 49, 30, 38, 35, 24, 30, 31,
19, 16, 26, 33, 24, 38, 32, 35, 24), Produkt_D = c(17, 16, 9,
23, 27, 25, 4, 8, 14, 19, 26, 23, 28, 7, 17, 17, 18, 22, 25,
27, 20, 25, 33, 23, 27, 17, 30, 5, 11, 26, 16, 32, 22, 19, 14,
11, 10, 7, 14, 28, 16, 22, 14, 10, 21, 26, 31, 20, 13, 7, 17,
20, 17, 29, 26, 8, 4, 18, 12, 14, 14, 15, 12, 10, 7, 25, 26,
25, 17, 13, 27, 8, 14, 20, 18, 19, 15, 14, 21, 30, 11, 37, 18,
26, 22, 27, 16, 27, 20, 29, 16, 7, 19, 15, 15, 16, 6, 8, 10,
9), Produkt_E = c(24, 14, 18, 20, 19, 46, 32, 30, 5, 6, 18, 14,
26, 17, 37, 25, 27, 9, 16, 39, 21, 14, 15, 7, 14, 17, 24, 20,
33, 18, 14, 24, 21, 20, 19, 16, 12, 12, 15, 15, 25, 14, 31, 18,
19, 16, 17, 25, 19, 15, 15, 21, 20, 21, 17, 16, 29, 10, 13, 11,
16, 15, 24, 20, 25, 12, 15, 17, 30, 24, 24, 21, 14, 18, 21, 25,
17, 27, 20, 20, 26, 17, 18, 23, 14, 27, 19, 22, 24, 23, 22, 22,
3, 18, 11, 16, 22, 19, 14, 11), Produkt_F = c(35, 19, 30, 32,
26, 28, 42, 39, 44, 45, 37, 37, 36, 35, 25, 40, 24, 24, 25, 36,
31, 26, 22, 27, 21, 21, 34, 49, 41, 33, 38, 33, 34, 40, 68, 80,
53, 32, 43, 49, 31, 52, 55, 31, 29, 29, 37, 37, 47, 44, 35, 32,
41, 37, 42, 57, 60, 57, 45, 54, 71, 60, 59, 48, 44, 37, 36, 48,
47, 64, 53, 40, 45, 49, 24, 46, 88, 54, 49, 38, 65, 60, 69, 72,
73, 51, 59, 62, 34, 36, 59, 43, 32, 85, 101, 85, 69, 100, 65,
54), day_number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 100), Variable_1 = c(20.2525, 11.7558333333333,
10.5270833333333, 13.0058333333333, 16.815, 17.56, 13.7058333333333,
15.96625, 10.5575, 10.295, 13.15125, 13.15, 16.3366666666667,
18.3704166666667, 16.15875, 14.8670833333333, 16.77875, 16.2008333333333,
17.5420833333333, 18.44125, 18.3858333333333, 17.1179166666667,
15.32375, 16.7, 16.3304166666667, 15.5291666666667, 14.015, 13.5420833333333,
11.9104166666667, 9.43166666666667, 10.9129166666667, 11.8391666666667,
12.66875, 11.4791666666667, 8.28375, 5.5125, 7.62375, 6.32958333333333,
8.79041666666667, 10.0675, 10.2854166666667, 9.14708333333333,
10.8925, 10.9716666666667, 12.175, 12.3229166666667, 11.63, 11.9608333333333,
11.3966666666667, 13.1179166666667, 10.9333333333333, 11.9541666666667,
9.16208333333333, 7.23791666666667, 5.10625, 4.1225, 1.80666666666667,
0.783333333333333, 2.23958333333333, 3.39208333333333, 4.68625,
5.58291666666667, 7.42, 6.32166666666667, 5.3175, 4.87583333333333,
5.68708333333333, 7.99666666666667, 7.2825, 7.60416666666667,
10.75625, 13.2133333333333, 9.9975, 9.67416666666667, 10.5054166666667,
11.0079166666667, 7.30416666666667, 8.43, 5.37666666666667, 3.07583333333333,
1.40791666666667, 0.157916666666667, -0.474166666666667, -0.190833333333333,
3.74333333333333, 4.21875, 2.1925, 4.93083333333333, 6.36416666666667,
9.21291666666667, 11.2970833333333, 11.095, 7.30041666666667,
1.94958333333333, 1.66875, 3.9075, 3.83333333333333, 1.32208333333333,
3.6825, 5.095), Variable_2 = c(27.08, 15.85, 16.71, 20.69, 23.88,
23.4, 20.01, 23.33, 14.19, 15.34, 17.88, 20.29, 23.75, 25.15,
19.86, 20.59, 22.79, 24.04, 23.57, 25.6, 24.86, 23.67, 22.81,
23.11, 21.98, 23.14, 19.35, 18.98, 18.11, 16.25, 17.26, 16.42,
18.41, 15.73, 11.22, 6.93, 14.21, 10.35, 16.43, 14.73, 14.3,
15.34, 17.19, 17.52, 18.35, 16.66, 14.73, 16.79, 17.14, 18.36,
20.95, 15.54, 15.1, 13.15, 10.5, 9.28, 5.6, 6.33, 7.84, 7.94,
10.86, 10.08, 12.01, 10.34, 6.98, 6.39, 10.29, 13.49, 9.03, 10.56,
13.51, 15.42, 11.35, 13.67, 12.94, 13.46, 9.06, 10.79, 6.98,
5.16, 5.71, 3.58, 2.21, 2.51, 7.77, 7.73, 7.19, 7.99, 9.9, 12.04,
12.67, 13.21, 12.13, 5.1, 5.73, 8.7, 6.81, 3.79, 5.94, 6.84),
Variable_3 = c(16.3108333333333, 8.77083333333333, 5.61916666666667,
10.2091666666667, 15.34625, 15.6825, 10.9925, 13.9241666666667,
6.25583333333333, 6.505, 11.4929166666667, 11.6275, 14.0754166666667,
16.5591666666667, 14.9191666666667, 14.0804166666667, 15.9579166666667,
13.21, 15.5595833333333, 16.3566666666667, 16.6279166666667,
14.2116666666667, 11.51625, 14.625, 14.7758333333333, 14.3008333333333,
13.3770833333333, 12.6420833333333, 9.81083333333333, 6.84875,
7.50125, 8.26791666666667, 11.1266666666667, 9.33958333333333,
5.27416666666667, 0.43625, 4.05916666666667, 0.0675, 4.48416666666667,
7.59791666666667, 7.93416666666667, 7.35416666666667, 8.72625,
7.64791666666667, 9.56041666666667, 10.08, 9.62041666666667,
9.22375, 7.78833333333333, 9.89583333333333, 8.34083333333333,
9.55833333333333, 5.55958333333333, 2.96541666666667, 0.897916666666667,
-0.807916666666667, -1.76875, -3.45458333333333, -2.23583333333333,
-0.18875, 0.573333333333333, 0.973333333333333, 4.225, 2.7525,
1.99083333333333, 1.50666666666667, 1.10333333333333, 2.66291666666667,
4.52166666666667, 4.9075, 6.25583333333333, 8.1975, 5.69625,
5.81625, 8.01458333333333, 6.28416666666667, -0.420833333333333,
-0.555416666666667, -0.485416666666667, -0.599583333333333,
-2.83291666666667, -4.32, -4.20708333333333, -5.2775, -1.79333333333333,
0.420416666666667, -3.52, 0.6125, 1.63625, 2.69125, 5.0475,
6.22791666666667, 2.54708333333333, -0.890833333333333, -2.08666666666667,
-0.840416666666667, -0.16, -2.14041666666667, -0.218333333333333,
2.30125), Variable_4 = c(22.49, 13.49, 10.19, 19.43, 23.17,
21.56, 15.64, 21.98, 10.96, 10.5, 16.38, 19.85, 23.08, 24.11,
18.98, 20.14, 22.88, 21.46, 21.14, 23.35, 23.62, 21.86, 18.47,
21.73, 21.8, 20.06, 19.04, 18.4, 16.13, 14.39, 12.82, 12.23,
15.88, 12.21, 7.94, 3.21, 9.03, 3.2, 13.8, 13.07, 11.59,
13.75, 16.78, 15.84, 17.63, 14.68, 12.68, 14.96, 14.67, 15.55,
21.4, 12.78, 11.87, 9.75, 8.01, 5.49, 2.09, 1.15, 3.77, 4.97,
7.88, 5.81, 9.09, 7.94, 3.42, 3.71, 6.81, 8.46, 8.17, 8.58,
9.64, 10.1, 7.22, 9.16, 11.14, 10.32, 0.76, 1.58, 0.99, 0.75,
0.38, -2.42, -2.44, -3.35, 3.05, 2.56, 0.86, 4.86, 4.04,
6.82, 6.83, 8.78, 5.55, 1.45, 2.79, 5.07, 2.44, 0.54, 3.49,
3.75), Variable_5 = c(16.3108333333333, 8.77083333333333,
5.61916666666667, 10.2091666666667, 15.34625, 15.6825, 10.9925,
13.9241666666667, 6.25583333333333, 6.505, 11.4929166666667,
11.6275, 14.0754166666667, 16.5591666666667, 14.9191666666667,
14.0804166666667, 15.9579166666667, 13.21, 15.5595833333333,
16.3566666666667, 16.6279166666667, 14.2116666666667, 11.51625,
14.625, 14.7758333333333, 14.3008333333333, 13.3770833333333,
12.6420833333333, 9.81083333333333, 6.84875, 7.50125, 8.26791666666667,
11.1266666666667, 9.33958333333333, 5.27416666666667, 0.43625,
4.05916666666667, 0.0675, 4.48416666666667, 7.59791666666667,
7.93416666666667, 7.35416666666667, 8.72625, 7.64791666666667,
9.56041666666667, 10.08, 9.62041666666667, 9.22375, 7.78833333333333,
9.89583333333333, 8.34083333333333, 9.55833333333333, 5.55958333333333,
2.96541666666667, 0.897916666666667, -0.807916666666667,
-1.76875, -3.45458333333333, -2.23583333333333, -0.18875,
0.573333333333333, 0.973333333333333, 4.225, 2.7525, 1.99083333333333,
1.50666666666667, 1.10333333333333, 2.66291666666667, 4.52166666666667,
4.9075, 6.25583333333333, 8.1975, 5.69625, 5.81625, 8.01458333333333,
6.28416666666667, -0.420833333333333, -0.555416666666667,
-0.485416666666667, -0.599583333333333, -2.83291666666667,
-4.32, -4.20708333333333, -5.2775, -1.79333333333333, 0.420416666666667,
-3.52, 0.6125, 1.63625, 2.69125, 5.0475, 6.22791666666667,
2.54708333333333, -0.890833333333333, -2.08666666666667,
-0.840416666666667, -0.16, -2.14041666666667, -0.218333333333333,
2.30125), Variable_6 = c(6.92875, 2.515, 4.37041666666667,
2.26166666666667, 1.79333333333333, 2.47333333333333, 2.83083333333333,
2.94, 4.53416666666667, 3.08375, 1.74958333333333, 1.7175,
3.23583333333333, 2.25541666666667, 2.3375, 1.4575, 2.0375,
4.51791666666667, 3.0675, 3.39875, 3.21083333333333, 3.99458333333333,
4.24166666666667, 2.38, 2.07041666666667, 1.70458333333333,
1.11125, 1.29166666666667, 1.98625, 1.4075, 2.85625, 3.79541666666667,
1.93, 1.77916666666667, 2.61833333333333, 4.50291666666667,
2.97583333333333, 5.98458333333333, 3.7675, 2.06583333333333,
1.99541666666667, 1.03916666666667, 2.26, 3.59125, 2.59458333333333,
1.97583333333333, 1.91791666666667, 3.00791666666667, 3.80833333333333,
3.87333333333333, 3.2175, 2.4125, 3.37416666666667, 3.95291666666667,
3.09291666666667, 3.68875, 1.51541666666667, 2.40791666666667,
2.78041666666667, 1.59541666666667, 2.82, 3.93708333333333,
2.46708333333333, 2.76541666666667, 2.35208333333333, 2.27291666666667,
3.62875, 4.41291666666667, 1.90916666666667, 1.5575, 5.05625,
6.83375, 5.00166666666667, 3.66416666666667, 2.64333333333333,
5.42791666666667, 8.6, 11.1095833333333, 5.98791666666667,
2.21708333333333, 2.44041666666667, 2.63541666666667, 1.52666666666667,
3.31083333333333, 5.35375, 2.77666666666667, 4.72875, 3.55583333333333,
4.14875, 7.35666666666667, 7.70666666666667, 6.15333333333333,
5.0575, 0.9975, 1.85041666666667, 3.17291666666667, 1.88833333333333,
1.55583333333333, 2.57375, 1.48041666666667), Variable_7 = c(61.75,
63.6666666666667, 53.6666666666667, 55.25, 60.625, 58.375,
64.4166666666667, 68.0833333333333, 70.125, 61.0833333333333,
72.3333333333333, 74.625, 67.2916666666667, 56.5833333333333,
73.2916666666667, 76.9166666666667, 74.0833333333333, 70.4583333333333,
64.875, 62.75, 65.7916666666667, 62.125, 57.125, 59.5, 65.125,
70.7916666666667, 80, 79.7083333333333, 74.0833333333333,
65.375, 65.0833333333333, 68.7083333333333, 80.9166666666667,
73.1666666666667, 80.9166666666667, 74.0833333333333, 75.5416666666667,
65.9166666666667, 66.7916666666667, 75.0416666666667, 76.4166666666667,
80.1666666666667, 81.5416666666667, 76.375, 70.75, 69.25,
75.7916666666667, 75.75, 72.375, 72.4166666666667, 86.0416666666667,
73.5, 75.3333333333333, 78.1666666666667, 72.875, 67.7916666666667,
72.375, 76.2916666666667, 70.2083333333333, 66.2916666666667,
71.125, 76, 78.4166666666667, 79.4166666666667, 83.0833333333333,
82.4166666666667, 70.6666666666667, 55.7083333333333, 78.7916666666667,
73.5833333333333, 71.9166666666667, 76.2916666666667, 81.0416666666667,
72.3333333333333, 81.7083333333333, 73.125, 71.6666666666667,
79.3333333333333, 83.0833333333333, 80.5, 73.9166666666667,
76.2916666666667, 77.5416666666667, 71.125, 87.75, 83.375,
74.9583333333333, 78.4166666666667, 73.375, 70.2916666666667,
73.0416666666667, 80.4583333333333, 84.2083333333333, 86.5416666666667,
75.125, 60.4583333333333, 56.5, 80.6666666666667, 77.625,
81.4166666666667), Variable_8 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Variable_9 = c(4.55,
5.98333333333333, 11.4166666666667, 11.3166666666667, 11.3333333333333,
11.1333333333333, 9.96666666666667, 8.58333333333333, 1.45,
4, 1.1, 9.73333333333333, 10.3333333333333, 6.5, 3.31666666666667,
7.05, 10.05, 9.95, 6.43333333333333, 10.7333333333333, 11.1,
11.0333333333333, 10.75, 9.13333333333333, 9.98333333333333,
8.48333333333333, 6.56666666666667, 8.51666666666667, 7.63333333333333,
10.5166666666667, 5.81666666666667, 5.05, 6.48333333333333,
5.23333333333333, 0, 0, 1.45, 1.18333333333333, 2.25, 3.56666666666667,
0.3, 5.75, 5.73333333333333, 5.45, 7.05, 7.41666666666667,
5.71666666666667, 5.31666666666667, 2.93333333333333, 6.8,
5.48333333333333, 0.516666666666667, 8.58333333333333, 8.73333333333333,
9.35, 7.05, 9.13333333333333, 1.76666666666667, 9.01666666666667,
6.58333333333333, 9.11666666666667, 2.18333333333333, 4.36666666666667,
2.56666666666667, 0, 0.216666666666667, 0.766666666666667,
1.66666666666667, 0, 4.11666666666667, 0.483333333333333,
1.35, 0.45, 2.53333333333333, 2.01666666666667, 4.4, 0.75,
1.4, 2.96666666666667, 4.18333333333333, 8.56666666666667,
7.7, 1.8, 8.51666666666667, 3.2, 0.05, 8.45, 0, 0.883333333333333,
0, 0.383333333333333, 0, 1.83333333333333, 4.9, 8.36666666666667,
5.78333333333333, 3.31666666666667, 5.4, 0.166666666666667,
0.4), Variable_10 = c(1.7, 0, 0, 0, 0, 0, 0, 1.3, 0.225,
0, 0.15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2.975, 1.5, 0, 0.925, 0, 0.425, 0, 0,
0, 0, 0, 0.675, 0, 0, 0.125, 0.4, 0, 0.05, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.475, 0.425, 0, 0.225, 0.375, 0, 0, 1.175, 0,
0.85, 0.7, 0.375, 0, 1.55, 0.1, 0, 0.475, 0.35, 0, 0, 0,
0, 0.175, 1.675, 0.075, 0, 3.325, 0, 0.8, 0.125, 0, 1.35,
0, 0, 0, 0, 0, 0, 0), Variable_11 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Variable_12 = c(0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), Variable_13 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
100L), class = "data.frame")
I think this is the desired output我认为这是所需的 output
formula <- as.formula(paste0("Produkt_A ~ weekday + ", paste0("Variable_", 1:13, collapse="+")) )
lapply(77:(nrow(data)-7), function(x){
cof <- coef(fit<-lm(formula, data=data[1:77 + (x-77),]))
if(any(is.na(cof))) fit <- lm(paste0("Produkt_",l," ~ ", paste0(names(cof)[!is.na(cof)][-1], collapse="+")), data=data[1:77 + (x-77),])
predict(fit, data[1:7+x, ])
})
[[1]]
78 79 80 81 82 83 84
175.6231 187.2455 172.6803 198.4860 196.4347 182.1002 176.8268
[[2]]
79 80 81 82 83 84 85
190.0454 177.9412 198.1522 195.6015 179.8084 172.5813 162.7283
[[3]]
80 81 82 83 84 85 86
180.2934 199.5343 197.2900 179.7369 173.6123 164.2395 197.3890
If you want to step by a week then change 77:(nrow(data)-7)
to seq(77,nrow(data)-7, 7)
如果你想按一周进行,然后将
77:(nrow(data)-7)
更改为seq(77,nrow(data)-7, 7)
[[1]]
78 79 80 81 82 83 84
175.6231 187.2455 172.6803 198.4860 196.4347 182.1002 176.8268
[[2]]
85 86 87 88 89 90 91
165.3612 202.1819 202.9133 192.1088 196.2086 190.3211 192.5323
[[3]]
92 93 94 95 96 97 98
190.0432 234.1672 240.5036 221.9270 199.6451 206.9269 225.3737
Also I think that you want to apply
the same code to all the products if so, the following code will give a named list where each element contains the predictions for that product:此外,我认为如果是这样,您希望对所有产品
apply
相同的代码,以下代码将给出一个命名列表,其中每个元素都包含对该产品的预测:
sapply(LETTERS[1:6], function(l){
formula <- as.formula(paste0("Produkt_",l, " ~ weekday + ", paste0("Variable_", 1:13, collapse="+")) )
unlist(lapply(seq(77,nrow(data)-7, 7), function(x){
cof <- coef(fit<-lm(formula, data=data[1:77 + (x-77),]))
if(any(is.na(cof))) fit <- lm(paste0("Produkt_",l," ~ weekday + ", paste0(names(cof)[!is.na(cof)][-1], collapse="+")), data=data[1:77 + (x-77),])
predict(fit, data[1:7+x, ])
}), use.names=F)
}) -> l
as.data.frame(l) -> l
names(l) <- paste0("Produkt_",LETTERS[1:6][-2])
Output Output
Produkt_A Produkt_B Produkt_C Produkt_D Produkt_E Produkt_F
1 183.1617 255.8523 34.78719 9.040423 19.85345 72.48134
2 179.0516 236.6205 33.66128 14.456406 15.23111 52.14860
3 169.4564 233.5267 36.56796 11.893548 18.04682 47.84604
4 195.5562 237.8086 39.53323 12.611249 18.62263 57.85943
5 195.7860 244.9322 42.66896 10.579288 20.81832 64.33085
6 185.1508 244.6651 44.30452 11.109312 21.15966 66.57021
7 180.0834 238.5539 42.82646 9.011723 23.64313 68.25384
8 172.3084 236.5883 33.38472 6.052889 23.20801 60.28078
9 199.3014 251.1623 35.95758 17.642626 15.75082 49.16527
10 199.2620 244.1736 36.81896 20.277373 15.65039 57.63127
11 185.4162 232.4378 33.42248 9.914847 14.76501 58.14915
12 198.1636 250.0573 38.33021 17.008143 17.60963 53.38972
13 192.1136 217.5651 30.07103 13.590327 18.93781 57.78008
14 196.7885 222.4964 31.21988 16.192406 23.95876 55.00223
15 195.3891 229.2507 31.85575 17.436000 29.62023 51.42169
16 231.2512 236.6823 29.52290 22.564247 21.73221 49.89134
17 238.8464 254.4130 31.74025 23.147904 27.33184 53.82207
18 220.2332 240.9582 32.17718 22.994808 24.63406 56.82193
19 200.0848 230.6290 33.54214 21.710842 20.59998 54.37489
20 209.8458 245.5775 38.18210 22.251512 22.91061 55.50162
21 229.8341 259.3681 37.87675 20.772142 29.79006 65.34059
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.