Hi there, I'm writing a thesis in econometrics. I'm analyzing financial timeseries and I'm forecasting Value at Risk-estimates with GARCH-models.

Now, I don't consider myself as a newbie in R, but this problem is really bothering me. I've tried to look around in the forum, because I know this has been asked about before.

My problem is, that I get the following error:

#Initializing
env_sp500 <- new.env()
getSymbols("^GSPC", env = env_sp500 , src = "yahoo", from = as.Date("2016-01-01"), to = as.Date("2020-01-01"), warnings = FALSE)
sp500_close <- sp500$GSPC.Close
sp500_returns <- Return.calculate(sp500_close, method = "discrete")
#GARCH specification
model.spec = ugarchspec(variance.model = list(model = 'sGARCH', garchOrder = c(1,1)),
mean.model = list(armaOrder = c(0,0)))
model.fit = ugarchfit(spec = model.spec, data = sp500_returns, solver = 'solnp')
#Forecasting
model.roll = ugarchroll(spec = model.spec, data = sp500_returns,
n.start = 500, refit.every = 50,
refit.window = 'moving')
fitdist(distribution = 'norm', x = sp500_returns)
Var95_norm <- mean(sp500_returns) + model.roll@forecast$density[,'Sigma']*qdist(distribution = 'norm', p = 0.05)
ptest <- (qplot(y = Var95_norm, x = 1:500, geom = 'line')
+ geom_point(aes(x = 1:500, y = sp500_returns[507:1006],
color = as.factor(sp500_returns[507:1006] < Var95_norm)))
+ scale_color_manual(values = c('gray', 'red'))
+ labs(y = 'Returns', x = 'Test Observations')
+ ggtitle('Backtesting')
+ theme(plot.title = element_text(hjust = 0.5)))
Error: Fejl: Aesthetics must be either length 1 or the same as the data (506): x

I have absolutely no clue how to fix this issue, not even by trying the fixes from before with this issue... Thanks in advance.

To help us help you, could you please turn this into a proper reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

The AnalyzeTS package was removed from CRAN, so I haven't run your code, but this error normally means that one of the data objects you've referenced inside aes doesn't have the same number of rows or elements as other data objects. It's difficult to tell where that's happening in your code, but maybe check the length of Var95_norm. Does it have 500 elements like the other vectors you're providing to qplot? More generally, I'd suggest putting all of these vectors into a single data frame and using ggplot (instead of qplot) to generate the plot. Using separate vectors with ggplot2 is prone to problems like this.

Here's a simple example that reproduces the error you're getting:

qplot(x=1:10, y=1:11, geom="line")
# Error: Aesthetics must be either length 1 or the same as the data (11): x

library(quantmod)
env_sp500 <- new.env()
getSymbols("^GSPC", env = env_sp500 , src = "yahoo", from = as.Date("2016-01-01"), to = as.Date("2020-01-01"), warnings = FALSE)
sp500_close <- sp500$GSPC.Close
#> Error in eval(expr, envir, enclos): object 'sp500' not found

^{Created on 2020-05-04 by the reprex package (v0.3.0)}

Not the case it breaks even at the third line and it is obviously not minimal since most of the code is not related with the error message you are showing, please read the guide and try to make a minimal self-contained reproducible example.