Need Help with GARCH analysis on R

I have designed a code on garch models and need someone to review it and answer my questions.

I have constructed a code that should check a number of models of garch type and should retrieve the one with smallest aic (optimal model) but i want the code to work optimally and also help on plotting the volatility prediction on r

library(rugarch)
library(astsa)
library(xts)
library(haven)
library(stats)
library(readxl)

wheat <- read_excel("/wheat.xlsx")
January<-read_excel("/wheat_jan.xlsx")

logret<-wheat$Logret
date<-as.Date(wheat$Date)
sample_mean<-mean(logret)
logret_w<- xts((logret-sample_mean), date)*1000
plot(logret_w,date,main="Log-returns of wheat")
price_wheat<-xts(wheat$Price,date)
dates<-as.Date(January$Date)

Tests<-function(returns){
  library(aTSA)
  library(FinTS)
  stationarity<-pp.test(returns)#stationarity test
  arch_eff<-ArchTest(returns, 30, demean=TRUE) #less than 0.05=reject Null Hyp = ARCH effects
  print(stationarity)
  print(arch_eff)
}

Tests(logret_w)

Garch_ch<-function(a,b,c,d,e,f,reg){
  garch_model<-ugarchspec(variance.model=list(model=e,garchOrder=c(c,d),external.regressors=reg),mean.model=list(armaOrder=c(a,b)),distribution.model = f) 
  egarchsnp.fit<- ugarchfit(garch_model, logret_w,out.sample = 1, solver = "hybrid") 
  print(egarchsnp.fit)
}

patata<-Garch_ch(0,0,1,1,"eGARCH","norm",NULL)


#model<-ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)), 
                  #mean.model = list(armaOrder = c(0, 0), include.mean = FALSE), 
                  #distribution.model = "norm")
#modelfit<-ugarchfit(spec=model,data=logret_w)
#spec = getspec(modelfit);
#setfixed(spec) <- as.list(coef(modelfit));
#forecast = ugarchforecast(spec, n.ahead = 26, n.roll = 1874, data = logret_w[1:1875, ,drop=FALSE], out.sample = 1874)
#plot(forecast)
#vol<-sigma(forecast)

kremmydi<-matrix(nrow = 2, ncol = 2)
models<-c("sGARCH","eGARCH","gjrGARCH")
distr<-c("norm","std","ged","nig","ghyp")
min_AIC<-matrix(nrow = 5,ncol = 3)
colnames(min_AIC)<-c("sGARCH","eGARCH","gjrGARCH")
rownames(min_AIC)<-c("norm","std","ged","nig","ghyp")
where_min_AIC<-vector()
which_distr<-vector()


for(k in 1:length(models)){
  for(l in 1:length(distr)){
    for(i in 1:2){
      for(j in 1:2){
        patata<-Garch_ch(0,0,i,j,models[k],distr[l],NULL)
        kremmydi[i,j]<-infocriteria(patata)[1]
      }
    }
    min_AIC[l,k]<-round(min(kremmydi),4)
    print(min_AIC)
    where_min_AIC[k]<-patata@model$modeldesc$vmodel
    which_distr[l]<-patata@model$modeldesc$distribution
  }
}

###The above function should check all garch models and should choose the optimal (smallest AIC value) and show distribution 
###moddel name and parameters
###Then I wanna plot the predicted volatility and use rmse or similar to see if the prediction is good or not


patata<-Garch_ch(0,0,1,1,"eGARCH","nig",NULL)
forecast<-ugarchforecast(patata, n.ahead = 21,n.roll = 1,out.sample = 1)
sim = ugarchsim(patata, n.start = 1,n.sim = 21, m.sim = 5, startMethod="sample")
forecasting<-xts(forecast@forecast$sigmaFor,Jun_Date)
prediction_<-xts(sim@simulation$seriesSim,Jun_Date)

dataset: https://www.kaggle.com/norendas/freelancer

© Copyright 2013-2020 Analytics Vidhya