我有weekly
数据:
weekly <- structure(list(date = c("2013-01-07", "2013-01-10", "2013-01-21",
"2018-01-23", "2018-02-00", "2013-02-11", "2013-02-13", "2013-02-25",
"2013-03-00", "2013-03-11", "2013-03-13", "2013-03-25", "2018-00-01",
"2018-00-08", "2018-00-15", "2018-00-22", "2018-00-29", "2018-05-06",
"2018-05-13"), count = c(1750L, 1993L, 1816L, 1264L, 2042L, 1989L,
2186L, 2118L, 2081L, 2110L, 2151L, 2069L, 1898L, 1862L, 1952L,
1891L, 1758L, 1169L, 2009L)), row.names = c(NA, -19L), class = "data.frame")
我想将此数据转换为时间序列ts
对象,以便可以预测数据。
我看到了this question,但是对我没有帮助。
下面的代码创建带有日期和计数的data.frame
agr <-aggregate(input[1], input[2], FUN = function(df) count=length(df))
colnames(agr)[2] <- "count"
在创建的weekly
数据下方:
weekly <- agr %>%
tq_transmute(select = count,
mutate_fun = apply.weekly,
FUN = sum)
现在,我想将每周数据转换为时间序列数据,以便可以应用ARIMA模型。
答案 0 :(得分:0)
您可以使用weekly
包,以小数频率使用ts
函数,将lubridate
数据帧转换为时间序列对象。然后,您可以使用HoltWinters
来预测下一个3周。请参见下面的代码:
weekly <- structure(list(date = c("2013-01-07", "2013-01-10", "2013-01-21",
"2018-01-23", "2018-02-00", "2013-02-11", "2013-02-13", "2013-02-25",
"2013-03-00", "2013-03-11", "2013-03-13", "2013-03-25", "2018-00-01",
"2018-00-08", "2018-00-15", "2018-00-22", "2018-00-29", "2018-05-06",
"2018-05-13"),
count = c(1750L, 1993L, 1816L, 1264L, 2042L, 1989L,
2186L, 2118L, 2081L, 2110L, 2151L, 2069L, 1898L, 1862L, 1952L,
1891L, 1758L, 1169L, 2009L)),
row.names = c(NA, -19L),
class = "data.frame")
library(lubridate)
weekly_ts <- ts(weekly$count,
freq=365.25/7,
start= decimal_date(ymd(weekly[1, 1])))
#weekly_ts<- ts(weekly$count, frequency = 52)
m <- HoltWinters(weekly_ts, alpha = TRUE, beta = TRUE, gamma = FALSE)
library(forecast)
p <- predict(m, 3, prediction.interval = TRUE)
plot(m, p)