在R中用data.frames计算每月returns
Calculate the monthly returns with data.frames in R
我想计算一段时间内证券列表的每月 returns。我拥有的数据具有以下结构:
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" 列的输出带有零。
有什么想法吗?
取决于用途,但我会将其转换为适当的时间序列对象,例如 xts
,然后使用价格序列:
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
我想计算一段时间内证券列表的每月 returns。我拥有的数据具有以下结构:
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" 列的输出带有零。
有什么想法吗?
取决于用途,但我会将其转换为适当的时间序列对象,例如 xts
,然后使用价格序列:
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