tags:

views:

60

answers:

1

I'm trying to calculate asset-weighted returns by asset class. For the life of me, I can't figure out how to do it using the aggregate command.

My data frame looks like this

dat <- data.frame(company, fundname, assetclass, return, assets)

I'm trying to do something like (don't copy this, it's wrong):

aggregate(dat, list(dat$assetclass), weighted.mean, w=(dat$return, dat$assets))
+5  A: 

For starters, w=(dat$return, dat$assets)) is a syntax error.

And plyr makes this a little easier:

> set.seed(42)   # fix seed so that you get the same results
> dat <- data.frame(assetclass=sample(LETTERS[1:5], 20, replace=TRUE), 
+                   return=rnorm(20), assets=1e7+1e7*runif(20))
> library(plyr)
> ddply(dat, .(assetclass),   # so by asset class invoke following function
+       function(x) data.frame(wret=weighted.mean(x$return, x$assets)))
  assetclass     wret
1          A -2.27292
2          B -0.19969
3          C  0.46448
4          D -0.71354
5          E  0.55354
> 
Dirk Eddelbuettel
It works like a charm. The first time I tried it, I replaced x in the function with dat (returning the same number for each asset class). Any idea why this won't work with the aggregate command?
Brandon Bertelsen
It seems `aggregate` aggregates every column wheres you desire computation over two columns. I think a while ago I use `doBy` or something like it -- but hey, `plyr` makes it easier and has other bells and whistles.
Dirk Eddelbuettel
You still need to learn about `summarise` ;)
hadley
I *knew* this would happen ;-) Thanks for waving the cluebat.
Dirk Eddelbuettel