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

R 3.4.4中实现ARIMA(p,d,q)参数循环并提取RMSE

Automate ARIMA Parameter Tuning and Generate RMSE Results Table in R 3.4.4

No problem, let's get this sorted out. Here's a straightforward, practical way to loop through all your ARIMA (p,d,q) parameter combinations, calculate the RMSE for each, and build the exact results table you need:

Step 1: Set up a container for results

First, create an empty data frame to store each parameter pair and its corresponding RMSE. We'll disable automatic factor conversion since R 3.4.4 defaults to turning character columns into factors, which we don't want here:

# Initialize empty results data frame
results <- data.frame(Order = character(), RMSE = numeric(), stringsAsFactors = FALSE)

Step 2: Loop through all parameter combinations

Use nested for loops to cover every possible (p,d,q) combination (where p=i, d=j, q=k). For each set of parameters, fit the ARIMA model, pull out the RMSE, format the order into a readable string, and add it to your results:

# Iterate over all p (0-5), d (0-3), q (0-5)
for (p in 0:5) {
  for (d in 0:3) {
    for (q in 0:5) {
      # Fit the ARIMA model with current parameters
      fit <- arima(df.train$Positive, order = c(p, d, q), include.mean = FALSE)
      
      # Extract RMSE from the model summary
      rmse_value <- as.data.frame(summary(fit))$RMSE
      
      # Format the order as a clean string like "(0, 0, 0)"
      order_label <- paste0("(", p, ", ", d, ", ", q, ")")
      
      # Append the current result to the data frame
      results <- rbind(results, data.frame(Order = order_label, RMSE = rmse_value, stringsAsFactors = FALSE))
    }
  }
}

Step 3: (Optional) Handle fitting errors gracefully

Some parameter combinations might cause numerical issues or model fitting failures. To keep the loop running instead of crashing, wrap the model fitting logic in a tryCatch block to skip problematic pairs and log a quick message:

# Robust loop with error handling
for (p in 0:5) {
  for (d in 0:3) {
    for (q in 0:5) {
      tryCatch({
        fit <- arima(df.train$Positive, order = c(p, d, q), include.mean = FALSE)
        rmse_value <- as.data.frame(summary(fit))$RMSE
        order_label <- paste0("(", p, ", ", d, ", ", q, ")")
        results <- rbind(results, data.frame(Order = order_label, RMSE = rmse_value, stringsAsFactors = FALSE))
      }, error = function(e) {
        message(paste("Skipping ARIMA", order_label, "- Error:", e$message))
      })
    }
  }
}

Step 4: Check your final results

Once the loop finishes, just print or inspect the results data frame—it’ll match the format you provided perfectly:

# Print the full results table
print(results)

# Or view it in RStudio's interactive viewer
View(results)

This will give you a table where each row lists the (p,d,q) parameter string and its corresponding RMSE value, exactly like your example.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:34:11