Question

I need to test alphas and betas on a single factor model for few portfolios using R. I see this as a problem where I need to test whether the coefficients of my regression are zero. I use data that I've uploaded on RStudio with the name ''French''. d1, d4, d6 and d10 are the portfolios I want to test and rmrf is the market proxy I use.

betas <- NULL
alphas <- NULL
for(j in French[,d1]+French[,d4]+French[,d6]+French[,d10]){
  m <- lm(French[,j]~French[,rmrf])
  betas(j) <- as.real(m$coefficients[2])
  alphas(j) <- as.real(m$coefficients[1])
}

After this I intend to run t-tests on alphas and betas. However I get the following message:

Error in .subset(x, j) : only 0's may be mixed with negative subscripts.

Could someone point out where I made a mistake with my code?

Was it helpful?

Solution

There are a lot of problems here. @Sven has addressed some of them. The subset error you are getting is because you are trying to use j as an index to an array, but j is not an index, but rather the actual data.

This should work:

betas<-c()
alphas<-c()
for(j in c("d1","d4","d6","d10")){
  m <- lm(French[,j] ~ French[,"rmrf"])
  betas<-c(betas,m$coefficients[2])
  alphas<-c(alphas,m$coefficients[1])
}

Incidentally, doing a t-test on your alphas and betas is probably not the best way to tell if they are statistically significant. A t-test on the array of alphas, for example, would test whether collectively these alphas differ significantly from 0, but it won't tell you whether any individual portfolio's alpha is statistically significant. Another, probably better choice, would be to test the significance of each regression coefficient individually. For example, this code will give you the T-statistics for the alpha and beta coefficients of each regression.

betaT<-c()
alphaT<-c()
for(j in c("d1","d4","d6","d10")){
  m <- lm(French[,j] ~ French[,"rmrf"])
  betaT<-c(betaT,summary(m)$coefficients[2,3])
  alphaT<-c(alphaT,summary(m)$coefficients[1,3])
}

See ?summary.lm for details and more info.

OTHER TIPS

You should use this corrected code:

betas <- NULL
alphas <- NULL
dat <- French[c("d1", "d4", "d6", "d10")] # a subset for the loop
for(j in seq(dat)) {
  m <- lm(dat[[j]] ~ French[,"rmrf"])
  betas[j] <- m$coefficients[2]
  alphas[j] <- m$coefficients[1]
}

There were four problems in your code:

  1. Indexing in R works with square brackets. I changed, e.g., alphas(j) to alphas[j].
  2. The command French[,d1]+French[,d4]+French[,d6]+French[,d10] adds the values in the correspondig columns. If you want to select a subset of columns, you can use French[c("d1", "d4", "d6", "d10")]
  3. To access a column by its name, you have to provide the name as a string, e.g., French[,"rmrf"].
  4. I added seq around the subset of the data frame. It returns a sequence from one to the number of columns.
  5. There is no function as.real. The returned coefficients are real numbers.

An easy way to generate a data frame with both alpha and beta coefficients is

setNames(as.data.frame(do.call(rbind, lapply(French[c("d1", "d4", "d6", "d10")],
  function(x) coef(lm(French[[1]] ~ x))))), c("alpha", "beta"))

The row names of the returned data frame correpsond to the column names of the input data frame.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top