用R中的data.frames计算月收益 [英] Calculate the monthly returns with data.frames in R

查看:120
本文介绍了用R中的data.frames计算月收益的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一段时间内一系列证券的月收益.我拥有的数据具有以下结构:

I want to calculate the monthly returns for a list of securities over a period of time. The data I have has the following structure:

date   name  value
"2014-01-31"   a    10.0
"2014-02-28"   a    11.1
"2014-03-31"   a    12.1
"2014-04-30"   a    11.9
"2014-05-31"   a    11.5
"2014-06-30"   a    11.88
"2014-01-31"   b    6.0
"2014-02-28"   b    8.5
"2014-03-31"   b    8.2
"2014-04-30"   b    8.8
"2014-05-31"   b    8.3
"2014-06-30"   b    8.9 

我尝试的代码:

database$date=as.Date(database$date)
monthlyReturn<- function(df) { (df$value[2] - df$value[1])/(df$value[1]) }
mon.returns <- ddply(database, .(name,date), monthlyReturn)

但是"monthlyReturn"列的输出为零.

However the output of "monthlyReturn" column is coming with zeros.

有什么想法吗?

推荐答案

取决于用途,但是我会将其转换为适当的时间序列对象,例如xts,然后使用价格序列:

Depends on the use, but I would convert it to a proper time-series object, like xts, and then work with the price-series:

library(reshape2)
library(xts)
myTs <- dcast(database, date~name)
myTs <- xts(myTs[,2:3], myTs[,1])
diff(myTs)/lag(myTs)
                     a           b
2014-01-31          NA          NA
2014-02-28  0.11000000  0.41666667
2014-03-31  0.09009009 -0.03529412
2014-04-30 -0.01652893  0.07317073
2014-05-31 -0.03361345 -0.05681818
2014-06-30  0.03304348  0.07228916

另一种方法是使用dplyr:

library(dplyr)
database %>% 
group_by(name) %>%
mutate(mReturn = value/lag(value) - 1)

         date name value monthReturn  
1  2014-01-31    a 10.00          NA
2  2014-02-28    a 11.10  0.11000000
3  2014-03-31    a 12.10  0.09009009
4  2014-04-30    a 11.90 -0.01652893
5  2014-05-31    a 11.50 -0.03361345
6  2014-06-30    a 11.88  0.03304348
7  2014-01-31    b  6.00          NA
8  2014-02-28    b  8.50  0.41666667
9  2014-03-31    b  8.20 -0.03529412
10 2014-04-30    b  8.80  0.07317073
11 2014-05-31    b  8.30 -0.05681818
12 2014-06-30    b  8.90  0.07228916

data.table:

library(data.table)
DT <- setDT(database)
DT[, mReturn := value/shift(value) - 1, by = name]
DT

          date name value     mReturn
 1: 2014-01-31    a 10.00          NA
 2: 2014-02-28    a 11.10  0.11000000
 3: 2014-03-31    a 12.10  0.09009009
 4: 2014-04-30    a 11.90 -0.01652893
 5: 2014-05-31    a 11.50 -0.03361345
 6: 2014-06-30    a 11.88  0.03304348
 7: 2014-01-31    b  6.00          NA
 8: 2014-02-28    b  8.50  0.41666667
 9: 2014-03-31    b  8.20 -0.03529412
10: 2014-04-30    b  8.80  0.07317073
11: 2014-05-31    b  8.30 -0.05681818
12: 2014-06-30    b  8.90  0.07228916

这篇关于用R中的data.frames计算月收益的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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