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()
}