簡體   English   中英

對於我的外生變量的所有可能組合,是否有 R function 適合 ARIMAX model?

[英]Is there an R function to fit an ARIMAX model for all possible combinations of my exogenous variables?

我正在嘗試擬合 ARIMAX model 來描述和預測使用 7 個不同的外生變量時間序列(遷移、出生率、預期壽命......)的人口規模時間序列的發展。 使用以下代碼,您可以獲得 ARIMAX model 的結果,遷移作為外生變量:

auto.arima(population, xreg=migration)

也可以使用所有外生變量獲得 ARIMAX model 的結果:

reg_matrix<-cbind(migration,LEmale0,LEfemale0,LEmale65,LEfemale65,birthage,birthrate)
auto.arima(population, xreg=reg_matrix)

我想為所有可能的外生變量組合獲得 ARIMAX model 的結果。 顯然,我可以為所有可能的組合執行所描述的步驟。 但是由於有 7 個外生變量,因此有 5040 種不同的可能性(7 * 6 * 5 * 4 * 3 * 2 * 1),這需要很長時間,我不敢相信沒有更快的方法可以做到這一點。

我是 R 的新手,非常感謝您的幫助! 提前非常感謝!

我對auto.arima不太熟悉,並且您沒有提供足夠的數據示例來完全處理,但這可能會有所幫助。 假設變量順序無關緊要(可能在 arima 中)。 唯一組合的數量只有127。您可以生成一個列表,將它們auto.arima提供給auto.arima ,如下所示

library(DescTools)
library(purrr)

variables <- c("migration", "LEmale0", "LEfemale0", "LEmale65", "LEfemale65", "birthage", "birthrate")

combo_list <- list()
for (i in 1:length(variables)) {
   combo_list[[i]] <- DescTools::CombSet(variables, i, 
                                         repl = FALSE, 
                                         ord = FALSE, 
                                         as.list = TRUE)
}

combo_list <- purrr::flatten(combo_list)
length(combo_list)
#> [1] 127

# various points in the list

combo_list[[1]]
#> [1] "migration"
combo_list[[34]]
#> [1] "migration" "LEfemale0" "LEmale65"
combo_list[[127]]
#> [1] "migration"  "LEmale0"    "LEfemale0"  "LEmale65"   "LEfemale65"
#> [6] "birthage"   "birthrate"

如果您為上述答案中的所有可能組合創建了名稱,則可以嘗試此代碼。 我使用dplyr package 與 dataframe (管道運算符%>%select )一起工作。 您必須使用 {{}} 鏈接所選列的名稱,然后變為矩陣。 您可以將所有結果保存到列表 object 中

數據在 mydf 中。

library(DescTools)
library(purrr)

variables <- c("migration", "LEmale0", "LEfemale0", "LEmale65", "LEfemale65", "birthage", "birthrate")

combo_list <- list()
for (i in 1:length(variables)) {
   combo_list[[i]] <- DescTools::CombSet(variables, i, 
                                         repl = FALSE, 
                                         ord = FALSE, 
                                         as.list = TRUE)
}

combo_list <- purrr::flatten(combo_list)

###################################################################################
library(dplyr)
library(forecast)

mod <- list()

for (j in 1 : length (combo_list){ 
for (i in mydf %>% select(combo_list[[j]]) %>% colnames) {
  message("Working with ",i)
  mod[[i]] <- auto.arima(mydf$population, xreg=mydf %>% select({{i}}) %>% as.matrix)
  }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM