将多个ID的每周数据转换为时间序列格式

时间:2018-11-12 02:49:13

标签: r dplyr time-series smoothing exponential

这是我的数据结构

structure(list(customer_id = c("A", "A", "A", "A", "A", "A", 
    "A", "A", "B", "B", "B", "B", "B"), state = c("NC", "NC", "NC", 
    "NC", "NC", "NC", "NC", "NC", "KA", "KA", "KA", "KA", "KA"), 
        value = c(20.4, 29, 26, 40, 35, 36, 28, 41, 70, 75, 78, 99, 
        40), Date = structure(c(17784, 17791, 17798, 17805, 17812, 
        17819, 17826, 17833, 17608, 17615, 17622, 17629, 17636), class = "Date")), row.names = c(NA, 
    -13L), class = "data.frame")

我有这么多来自不同状态,开始日期和结束日期的数据客户。 我想将此数据转换为time_series数据,并使用hw方法执行指数平滑。 我尝试转换为time_series的代码是:

temp <- multiple_ts %>%
  group_by(state,customer_id)  %>%
 ts(multiple_ts$value, frequency = 52)

我使用频率为52,因为数据是weekly_data,但是代码抛出错误

Warning messages:
1: In data.matrix(data) : NAs introduced by coercion
2: In data.matrix(data) : NAs introduced by coercion

我的output_data是

structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 20.4, 29, 
26, 40, 35, 36, 28, 41, 70, 75, 78, 99, 40, 17784, 17791, 17798, 
17805, 17812, 17819, 17826, 17833, 17608, 17615, 17622, 17629, 
17636), .Dim = c(13L, 4L), .Dimnames = list(NULL, c("customer_id", 
"state", "value", "Date")), .Tsp = c(20.9384615384615, 21.1692307692308, 
52), class = c("mts", "ts", "matrix"))

有人可以在R中帮助我吗? 在此先感谢

0 个答案:

没有答案