如何计算不同跨度内不同行的百分比变化 [英] How to calculate percentage change from different rows over different spans

查看:27
本文介绍了如何计算不同跨度内不同行的百分比变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算由 gvkey(1001、1384 等...)识别的公司的季度数据的价格变化百分比.它是相应的季度股票价格,PRCCQ.

I am trying to calculate the percentage change in price for quarterly data of companies recognized by a gvkey(1001, 1384, etc...). and it's corresponding quarterly stock price, PRCCQ.

    gvkey  PRCCQ
1   1004 23.750
2   1004 13.875
3   1004 11.250
4   1004 10.375
5   1004 13.600
6   1004 14.000
7   1004 17.060
8   1004  8.150
9   1004  7.400
10  1004 11.440
11  1004  6.200
12  1004  5.500
13  1004  4.450
14  1004  4.500
15  1004  8.010

我想要做的是添加 8 列,显示 1 个季度回报、2 个季度回报等,一直到 8 个季度.我已经能够通过使用 quantmoddelt 函数和 plyrddply 来计算每个 PRCCQ 的 1 个季度回报>,我还可以通过更改 k 使用相同的代码获得第二季度的回报.

What I am trying to do is add 8 columns showing 1 quarter return, 2 quarter return, etc. all the way to 8 quarters. I have been able to calculate 1 quarter return for each PRCCQ by using the delt function of quantmod and ddply of plyr, and I was also able to get the 2 quarter return using the same code by altering k.

ddply(data, "gvkey", transform,  DeltaCol = Delt(PRCCQ,k=2))

然而,这个等式不允许我高于 k=2 而不会给我 不同行数 2,3 的错误.我现在尝试使用许多替代方法,但效果很好.是否有一个函数可以插入到 ddply 代码中,我必须替换 Delt 或者另一个完全替代的代码行,以在各个列中显示所有 8 个季度的回报?

However, this equation will NOT allow me to go higher than k=2 without giving me an error of differing number of rows 2,3. I've tried using many alternate methods now but dint work. Is there a function I can plug into the ddply code I have to replace Delt or maybe another completely alternative line of code to display all 8 quarters of return in individual columns?

推荐答案

您可以将数据声明为 ts() 并使用 cbind()diff()

You can declare your data as ts() and use cbind() and diff()

data <- read.table(header=T,text='gvkey  PRCCQ
   1004 23.750
   1004 13.875
   1004 11.250
   1004 10.375
   1004 13.600
   1004 14.000
   1004 17.060
   1005  8.150
   1005  7.400
  1005 11.440
  1005  6.200
  1005  5.500
  1005  4.450
  1005  4.500
  1005  8.010')

data <- split(data,list(data$gvkey))
(newdata <- do.call(rbind,lapply(data,function(data) { data <- ts(data) ; cbind(data,Quarter=diff(data[,2]),Two.Quarter=diff(data[,2],2))})))

      data.gvkey data.PRCCQ Quarter Two.Quarter
 [1,]       1004     23.750      NA          NA
 [2,]       1004     13.875  -9.875          NA
 [3,]       1004     11.250  -2.625     -12.500
 [4,]       1004     10.375  -0.875      -3.500
 [5,]       1004     13.600   3.225       2.350
 [6,]       1004     14.000   0.400       3.625
 [7,]       1004     17.060   3.060       3.460
 [8,]       1005      8.150      NA          NA
 [9,]       1005      7.400  -0.750          NA
[10,]       1005     11.440   4.040       3.290
[11,]       1005      6.200  -5.240      -1.200
[12,]       1005      5.500  -0.700      -5.940
[13,]       1005      4.450  -1.050      -1.750
[14,]       1005      4.500   0.050      -1.000
[15,]       1005      8.010   3.510       3.560

另一种方式,没有 split()lapply()(可能更快)

Another way, without split() and lapply() (probably faster)

data <- read.table(header=T,text='gvkey  PRCCQ
       1004 23.750
       1004 13.875
       1004 11.250
       1004 10.375
       1004 13.600
       1004 14.000
       1004 17.060
       1005  8.150
       1005  7.400
      1005 11.440
      1005  6.200
      1005  5.500
      1005  4.450
      1005  4.500
      1005  8.010')
newdata <- do.call(rbind,by(data, data$gvkey,function(data) { data <- ts(data) ; cbind(data,Quarter=diff(data[,2]),Two.Quarter=diff(data[,2],2))}))

这篇关于如何计算不同跨度内不同行的百分比变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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