Aesthetics must be either length 1...

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.

Hi!

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:

Hi andresrcs, thanks for your answer. But my code is making an environment, so if you just copy-paste my code from above, that should do it.

Although I forgot to put in the packages that I'm using.

library(quantmod)
library(ggplot2)
library(reshape2)
library(timeDate)
library(gridExtra)
library(PerformanceAnalytics)
library(AnalyzeTS)
library(rugarch)
library(fGarch)
library(plotly)
library(forecast)

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.

Thanks for your reply. Could you give me an example on how to rewrite it into ggplot?

Here's an example:

library(tidyverse)

d = data.frame(time=1:500, 
               Var95_norm, 
               sp500_returns=sp500_returns[507:1006])

ggplot(d, aes(x=time)) + 
  geom_line(aes(y=Var95_norm)) + 
  geom_point(aes(y=sp500_returns, colour = sp500_returns < Var95_norm))

This chapter from R for Data Science might be helpful for learning the basics of ggplot2.

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