Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conveniently add a large set of regressors in R?

Tags:

r

regression

I have to add approximately 30 dummy variables to a regression.

If my variables would be named dummy1 - dummy30, I would denote this with an asterisk wildcard in STATA. It would be simply regress y dummy* and STATA would add all variables starting with 'dummy'.

Can anyone hint me to a similar convenient procedure in [R] which prevents me from writing out 30 variable names?

like image 655
Raynor Avatar asked Oct 17 '25 17:10

Raynor


2 Answers

You have two options. Either subset a data.frame to contain only dummy* variables and the dependent variable. In which case, you can call lm(dep ~ ., data = your.data). The dot argument will assume you're trying to use all but dep as predictors. To subset a data.frame of only dep and predictors, you can use your.data[grepl("dep|dummy", names(your.data)), ].

Second option is to construct a formula argument using paste.

formula(paste("dep ~", paste("dummy", 1:10, sep = "", collapse = "+")))
like image 186
Roman Luštrik Avatar answered Oct 20 '25 07:10

Roman Luštrik


The function reformulate is the right option for creating formulas based on strings.

An example data frame:

set.seed(1)
dat <- data.frame(y = rnorm(10), 
                  dummy1 = rnorm(10),
                  dummy2 = rnorm(10),
                  dummy3 = rnorm(10),
                  other = rnorm(10))

Now, grep is used to find all dummy* variables. The result is used for the function reformulate:

form <- reformulate(grep("^dummy", names(dat), value = TRUE), response = "y")
# y ~ dummy1 + dummy2 + dummy3

This formula can be used for lm:

lm(form, dat)
# Call:
#   lm(formula = form, data = dat)
# 
# Coefficients:
#   (Intercept)       dummy1       dummy2       dummy3  
# 0.04785      0.09323     -0.63404     -0.19547
like image 21
Sven Hohenstein Avatar answered Oct 20 '25 07:10

Sven Hohenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!