如何使用循环来预测每日销售额?

时间:2018-11-29 18:39:18

标签: r for-loop while-loop prediction

我试图预测收盘前剩余几天的销售情况。我正在使用线性回归来基于过去7天的平均值(除其他因素外)预测每一天。但是,当剩下的唯一一天是休息日时,那么我想使用其他函数来预测该天。该模型还必须单独更新每日销售额,以便每个预测都可以用于下一次迭代。

下面是我尝试编写一个循环,该循环将连续预测剩余每一天的每笔交易。

Link to R Code

### loop attempt
nnextdp <- DataforQuestion[c(74),]        #first na data point -- row 74, column Day6  for reference
   while (is.na(DataforQuestion[c(nnextdp),]) = TRUE) {      #while data point is NA, continue the loop
       if(nnextdp(head != Day0)) {Prior_week_average <- mean(nnextdp + right(7) - nnextdp) #set wkprioravg to the last 7 days of sales
          lmDayX <- lm(Day[right(head(nnextdp),1)] ~ Month + CDoW + 
           Prior_week_average, DataforQuestion)  #if datapoint is not in column Day0, let's use the lmDayX function
            predict(lmDayX, newdata = nnextdp)        #predict the data point's value & add it to the data set
                      nnextdp = nnextdp[c(-1)]                  #move the data point to the next na space
}   else if (nnextdp(head = Day0)) {Total_Not_Including_CSales <- aggreagate(Day1...Day203)  #if data point header is Day0, then sum the day1 - Day203 to get the total sales prior to close day
         lmDay0 <- lm(Day0 ~ Month + CDoW + Day1Sales + Total_Not_Including_CSales, DataforQuestion)  #if data point header is Day0, then run the other regression model
          predict(lmDay0, newdata = nnextdp)        #lmDay0 to predict the sales & fill in the value to the NA space
           nnextdp = nnextdp[c(-1)]  #move the data point to be predicted to the next NA space to the left of the previous
}  else
     print("complete")    #else print completed
}

基本上,我需要使用循环来预测Day0…... Day74列中的NA值的每日销售额。但前提是之前的非NA数据至少有7个销售天。

我在此处提供了指向数据集的链接。

Link to dataset

感谢您的帮助,请告诉我是否有什么可以澄清的内容!

0 个答案:

没有答案