[R] exces return by mktcap decile for each year

Frank Hansen hansenfrank at yahoo.com
Tue Jul 10 22:01:43 CEST 2007


I have a data frame, lets call it dat,
with 3 columns ( mc, yr, ret) which represent market
cap, year, and return. mc is a factor, mc, and ret are
real numbers.

I want to add a column to the data calculated as
follows.

For each year, I want to split the data by mc decile,
then calculate the mean ret within that mc decile, and
finally subtract that year's decile mean from the raw
return. Then I want that mean adjusted ret to be the
new column.

I can get the market cap deciles with

my.cut <- function(x) {
  cut( x, quantile( x, probs=seq(0,1,0.1),
na.rm=TRUE))
}
mc.deciles <- by( dat$mc, dat$yr, my.cut)

I don't know how to associate the values in mc.deciles
with a particular row of the original data frame dat.

I don't think I can unlist mc.deciles because the
order won't be the same.

If I could append mc.deciles as a new column to dat,
then I could do something like

by( dat$ret, list( dat$yr, dat$decile), mean)

In which case I would still be faced with finding the
right mean to subtract from each entry in dat$ret.

Maybe I'm just stuck on by() and cut() when there is
an easier way to handle this. Any suggestions? Thanks.



More information about the R-help mailing list