R:在数据表中使用quantmod的Delt [英] R: Using quantmod's Delt in a data.table

查看:393
本文介绍了R:在数据表中使用quantmod的Delt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 R data.table返回计算和设置()
我想问我如何使用 Delt()库(quantmod) data.table()中的时间序列。
到目前为止,感谢Frank,我有:

  set.seed(42)
DT< ; - data.table(
ticker = rep(letters,each = 5),
priceA = runif(5 * 26 ^ 2),
priceB = runif(5 * 26 ^ 2) )

DT [,粘贴('返回',LETTERS [1:2],sep =''):= {
lapply(.SD,function(x){
old< - head(x,-1)
new< - tail(x,-1)
c(NA,(new-old)/ old)
})
},by = ticker,.SDcols = grep('^ price',names(DT))]

结果(对于此种子值)为:

  ticker priceA priceB returnsA returnedB 
1:a 0.9148060 0.7956245 NA NA
2:a 0.9370754 0.9314941 0.02434327 0.1707710
3:a 0.2861395 0.6269996 -0.69464620 -0.3268883
4:a 0.8304476 0.1666758 1.90224707 -0.7341691
5:a 0.6417455 0.6483800 -0.22722939 2.8900659
---
3376:z 0.2887293 0.3473923 -0.54132570 -0.3514041
3377:z 0.9013438 0.1788842 2.12176058 -0.4850656
3378:z 0.3126429 0.7648157 -0.65313686 3.2754788
3379 :z 0.8791381 0.1300418 1.81195584 -0.3299698
3380:z 0.8160158 0.8159330 -0.07180019 5.2743905

我使用 Delt()(或类似的%更改函数)而不是 function(x){
old < ,-1)
new< - tail(x,-1)
c(NA,(new-old)/ old)
}
p>

很多谢谢!

解决方案

Delt 到一个简单的向量:

  DT [,
paste('returns',LETTERS [1:2],sep =''):= lapply(.SD,function(x)c(Delt(x))),
by = ticker,.SDcols = grep('^ price',names(DT))
]

/ p>

 股票价格A股价B回报A回报B 
1:a 0.9148060 0.7956245 NA NA
2:a 0.9370754 0.9314941 0.02434327 0.1707710
3:a 0.2861395 0.6269996 -0.69464620 -0.3268883
4:a 0.8304476 0.1666758 1.90224707 -0.7341691
5:a 0.6417455 0.6483800 -0.22722939 2.8900659
---


Following R data.table Return calculation and set() I would like to ask how I can use Delt() from library(quantmod) to find returns for a time-series in a data.table(). So far, thanks to Frank, I have:

set.seed(42)
DT <- data.table(
  ticker=rep(letters,each=5),
  priceA=runif(5*26^2),
  priceB=runif(5*26^2))

DT[,paste('returns',LETTERS[1:2],sep=''):={
  lapply(.SD,function(x){
    old <- head(x,-1)
    new <- tail(x,-1)
    c(NA,(new-old)/old)
  })
},by=ticker,.SDcols=grep('^price',names(DT))]

The result (for this seed value) is:

      ticker    priceA    priceB    returnsA   returnsB
   1:      a 0.9148060 0.7956245          NA         NA
   2:      a 0.9370754 0.9314941  0.02434327  0.1707710
   3:      a 0.2861395 0.6269996 -0.69464620 -0.3268883
   4:      a 0.8304476 0.1666758  1.90224707 -0.7341691
   5:      a 0.6417455 0.6483800 -0.22722939  2.8900659
  ---                                                  
3376:      z 0.2887293 0.3473923 -0.54132570 -0.3514041
3377:      z 0.9013438 0.1788842  2.12176058 -0.4850656
3378:      z 0.3126429 0.7648157 -0.65313686  3.2754788
3379:      z 0.8791381 0.1300418  1.81195584 -0.8299698
3380:      z 0.8160158 0.8159330 -0.07180019  5.2743905

How can I use Delt() (or a similar %change function) instead of function(x){ old <- head(x,-1) new <- tail(x,-1) c(NA,(new-old)/old) } ??

Many many thanks!

解决方案

You just need to convert the return value of Delt to a simple vector:

DT[,
  paste('returns',LETTERS[1:2],sep=''):=lapply(.SD,function(x) c(Delt(x))),
  by=ticker,.SDcols=grep('^price',names(DT))
]

This produces:

    ticker    priceA    priceB    returnsA   returnsB
 1:      a 0.9148060 0.7956245          NA         NA
 2:      a 0.9370754 0.9314941  0.02434327  0.1707710
 3:      a 0.2861395 0.6269996 -0.69464620 -0.3268883
 4:      a 0.8304476 0.1666758  1.90224707 -0.7341691
 5:      a 0.6417455 0.6483800 -0.22722939  2.8900659
---                                                   

这篇关于R:在数据表中使用quantmod的Delt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆