I have a dataset (data frame) with 5 columns all containing numeric values.
I\'m looking to run a simple linear regression for each pair in the dataset.
For ex
Using combn
for all combination of names of column (in the following example I assumed you want combination of two columns only) and Map
for running over loops.
Example using mtcars data from R:
colc<-names(mtcars)
colcc<-combn(colc,2)
colcc<-data.frame(colcc)
kk<-Map(function(x)lm(as.formula(paste(colcc[1,x],"~",paste(colcc[2,x],collapse="+"))),data=mtcars), as.list(1:nrow(colcc)))
head(kk)
[[1]]
Call:
lm(formula = as.formula(paste(colcc[1, x], "~", paste(colcc[2,
x], collapse = "+"))), data = mtcars)
Coefficients:
(Intercept) cyl
37.885 -2.876
[[2]]
Call:
lm(formula = as.formula(paste(colcc[1, x], "~", paste(colcc[2,
x], collapse = "+"))), data = mtcars)
Coefficients:
(Intercept) disp
29.59985 -0.04122
[[3]]
Call:
lm(formula = as.formula(paste(colcc[1, x], "~", paste(colcc[2,
x], collapse = "+"))), data = mtcars)
Coefficients:
(Intercept) hp
30.09886 -0.06823
[[4]]
Call:
lm(formula = as.formula(paste(colcc[1, x], "~", paste(colcc[2,
x], collapse = "+"))), data = mtcars)
Coefficients:
(Intercept) drat
-7.525 7.678
[[5]]
Call:
lm(formula = as.formula(paste(colcc[1, x], "~", paste(colcc[2,
x], collapse = "+"))), data = mtcars)
Coefficients:
(Intercept) wt
37.285 -5.344
[[6]]
Call:
lm(formula = as.formula(paste(colcc[1, x], "~", paste(colcc[2,
x], collapse = "+"))), data = mtcars)
Coefficients:
(Intercept) qsec
-5.114 1.412