I have several products with corresponding daily sales. I want to predict the expected daily sales of these products based on the current cumulative sales for each product and the total amount that I expect to sell over a period of time.
The first table (the "key") has the total sales expected for each product, as well as how much I plan to sell every day depending on how much has already been sold (that is, if my cumulative sales for product A 650, I sold 43% of the total 1,500 and therefore forecast to sell 75 the next day because 40% <43% <60%).
I want to update the cumulative sales of the second table (βdataβ) for each product based on projected sales. The forecasted volumes depend on cumulative sales for the previous period, that is, I can not calculate each column myself, and therefore I think I need to use cycles.
However, my database contains more than 500,000 rows, and my best attempt to use for loops is too slow to be doable. Thoughts? I think that implementing Rcpp might be a potential solution, but I have not used this package or C ++ before. The desired final answer is shown below ("final").
library(data.table)
key <- data.table(Product = c(rep("A",5), rep("B",5)), TotalSales =
c(rep(1500,5),rep(750,5)), Percent = rep(seq(0.2, 1, 0.2),2), Forecast =
c(seq(125, 25, -25), seq(75, 15, -15)))
data <- data.table(Date = rep(seq(1, 9, 1), 2), Product=rep(c("A", "B"),
each=9L), Time = rep(c(rep("Past",4), rep("Future",5)),2), Sales = c(190,
165, 133, 120, 0, 0, 0, 0, 0, 72, 58, 63, 51, 0, 0, 0, 0, 0))
final <- data.table(data, Cum = c(190, 355, 488, 608, 683, 758, 833, 908,
958, 72, 130, 193, 244, 304, 349, 394, 439, 484), Percent.Actual = c(0.13,
0.24, 0.33, 0.41, 0.46, 0.51, 0.56, 0.61, 0.64, 0.10, 0.17, 0.26, 0.33,
0.41, 0.47, 0.53, 0.59, 0.65), Forecast = c(0, 0, 0, 0, 75, 75, 75, 75, 50,
0, 0, 0, 0, 60, 45, 45, 45, 45))