5

I have a dataframe like below :

  a1 a2 a3 a4
1  3  3  5  5
2  4  3  5  5
3  5  4  6  5
4  6  5  7  3

I want to do linear regression for every two columns in the dataframe, and set intercept as 0.

In other words, I want to get the coefficients of lm(a1~a2+0), lm(a1~a3+0), lm(a1~a4+0), lm(a2~a1+0), lm(a2~a3+0)...

In cor(), if I input a dataframe, I will get a matrix back, e.g. below,

          a1        a2        a3        a4
a1 1.0000000 0.9467293 0.8944272 0.2045983
a2 0.9467293 1.0000000 0.9622504 0.4989222
a3 0.8944272 0.9622504 1.0000000 0.4574957
a4 0.2045983 0.4989222 0.4574957 1.0000000

In lm() is there any way to get the same kind of matrix?

Thanks.

rankthefirst
  • 1,370
  • 2
  • 14
  • 26
  • I am sorry for my comment not related to your question but what that `+ 0` means? Are you trying to do not include an intercept in the model? – SabDeM May 31 '15 at 15:33
  • @SabDeM `+0` means removing the intercept. – rankthefirst May 31 '15 at 15:34
  • I guessed so. I only knew to put a ` - 1` but now I know that `+ 0` works well too as it is been said here: http://stackoverflow.com/questions/14216893/how-to-remove-intercept-in-r. Thank You. – SabDeM May 31 '15 at 15:37
  • @SabDeM Ah, I tried, also works....I don't know either....I'll check them, thanks~~ – rankthefirst May 31 '15 at 15:42
  • @SabDeM in `? formula` it says `It can also used to remove the intercept term: when fitting a linear model y ~ x - 1 specifies a line through the origin. A model with no intercept can be also specified as y ~ x + 0 or y ~ 0 + x.` So I think they are the same. – rankthefirst May 31 '15 at 15:46

1 Answers1

4

Here's a pretty general strategy

dd<-read.table(text="a1 a2 a3 a4
1  3  3  5  5
2  4  3  5  5
3  5  4  6  5
4  6  5  7  3", header=T)

mm<-diag(ncol(dd))
mm[lower.tri(mm)] <- combn(dd, 2, function(x) coef(lm(x[,2]~x[,1]+0)))
mm[upper.tri(mm)] <- rev(combn(dd[length(dd):1], 2, function(x) coef(lm(x[,2]~x[,1]+0))))

This gives the matrix

mm
#           [,1]     [,2]      [,3]      [,4]
# [1,] 1.0000000 1.202381 0.7738095 0.9285714
# [2,] 0.8255814 1.000000 0.6592593 0.7925926
# [3,] 1.2441860 1.508475 1.0000000 1.2033898
# [4,] 0.9069767 1.101695 0.7481481 1.0000000

where element [4,1] is the same as coef(lm(a4~a1+0, dd)) and element [2,3] is the same as coef(lm(a2~a3+0, dd))

MrFlick
  • 195,160
  • 17
  • 277
  • 295