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

R语言中创建保留原结构的ARIMA模型列表方法问询

How to Preserve ARIMA Model Structure in R Lists and Data Tables?

Great question—let's break down what's happening and fix this step by step.

First, let's clarify the core issue: when you use c(m1, m2) or directly assign ARIMA models to standard data.table columns, R is flattening the model objects (which are S3 list-based structures) into their individual components, rather than keeping the full, intact model objects.

1. Correctly Creating a List of ARIMA Models

Your initial attempt with m.list <- list(m1, m2) is actually the right approach! This list stores each full ARIMA model as a separate element, preserving all their structure.

To verify and use it:

# Check the class of the first model in the list
class(m.list[[1]])  # Returns "Arima" (full model object)

# Access model components just like you would with the original m1/m2
m.list[[1]]$coef       # Get coefficients
m.list[[1]]$residuals  # Get residuals
m.list[[1]]$aic        # Get AIC value

The mistake with m.comb <- c(m1, m2) is that c() merges the components of the two model lists into a single flat list, rather than keeping each model as a distinct element. That's why m.comb[1] only returns a coefficient instead of the full model.

2. Storing Full ARIMA Models in a Data Table

If you want to store complete models in a data.table, you can't assign them directly to regular columns (since data.table will try to unpack the model components). Instead, use list columns—columns where each element is a full model object:

library(data.table)

# Create a data.table with a list column for models, plus a name column for clarity
m.dt <- data.table(
  model_label = c("ARIMA(0,0,5)", "ARIMA(0,1,1)"),
  model_obj = list(m1, m2)
)

# Access the full model from the data table
class(m.dt$model_obj[[1]])  # Returns "Arima" (intact model)

# Use the model just like the original
m.dt$model_obj[[2]]$coef  # Get coefficients for the second model
m.dt$model_obj[[2]]$fitted  # Get fitted values

Why Your Previous Attempts Failed

  • c(m1, m2): ARIMA models are S3 list objects. Using c() on two lists concatenates their elements, so you end up with a single list of all the components from both models (e.g., coefficients, residuals, etc.), not a list of two full models.
  • data.table(a=m1, b=m2): Data tables automatically unpack complex objects into separate columns when you assign them directly. This breaks the model's structure, leaving you with disconnected components instead of a usable model object.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:24:43