R timeseries:lapply(listed_ts,function(x)auto.arima(x,allowmean = F))中的错误:对象“listed_ts”;未找到
我想分别对每个 sales_point_id 进行每周时间序列分析,并得出事实值和预测结果。
dput()
timeseries=structure(list(sales_point_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), calendar_id_operday = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L), line_fact_amt = c(55767L,
59913L, 36363L, 48558L, 505L, 76344L, 22533L, 11965L, 78944L,
36754L, 30621L, 55716L, 32470L, 62165L, 57986L, 2652L, 16487L,
72849L, 73715L, 65656L, 64411L, 47460L, 61866L, 10877L, 72392L,
53011L, 23544L, 76692L, 10388L, 24255L, 56684L, 59329L, 6655L,
65612L, 17495L, 10389L, 63702L, 47407L, 78782L, 22898L, 21151L,
32587L)), class = "data.frame", row.names = c(NA, -42L))
我需要每周预测,week=1 其平均值为 20210101-20210108(ymd) 但这里没有日期格式只有星期,只是这些数据的特殊性。 这是我徒劳的尝试,
library("lubridate")
# first the grouping variable
timeseries$group <- paste0(timeseries$sales_point_id)
groups <- unique(timeseries$group)
# find starting date per group and save them as a list of elements
timeseries$date <- as.Date(as.character(timeseries$calendar_id_operday), )
timeseries <- timeseries[order(timeseries$calendar_id_operday),]
start_dates <- format(timeseries$date[match(groups, timeseries$group)], )
start_dates <- strsplit(start_dates, ' ')
listed <- split(timeseries,timeseries$group)
str(listed)
listed_ts
listed_arima <- lapply(listed_ts,function(x) auto.arima(x,allowmean = F ))
#Now the forecast for each arima:
listed_forecast <- lapply(listed_arima,function(x) forecast(x,12) )# forecast 12 weeks ahead
listed_forecast
# need to flat it down to a data.frame, do.call and rbind help:
do.call(rbind,listed_forecast)
#Get a prediction of initial values
lapply(listed_arima, fitted)
Error in lapply(listed_ts, function(x) auto.arima(x, allowmean = F))
我做错了什么以及如何修复以正确地处理时间序列。 我理想和期望的结果只是示例输出格式。
sales_point_id calendar_id_operday line_fact_amt.fact
1 1 1 436
2 1 2 56
3 1 3 66
4 1 4 NaN
5 1 5 NaN
6 1 6 NaN
7 1 7 NaN
8 1 8 NaN
9 1 9 NaN
10 1 10 NaN
11 1 11 NaN
12 1 12 NaN
13 1 13 NaN
14 1 14 NaN
15 1 15 NaN
line_fact_amt.predict forecast.ahead
1 435 NaN
2 57 NaN
3 70 NaN
4 NaN 524
5 NaN 945
6 NaN 235
7 NaN 200
8 NaN 326
9 NaN 437
10 NaN 7
11 NaN 191
12 NaN 321
13 NaN 919
14 NaN 407
15 NaN 82
一如既往,我感谢您的任何帮助。
I want to do a weekly time series analysis for each sales_point_id separately with the results of fact value and what was predicted.
dput()
timeseries=structure(list(sales_point_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), calendar_id_operday = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L), line_fact_amt = c(55767L,
59913L, 36363L, 48558L, 505L, 76344L, 22533L, 11965L, 78944L,
36754L, 30621L, 55716L, 32470L, 62165L, 57986L, 2652L, 16487L,
72849L, 73715L, 65656L, 64411L, 47460L, 61866L, 10877L, 72392L,
53011L, 23544L, 76692L, 10388L, 24255L, 56684L, 59329L, 6655L,
65612L, 17495L, 10389L, 63702L, 47407L, 78782L, 22898L, 21151L,
32587L)), class = "data.frame", row.names = c(NA, -42L))
i need weekly forecast and week=1 its mean 20210101-20210108(ymd) but here there is no date format only week, just such a specificity of these data .
Here are my futile attempts
library("lubridate")
# first the grouping variable
timeseries$group <- paste0(timeseries$sales_point_id)
groups <- unique(timeseries$group)
# find starting date per group and save them as a list of elements
timeseries$date <- as.Date(as.character(timeseries$calendar_id_operday), )
timeseries <- timeseries[order(timeseries$calendar_id_operday),]
start_dates <- format(timeseries$date[match(groups, timeseries$group)], )
start_dates <- strsplit(start_dates, ' ')
listed <- split(timeseries,timeseries$group)
str(listed)
listed_ts
listed_arima <- lapply(listed_ts,function(x) auto.arima(x,allowmean = F ))
#Now the forecast for each arima:
listed_forecast <- lapply(listed_arima,function(x) forecast(x,12) )# forecast 12 weeks ahead
listed_forecast
# need to flat it down to a data.frame, do.call and rbind help:
do.call(rbind,listed_forecast)
#Get a prediction of initial values
lapply(listed_arima, fitted)
Error in lapply(listed_ts, function(x) auto.arima(x, allowmean = F))
What i do wrong and how fix to correct working of timeseries.
my ideal and desired result as just sample output format .
sales_point_id calendar_id_operday line_fact_amt.fact
1 1 1 436
2 1 2 56
3 1 3 66
4 1 4 NaN
5 1 5 NaN
6 1 6 NaN
7 1 7 NaN
8 1 8 NaN
9 1 9 NaN
10 1 10 NaN
11 1 11 NaN
12 1 12 NaN
13 1 13 NaN
14 1 14 NaN
15 1 15 NaN
line_fact_amt.predict forecast.ahead
1 435 NaN
2 57 NaN
3 70 NaN
4 NaN 524
5 NaN 945
6 NaN 235
7 NaN 200
8 NaN 326
9 NaN 437
10 NaN 7
11 NaN 191
12 NaN 321
13 NaN 919
14 NaN 407
15 NaN 82
As always I appreciate any of your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
fable
包而不是forecast
包,您会发现生活变得更加轻松。它可以更好地处理每周数据,并且可以同时预测多个系列。这是使用您的数据的示例。首先,我们将数据转换为 tsibble 对象,这是
fable
所需的构造。它就像一个数据框,只不过它有一个时间索引和一个可选的key
来定义单独的时间序列。然后,我们对每个序列拟合 ARIMA 模型,生成未来 12 周的预测,并根据要求将拟合值与预测相结合。
.mean
列包含点预测。由 reprex 包 (v2.0.1) 创建于 2022 年 3 月 1 日
请参阅我的教科书位于 OTexts.com/fpp3 以获取更多信息。
You would find life much easier if you used the
fable
package instead of theforecast
package. It handles weekly data better, and it allows forecasts of multiple series at once.Here is an example using your data. First, we turn the data into a tsibble object, which is the construction needed for
fable
. It is like a data frame except it has a time index, and an optionalkey
to define separate time series.Then we fit an ARIMA model to each series, produce forecasts for 12 weeks ahead, and combine the fitted values with the forecasts as requested. The
.mean
column contains the point forecasts.Created on 2022-03-01 by the reprex package (v2.0.1)
See my textbook at OTexts.com/fpp3 for more information.