I am trying to compare an actual portfolio's performance to the performances of hypothetical random portfolios.
我试图将实际投资组合的表现与假设的随机投资组合的表现进行比较。
Here is a sample of the data set I am working with. It shows two months worth of data, the names of the managers in the portfolios, and the returns, allocations, and attributions of those managers.
以下是我正在使用的数据集的示例。它显示了两个月的数据,投资组合中经理的姓名,以及这些经理的回报,分配和归因。
"date" "manager" "return" "allocation" "attribution"
2005-01-31 "manager01" -0.00763241754291056 0.146 6.94549996404861e-05
2005-01-31 "manager02" 0.0292205518315147 0.048 4.09087725641205e-05
2005-01-31 "manager03" -0.0354047394153526 0.049 -8.85118485383814e-05
2005-01-31 "manager04" 0.0424244772606645 0.124 -0.000148485670412326
2005-01-31 "manager05" -0.0574606103881735 0.134 0.000206858197397425
2005-01-31 "manager06" 0.0465278163188542 0.098 -0.000265208553017469
2005-01-31 "manager07" 0.157063203979822 0.142 -0.000219888485571751
2005-01-31 "manager08" -0.0594342759491509 0.071 2.97171379745754e-05
2005-01-31 "manager09" -0.0199466865109495 0.093 6.18347281839434e-05
2005-01-31 "manager10" 0.118839410130508 0.095 0.000190143056208813
2005-02-28 "manager01" 0.0403671815817711 0.119 -0.000460185870032191
2005-02-28 "manager02" 0.0246109773791459 0.064 -3.93775638066334e-05
2005-02-28 "manager03" 0.00868489880733732 0.065 -4.08190243944854e-05
2005-02-28 "manager04" -0.082332291530606 0.105 2.46996874591818e-05
2005-02-28 "manager05" -0.0903959999837099 0.114 -0.000117514799978823
2005-02-28 "manager06" 0.0514735666329574 0.081 -6.17682799595489e-05
2005-02-28 "manager07" -0.00914374153663751 0.164 -8.41224221370651e-05
2005-02-28 "manager08" -0.0367283709786134 0.083 -4.77468822721974e-05
2005-02-28 "manager09" -0.04752320926613 0.079 -3.8018567412904e-05
2005-02-28 "manager10" -0.0657464361573664 0.126 -0.000309008249939622
In order to get the data into R, copy the data to the clipboard and then
为了将数据导入R,将数据复制到剪贴板然后
mydata<-read.table("clipboard",header=TRUE)
In order to create the random portfolios I then use ddply
, mutate
, and rlongonly
functions from plyr
and rportfolio
.
为了创建随机组合,我然后使用plyr和rportfolio中的ddply,mutate和rlongonly函数。
library(plyr)
library(rporfolio)
mydata.new<-ddply(mydata,.(date),mutate,new.attr=t(rlongonly(m=1,n=length(date),k=10,x.u=.15))*return)
In the rlongonly
function:
在rlongonly功能:
- The value for m is the number of random portfolios I want to create.
- The value for n is the number of allocations for the time period.
- The value for k is the number of non zero allocations.
- The value for x.u is the upper limit for an allocation.
m的值是我想要创建的随机组合的数量。
n的值是该时间段的分配数。
k的值是非零分配的数量。
x.u的值是分配的上限。
Attribution is merely return * allocation.
归因只是返回*分配。
If I have m=1, everything is fine. If I have m>1, the dimensions of the output are not correct.
如果我有m = 1,一切都很好。如果我的m> 1,则输出的尺寸不正确。
mydata.new2<-ddply(mydata,.(date),mutate,new.attr=t(rlongonly(m=2,n=length(date),k=10,x.u=.15))*return)
dim(mydata.new)
mydata.new2 has only 6 columns when it should have 7. The last column "new.attr" is basically 2 columns in one.
当mydata.new2应该有7列时,它只有6列。最后一列“new.attr”基本上是2列。
When I try to melt
mydata.new2, I get the following error.
当我尝试融化mydata.new2时,我收到以下错误。
library(reshape2)
drop<-names(mydata.new2) %in% c("manager","return","allocation")
melt(mydata.new2[!drop],id="date")
> Error in rbind(deparse.level, ...) :
> numbers of columns of arguments do not match
How do I split up the "new.attr" column so that I can melt and graph the data?
如何拆分“new.attr”列以便我可以融合和绘制数据?
1 个解决方案
#1
1
First I regenrate your data, you have to use dput(mydata) and post the result next time.
首先我重新生成你的数据,你必须使用dput(mydata)并在下次发布结果。
Then I generate your mydata.new2 vector.
然后我生成你的mydata.new2向量。
library(plyr)
library(rportfolios)
mydata.new2<-ddply(mydata,
.(date),
mutate,
new.attr=t(rlongonly(m=2,n=length(date),k=10,x.u=.15))*return)
I round the numeric values , I and I show the data
我舍入数值,我和我显示数据
mydata.new2[,-c(1,2)] <- numcolwise(round_any)(mydata.new2,0.0001)
head(mydata.new2)
date manager return allocation attribution new.attr.1 new.attr.2
1 2005-01-31 manager01 -0.0076 0.146 1e-04 -0.0009 -0.0007
2 2005-01-31 manager02 0.0292 0.048 0e+00 0.0032 0.0040
3 2005-01-31 manager03 -0.0354 0.049 -1e-04 -0.0024 -0.0049
4 2005-01-31 manager04 0.0424 0.124 -1e-04 0.0029 0.0025
5 2005-01-31 manager05 -0.0575 0.134 2e-04 -0.0047 -0.0042
6 2005-01-31 manager06 0.0465 0.098 -3e-04 0.0051 0.0039
Here I have 7 columns and not 6 columns as you said.
正如你所说,我有7列,而不是6列。
I try to melt the data:
我试着融化数据:
library(reshape2)
drop<-names(mydata.new2) %in% c("manager","return","allocation")
melt(mydata.new2[!drop],id="date")
But here you get the error:
但是在这里你得到错误:
numbers of columns of arguments do not match
beacuse of the nested data.frame new.attr in mydata.new2 data.frame. This is due to the use of mutate. Here it is better to use transform because you don't need to do transformation iteratively.
因为在mydata.new2 data.frame中嵌套了data.frame new.attr。这是由于mutate的使用。这里最好使用transform,因为您不需要迭代地进行转换。
So :
mydata.new2<-ddply(mydata,
.(date),
transform,
new.attr=t(rlongonly(m=2,n=length(date),k=10,x.u=.15))*return)
and you get your result
你得到了你的结果
head(melt(mydata.new2[!drop],id="date"))
date variable value
1 2005-01-31 attribution 6.945500e-05
2 2005-01-31 attribution 4.090877e-05
3 2005-01-31 attribution -8.851185e-05
4 2005-01-31 attribution -1.484857e-04
5 2005-01-31 attribution 2.068582e-04
6 2005-01-31 attribution -2.652086e-04
#1
1
First I regenrate your data, you have to use dput(mydata) and post the result next time.
首先我重新生成你的数据,你必须使用dput(mydata)并在下次发布结果。
Then I generate your mydata.new2 vector.
然后我生成你的mydata.new2向量。
library(plyr)
library(rportfolios)
mydata.new2<-ddply(mydata,
.(date),
mutate,
new.attr=t(rlongonly(m=2,n=length(date),k=10,x.u=.15))*return)
I round the numeric values , I and I show the data
我舍入数值,我和我显示数据
mydata.new2[,-c(1,2)] <- numcolwise(round_any)(mydata.new2,0.0001)
head(mydata.new2)
date manager return allocation attribution new.attr.1 new.attr.2
1 2005-01-31 manager01 -0.0076 0.146 1e-04 -0.0009 -0.0007
2 2005-01-31 manager02 0.0292 0.048 0e+00 0.0032 0.0040
3 2005-01-31 manager03 -0.0354 0.049 -1e-04 -0.0024 -0.0049
4 2005-01-31 manager04 0.0424 0.124 -1e-04 0.0029 0.0025
5 2005-01-31 manager05 -0.0575 0.134 2e-04 -0.0047 -0.0042
6 2005-01-31 manager06 0.0465 0.098 -3e-04 0.0051 0.0039
Here I have 7 columns and not 6 columns as you said.
正如你所说,我有7列,而不是6列。
I try to melt the data:
我试着融化数据:
library(reshape2)
drop<-names(mydata.new2) %in% c("manager","return","allocation")
melt(mydata.new2[!drop],id="date")
But here you get the error:
但是在这里你得到错误:
numbers of columns of arguments do not match
beacuse of the nested data.frame new.attr in mydata.new2 data.frame. This is due to the use of mutate. Here it is better to use transform because you don't need to do transformation iteratively.
因为在mydata.new2 data.frame中嵌套了data.frame new.attr。这是由于mutate的使用。这里最好使用transform,因为您不需要迭代地进行转换。
So :
mydata.new2<-ddply(mydata,
.(date),
transform,
new.attr=t(rlongonly(m=2,n=length(date),k=10,x.u=.15))*return)
and you get your result
你得到了你的结果
head(melt(mydata.new2[!drop],id="date"))
date variable value
1 2005-01-31 attribution 6.945500e-05
2 2005-01-31 attribution 4.090877e-05
3 2005-01-31 attribution -8.851185e-05
4 2005-01-31 attribution -1.484857e-04
5 2005-01-31 attribution 2.068582e-04
6 2005-01-31 attribution -2.652086e-04