samedi 12 décembre 2020

Print Only When Condition is True in R

This solution Automate Seed as a Vector Instead of an Integer in R

library(forecast)
SEED_vector <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)
arima_order_results = data.frame()
for (my_seed in SEED_vector){
  set.seed(my_seed)
  ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
  ar2 <- auto.arima(ar1, ic ="aicc")

  arima_order = arimaorder(ar2)
  arima_order = t(as.data.frame(arima_order))
  # Print the arima order.
  print(arima_order)
  # This line of code is just if you want to store the results in a dataframe
  arima_order_results = rbind(arima_order_results,arima_order)
}

I will only want arimaorder of (1, 0, 0) to be printed instead of all

I tried this and jammed with error:

SEED_vector <- 1:100
arima_order_results <- data.frame()
while(TRUE){for (my_seed in SEED_vector) {set.seed(my_seed)
    ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
    ar2 <- auto.arima(ar1, ic ="aicc")
    if(all(arimaorder(ar2)==c(1,0,0))) print(arima_order_results)
    arima_order = arimaorder(ar2)
    arima_order = t(as.data.frame(arima_order))
    # Print the arima order.
    print(arima_order)
    # This line of code is just if yo uwant to store the results in a dataframe
    arima_order_results = rbind(arima_order_results,arima_order)

}}

I want something like this:

#arima_order14 1 0 0 
#arima_order39 1 0 0 
#arima_order47 1 0 0 

Aucun commentaire:

Enregistrer un commentaire