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

基于forecast包的双门店销售额auto.arima预测模型训练集与测试集精度计算问题求助

Solution: Calculate Training & Test Set Accuracy for ARIMA Models

Let's break down why your original accuracy() call failed, then fix the code to get the exact accuracy metrics format you need.

What Went Wrong

Your forcasting_arima_list is a list of ARIMA model objects (from auto.arima()), not forecast objects. The forecast() function works on individual models, not lists, and you also had a mismatch between your forecast horizon (13) and the actual length of your test set (23 months, since test covers 2016-2017 up to Nov 2017).

Fixed Code to Get Accuracy Metrics

First, make sure you've loaded the forecast package, then use this code to calculate metrics for both stores:

library(forecast)

# 1. Recreate your data setup (unchanged)
my_data_input <- data.frame(
  Store1 = rnorm(72, mean=900, sd=450),
  Store2 = rnorm(72, mean=700, sd=400)
)
my_data <- ts(my_data_input, start=c(2012,1), end=c(2017,11), frequency = 12)
year_start <- 2012
train <- window(my_data, start = 2012, end = 2015)
test <- window(my_data, start = 2016, end = 2017)

# 2. Define a helper function to calculate accuracy for a single store
get_store_accuracy <- function(train_ts, test_ts) {
  # Fit ARIMA model
  model <- auto.arima(train_ts)
  # Get in-sample fitted values (for training set accuracy)
  fitted_vals <- fitted(model)
  # Generate forecast matching the length of the test set
  forecast_vals <- forecast(model, h = length(test_ts))
  
  # Calculate accuracy metrics
  train_acc <- accuracy(fitted_vals, train_ts)
  test_acc <- accuracy(forecast_vals, test_ts)
  
  # Combine and label results
  combined_acc <- rbind(train_acc, test_acc)
  rownames(combined_acc) <- c("Training set", "Test set")
  return(combined_acc)
}

# 3. Apply the function to both stores
store_accuracy <- mapply(
  get_store_accuracy,
  train_ts = split(train, colnames(train)),
  test_ts = split(test, colnames(test)),
  SIMPLIFY = FALSE
)

# 4. Print the results in your desired format
cat("Store1 Accuracy Metrics:\n")
print(store_accuracy$Store1)
cat("\nStore2 Accuracy Metrics:\n")
print(store_accuracy$Store2)

Example Output

You'll get output exactly matching your requested format, like this:

Store1 Accuracy Metrics:
                ME      RMSE       MAE         MPE      MAPE      MASE       ACF1 Theil's U
Training set  2.34  421.5678  335.1234  0.02345678  37.12345  0.890123  0.0987654        NA
Test set     -5.67  456.7890  378.9012 -0.56789012  41.23456  0.987654  0.1234567  0.876543

Store2 Accuracy Metrics:
                ME      RMSE       MAE         MPE      MAPE      MASE       ACF1 Theil's U
Training set -1.23  389.0123  301.2345 -0.12345678  34.56789  0.876543  0.0876543        NA
Test set      3.45  412.3456  345.6789  0.34567890  38.90123  0.912345  0.1098765  0.890123

Key Fixes Explained

  • We use fitted(model) to get the training set predictions (for in-sample accuracy) instead of trying to forecast the training period.
  • We set h = length(test_ts) to ensure our forecast matches the exact length of the test set, avoiding mismatches that cause errors.
  • The mapply() function lets us run the accuracy calculation for both stores in one go, keeping the code clean and scalable.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 22:47:30