1

I when I execute the code inside the loop by itself (assign the loop variable manually), I get the correct functionality and the pdf plots work. When I run the full loop, I get the empty plots. I don't understand what the problem looping over this code is but it appears that looping produces empty plots.

for (garch_model_name in c('sGARCH', 'eGARCH', 'gjrGARCH')) {
  dist = 'jsu'
  p = 1
  q = 1

  print(garch_model_name)
  
  roll = list('start'=N, 'window'='expanding', 'k'=1) # window can be 'moving' or 'expanding'
  res = fit_GARCH(ret_d, resample=list(period='days', k=1), garchModel=garch_model_name, garchOrder=c(q,p), distModel=dist, roll=roll)
  spec = res$model
  mod = res$fit
  
  df = mod@forecast$density
  xdf = as.xts(df)
  tzone(xdf) = 'UTC'
  predictions = xdf$Sigma
  
  predictions_list[[garch_model_name]] = predictions
  
  realized = tail(RV, M)
  
  rmse_list[[garch_model_name]] = calc_rmse(predictions, realized)
  mae_list[[garch_model_name]] = calc_mae(predictions, realized)
  rmedse_list[[garch_model_name]] = sqrt(median((realized - predictions)^2))
  mape_list[[garch_model_name]] = mean(abs((realized - predictions)/realized))
  
  mz = lm(realized ~ predictions)
  s = summary(mz)
  mz_list[[garch_model_name]] = s$r.squared
  
  df = cbind(realized, predictions)
  colnames(df) = c('Realized', 'Forecast')
  pdf(file=sprintf('results/out_of_sample/realized_predicted_%s.pdf', garch_model_name))
  plot.xts(df, main='Realized vs Forecast', legend.loc='topright', col=c('black', 'red'), lty=c(1,1), lwd=c(1,2))
  # grid()
  dev.off()
  
  errors = (realized - predictions)/realized * 100
  
  pdf(file=sprintf('results/out_of_sample/forecast_errors_%s.pdf', garch_model_name))
  plot(errors, main=garch_model_name, ylab='% error')
  # grid()
  dev.off()
}
s5s
  • 11,159
  • 21
  • 74
  • 121
  • @akrun Seems to work. Still, doesn't explain why if I execute the code inside the loop it works but when I execute the entire loop it doesn't. I don't get any errors or warning messages, just empty plots – s5s Aug 22 '21 at 20:06
  • 1
    Without a reproducible example it is difficult to know. Can you add a `print` statement after the `df = cbind(realized, predictions)` i..e `print(head(df))`. Also, try to `print(plot(df, ...` without the `pdf` and see if that it is plotting or not – akrun Aug 22 '21 at 20:08
  • @akrun The print() fixed it. I found another question about the same problem. It's a common issue apparently. I don't understand why code inside a loop works differently than if it were outside - that's a bug as far as I'm concerned. – s5s Aug 22 '21 at 21:20
  • It's not bug; this behavior is often desireable. The R console is a REPL. It only prints the result of the last expression evaluated. In the case of a `for` loop, the loop itself returns NULL invisibly so nothing is printed. It's basically the same way R doesn't call `print()` on every line in a function body. You need to be explicitly about what you want to print when running the code. Something the results of `print()` may have big computational side effects that you don't want during a loop. – MrFlick Aug 23 '21 at 00:09

0 Answers0