Deb Deb - 1 year ago 95
R Question

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)


enter image description here

Answer Source

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))

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download