如何计算R中标准差的滚动窗口?

时间:2018-04-21 23:43:14

标签: r stock standard-deviation

我尝试下载股票价格并计算滚动窗口中的标准差。我找到了库PerformanceAnalytics,但它们只有Mean而非标准偏差的滚动窗口。

library("tseries")
library("zoo")
library("forecast")
library("FinTS")
library("rugarch")


AAL.data = get.hist.quote(instrument="AAL", start="2014-01-01",  end="2018-01-01", quote="AdjClose", provider="yahoo", compression="d", retclass="zoo")
plot(AAL.data, main = "AAL closing price", ylab = "Price (USD)", xlab = "Date")

#install.packages("PerformanceAnalytics")
library(PerformanceAnalytics)
chart.RollingMean(XOM.sr, width = 60, xaxis = TRUE, ylim = NULL)

2 个答案:

答案 0 :(得分:2)

试试这个:

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

有关详细信息,请参阅?rollapply

答案 1 :(得分:0)

对于如今正在寻找的人,请使用roll_sd软件包(rdrr.io link)中的roll

                          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