Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass a vector or string with several elements for the RHS variables to feols

I need to run multiple estimations with feols, using sw (stepwise). Since I have many variables for the right-hand side, I would like to pass them as a vector or string with the vector elements. I must me making a beginner’s mistake.

The following code works:

library(fixest)

set.seed(42)
data <- data.frame(
  y = runif(100, min = 1, max = 100),
  x1 = runif(100, min = 1, max = 100),
  x2 = runif(100, min = 1, max = 100)
)

rm <- feols(log(y) ~ sw(log(x1), log(x2)), data=data)
summary(rm)

However, the following does not:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(fixest)

set.seed(42)
data <- data.frame(
  y = runif(100, min = 1, max = 100),
  x1 = runif(100, min = 1, max = 100),
  x2 = runif(100, min = 1, max = 100)
)

regressors <- c("log(x1)", "log(x2)")
regressors <- paste(regressors, collapse = ", ")

rm <- feols(log(y) ~ sw(regressors), data=data)
summary(rm)

How can I pass right-hand-side variables as a vector or string to feols?

Here is the error message:

Error in feols(log(y) ~ sw(regressors), data = data) : 
  The variable 'regressors' is in the RHS of the formula but not in the data set.

>Solution :

regressors <- c("log(x1)", "log(x2)")
form <- reformulate(sprintf('sw(%s)', toString(regressors)), 'log(y)')
fixest::feols(form, data)

Standard-errors: IID 
Expl. vars.: log(x1)
             Estimate Std. Error  t value   Pr(>|t|)    
(Intercept)  4.268400   0.454189  9.39785 2.4549e-15 ***
log(x1)     -0.168033   0.119207 -1.40959 1.6183e-01    
---
Expl. vars.: log(x2)
            Estimate Std. Error t value  Pr(>|t|)    
(Intercept) 3.031253   0.426176 7.11268 1.878e-10 ***
log(x2)     0.175165   0.117879 1.48597 1.405e-01    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading