How can compute rolling window for standard deviation in R?

后端 未结 2 743
离开以前
离开以前 2021-01-16 11:29

I tried to download the stock price and compute the standard deviation in rolling window. I found the library PerformanceAnalytics but they only have the rollin

相关标签:
2条回答
  • 2021-01-16 11:59

    Try this:

    library(zoo)
    rollapplyr(1:10, 3, sd, fill = NA)
    ## [1] NA NA  1  1  1  1  1  1  1  1
    

    See ?rollapply for more info.

    0 讨论(0)
  • 2021-01-16 12:06

    For who is looking for this nowdays, use roll_sd of roll package (rdrr.io link).

                              expr        min         lq        mean   median        uq        max neval
     rollapply(STK, width = 5, sd) 34703.2817 37535.3761 40422.77272 38005.02 38357.489 53512.7018     5
                   roll_sd(STK, 5)    18.9334    18.9973    23.90904    19.14    30.608    31.8665     5
    
    0 讨论(0)
提交回复
热议问题