plot not displaying the legend

Dear R users,

I have been trying to add a legend to a plot but it doesn't display. I have tried many different things without any luck.

It follows my R code

library(Quandl)
gold <- Quandl("CHRIS/CME_GC1",api_key="BscK4p1XrW6yXwNP7Qgh", start_date="2016-01-01",end_date="2020-12-29")
wti <- Quandl("CHRIS/CME_CL1",api_key="BscK4p1XrW6yXwNP7Qgh", start_date="2016-01-01",end_date="2020-12-29")
plat <- Quandl("CHRIS/CME_PL1",api_key="BscK4p1XrW6yXwNP7Qgh", start_date="2016-01-01",end_date="2020-12-29")
corn <- Quandl("CHRIS/CME_C1",api_key="BscK4p1XrW6yXwNP7Qgh", start_date="2016-01-01", end_date="2020-12-29")
soy <- Quandl("CHRIS/CME_S1",api_key="BscK4p1XrW6yXwNP7Qgh", start_date="2016-01-01", end_date="2020-12-29")
ore <- Quandl("CHRIS/CME_TIO1",api_key="BscK4p1XrW6yXwNP7Qgh", start_date="2016-01-01", end_date="2021-12-29")


#### Keep only colluns that I want - Removendo as colunas que não interessam para o estudo
corn <- corn[-c(2:6,8:9)]
gold <- gold[-c(2:6,8:9)]
ore <- ore[-c(2:6,8:9)]
plat <- plat[-c(2:6,8:9)]
soy <- soy[-c(2:6,8:9)]
wti <- wti[-c(2:6,8:9)]

### Converter df para daily time seires objects 

## CORN

# Converter df to time series
date_corn <- as.Date(as.character(corn$Date), "%Y-%m-%d")
Set_corn <- as.numeric(corn$Settle)

library(xts)
ts_corn <- xts(Set_corn, date_corn)
ts_corn

# Alterar titulo coluna
colnames(ts_corn) <- c("corn")

# Calcular retornos
ret_corn <- (diff(log(ts_corn$corn)))*100
#Plot w/zero line
plot(ret_corn,ylab="",xlab="")
abline(h=0,col="dark grey",lty=3,lwd=3)

# Fazer diagnostico da série temporal- preciso avisar para comando esquecer dados faltantes
acf(ret_corn$corn,na.action = na.pass)
pacf(ret_corn$corn,na.action = na.pass)



#Install and use 'rugarch' package
library(rugarch)


# Eliminar NA da dataframe
ret_corn = ret_corn[-1,]
# Contar numero de NA
apply(is.na(ret_corn),2,sum)


#specify GARCH(1,1) model
#note: "s" for "standard"
#GARCH order = (1,1)
#mean model = AR(1)
g1<-ugarchspec(variance.model=list(model="sGARCH",garchOrder=c(1,1)),mean.model=list(armaOrder=c(1,0)),distribution.model="std") 
#fit model
garch11<-ugarchfit(g1,data = ret_corn$corn)
#examine coefficients and model
garch11

# Obter série de volatilidade do modelo GARCH estimado
vol_corn <- garch11@fit$sigma^2

# Converter para time series - inseri na mesma base de dados corn: 
corn_ret_vol <- cbind2(ret_corn, vol_corn)
# Alterar nome das variáveis na dataframe
colnames(corn_ret_vol) <- c("corn","vol")

#Plot with axes and header
plot(corn_ret_vol$vol,xlab="",ylab="",main="Corn Volatility (GARCH[1,1])")


# EXPONENTIAL GARCH (EGARCH): This allows for asymmetries between positive and negative shocks

g1e<-ugarchspec(variance.model=list(model="eGARCH",garchOrder=c(1,1)),mean.model=list(armaOrder=c(1,0)),distribution.model="std")

#fit model
garch11e<-ugarchfit(g1e,data = ret_corn$corn)

# Coeficientes 
coef(garch11e)

#examinar modelo

garch11e
#AIC, coefficients -> "Standard" GARCH probably preferred

# Obter volatilidade estimado pelo EGARCH

vol_e_corn <- garch11e@fit$sigma^2

# Outra forma de inserir dados - converter série em time series
corn_data <- merge(corn_ret_vol, vol_e_corn)

# Confirmar que não existem dadosaltantes na nova base de dados:
apply(is.na(corn_data),2,sum)

plot(corn_data$vol_e_corn,xlab="",ylab="",main="Corn Volatility (EGARCH[1,1])")


#How similar are the series?
cor(corn_data$vol,corn_data$vol_e_corn)

ot(x = corn_data[,"vol"], xlab = "Date", ylab = "",
     main = "SGARCH x EGARCH ", ylim = c(0.0, 7), major.ticks= "years",
     minor.ticks = FALSE, col = "red")
lines(x = corn_data[,"vol_e_corn"], col = "blue")
legend(x = "center",
       col = c("red", "blue"), lty = 1, lwd = 1,
       legend = c('SGARCH', 'EGARCH'))

Thank you all,

Max

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.