Re: [R] exces return by mktcap decile for each year
here is one way of doing it using 'ave': dat - read.table(textConnection(mc yrret + 32902.233 01/01/1995 0.426 + 15793.691 01/01/1995 0.024 + 2375.868 01/01/1995 0.660 + 54586.558 01/01/1996 0.497 + 10674.900 01/01/1996 0.405 +859.656 01/01/1996 -0.033 +770.963 01/01/1995 -1.248 +423.480 01/01/1995 0.654 + 2135.504 01/01/1995 0.394 +696.599 01/01/1995 -0.482 + 5115.476 01/01/1995 0.352 +821.347 01/01/1995 0.869 + 43329.695 01/01/1995 0.495 + 7975.151 01/01/1995 0.112 +396.450 01/01/1995 0.956 +843.870 01/01/1995 0.172 + 2727.037 01/01/1995 -0.358 +114.584 01/01/1995 -1.015 + 1347.327 01/01/1995 -0.083 + 4592.049 01/01/1995 -0.251 +674.305 01/01/1995 -0.327 + 39424.887 01/01/1996 0.198 + 4447.383 01/01/1996 -0.045 + 1608.540 01/01/1996 -0.109 +217.151 01/01/1996 0.539 + 1813.320 01/01/1996 0.754 +145.170 01/01/1996 0.249 + 3176.298 01/01/1996 -0.202 + 14379.686 01/01/1996 0.013 + 3009.059 01/01/1996 -0.328 + 1781.406 01/01/1996 -0.158 + 2576.215 01/01/1996 0.514 + 1236.317 01/01/1996 0.346 + 3003.735 01/01/1996 0.151 + 1544.003 01/01/1996 0.482 + 7588.657 01/01/1996 0.306 + 1516.625 01/01/1996 0.183 + 1596.098 01/01/1996 0.674 + 2792.192 01/01/1996 0.528 + 1276.702 01/01/1996 0.010 +875.716 01/01/1996 0.189 + 4858.450 01/01/1995 0.250 + 2033.623 01/01/1995 -0.582 + 2164.125 01/01/1995 0.631), header=TRUE) # quantiles by year (need as grouping in next statement dat$qByYr - ave(dat$mc, dat$yr, FUN=function(x){ + cut(x, quantile(x, prob=seq(0, 1, .1)), include.lowest=TRUE) + }) # compute the mean for year/quantile dat$dec.mean - ave(dat$ret, dat$yr, dat$qByYr, FUN=mean) # mean adjusted return dat$mean.adjusted - dat$ret - dat$dec.mean dat mc yrret qByYr dec.mean mean.adjusted 1 32902.233 01/01/1995 0.42610 0.4605000 -0.03450 2 15793.691 01/01/1995 0.024 9 0.068 -0.04400 3 2375.868 01/01/1995 0.660 6 0.6455000 0.01450 4 54586.558 01/01/1996 0.49710 0.236 0.26100 5 10674.900 01/01/1996 0.405 9 0.3555000 0.04950 6859.656 01/01/1996 -0.033 1 0.2516667 -0.28467 7770.963 01/01/1995 -1.248 3 -0.1895000 -1.05850 8423.480 01/01/1995 0.654 1 0.198 0.45567 9 2135.504 01/01/1995 0.394 5 -0.094 0.48800 10 696.599 01/01/1995 -0.482 2 -0.4045000 -0.07750 11 5115.476 01/01/1995 0.352 8 0.301 0.05100 12 821.347 01/01/1995 0.869 3 -0.1895000 1.05850 13 43329.695 01/01/1995 0.49510 0.4605000 0.03450 14 7975.151 01/01/1995 0.112 9 0.068 0.04400 15 396.450 01/01/1995 0.956 1 0.198 0.75767 16 843.870 01/01/1995 0.172 4 0.0445000 0.12750 17 2727.037 01/01/1995 -0.358 7 -0.3045000 -0.05350 18 114.584 01/01/1995 -1.015 1 0.198 -1.21333 19 1347.327 01/01/1995 -0.083 4 0.0445000 -0.12750 20 4592.049 01/01/1995 -0.251 7 -0.3045000 0.05350 21 674.305 01/01/1995 -0.327 2 -0.4045000 0.07750 22 39424.887 01/01/1996 0.19810 0.236 -0.03800 23 4447.383 01/01/1996 -0.045 8 -0.1235000 0.07850 24 1608.540 01/01/1996 -0.109 5 0.162 -0.27133 25 217.151 01/01/1996 0.539 1 0.2516667 0.28733 26 1813.320 01/01/1996 0.754 5 0.162 0.59167 27 145.170 01/01/1996 0.249 1 0.2516667 -0.00267 28 3176.298 01/01/1996 -0.202 8 -0.1235000 -0.07850 29 14379.686 01/01/1996 0.01310 0.236 -0.22300 30 3009.059 01/01/1996 -0.328 7 -0.0885000 -0.23950 31 1781.406 01/01/1996 -0.158 5 0.162 -0.32033 32 2576.215 01/01/1996 0.514 6 0.521 -0.00700 33 1236.317 01/01/1996 0.346 2 0.2675000 0.07850 34 3003.735 01/01/1996 0.151 7 -0.0885000 0.23950 35 1544.003 01/01/1996 0.482 4 0.578 -0.09600 36 7588.657 01/01/1996 0.306 9 0.3555000 -0.04950 37 1516.625 01/01/1996 0.183 3 0.0965000 0.08650 38 1596.098 01/01/1996 0.674 4 0.578 0.09600 39 2792.192 01/01/1996 0.528 6 0.521 0.00700 40 1276.702 01/01/1996 0.010 3 0.0965000 -0.08650 41 875.716 01/01/1996 0.189 2 0.2675000 -0.07850 42 4858.450 01/01/1995 0.250 8 0.301 -0.05100 43 2033.623 01/01/1995 -0.582 5 -0.094 -0.48800 44 2164.125 01/01/1995 0.631 6 0.6455000 -0.01450 On 7/11/07, Frank Hansen [EMAIL PROTECTED] wrote: Hi Jim, Thanks for getting back on this. I did not see your email on the help list. I or you can post this solution You are right I mis-stated about mc. mc is real, it is yr that is a factor. Here is a solution, which works, but it is clunky. I thought there might be a better/more R-like less for-loop way to
[R] exces return by mktcap decile for each year
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. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.