Reputation: 31
I'm sure this is very obvious but i'm a begginer in R and i spent a good part of the afternoon trying to solve this...
I'm trying to create a rolling window to calculate the Value at Risk (VaR) over time.
I already calculated the unconditional VaR for my entire timeserie of 7298 daily returns.
Now, what i'm trying to do is do a rolling window that calculates VaR for a window of 25 days that will roll every one observation for my entire timeserie.
I tried
apply.rolling(nas, trim = TRUE, gap = 25, by = 1, FUN = function(x) VaR(R = nas, p = 0.99, method="historical"))
and
rollapply(nas, width = 25, FUN = function(x) VaR(R = nas, p = 0.99, method="historical"))
where nas
is my time serie.
My code still runs from an hour ago... I don't know what I did wrong...
Thank you very much in advance for any help you can provide.
H.
Upvotes: 1
Views: 1474
Reputation: 782
It should be:
rollapply(nas, width = 25, FUN = function(x) VaR(R = x, p = 0.99, method="historical"))
Basically you applying a function that takes in value x
(which is a filtered nas
to 25 time units), and produces output based on the x
. In your original attempt, the function was function(x) VaR(R = nas, p = 0.99, method="historical")
, so it takes in value x
, but still calculate VaR of the whole nas
, and it does that >7000 times, hence it takes forever.
Upvotes: 2