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

如何从auto.arima模型输出中提取ARIMA的非季节性与季节性阶数

提取auto.arima生成模型的阶数参数

方法一:直接调用模型对象的内置属性

forecast包生成的Arima模型对象自带明确的阶数属性,直接提取即可:

  • 非季节性阶数:test_model$p(AR阶数)、test_model$d(差分阶数)、test_model$q(MA阶数)
  • 季节性阶数:test_model$P(季节性AR阶数)、test_model$D(季节性差分阶数)、test_model$Q(季节性MA阶数)
  • 季节性周期:test_model$period

示例代码:

# 提取各阶数参数
p <- test_model$p
d <- test_model$d
q <- test_model$q
P <- test_model$P
D <- test_model$D
Q <- test_model$Q
period <- test_model$period

# 输出与模型打印一致的标识
cat(sprintf("ARIMA(%d,%d,%d)(%d,%d,%d)[%d]\n", p, d, q, P, D, Q, period))

运行后会输出:ARIMA(1,0,1)(2,1,0)[24],和模型的打印结果完全匹配。

方法二:正则表达式解析模型文本(备选)

如果遇到旧版本包导致属性不可用的情况,可以通过解析模型的打印字符串提取阶数:

# 捕获模型的打印文本
model_output <- capture.output(print(test_model))[2]
# 用正则匹配阶数
match_result <- regmatches(model_output, regexec("ARIMA\\((\\d+),(\\d+),(\\d+)\\)\\((\\d+),(\\d+),(\\d+)\\)\\[(\\d+)\\]", model_output))[[1]]

# 转换为整数
p <- as.integer(match_result[2])
d <- as.integer(match_result[3])
q <- as.integer(match_result[4])
P <- as.integer(match_result[5])
D <- as.integer(match_result[6])
Q <- as.integer(match_result[7])
period <- as.integer(match_result[8])

为什么之前的方法有问题?

  • test_model$arma返回的是长度为7的向量,顺序为c(p, d, q, P, D, Q, period),若对向量顺序不熟悉容易出错,不如直接调用命名属性直观。
  • test_model$model$phi、test_model$model$theta等返回的是模型的系数向量,而非阶数(阶数是向量的长度),这种方式需要区分季节性和非季节性系数,步骤繁琐且易混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 21:35:22