简体   繁体   中英

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.

Try using . to replace all the rest of regressors:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM