You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中无法用autoplot绘制数据框时间序列的问题求助

呼叫量时间序列预测:autoplot函数报错解决方法

问题背景

需基于指定数据集完成未来2年的月度呼叫量时间序列预测,选用业务线为Driver and Vehicle Services queues (English + French),数据包含三列:呼叫业务线、日期(年月)、已处理呼叫数。在初步分析时使用autoplot函数遇到报错。

报错代码

# Load the forecasting package
library(fpp3)

# Load the Data
load(Book3)

# Declare time series data
Book3 <- ts( Book3, [,3], start = 2016,1, frequency = 12)

############################################
Preliminary analysis
############################################
#Time Plot
autoplot(Book3) + 
  ggtitle("Test plot") + 
  ylab("Calls Handled")

错误信息

Error in `autoplot()`:
! Objects of type tbl_df/tbl/data.frame not supported by autoplot.
Run `rlang::last_error()` to see where the error occurred.

数据集重建代码

structure(list(`Queue Group/Line of Business` = c("Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)", 
"Driver and Vehicle Services queues (English + French)", "Driver and Vehicle Services queues (English + French)"
), DATE = c("2016-Jan", "2016-Feb", "2016-Mar", "2016-Apr", "2016-May", 
"2016-Jun", "2016-Jul", "2016-Aug", "2016-Sep", "2016-Oct", "2016-Nov", 
"2016-Dec", "2017-Jan", "2017-Feb", "2017-Mar", "2017-Apr", "2017-May", 
"2017-Jun", "2017-Jul", "2017-Aug", "2017-Sep", "2017-Oct", "2017-Nov", 
"2017-Dec", "2018-Jan", "2018-Feb", "2018-Mar", "2018-Apr", "2018-May", 
"2018-Jun", "2018-Jul", "2018-Aug", "2018-Sep", "2018-Oct", "2018-Nov", 
"2018-Dec", "2019-Jan", "2019-Feb", "2019-Mar", "2019-Apr", "2019-May", 
"2019-Jun", "2019-Jul", "2019-Aug", "2019-Sep", "2019-Oct", "2019-Nov", 
"2019-Dec", "2020-Jan", "2020-Feb", "2020-Mar", "2020-Apr", "2020-May", 
"2020-Jun", "2020-Jul", "2020-Aug", "2020-Sep", "2020-Oct", "2020-Nov", 
"2020-Dec", "2021-Jan", "2021-Feb", "2021-Mar", "2021-Apr", "2021-May", 
"2021-Jun", "2021-Jul", "2021-Aug", "2021-Sep"), `Calls Handled` = c(70660, 
61905, 63322, 68323, 72465, 67761, 64797, 77825, 71506, 71563, 
73651, 62384, 78243, 66130, 72600, 57756, 71920, 75819, 73144, 
82160, 70994, 73033, 66115, 53979, 71682, 57638, 66280, 56167, 
75368, 70826, 73149, 79144, 70100, 72516, 63654, 53070, 73814, 
57584, 66132, 63191, 72111, 63801, 68660, 67934, 57454, 56167, 
49349, 45312, 60766, 50218, 56790, 62748, 84383, 95169, 79610, 
70333, 80930, 75381, 77950, 78096, 73031, 60816, 89674, 74114, 
74114, 74717, 64373, 64202, 99922)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -69L))

解决方案

报错原因有两点:

  1. ts()函数语法错误:正确的索引方式应为Book3[,3],你遗漏了数据集名称;
  2. fpp3包基于**tidy时间序列对象(tsibble)**而非base R的ts对象,autoplot.fpp3仅支持tsibble或相关对象。

修正后的代码如下:

library(fpp3)

# 处理日期列,转换为年月格式的时间对象
Book3$DATE <- yearmonth(Book3$DATE)

# 转换为tsibble对象,指定时间索引和频率
calls_ts <- Book3 %>%
  as_tsibble(index = DATE, frequency = 12) %>%
  # 过滤目标业务线(若数据集包含多条业务线)
  filter(`Queue Group/Line of Business` == "Driver and Vehicle Services queues (English + French)")

# 绘制时间序列图
autoplot(calls_ts, `Calls Handled`) +
  ggtitle("月度已处理呼叫量趋势") +
  ylab("已处理呼叫数")

后续预测步骤参考

修正数据格式后,可使用fpp3工具完成分析与预测:

  • 分解时间序列:calls_ts %>% model(STL(Calls Handled ~ trend(window=7) + season(window='periodic'))) %>% components() %>% autoplot()
  • 建立预测模型:例如ARIMA模型calls_ts %>% model(ARIMA(Calls Handled)) %>% forecast(h=24)(h=24对应未来2年共24个月)
  • 可视化预测结果:forecast_result %>% autoplot(calls_ts)

内容的提问来源于stack exchange,提问作者AtFirstYouTry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:35:18