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

如何从auto.arima模型提取不含截距的AR系数并求和(多模型场景)

Extract and Sum AR Coefficients from auto.arima Models (Excluding Intercept)

Got it, let's fix this for you—you're right that sum(coef(bsp_auto)) includes the intercept/mean term, and handling lists of models just needs a small adjustment to target only the AR coefficients. Here's how to do it for both single and multiple models:

Single Model Solution

First, let's tackle the single model case. The key is to filter coefficients whose names start with ar (since auto.arima names AR coefficients like ar1, ar2, etc., while the intercept is usually named intercept or mean).

bsp_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp_auto <- auto.arima(bsp_ts, max.p = 12, max.q = 0, seasonal = FALSE, d=0)

# Filter AR coefficients only, then sum
ar_coefs <- coef(bsp_auto)[grepl("^ar", names(coef(bsp_auto)))]
sum(ar_coefs)

The grepl("^ar", names(coef(bsp_auto))) part creates a logical vector that picks out only coefficients with names starting with ar, excluding the intercept term entirely.

Multiple Models Solution

For your list of models, we'll use lapply to iterate over each model in the list, applying the same filtering and summing logic as above. Your earlier attempt had a small mistake (using ARpers_li$x instead of the function parameter x)—here's the corrected code:

bsp1_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp2_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp3_ts <- ts(c(1,2,1,2,3,2,3,2,4,3,4,3,4,5,4,3,5,6,5,4,5,6,7,6))
bsp_ts <- list(bsp1_ts, bsp2_ts, bsp3_ts)
bsp_auto <- lapply(bsp_ts, function(x) auto.arima(x, max.p = 12, max.q = 0, seasonal = FALSE, d=0))

# Calculate sum of AR coefficients for each model in the list
ar_coef_sums <- lapply(bsp_auto, function(model) {
  ar_coefs <- coef(model)[grepl("^ar", names(coef(model)))]
  sum(ar_coefs)
})

# Optional: Convert the list result to a vector for easier viewing
unlist(ar_coef_sums)

This will give you a list (or vector, if you use unlist()) where each entry is the sum of AR coefficients for the corresponding model in bsp_auto.

Quick Note

No matter if the intercept is named intercept or mean, filtering with grepl("^ar") will always work because auto.arima consistently names AR coefficients with the ar prefix followed by the lag number.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:33:51