r - rolling computations in xts by month part2 -
i want calculate var @ end of month historical method. time series start @ beginning of 2000 until now. calculation should start lets in 2005 have enough data. there similar post rolling computations in xts month , have tried modify code case. var @ end of each month should use past data.
here code (here starts in 2012 because otherwise take long):
library(quantmod) getsymbols("^gspc",return.class = "zoo",from = "2012-01-01",to = sys.date()) sp500 <- ad(gspc) ldr_sp500 <- return.calculate(sp500, method = "log") ldr_sp500 <- na.omit(ldr_sp500) idx <- index(ldr_sp500)[endpoints(ldr_sp500, 'months')] out <- lapply(idx, function(i) { as.xts(rollapplyr(as.zoo(ldr_sp500), 30, var)) }) sapply(out, nrow)
first of there big error in code. should width be? possible give output zoo object? iam beginner kind of functions... when dont want use historical method rather gaussian method use:
apply.monthly(as.xts(ldr_sp500), var, method="gaussian")
it seems works fine non overlapping periods...
in code, function in lapply
not use argument i
: computing same thing (the var each day in period) on , over.
in addition, default method var
modified
: need specify method="historical"
.
if want compute value @ risk daily returns of current month, suggestion use use apply.monthly
works:
apply.monthly(ldr_sp500, var, method="historical")
if want expanding window instead:
library(quantmod) library(performanceanalytics) getsymbols("^gspc", = "2012-01-01" ) x <- return.calculate( ad(gspc), method = "log" ) idx <- index(x)[endpoints(x, 'months')] result <- sapply( idx, function(i) var( x[paste0("/",i)], method = "historical" ) ) xts( result, idx )
Comments
Post a Comment