I have a 10 year dataset from Tesla returns (2 day difference percentage)
tsla <- quantmod::getSymbols("TSLA", from = base::as.Date("2011-01-01"), to = base::as.Date("2022-01-31"), auto.assign = F)
tsla = as_tibble(tsla)
head(tsla)
Also I have a function of interest :
alpha = 0.01
garnor = function(x){
require(fGarch)
t = length(x)
fit = garchFit(~garch(1,1),data=x,trace=F,cond.dist="norm")
m = fit@fitted
cv = fit@sigma.t
var = m+cv*qnorm(1-alpha)
return(var[t])
}
Now I want to back test the function back in a tricky way: The look-back period will be the first 252 trading days.And then every month (i.e 21 days) I want to assess the risk.Which means that for 21 days the risk will remain the same.But the rollapply function outputs NA.I want to replace the NA with the previous value and then plot the y values (y column) and the back values (back column in one plot).
d = tsla%>%
dplyr::select(TSLA.Adjusted)%>%
dplyr::mutate(Close = TSLA.Adjusted)%>%
dplyr::mutate(y = as.numeric((Close - dplyr::lag(Close, 2)) / Close))%>%
dplyr::select(Close,y)%>%
tidyr::drop_na()%>%
dplyr::mutate(back = zoo::rollapplyr(y,width = 252,FUN = garnor,by = 21,fill=NA))
for example the output looks like this :
252 5.54 -0.0307 0.0927
253 5.42 -0.0354 NA
254 5.38 -0.0297 NA
255 5.45 0.00477 NA
256 5.52 0.0257 NA
257 5.65 0.0347 NA
258 5.65 0.0223 NA
259 4.56 -0.239 NA
260 5.32 -0.0620 NA
261 5.36 0.150 NA
262 5.35 0.00598 NA
263 5.32 -0.00789 NA
264 5.35 0.000374 NA
265 5.48 0.0299 NA
266 5.59 0.0429 NA
267 5.79 0.0525 NA
268 5.87 0.0464 NA
269 5.91 0.0213 NA
270 5.81 -0.00894 NA
271 5.92 0.000338 NA
272 6.05 0.0390 NA
273 6.23 0.0504 0.104
274 6.36 0.0487 NA
275 6.32 0.0142 NA
276 6.39 0.00407 NA
277 6.52 0.0301 NA
278 6.22 -0.0267 NA
279 6.30 -0.0346 NA
280 6.63 0.0624 NA
281 6.72 0.0628 NA
282 6.84 0.0295 NA
283 6.99 0.0392 NA
284 6.9 0.00928 NA
285 6.84 -0.0219 NA
286 6.91 0.000869 NA
287 6.75 -0.0139 NA
288 6.72 -0.0271 NA
289 6.76 0.00177 NA
290 6.68 -0.00629 NA
291 6.88 0.0174 NA
292 6.81 0.0185 NA
293 6.75 -0.0190 NA
294 6.62 -0.0281 0.0950
295 6.62 -0.0196 NA
296 6.61 -0.00121 NA
297 6.95 0.0466 NA
298 7.20 0.0816 NA
299 7.22 0.0374 NA
300 7.06 -0.0204 NA
which ideally I want (for example) the value 0.0927 to be repeated until the next value 0.104 occurs.And the latter to be repeated in the same fashion (pattern).How can I do this replacement?
And then plot them together ?
Use na.locf0 which stands for last occurrence carried forward. Below we have simplified the code. Omit facet = NULL if you want separate panels. The code below does not use dplyr so you can write just lag in place of stats::lag if you don't have dplyr loaded. (dplyr clobbers R's lag with its own incompatible version.)
library(quantmod)
library(fGarch)
library(ggplot2)
garnor <- function(x, alpha = 0.01) {
t <- length(x)
fit <- garchFit(~ garch(1,1), data = x, trace = FALSE, cond.dist = "norm")
m <- fit@fitted
cv <- fit@sigma.t
var <- m+cv*qnorm(1-alpha)
var[t]
}
getSymbols("TSLA", from = "2011-01-01", to = "2022-01-31")
adj <- Ad(TSLA)
y <- na.omit(1 - stats::lag(adj, 2) / adj)
back <- na.locf0(rollapplyr(y, 252, by = 21, garnor))
names(back) <- "Back"
autoplot(cbind(y, back), facets = NULL)