在 R 中预测时自动绘图功能出错
Posted
技术标签:
【中文标题】在 R 中预测时自动绘图功能出错【英文标题】:Error With the Autoplot Function When Forecasting in R 【发布时间】:2020-10-10 17:49:53 【问题描述】:我正在尝试进行季节性预测项目。在下面的示例数据中,从 2016 年 1 月到 2020 年 5 月,我有 3 个变量(A、B、C)。我将数据集转换为 tibble 数据框,使用一阶差分对数据进行去趋势处理,然后使用 TSLM
函数回归一组每月虚拟变量的去趋势值。
我的问题是我想根据实际值绘制预测值,但我遇到了这个错误:Error: Can't proceed with the key of multiple variables.
这个错误让我失望,因为两天前我能够毫无问题地执行我的代码,但今天我得到了错误。非常感谢有关此错误的任何指导
我的带数据的代码:
library(fable)
library(fabletools)
library(tibble)
library(dplyr)
library(forecast)
df<-data.frame(
A=c(27.9938, 28.0004,28.03154, 28.04374, 28.08446, 28.12364, 28.15138, 28.15892, 28.15968, 28.07314, 28.04036, 28.08632, 27.98332, 27.96234, 27.97982, 28.0106, 28.02084, 28.06482, 28.0602, 28.05052, 28.05138, 27.99212, 27.96588, 27.97364, 27.90562, 27.87026, 27.88142, 27.89572, 27.9344, 27.99952, 28.02304, 28.01158, 28.0443, 28.04298, 28.00724, 28.01248, 27.95378, 27.95702, 27.95918, 27.98758, 28.0348, 28.05396, 28.07982, 28.07998, 28.06708, 28.08386, 28.04272, 28.03444, 27.98222, 27.96958, 28.0002),
B=c(27.92668, 27.9936, 28.02364, 28.04648, 28.04492, 28.05294, 28.0191, 28.00362, 27.96778, 27.96118, 27.8963, 27.93334, 27.9451, 27.94296, 28.00206, 28.05464, 28.04646, 28.07908, 28.02212, 27.94902, 27.93564, 27.87986, 27.87676, 27.93026, 27.91306, 27.96704, 27.95872, 28.0064, 27.98354, 28.00128, 27.94422, 27.89578, 27.9424, 27.89796, 27.90042, 27.91856, 27.9423, 28.00996, 28.03854, 28.10654, 28.1248, 28.24094, 28.1972, 28.1662, 28.18706, 28.19138, 28.1637, 28.14834, 28.17378, 28.187, 28.22642),
C=c(27.7977, 27.94492, 27.89732, 27.7361, 27.9497, 27.80912, 27.8867, 27.8085, 27.96406, 27.96548, 27.99574, 27.93068, 27.95904, 27.96142, 28.17876, 28.16956, 28.14324, 28.0869, 28.17692, 28.15626, 27.83046, 28.02534, 28.10704, 28.2005, 27.981, 28.46818, 28.32172, 28.23436, 27.93638, 27.87202, 28.04238, 27.76178, 27.14638, 27.42408, 27.65992, 28.0452, 27.97926, 28.18418, 28.24784, 27.87126, 27.5862, 27.60312, 27.92416, 28.05302, 27.70676, 27.87256, 27.54304, 27.72288, 27.6785, 27.47676, 27.36466))
df <- df %>%
mutate(Month = yearmonth(seq(as.Date("2016-01-01"), by = "1 month", length = NROW(df))))%>% > pivot_longer(-Month, names_to = "Series", values_to = "value") %>%
as_tsibble(index = Month, key = Series)
df<-df%>%group_by(Series)%>%mutate(det=c(NA,diff(value)))
df<-df%>%as_tsibble(index = Month, key = Series)
train<-df%>%dplyr::filter(Month<=as.Date('2019-03-01'))
seas<-function(data, det)
seasonality=data%>%model(TSLM(det~season()))
x<-seas(data = train, det = det)
forag<-x%>%forecast(h=12,bias_adjust=TRUE)%>%filter(Series=="A")%>%autoplot(df)`
【问题讨论】:
【参考方案1】:问题中的代码太复杂了,调用as_tsibble
两次(!),分配df
3次(!!!)和调用yearmonth
太早了。以下是相同代码的工作版本。
library(fable)
library(fabletools)
library(tibble)
library(dplyr)
library(forecast)
library(tsibble)
df <- df %>%
mutate(Month = seq(as.Date("2016-01-01"), by = "1 month", length = NROW(df))) %>%
pivot_longer(-Month, names_to = "Series", values_to = "value") %>%
group_by(Series) %>%
mutate(det = c(NA, diff(value))) %>%
as_tsibble(index = Month, key = Series)
train <- df %>%
dplyr::filter(Month <= as.Date('2019-03-01')) %>%
mutate(Month = yearmonth(Month))
seas <- function(data, det)
seasonality <- data %>% model(TSLM(det ~ season()))
seasonality
x <- seas(data = train, det = det)
forag <- x %>%
forecast(h = 12, bias_adjust = TRUE) %>%
filter(Series == "A") %>%
autoplot()
forag
【讨论】:
以上是关于在 R 中预测时自动绘图功能出错的主要内容,如果未能解决你的问题,请参考以下文章
在 R 中绘制 ROC 曲线时出错 - UseMethod("predict") 中的错误:没有适用于“预测”的方法应用于“因子”类的对象