0

I have estimated several VAR models, which I would now like to compare looking at the residuals. I have tried several ways to access the residuals but none seem to work. Maybe it is because R does not seem to understand when I indedx $pval, as the picture here shows enter image description here:

 library(vars)
 optimal_lag <- VARselect(energy.bv, lag.max = 10, type = "both")$selection["AIC(n)"]
 model_season_none <- VAR(energy.bv, p = optimal_lag, type = "none", season = 12)
 model_season_const <- VAR(energy.bv, p = optimal_lag, type = "const", season = 12)
 model_season_trend <- VAR(energy.bv, p = optimal_lag, type = "trend", season = 12)
 model_season_both <- VAR(energy.bv, p = optimal_lag, type = "both", season = 12)

 summary(model_season_none)
 # I checked there was no non finite values 
 if (any(!is.finite(model_season_none$residuals))) {
   cat("Non-finite values exist in the residuals\n")
 } else {
   cat("No non-finite values in the residuals\n")
 }


 serial_test_none <- serial.test(model_season_none, lags.pt = 12,type = "PT.asymptotic")
 serial_test_const <- serial.test(model_season_const, lags.pt = 12,type = 
 "PT.asymptotic")
 serial_test_trend <- serial.test(model_season_trend, lags.pt = 12,type = 
 "PT.asymptotic")
 serial_test_both <- serial.test(model_season_both, lags.pt = 12,type = "PT.asymptotic")


 cat("P-values pour le test d'auto corrélation des résidus : \n")
 cat("None Season: ", serial_test_none$pval, "\n")
 cat("Constant Season: ", serial_test_const$pval, "\n")
 cat("Trend Season: ", serial_test_trend$pval, "\n")
 cat("Both: ", serial_test_both$pval, "\n")

Seeing as it didn't print anything (except the character strings, but not the p-values of the tests), I tried this :

 print("P-values pour le test d'auto corrélation des résidus : ")
 print(paste("None Season: ", serial_test_none$pval))
 print(paste("Constant Season: ", serial_test_const$pval))
 print(paste("Trend Season: ", serial_test_trend$pval))
 print(paste("Both: ", serial_test_both$pval))

I didn't work too, so I tried this :

 res_df <- data.frame(Model = c("None Season", "Constant Season", "Trend Season", 
 "Both"),
                      P_value = c(serial_test_none$pval, serial_test_const$pval, 
 serial_test_trend$pval, serial_test_both$pval))

  # Access the p-values:
 res_df$P_value[1] # None Season
 res_df$P_value[2] # Constant Season
 res_df$P_value[3] # Trend Season
 res_df$P_value[4] # Both

Here is the message of error I got :

 Error in data.frame(Model = c("None Season", "Constant Season", "Trend Season",  : 
    les arguments impliquent des nombres de lignes différents : 4, 0

Could anyone help me to access the p-values of this test?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128

0 Answers0