I'm looking for a Tidyverse / broom solution that can solve this puzzle:
Let's say I have different DVs and a specific set of IVS and I want to perform a regression that considers every DV and this specific set of IVs. I know I can use something like for i in or apply family, but I really want to run that using tidyverse.
The following code works as an example
ds <- data.frame(income = rnorm(100, mean=1000,sd=200),
happiness = rnorm(100, mean = 6, sd=1),
health = rnorm(100, mean=20, sd = 3),
sex = c(0,1),
faculty = c(0,1,2,3))
mod1 <- lm(income ~ sex + faculty, ds)
mod2 <- lm(happiness ~ sex + faculty, ds)
mod3 <- lm(health ~ sex + faculty, ds)
summary(mod1)
summary(mod2)
summary(mod3)
Income, happiness, and health are DVs. Sex and Faculty are IVs and they will be used for all regressions.
That was the closest I found
Let me know If I need to clarify my question. Thanks.
Another method is to
gather
the dependent variables and use a grouped data frame to fit the models withdo
. This is the method explained in the broom and dplyr vignette.Created on 2018-08-01 by the reprex package (v0.2.0).
As you have different dependent variables but the same independent, you can form a matrix of these and pass to
lm
.And I think
broom::tidy
worksWe can loop through the column names that are dependent variables, use
paste
to create theformula
to be passed intolm
and get the summary statistics withtidy
(frombroom
)If we want it in a single
data.frame
with a column identifier for dependent variable,