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.