在 R 中使用 auto.arima 显示初始数据的预测值 [英] display predicted values for initial data using auto.arima in R
问题描述
让我们处理这个数据样本
Let's work with this data sample
timeseries<-structure(list(Data = structure(c(10L, 14L, 18L, 22L, 26L, 29L,
32L, 35L, 38L, 1L, 4L, 7L, 11L, 15L, 19L, 23L, 27L, 30L, 33L,
36L, 39L, 2L, 5L, 8L, 12L, 16L, 20L, 24L, 28L, 31L, 34L, 37L,
40L, 3L, 6L, 9L, 13L, 17L, 21L, 25L), .Label = c("01.01.2018",
"01.01.2019", "01.01.2020", "01.02.2018", "01.02.2019", "01.02.2020",
"01.03.2018", "01.03.2019", "01.03.2020", "01.04.2017", "01.04.2018",
"01.04.2019", "01.04.2020", "01.05.2017", "01.05.2018", "01.05.2019",
"01.05.2020", "01.06.2017", "01.06.2018", "01.06.2019", "01.06.2020",
"01.07.2017", "01.07.2018", "01.07.2019", "01.07.2020", "01.08.2017",
"01.08.2018", "01.08.2019", "01.09.2017", "01.09.2018", "01.09.2019",
"01.10.2017", "01.10.2018", "01.10.2019", "01.11.2017", "01.11.2018",
"01.11.2019", "01.12.2017", "01.12.2018", "01.12.2019"), class = "factor"),
client = structure(c(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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("Horns", "Kornev"), class = "factor"), stuff = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("chickens",
"hooves", "Oysters"), class = "factor"), Sales = c(374L,
12L, 120L, 242L, 227L, 268L, 280L, 419L, 12L, 172L, 336L,
117L, 108L, 150L, 90L, 117L, 116L, 146L, 120L, 211L, 213L,
67L, 146L, 118L, 152L, 122L, 201L, 497L, 522L, 65L, 268L,
441L, 247L, 348L, 445L, 477L, 62L, 226L, 476L, 306L)), .Names = c("Data",
"client", "stuff", "Sales"), class = "data.frame", row.names = c(NA,
-40L))
我想使用 auto.arima 按组进行预测
I want to perform forecast using auto.arima by group
# first the grouping variable
timeseries$group <- paste0(timeseries$client,timeseries$stuff)
# now the list
listed <- split(timeseries,timeseries$group)
library("forecast")
library("lubridate")
listed_ts <- lapply(listed,
function(x) ts(x[["Sales"]], start = start = c(2017, 1), frequency = 12) )
listed_ts
listed_arima <- lapply(listed_ts,function(x) auto.arima(x) )
#Now the forecast for each arima:
listed_forecast <- lapply(listed_arima,function(x) forecast(x,2) )
listed_forecast
do.call(rbind,listed_forecast)
如果我这样做,我会得到对未来的预测,但我想看看 auto.arima 模型对我的示例中的初始值的预测.要更清楚.在我的示例 Sales
中,01.04.2017 Horns chickens=374.对?如何从示例数据中查看 auto.arima 模型为此日期和其他日期预测的值.
If i do so i get forecast on future, but i want see, what auto.arima model predicts for initial value from my example.
To be more clear.
In my example Sales
for 01.04.2017 Horns chickens=374. Right? How can I see what value the auto.arima model predicted for this date and another dates from example data.
所以输出
推荐答案
这些值被称为 fitted 值,它们可以通过函数 fitted
获得,如下所示:
Those values are known as fitted values and they can be obtained with the function fitted
as follows:
lapply(listed_arima, fitted)
# $Hornschickens
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
# 2017 223.8182 223.8182 223.8182 223.8182 223.8182 223.8182 223.8182 223.8182 223.8182 223.8182 223.8182
#
# $Hornshooves
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# 2017 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231 336.9231
# 2018 336.9231
#
# $KornevOysters
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# 2017 137.125 137.125 137.125 137.125 137.125 137.125 137.125 137.125 137.125 137.125 137.125 137.125
# 2018 137.125 137.125 137.125 137.125
在这种情况下,结果不是很有趣,因为所有拟合模型都是 ARIMA(0,0,0) - 白噪声.
In this case results are not very interesting as all the fitted models are ARIMA(0,0,0) - white noise.
作为旁注,请注意该解决方案等效于
As a side comment, note that the solution is equivalent to
lapply(listed_arima, function(x) fitted(x))
出于同样的原因,您也可以使用
For the same reason you may also use
listed_arima <- lapply(listed_ts, auto.arima)
这篇关于在 R 中使用 auto.arima 显示初始数据的预测值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!