add-semi-colons
add-semi-colons

Reputation: 18840

Multiple regression leave out one variable (column)

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.

Upvotes: 7

Views: 6743

Answers (2)

Idin
Idin

Reputation: 21

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)

Upvotes: 2

Ben Bolker
Ben Bolker

Reputation: 227061

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)

Upvotes: 13

Related Questions