简体   繁体   English

将 R 中的 NA 替换为当前的 rollapply 值

[英]Replace NA's in R with the current rollapply value

I have a 10 year dataset from Tesla returns (2 day difference percentage)我有一个来自特斯拉回报的 10 年数据集(2 天差异百分比)

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:我还有一个感兴趣的 function:


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 (ie 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).现在我想以一种棘手的方式回溯测试 function:回顾期将是前 252 个交易日。然后每个月(即 21 天)我想评估风险。这意味着 21 天风险将保持不变。但是 rollapply function 输出 NA。我想用以前的值替换 NA,然后 plot y 值(y 列)和后向值(一个图中的后列)。

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:例如 output 看起来像这样:

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?理想情况下,我希望(例如)重复值 0.0927,直到出现下一个值 0.104。而后者以相同的方式(模式)重复。我该如何进行替换?

And then plot them together?然后 plot 他们在一起?

Use na.locf0 which stands for last occurrence carried forward.使用代表最后一次出现结转的 na.locf0。 Below we have simplified the code.下面我们简化了代码。 Omit facet = NULL if you want separate panels.如果您想要单独的面板,请省略 facet = NULL。 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,因此如果您没有加载 dplyr,您可以只写 lag 代替 stats::lag。 (dplyr clobbers R's lag with its own incompatible version.) (dplyr 用它自己的不兼容版本破坏了 R 的滞后。)

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)

截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM