简体   繁体   English

R中lm回归中预测变量的编码

[英]coding for predictor variables in lm regression in R

I have a data set (mydata) with 1000 records (rows) and 20 variables (columns, x1....x20).我有一个包含 1000 条记录(行)和 20 个变量(列,x1....x20)的数据集 (mydata)。 The first column is my response variable (y).第一列是我的响应变量 (y)。 All data is numeric with no missing values.所有数据都是数字,没有缺失值。

This works fine:这工作正常:

fit <- y ~ x2 + x3 + ..... x20, data = mydata); summary(fit)

I am trying to figure out how to avoid typing in all the variable names (ie x1 + x2 + x3 etc).我想弄清楚如何避免输入所有变量名称(即 x1 + x2 + x3 等)。

I've tried:我试过了:

predictors <- mydata[2:20]
fit <- lm(y ~ mydata[ c(2:20) ]  # as well as mydata[2:20] and predictors

Error - invalid type (list) for variable 'predictors'.错误 - 变量“预测器”的无效类型(列表)。

Is there a way around this?有没有解决的办法? Thank you for any assistance.感谢您提供任何帮助。

We can use .我们可以使用. to include all the other variables包括所有其他变量

lm(y~ ., data = mydata)

If there are also columns other than 'x\\d+'如果还有除 'x\\d+' 以外的列

lm(y ~ ., data = mydata[c('y', grep("^x\\d+$", names(mydata), value = TRUE))])

Reproducible example with mtcars mtcars重现示例

lm(mpg ~ ., data = mtcars)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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