library(tseries)
sie <- get.hist.quote(instrument="SIE.DE",
start="2010-01-01", quote="AdjClose")
vow <- get.hist.quote(instrument="VOW.DE",
start="2010-01-01", quote="AdjClose")
lin <- get.hist.quote(instrument="LIN.DE",
start="2010-01-01", quote="AdjClose")
dax <- get.hist.quote(instrument="^GDAXI",
start="2010-01-01", quote="AdjClose")
returns.table <- diff(log(na.omit(merge(dax, lin, sie, vow))))
# My function to get the betas pval is:
B <- function(share,bench){
beta <- summary(lm(share~bench))$coef[2]
pval <- summary(lm(share~bench))$coef[8]
coefs <- t(as.matrix(c(beta, pval), ncol=2, byrow=TRUE))
coefs
}
# The function B works fine:
B(returns.table[,2], returns.table[,1])
# this loop should work
result <- matrix(NA, length(names(returns.table))-1, 2)
for(i in 2:4){
result[i-1,] <- B(returns.table[,i],returns.table[,1])
}
result
On Sunday, March 13, 2011 at 8:03 AM, herr dittmann wrote:
> Dear useRs,
>
> I am stuck with a piece of code and hope you could give me some pointers.
>
> My aim is to calculate the lm-regression coefficients of individual stocks
against an index. I am interested in both the coefficient and the pval. While I
could do this manually for a select hand full, I hope to scale this up say for
30+ stocks (DAX-30, FTSE-100 etc.) to eventually have a matrix of coefficients
and p-values for each individual stock.
>
> First, let's get share prices:
>
> library(tseries)
>
> sie <- get.hist.quote(instrument="SIE.DE",
start="2010-01-01", quote="AdjClose")
> vow <- get.hist.quote(instrument="VOW.DE",
start="2010-01-01", quote="AdjClose")
> lin <- get.hist.quote(instrument="LIN.DE",
start="2010-01-01", quote="AdjClose")
> dax <- get.hist.quote(instrument="^GDAXI",
start="2010-01-01", quote="AdjClose")
>
> returns.table <- diff(log(na.omit(merge(dax, lin, sie, vow))))
>
> My function to get the betas pval is:
>
> B <- function(share,bench){
> beta <- summary(lm(share~bench))$coef[2]
> pval <- summary(lm(share~bench))$coef[8]
> coefs <- t(as.matrix(c(beta, pval), ncol=2, byrow=TRUE))
> coefs
> }
>
> The function B works fine:
>
> B(returns[,2],returns[,1])
>
> > B(returns[,2],returns[,1])
> [,1] [,2]
> [1,] 0.7568787 9.740043e-47
>
>
> Now, at the following step I am stuck. I am trying to loop through my
returns.table:
>
> Attempt 1:
>
> for(i in 2:4){result[i] <- B(returns[,i],returns[,1]); result}
> Error in result[i] <- B(returns[, i], returns[, 1]) :
> object 'result' not found
>
>
> Attempt 2:
>
> for(i in 2:4){print(B(returns[,i],returns[,1]))}
> [,1] [,2]
> [1,] 0.7568787 9.740043e-47
> [,1] [,2]
> [1,] 1.311835 2.924594e-86
> [,1] [,2]
> [1,] 1.023310 1.078007e-30
>
> Attempt 2 gets me a little closer to the desired matrix of coefficient and
pval by each share.
>
>
> What am I doing wrong here?
>
> Any pointers most welcome.
>
> Many thanks in advance!
>
> Regards,
>
> Bernd
>
>
>
>
>
> ______________________________________________
> R-help@r-project.org 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.
>
[[alternative HTML version deleted]]