Search code examples
rplotxtszooecdf

Empirical CDF function `ecdf` does not work for an "xts" time series


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


Solution

  • 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