My current dataset looks like:
N = 10000
wage <- rnorm(N)
educ <- rnorm(N)
age <- rnorm(N)
tce <- rnorm(N)
work <- rbinom(n = N, size = 1, prob = 0.05)
manu <- rbinom(n = N, size = 1, prob = 0.05)
id <- sample(10, N, replace = TRUE)
df <- data.frame(wage, educ, age, tce, work, manu, id)
wage
, work
and manu
are my dependent variables, and the rest of my variables are my independent variables.
Currently, I am repeating the syntax, but just changing the outcome variable as such:
library(fixest)
model1 <- feols(work ~ educ + age + tce | id, data = df)
model2 <- feols(manu ~ educ + age + tce | id, data = df)
model2 <- feols(wage~ educ + age + tce | id, data = df)
Is there a way I can use a for loop to run such regressions?
Moreover, after running the regressions, I would also like to plot the coefficients of the regressions as such:
library(modelsummary)
modelplot(
list(model1, model2, model3)
)
However, since for-loops don't create new objects how can I plot the coefficients?
Multiple estimations is a built-in functionality in
fixest
. Usec(v1, v2)
to run regressions across multiple dependent variables. By the way, this will also be substantially faster than looping.Note that if you have the variables names in a vector, you can plug them in directly in the formula thanks to the dot-square-bracket operator:
You can find some documentation on multiple estimations in the dedicated vignette.