Search code examples
rdataframetime-seriesexponentialunnest

Unnest a ts class


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.


Solution

  • Firstly took all the unique customer_id into a variable called 'k'

     k <- unique(data_set$customer_id)
    

    Created a empty data frame

       b <- data.frame()
    

    extracted all the fitted values using a for loop and stored in 'a'.Using the rbind function attached all the fitted values to data frame 'b'

    for(key in k){    
      print(a <- as.data.frame((as.numeric(HW_ses[[key]]$model$fitted))))
      b <- rbind(b,a)     
        }
    

    Finally using column bind function attached the input data set with data frame 'b'

    data_set_final <- cbind(data_set,b)