Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据帧上的R日志转换_R_Transformation_Stocks - Fatal编程技术网

数据帧上的R日志转换

数据帧上的R日志转换,r,transformation,stocks,R,Transformation,Stocks,我有一个数据框(df),其中包含不同股票在不同日期(t)的值(V)。我想得到一个新的df,每个时间段都有盈利能力。 盈利能力为:项次(Vi_t/Vi_t-1) 其中: ln是自然对数 Vi_t是股票i在t日的价值 Vi_t-1相同股票在之前日期的价值 这是df[1:3,1:10]的输出 date SMI Bond ABB ADDECO Credit Holcim Nestle Novartis Roche 1 01/08/88 1507.5 3.63 4.98 159.20

我有一个数据框(df),其中包含不同股票在不同日期(t)的值(V)。我想得到一个新的df,每个时间段都有盈利能力。 盈利能力为:项次(Vi_t/Vi_t-1) 其中:

ln是自然对数

Vi_t是股票i在t日的价值

Vi_t-1相同股票在之前日期的价值

这是df[1:3,1:10]的输出

      date    SMI Bond  ABB ADDECO Credit Holcim Nestle Novartis Roche
1 01/08/88 1507.5 3.63 4.98 159.20  15.62  14.64   4.01     4.59 11.33
2 01/09/88 1467.4 3.69 4.97 161.55  15.69  14.40   4.06     4.87 11.05
3 01/10/88 1538.0 3.27 5.47 173.72  16.02  14.72   4.14     5.05 11.94
具体来说,我想要的是ln(1467.4/1507.5)的盈利能力,而不是[2,“SMI”]中的1467.4,并且对于数据帧中的所有其他值都是相同的。 因为我是新手,所以我被卡住了。我在考虑使用mapply之类的东西,自己创建转换函数。
非常感谢您的帮助。

这将计算利润率(假设数据位于data.frame调用
d
):

(d2<- log(embed(as.matrix(d[,-1]), 2) / d[-dim(d)[1], -1]))
#          SMI        Bond          ABB     ADDECO      Credit      Holcim     Nestle   Novartis       Roche
#1 -0.02696052  0.01639381 -0.002010051 0.01465342 0.004471422 -0.01652930 0.01239173 0.05921391 -0.02502365
#2  0.04699074 -0.12083647  0.095858776 0.07263012 0.020814375  0.02197891 0.01951281 0.03629431  0.07746368
d2$date <- d$date[-1]
(d2 <- apply(d[-1], 2, function(x) diff(log(x))))
#             SMI        Bond          ABB     ADDECO      Credit      Holcim     Nestle   Novartis       Roche
#[1,] -0.02696052  0.01639381 -0.002010051 0.01465342 0.004471422 -0.01652930 0.01239173 0.05921391 -0.02502365
#[2,]  0.04699074 -0.12083647  0.095858776 0.07263012 0.020814375  0.02197891 0.01951281 0.03629431  0.07746368