Logister
Logister

Reputation: 1894

R Regression: referring to multiple independent variables simultaneously

I would like to run a regression on a set of variables without actually specifying the names of the variables themselves.

For example, given the data frame:

data(iris)
iris=iris 

Instead of running:

lm(formula = (Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width), data=iris)

I would like to specify the independent variables all at once without naming them. Hopefully, this would look something like:

lm(formula = (Sepal.Length ~ iris[, 2:4], data=iris)

R has to have some functionality that allows me to do this, but despite extensive experimentation I haven't been able to suss it out.

Upvotes: 1

Views: 141

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61154

Try using . to replace all the rest of regressors:

> lm(Sepal.Length ~ ., data=iris[, -5])

Upvotes: 2

Related Questions