R语言中创建保留原结构的ARIMA模型列表方法问询
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. Usingc()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

