如果新列是根据函数计算的,如何保留动物园对象的日期?

How do I retain the dates of a zoo object if the new column is calculated from a function?

我从保留日期的 get.hist.quote 获得了动物园对象形式的每日收盘价。对数 returns 进行了计算和修剪以删除 NA 值。

stockEBAY$Data 将日期重新训练为 x 值,以便稍后使用 autoplot.zoo 进行绘图 stockEBAY$LogReturns 将日期重新训练为 x 值,以便稍后使用 autoplot.zoo

进行绘图
library("tseries")
library("zoo")
library("ggplot2")
AnalyzeStock <- function(ticker){
  DailyClosingPrice <- get.hist.quote(ticker,quote="Close",quiet=TRUE)  
  logreturns <- log(lag(DailyClosingPrice))-log(DailyClosingPrice)
  logreturns <- na.trim(logreturns, sides = "both")
  list(Data=DailyClosingPrice, LogReturns=logreturns)
}
stockEBAY <- AnalyzeStock("EBAY")
autoplot.zoo(stockEBAY$Data) + 
  xlab("Year") + ylab("Closing Price (U.S. Dollars)")

Plot of EBAY stock with year retained as x axis

然而,为了产生波动性,我不得不根据该函数创建一个新的数据框,但我不知道如何编写它以便将 stockEBAY$LogReturns 中的原始日期保留在新数据框。

Vol <- function(d, logreturns)
{
  var = 0
  lam = 0
  varlist <- c()
  for (r in logreturns) {
    lam = lam*(1 - 1/d) + 1
    var = (1 - 1/lam)*var + (1/lam)*r^2
    varlist <- c(varlist, var)
  }
  sqrt(varlist)
}
#retrieve volatility for decays 10, 30, and 100
vol10 <- Vol(10,stockEBAY$LogReturns)
vol30 <- Vol(30,stockEBAY$LogReturns)
vol100 <- Vol(100,stockEBAY$LogReturns)
plot(vol10,type="l",xlab="Year",ylab="Volatility")
lines(vol30,type="l", col="red")
lines(vol100,type="l",col="blue")

overlaying volatility plots, but the dates were not retained as the x axis and instead is now index, which messes up the graph

第 10 卷、第 30 卷、第 100 卷均以 x 轴作为索引,但缺少原始日期。

我希望 vol10、vol30 和 vol100 的新数据帧都保留来自 stockEBAY$LogReturns 的原始日期。

或者,是否有可能将新的波动率列附加到原始 stockEBAY$LogReturns 数据框?

我需要一种方法来更简洁地从函数求解波动率列,并将原始动物园对象的日期保留到新数据框中以供以后绘图叠加。

只要我可以绘制并叠加 3 个波动率列,以便 x 轴可以保留为日期,而不是索引,这就是目标。

谢谢

编辑:zoo 对象令人沮丧,....这不是很好

> stockEBAY$LogReturns$vol10 <- Vol(10,stockEBAY$LogReturns)
Error in NextMethod("[<-") : 
  number of items to replace is not a multiple of replacement length
In addition: Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
  invalid graphics state
2: In doTryCatch(return(expr), name, parentenv, handler) :
  invalid graphics state
> stockEBAY$LogReturns$vol30 <- Vol(30,stockEBAY$LogReturns)
Error: all(sapply(args, function(x) is.zoo(x) || !is.plain(x) || (is.plain(x) &&  .... is not TRUE
> stockEBAY$LogReturns$vol100 <- Vol(100,stockEBAY$LogReturns)
Error: all(sapply(args, function(x) is.zoo(x) || !is.plain(x) || (is.plain(x) &&  .... is not TRUE
> autoplot.zoo(stockEBAY$LogReturns$vol10)

日期保存在 zoo 对象的属性中。你可以通过 attr(stockEBAY$LogReturns,"index")。所以你可以设置一个新的 df 用于绘图如下...

df <- data.frame(Date=as.Date(attr(stockEBAY$LogReturns,"index")),
                 vol10=vol10,
                 vol30=vol30,
                 vol100=vol100)

然后使用 df$Date 作为 x 轴绘制。

plot(x=df$Date,y=df$vol10,type="l",xlab="Year",ylab="Volatility")
lines(df$Date,df$vol30,type="l", col="red")
lines(df$Date,df$vol100,type="l", col="blue")

谢谢 Andrew Gustar 提供的入门代码,我用它来输出我想要的图形,但如果其他人有更好的编码方法,我洗耳恭听

stockEBAY$LogReturns$vol10 <- Vol(10,stockEBAY$LogReturns$Close)
stockEBAY$LogReturns$vol30 <- Vol(30,stockEBAY$LogReturns$Close)
stockEBAY$LogReturns$vol100 <- Vol(100,stockEBAY$LogReturns$Close)
plot(stockEBAY$LogReturns$vol10,type="l",xlab="Year",ylab="Volatility", ylim = c(0,0.35))
par(new=TRUE)
plot(stockEBAY$LogReturns$vol30,type="l",xlab="Year",ylab="Volatility", ylim = c(0,0.35),  col="red")
par(new=TRUE)
plot(stockEBAY$LogReturns$vol100,type="l",xlab="Year",ylab="Volatility", ylim = c(0,0.35),  col="blue")

The graph