多个时间序列数据的指数时间序列

exponential time series for multiple time series data

我的数据有不同的起点和终点。

structure(list(item = c("Card", "Card", "Card", "Card", "Card", 
"Card", "Card", "Card", "battery", "battery", "battery", "battery", 
"battery", "laptop", "laptop", "laptop", "laptop", "laptop", 
"laptop", "laptop"), sales = c(20.4, 29, 26, 40, 35, 36, 28, 
41, 70, 75, 78, 99, 40, 100, 132, 123, 145, 125, 145, 124), Date = structure(c(17784, 
17791, 17798, 17805, 17812, 17819, 17826, 17833, 17608, 17615, 
17622, 17629, 17636, 17713, 17726, 17739, 17752, 17765, 17778, 
17791), class = "Date")), row.names = c(NA, -20L), class = "data.frame")

我试过

ts_test <- ts(multiple_ts, frequency=52)

转换为时间序列但失败了

structure(c(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, 100, 132, 123, 145, 125, 145, 124, 17784, 
17791, 17798, 17805, 17812, 17819, 17826, 17833, 17608, 17615, 
17622, 17629, 17636, 17713, 17726, 17739, 17752, 17765, 17778, 
17791), .Dim = c(20L, 3L), .Dimnames = list(NULL, c("item", "sales", 
"Date")), .Tsp = c(1, 1.36538461538462, 52), class = c("mts", 
"ts", "matrix"))

有人可以帮助我如何按项目转换为时间序列并对每个项目应用指数平滑。 提前致谢!

将数据框转换为 3 列动物园对象 z 并从 z 创建 ts 个对象的列表 L。对 L 的每个分量应用指数平滑,给出 HW。然后绘制每一个。注意ts对象不能直接表示Dateclass所以我们省略了X轴在pltHW.

中自己绘制
library(zoo)

z <- read.zoo(multiple_ts, index = "Date", split = "item")
L <- lapply(as.list(z), function(x) as.ts(na.omit(x)))
HW <- lapply(L, HoltWinters, beta = FALSE, gamma = FALSE)

# given HoltWinters object x get fitted values as zooreg object
fitHW <- function(x) {
  fitted <- fitted(x)
  zooreg(fitted[, 1], as.Date(start(fitted)), frequency = frequency(fit))
}

# plot
pltHW <- function(x, sub) {
  plot(x, sub = sub, xaxt = "n")
  fit <- fit(x)
  Axis(time(fit), side = 1)
  invisible(x)
}

par.old <- par(mfrow = c(3, 1))
junk <- Map(pltHW, HW, names(HW))
par(par.old)