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?
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
).
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.