【问题标题】:XTS object from Quantmod来自 Quantmod 的 XTS 对象
【发布时间】:2014-05-07 12:44:47
【问题描述】:

您好,我想知道如何将适用于单个代码的相同代码应用于具有多个代码的 XTS 对象。

这是适用于 1 个股票代码(收盘价)的代码:

getSymbols("IYR",from="1995-01-01",to="2014-01-01")

adj<-IYR$IYR.Adjusted
rtnM<-ROC(adj)[2:length(adj)]
r05<-rtnM[rtnM<= -.05]
plot(sort(r05),type='o',main='US ETF Drops 1995-present returns <= -5%')
100*sort(r05)

将这两条线应用到我的完整股票列表时,我似乎在挣扎

mb<- sub[sub<= -.05]
plot.xts(sort(mb),type='o',main='US ETF <= -5%')

如果我没有明确表示这是第一篇文章,我深表歉意。

> head(sub)
           DIA.Close    EEM.Close    EFA.Close    EWZ.Close    FXI.Close    GLD.Close

    GSG.Close
2012-01-03  0.0145024539  0.030116502  0.026891372  0.039461948  0.031336368  0.025528326  0.035154052
2012-01-04  0.0027463668 -0.005642487 -0.005913678  0.003678319 -0.014839976  0.005053908  0.004672906
2012-01-05 -0.0001613424 -0.004382015 -0.014939029 -0.013610224  0.006747285  0.006804694 -0.015267472
2012-01-06 -0.0033942169 -0.012739026 -0.013740366 -0.010031539 -0.014677091 -0.003682778  0.004428052
2012-01-09  0.0010518226  0.010411338  0.003858265  0.020800615  0.021100797 -0.004462870  0.001177510
2012-01-10  0.0058055315  0.021517224  0.015086276  0.020376751  0.022299616  0.013581474  0.007620201

> mb<- sub[sub<= -.05]
Error in `[.xts`(sub, sub <= -0.05) : 'i' or 'j' out of range

【问题讨论】:

  • sub 每列有一个代码......你想精确地设置什么子集?我的意思是 a) 仅行中所有字段小于 -.05 的行或 b) 每列删除所有小于 -0.05 的字段?
  • 作为旁注,您可以将前几行简化为rtnM &lt;- ROC(Ad(IYR), na.pad=FALSE)
  • 最重要的是,在对 xts 对象进行排序时,请确保使用 coredata: sort(coredata(r05)),没有它我不会对 r05 进行排序。
  • 非常感谢您的回答,他们非常棒。谢谢

标签: r xts quantmod


【解决方案1】:

假设您想为每个股票一起绘制小于 -5% 的排序值,

正如@GSee 上面所说的,前两行

adj<-IYR$IYR.Adjusted
rtnM<-ROC(adj)[2:length(adj)]

可以通过这种方式组合删除第一行的 NA:

#
rtnM <- na.omit(ROC(IYR[,6]))  # remove all NA's 

使用您的 xts 对象 sub 来保存 ROC 的每列,

# get a list per column, with all the fields that are <= -0.05
answer.ls <- lapply(1:ncol(sub), function(i) sub[sub[,i] <= -0.05, i])

如果你直接使用列表,那么你的函数会更简单:

# import libraries
library(xts)
library(quantmod)
library(RColorBrewer)

mySymbols <- c("IYR", "DIA", "EEM", "EFA", "EWZ", "FXI", "GLD", "GSG")

# get symbols
getSymbols(mySymbols,
           from = "1995-01-01",
           to = "2014-01-01")

# apply ROC to the last column of each of them, 
# get() will allow to access the object using the character name
sub.ls <- lapply(mySymbols,
                 function(symbol) na.omit(ROC(get(symbol)[,6])))

# check dimensions (all are different)
lapply(sub.ls, dim)

answer.ls <- lapply(sub.ls,
                    function(sub.ticker) sub.ticker[sub.ticker[,1] <= -0.05, 1])

# each ticker has a different dimension
lapply(answer.ls, dim)

然后画出来:

# plot each list
require(RColorBrewer)

# create colors palette
pal <- brewer.pal(length(answer.ls), "Set3")

# plot all series 
plot(sort(coredata(answer.ls[[1]])),
     type   = "o",
     col    = pal[1])
for(i in 2:length(answer.ls)){
   lines(sort(coredata(answer.ls[[i]])),
         col = pal[i])
}
legend("bottomright",
       legend  = mySymbols,
       col     = pal,
       pch     = c("o", rep("-", length(answer.ls) - 1)),
       lwd     = 4)

在对 XTS 对象进行排序之前不要忘记 coredata()

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 2020-07-19
    • 2014-12-25
    • 2018-09-03
    • 2019-09-02
    • 1970-01-01
    • 2016-11-30
    • 2012-11-18
    • 2013-05-27
    相关资源
    最近更新 更多