2016-09-16 55 views
2

试图把误差线在时间序列图plot.xts[R plot.xts误差线使用

> myTS[1:20] 
      [,1] 
2013-07-01 29 
2013-07-03 24 
2013-07-03 16 
2013-07-03 16 
2013-07-03 12 
2013-07-03 12 
2013-07-03 16 
2013-07-03 21 
2013-07-03 21 
2013-07-03 16 
2013-07-05 12 
2013-07-05 12 
2013-07-05 12 
2013-07-05 12 
2013-07-08 16 
2013-07-08 23 
2013-07-08 16 
2013-07-08 12 
2013-07-09 16 
2013-07-09 12 

我这个聚合使用myTSquarterly = apply.quarterly(myTS,mean)

> myTSquarterly 
       [,1] 
2013-09-30 24.50829 
2013-12-31 23.79624 
2014-03-31 24.15170 
2014-06-30 24.57641 
2014-09-30 23.71467 
2014-12-31 22.99500 
2015-03-31 24.50423 
2015-06-30 25.19950 
2015-09-30 24.76330 
2015-12-31 24.65810 
2016-03-31 25.35616 
2016-06-30 22.71066 
2016-07-27 20.63636 

我可以用plot.xts(myTSquarterly)绘制容易:

enter image description here

我可以计算很容易与标准偏差apply.quarterly(myTS,sd)

我想添加这些标准偏差信息作为错误条图,但我找不到这样的方法?

回答

2

如果您打算使用xts进行绘图,您可能需要考虑使用版本为v0.10的xts的开发版本(我认为CRAN版本仍然较低),其中plot.xts的绘图功能已得到改进。 。然后检查简单示例?plot.xts

你想做的事可以做什么如下:

# Want xts v0.10.0 
library(devtools) 
install_github("joshuaulrich/xts") 


library(quantmod) 
getSymbols("GOOG") 
myTS <- GOOG[, 4] 
myTSquarterly_mean <- apply.quarterly(myTS,mean) 
myTSquarterly_sd <- apply.quarterly(myTS, sd) 
c <- 2 
plot(myTSquarterly_mean) 
lines(myTSquarterly_mean - c * myTSquarterly_sd, col = "red") 
lines(myTSquarterly_mean + c * myTSquarterly_sd, col = "red", pch = 17) 
points(myTSquarterly_mean + c * myTSquarterly_sd, col = "red", pch = 17) 

enter image description here

(如果你想使用的是旧xts库的一个情节绘制多条曲线继续,考虑转换时间系列到动物园类型(as.zoo),然后绘图)

+0

使用quantmod的“chartSeries”和添加Bollinger乐队与标准的开发人员可以很容易地创建相同的图表。 2. – hvollmeier

+2

没错,尽管OP在询问有关'xts'的绘图,而不是'quantmod'绘图功能。 – FXQuantTrader