7

Where there are many columns in data frame and you want to just leave out one or two columns and include everything else in the multiple regression, How can we accomplish that without writing out a large formula?

for example to include all:

lm(y ~., data=myFrame)

Then if you want to handpick one by one

lm(y ~ x1 + x2 + x3)

but if you have 50 variables but want to leave out few what's the best way? Because I want to leave out two or three, include all the rest,and then do forward and backward selection.

0

2 Answers 2

13

Use the . operator for "everything in the data frame except the response variable" and the - operator for "but leave these out" ...

lm(y ~ . - excluded_1 - excluded_2, data = myFrame)
2

You can use R's built-in subsetting:

all.but.x1x2x3 <- myFrame[, !(colnames(myFrame) %in% c('x1', 'x2', 'x3'))]
lm(y ~., data=all.but.x1x2x3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.