[R-SIG-Finance] [R-sig-finance] Rolling Beta

Brian G. Peterson brian at braverock.com
Mon Oct 19 19:26:17 CEST 2009


As with most things in R, you can see the code by typing the function 
name without the parentheses.

In the case of rolling beta, the important line is this:

rollapply(na.omit(merged.assets[,, drop = FALSE]),
                  width = width, FUN = function(x) lm(x[,1, drop = FALSE]
                  ~ x[, 2, drop = FALSE])$coefficients[2],
                  by = 1, by.column = FALSE, na.pad = na.pad,
                  align = "right")

Regards,

   - Brian


Eduard Pieterse (Macquarie Securities) wrote:
> Hi Brian,
>
> Chart.rollingRegression seems to be exactly what I'm after. How would I
> "disconnect" the chart output and be able to save/use the data behind
> it?
>
> I am still finding my feet with R and know what I need to do, but my R
> language knowledge is lacking somewhat.
>
> Thanks,
> Eduard
>
> -----Original Message-----
> From: Brian G. Peterson [mailto:brian at braverock.com] 
> Sent: 19 October 2009 17:28
> To: Eduard Pieterse (Macquarie Securities)
> Cc: r-sig-finance at stat.math.ethz.ch
> Subject: Re: [R-SIG-Finance] [R-sig-finance] Rolling Beta
>
> see PerformanceAnalytics functions
>
> CAPM.beta
> and
> chart.RollingRegression
> and
> charts.RollingRegression
>
> Cheers,
>
>   - Brian
>
> ehxpieterse wrote:
>   
>> Hi,
>>
>> I have had a *very* rough attempt at estimating the beta between 
>> indices. I am convinced that someone on this list has had experience 
>> with this before, but my search yielded little information. I have 
>> daily log returns from 01/01/2000 on 13 different equity indices. I am
>>     
>
>   
>> just looking for an efficient way to produce a rolling beta for all
>>     
> index pairs.
>   
>> I would appreciate any advice.
>>
>> Thanks,
>> Eduard
>>
>> CODE:
>>
>> IndexLocal <- read.csv("IndexLocal.csv", header=TRUE, sep = ",") MyLag
>>     
>
>   
>> <- 30 IndexDim <- dim(IndexLocal) Results <- NULL reg <- NULL k <- 0
>>
>> for (i in 2:14)
>> {
>> 	for (j in 2:14)
>> 	{
>> 			k <- k + 1
>> 			for (r in MyLag :IndexDim[1])
>> 			{
>> 				x. <- IndexLocal[(r-MyLag+1):r,i]
>> 				y. <- IndexLocal[(r-MyLag+1):r,j]
>>     
>
>   
>> 				glm.linear<-coef(glm(y. ~ x.))
>> 				reg[r] <- glm.linear[2]
>> 			}
>> 				if (j >= i)
>> 				{
>> 					Results <- cbind(Results,reg)
>> 				}
>> 	}
>> }
>>
>>     

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list