我有一个这样的面板数据框
date firms return mkreturn
5/1/1988 A 5 4
6/1/1988 A 6 6
7/1/1988 A 4 12
8/1/1988 A 5 3
9/1/1988 A 6 12
11/1/1988 A 6 14
12/1/1988 A 12 5
13/01/1988 A 3 6
14/01/1988 A 2 4
15/01/1988 A 5 5
16/01/1988 A 2 6
18/01/1988 A 7 6
19/01/1988 A 3 2
20/01/1988 A 5 7
21/01/1988 A 7 2
22/01/1988 A 5 5
23/01/1988 A 9 7
25/01/1988 A 1 5
26/01/1988 A 5 6
27/01/1988 A 2 6
28/01/1988 A 7 12
29/01/1988 A 2 3
5/1/1988 B 5 2
6/1/1988 B 7 5
7/1/1988 B 5 5
8/1/1988 B 9 7
9/1/1988 B 1 5
11/1/1988 B 5 6
12/1/1988 B 2 12
13/01/1988 B 7 6
14/01/1988 B 2 11
15/01/1988 B 5 2
16/01/1988 B 6 14
18/01/1988 B 8 12
19/01/1988 B 5 15
20/01/1988 B 4 8
21/01/1988 B 3 9
22/01/1988 B 18 10
23/01/1988 B 5 3
25/01/1988 B 2 5
26/01/1988 B 7 6
27/01/1988 B 3 8
28/01/1988 B 9 5
29/01/1988 B 2 3
我想找出每个公司的收益与市场收益的每月协方差。因此,预期的输出就像
date Firms cov(return, mkreturn)
Jan-88 A ....
Jan-88 B ....
我使用以下公式找出月度差异
df_var<-aggregate( return ~ Month+Year+firms, df , var )
如何修改此公式以找出协方差?请在这方面帮助我。
答案 0 :(得分:0)
我们在Date
步骤中将'date'转换为format
类,将group_by
转换为'month-year'格式,并使用'firmware'并应用{{1 }}
cov
或带有library(dplyr)
library(lubridate)
df %>%
group_by(date = format(dmy(date), '%b-%y'), firms) %>%
summarise(cov = cov(return, mkreturn))
data.table
或者使用library(data.table)
setDT(df)[, .(cov = cov(return, mkreturn)),
.(date = format(as.Date(date, '%d/%m/%Y'), '%b-%y'), firms)]
# date firms cov
#1: Jan-88 A 1.727273
#2: Jan-88 B 1.653680
的{{1}}
base R
答案 1 :(得分:0)
一种base
方式:使用by
拆分数据并将其应用于函数。
group1 <- strftime(as.Date(df$date, "%d/%m/%Y"), "%Y-%m")
group2 <- df$firms
协方差矩阵的返回
by(df[-(1:2)], list(group1, group2), cov)
# : 1988-01
# : A
# return mkreturn
# return 6.712121 1.727273
# mkreturn 1.727273 11.160173
# ------------------------------------------------------------
# : 1988-01
# : B
# return mkreturn
# return 13.30736 1.65368
# mkreturn 1.65368 14.18398
协方差返回
by(df[-(1:2)], list(group1, group2), function(x){
cov(x[[1]], x[[2]])
})
# : 1988-01
# : A
# [1] 1.727273
# ------------------------------------------------------------
# : 1988-01
# : B
# [1] 1.65368