loop :(

mail@pastecode.io avatar
unknown
r
2 years ago
975 B
2
Indexable
Never
# Create a list to store the models
all_models <- list()

# Use expand.grid() to generate all possible combinations of p1, d1, and q1
combinations <- expand.grid(p1 = 0:3, d1 = 0:2, q1 = 0:2)

# Loop through each combination
for (i in 1:nrow(combinations)) {
  # Extract the values of p1, d1, and q1 from the current row
  p1 <- combinations[i, "p1"]
  d1 <- combinations[i, "d1"]
  q1 <- combinations[i, "q1"]
  
  # Create the model with the current values of p1, d1, and q1
  # The combinations in the if statements are to prevent invalid models
  if(!(d1 == 0 && q1 > 0)  && !(p1 == 3 && d1 < 2 ) 
     && !(p1 > 0 && d1 ==0 && q1 == 0)) {
    # Add the valid models are added to the list
    all_models[[i]] <- box_US_GDP %>% model(ARIMA(GDP ~ pdq(p1,d1,q1)))
  }
  # Removing nulls being caused by a bug
  all_models <- compact(all_models)
}

for (i in 1:length(all_models)) {
  print(i)
  final_models <- box_US_GDP %>% model(all_models[[10]])
  report(final_models)
}