Deb Deb - 3 months ago 13
R Question

Empirical CDF in R

I am trying to plot the Empirical CDF of the daily returns distribution of S&P500 data. Below is the code I am trying to use. But as soon as I try to plot the ECDF, the graph doesn't look anything like a CDF graph. Please help me understand what I am doing wrong:--

library(quantmod) # Loading quantmod library

getSymbols("^GSPC", from = as.character(Sys.Date()-365*16)) # SPX price date for 16 yrs

SPX <- dailyReturn(GSPC)
SPX_ecdf <- ecdf(SPX)

plot(SPX_ecdf)


enter image description here

Answer

You need to use as.numeric or unclass to drop "xts" class first.

SPX_ecdf <- ecdf(as.numeric(SPX))
#or: SPX_ecdf <- ecdf(unclass(SPX))
plot(SPX_ecdf)

enter image description here