【问题标题】:Unexpected results when adding xts objects添加 xts 对象时出现意外结果
【发布时间】:2014-09-27 06:27:51
【问题描述】:

我在添加两个 xts 对象时遇到问题。

对象 1:bk

head(bk)
            iroqu
1962-07-03     0
1962-07-05     0
1962-07-06     0
1962-07-09     0
1962-07-10     0
1962-07-11     0

对象2:在for循环中计算为权重

                [,1]
1962-07-03        NA
1962-07-05 0.9374210
1962-07-06 0.9367212
1962-07-09 0.9452369
1962-07-10 0.9464487
1962-07-11 1.0819963

当我在for循环里面做的时候,

bk <- bk + alphas[i] * weight

bk 变成

bk
Data:
numeric(0)

Index:
numeric(0)

我检查了 alphas[i] * weight 不是问题,并且向量的尺寸也匹配。为什么我不能使用 + 号添加两个对象?有没有办法添加这些?我实际上只想要 1 列。

我正在尝试的完整示例来自博客http://optimallog.blogspot.in/2012/06/universal-portfolio-part-4.html first code sn-p

【问题讨论】:

  • 我猜bkweight 的时区不同,但我不能确定,因为您没有提供reproducible example
  • 我正在尝试此博客链接中的示例optimallog.blogspot.in/2012/06/universal-portfolio-part-4.html 代码 sn-p 以 Cover "Univ.... 的图 8.4 开头并以第一张图片结尾
  • 该代码无法运行。 nyse.cover.1962.1984 未定义(但可以用data 加载),crps 未定义(我也不知道应该是什么)。

标签: r xts


【解决方案1】:

从帖子中可以看出,问题是我在评论中建议的:这两个对象具有不同的时区(因此索引值不同)。日期索引的 xts 对象的索引时区应为 "UTC"

library(logopt)
data(nyse.cover.1962.1984)
x <- nyse.cover.1962.1984

# change the index timezone attribute
indexTZ(x) <- "UTC"
# force a recalculation of the actual index values
index(x) <- index(x)

xik <- x[,c("iroqu","kinar")]
nDays <- dim(xik)[1]
Days <- 1:nDays
pik <- cumprod(xik)
alphas <- seq(0,1,by=0.05)
bk <- xik[,1] * 0
w <- xik[,1] * 0
# crps should be alphas???
crps <- alphas
for (i in 1:length(crps)) {
  # we calculate bk by weighting the b by the realized wealth lagged one
  weight <- lag(crp(xik, c(alphas[i], 1-alphas[i])), 1)
  bk <- bk + alphas[i] * weight
  w <- w + weight
}

【讨论】:

  • 感谢约书亚! 1) 是的,需要加载数据。我错过了那一步。 2) alphas 是权重,用 alphas 替换 crps 是对的。
猜你喜欢
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 2018-08-17
  • 2018-07-18
  • 2013-11-07
  • 2015-05-30
相关资源
最近更新 更多