我的数据如下所示:
df <- structure(list(Variable =c("A", "B", "A", "B", "A", "B", "A", "B", "A"),
Quantity=c("1", "100", "2", "5", "6", "30", "8", "15", "133"),
YearQuarter=c("2017Q2", "2017Q2", "2017Q3", "2017Q3", "2017Q4", "2017Q4", "2018Q1", "2018Q1", "2018Q2"),
Week=c("1", "10", "1", "2", "1", "6", "2", "9", "13")),
class= "data.frame", row.names=c(NA, -9L))
没有实际日期,但我想将其转换为时间序列数据集,以便我可以预测。时间序列的格式是每年/ 1tr的第1-13周。理想情况下,我可以将其设置为52周的频率,因此我可以预测每个预测的52周。
答案 0 :(得分:1)
我们不清楚你想要什么,但如果我们假设df
代表两个系列A
和B
,你想要一个有规律的ts
结果系列频率52然后使用以下。
library(zoo)
df2 <- transform(df, Quantity = as.numeric(Quantity))
toTime <- function(yq, wk) as.numeric(as.yearqtr(yq)) + (as.numeric(wk) - 1)/13/4
z <- read.zoo(df2, index = c("YearQuarter", "Week"), split = "Variable", FUN = toTime)
tt <- as.ts(z)
frequency(tt)
## [1] 52
如果您想将Variable
视为一系列1和2(分别为A
和B
):
df3 <- transform(df, Quantity = as.numeric(Quantity),
Variable = match(Variable, c("A", "B")))
z3 <- read.zoo(df3, index = c("YearQuarter", "Week"), FUN = toTime)
tt3 <- as.ts(z3)
请注意,大多数时间序列预测例程都需要定期间隔的非缺失数据,并且问题中显示的数据会在定期间隔时生成许多NA。