Hercules Apergis
Hercules Apergis

Reputation: 421

By two combinations of predictors in linear regression in R

Suppose that I have X1,...,X14 potential predictors.

Now for a given Y i want to make the OLS scheme:

Y~X1+X2
Y~X1+X3
 ....
Y~X1+X14
....
Y~X14+X13

which is basically all the by two combinations of all the predictors. After all those regressions are created I want to use them in the predict function (if possible).

My question is: How do i make all those regressions with all by two combinations of the regressors?

Upvotes: 2

Views: 604

Answers (2)

lmo
lmo

Reputation: 38500

You could also put them into formulas in one line like this:

mySpecs <- combn(letters[1:3], 2, FUN=function(x) reformulate(x, "Y"),
                 simplify=FALSE)

which returns a list that can be used in lapply to run regressions:

mySpecs
[[1]]
Y ~ a + b
<environment: 0x4474ca0>

[[2]]
Y ~ a + c
<environment: 0x4477e68>

[[3]]
Y ~ b + c
<environment: 0x447ae38>

You would then do the following to get a list of regression results.

myRegs <- lapply(mySpecs, function(i) lm(i, data=df))

Upvotes: 3

LyzandeR
LyzandeR

Reputation: 37879

You can use combn for all the combinations and then use an apply to create all the formulas:

#all the combinations
all_comb <- combn(letters, 2)

#create the formulas from the combinations above and paste
text_form <- apply(all_comb, 2, function(x) paste('Y ~', paste0(x, collapse = '+')))

Output

> text_form
  [1] "Y ~ a+b" "Y ~ a+c" "Y ~ a+d" "Y ~ a+e" "Y ~ a+f" "Y ~ a+g".....

Then you can feed the above formulas into your regression using as.formula to convert the texts into formulas (most likely in another apply).

Upvotes: 3

Related Questions