My data has multiple customers data with different start and end dates along with their sales data.So I did simple exponential smoothing.
I applied the following code to apply ses
library(zoo)
library(forecast)
z <- read.zoo(data_set,FUN = function(x) as.Date(x) + seq_along(x) / 10^10 , index = "Date", split = "customer_id")
L <- lapply(as.list(z), function(x) ts(na.omit(x),frequency = 52))
HW <- lapply(L, ses)
Now my output class is list
with uneven lengths.Can someone help me how to unnest or unlist the output in to a data frame and get the fitted values,actuals,residuals along with their dates,sales and customer_id.
Note : the reson I post my input data rather than data of HW
is,the HW
data is too large.
Can someone help me in R.
答案 0 :(得分:1)
我将使用tidyverse
软件包来解决此问题。
map(HW, ~ .x %>%
as.data.frame %>% # convert each element of the list to data.frame
rownames_to_column) %>% # add row names as columns within each element
bind_rows(.id = "customer_id") # bind all elements and add customer ID
我不确定如何将日期和实际销售额与您的输出(HW
)相关联。如果您对此进行了解释,我也可能会提供解决该问题的方法。
答案 1 :(得分:0)
首先将所有唯一的customer_id放入名为“ k”的变量
k <- unique(data_set$customer_id)
创建一个空的数据框
b <- data.frame()
使用for循环提取所有拟合值并存储在'a'中。使用rbind函数将所有拟合值附加到数据框'b'
for(key in k){
print(a <- as.data.frame((as.numeric(HW_ses[[key]]$model$fitted))))
b <- rbind(b,a)
}
最后使用列绑定功能将输入数据集附加到数据框'b'
data_set_final <- cbind(data_set,b)