-1

I have the following code to get the famafrench regression of a set of data:

#Regression
ff_reg = lm(e25 ~ rmrf+smb+hml, data=dat);

However, I keep getting the error "invalid type (list) for variable e25".

e25 was defined earlier in the program as a set of data obtained from subtracting 'rf' from a matrix made up of 25 columns:

e25 = (dat[,7:31]) - dat$rf;

(where dat is an CSV file read in to R and rf is one of the columns within that file)

Why is this error coming up and how can I resolve it?

On advice, here is the full code that I am running...

dat = read.csv("ff2014.csv", as.is=TRUE);

##excess portfolio returns
e25 = (dat[,7:31]) - dat$rf;
#print(e25);

#Regression
ff_reg = lm(e25 ~ rmrf+smb+hml, data=dat);
print(summary(ffreg));
mobygrape
  • 13
  • 2
  • Shouldn't you append the e25 result as a new column in the dat matrix? – duffymo Oct 03 '14 at 15:13
  • 3
    In a simple linear regression, you expect a single response. Sounds here like you are trying to include 25 columns as a response. That's not a simple linear model. What regressions are you actually trying to perform. You really should include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we know what your data actually looks like and can run the code ourselves to see where the error is coming form. – MrFlick Oct 03 '14 at 15:20
  • @duffymo The e25 result is 25 columns wide, would that make it unwieldy? Also, if I do it this way, what would be on the LHS of the equals sign in the formula in the lm function? – mobygrape Oct 03 '14 at 15:25
  • Sorry, I missed that. 25 columns on the LHS makes no sense. – duffymo Oct 03 '14 at 15:27
  • 1
    e25 should be a single variable. Your e25 seems like its a data frame containing 25 different variables. For regression, your dependent variable (which here is e25) should be a single column vector – j1897 Oct 03 '14 at 15:32
  • @MrFlick It's perfectly valid to have a matrix on the LHS. From the documentation: "If response is a matrix a linear model is fitted separately by least-squares to each column of the matrix." Obviously, `e25` isn't a matrix, but a data.frame. Maybe all they need is `e25 <- as.matrix(e25)`. – Roland Oct 03 '14 at 16:22
  • Fair enough @Roland, it's just that's not the kind of question I expect from a self proclaimed "R newbie" that doesn't even explicitly mention the intention to fit multiple models with different responses. – MrFlick Oct 03 '14 at 16:54
  • @Roland Thanks for the help guys, it is much appreciated, I'll go with Roland on this one as it seems to be giving me the results I need/want. Thanks again – mobygrape Oct 03 '14 at 19:51

1 Answers1

2

From help("lm"):

If response is a matrix a linear model is fitted separately by least-squares to each column of the matrix.

So, if that's what you intend to do, you need to make your data.frame a matrix before you call lm:

e25 <- as.matrix(e25)
Roland
  • 127,288
  • 10
  • 191
  • 288