如何在R或python中应用具有线性回归和多个变量的滚动窗口? 输出输出

问题描述

我约会的时间超过708天。该行中有6个产品列,其中包含销售编号。
还有13个变量。其中大多数是天气日期,例如星期日或温度,其他则是二进制日期(无event = {0,event = 1)。

目标是预测产品的销售数量。

图片中的数据是通用的。

r dataframe (generalized)

我想做的是滚动窗口。
这样我就可以用77天的时间来预测下周(以1:77的速度预测第78至84天),然后以此类推(以8:84的速度预测85:93)。
而且我还想将合并的预测(78:708)与实际日期进行比较。

我找不到在Rwich工作中应用Rolling ln函数的方法。

我的python技能非常基础,但也欢迎使用python解决方案。

型号:

model_A 前100个数据点:

data <- structure(list(weekday = c(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,209,188,176,192,201,256,216,198,169,173,186,166,200,206,153,171,208,179,177,149,223,154,172,215,207,141,167,181,168,197,191,210,196,214,127,233,227,134),Prdukt_B = c(213,237,228,247,246,230,229,205,232,174,212,241,286,255,288,226,258,238,270,224,244,218,275,250,243,253,264,221,185,267,261,242,254,269,308,278,234,259,225,257,220,159),Produkt_C = c(18,27,37,21,20,35,15,23,22,19,14,28,34,33,40,42,44,38,31,32,30,24,29,26,36,39,51,41,48,60,49,16,24),Produkt_D = c(17,9,25,8,17,18,11,10,13,12,9),Produkt_E = c(24,46,11),Produkt_F = c(35,45,68,80,53,43,52,55,47,57,54,71,59,64,88,65,69,72,73,62,85,101,100,54),day_number = c(1,50,56,58,61,63,66,67,70,74,75,76,77,78,79,81,82,83,84,86,87,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,17.19,17.52,18.35,16.66,16.79,17.14,18.36,20.95,15.54,15.1,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,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,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,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,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,10.19,19.43,23.17,21.56,15.64,10.96,16.38,19.85,23.08,24.11,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,3.21,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,3.42,3.71,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,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,79.7083333333333,65.375,65.0833333333333,68.7083333333333,80.9166666666667,73.1666666666667,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,76.2916666666667,70.2083333333333,66.2916666666667,71.125,78.4166666666667,79.4166666666667,83.0833333333333,82.4166666666667,70.6666666666667,55.7083333333333,78.7916666666667,73.5833333333333,71.9166666666667,81.0416666666667,81.7083333333333,73.125,71.6666666666667,79.3333333333333,80.5,73.9166666666667,77.5416666666667,87.75,83.375,74.9583333333333,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),Variable_9 = c(4.55,5.98333333333333,11.4166666666667,11.3166666666667,11.3333333333333,11.1333333333333,9.96666666666667,8.58333333333333,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,1.18333333333333,2.25,3.56666666666667,0.3,5.75,5.73333333333333,5.45,7.41666666666667,5.71666666666667,5.31666666666667,2.93333333333333,6.8,5.48333333333333,0.516666666666667,8.73333333333333,9.35,1.76666666666667,9.01666666666667,6.58333333333333,9.11666666666667,2.18333333333333,4.36666666666667,2.56666666666667,0.216666666666667,0.766666666666667,1.66666666666667,4.11666666666667,0.483333333333333,1.35,0.45,2.53333333333333,2.01666666666667,4.4,1.4,2.96666666666667,4.18333333333333,8.56666666666667,7.7,1.8,0.05,8.45,0.883333333333333,0.383333333333333,1.83333333333333,4.9,8.36666666666667,5.78333333333333,5.4,0.166666666666667,0.4),Variable_10 = c(1.7,1.3,0.225,0.15,2.975,1.5,0.925,0.425,0.675,0.125,0.4,0.475,0.375,1.175,0.85,0.7,1.55,0.1,0.35,0.175,1.675,0.075,3.325,0.8,Variable_11 = c(0,Variable_12 = c(0,Variable_13 = c(0,0)),row.names = c(NA,100L),class = "data.frame")

解决方法

我认为这是期望的输出

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="+")),])
    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 

如果您要分一周进行操作,请将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 

我还认为,如果要对所有产品apply使用相同的代码,则以下代码将给出一个命名列表,其中每个元素都包含该产品的预测:

sapply(LETTERS[1:6],function(l){
  formula <- as.formula(paste0("Produkt_"," ~ weekday + ",collapse="+")) )
  unlist(lapply(seq(77,7),]) 
  }),use.names=F)
}) -> l
as.data.frame(l) -> l
names(l) <- paste0("Produkt_",LETTERS[1:6][-2])

输出

   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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...