Hi all,
I'm curious if an expert has thoughts on how to use the tidy models framework for a 'Many models' kind of analysis, similar to the chapter in Advanced R.
As an example, I would be curious how to translate the following workflow that tests multiple mediators at once into a tidy models workflow:
analysis.dict <-
tribble(
~vname, ~role,
"lifespan", "outcome",
"county_voting_2000", "predictor",
"county_health_spending", "mediator",
"county_economic_growth", "mediator",
"county_education", "mediator"
)
analysis.formulas <-
tribble(
~formula_step, ~formula_fun,
"mediation", "test_mediator"
)
test_mediator <- function( mediator ) {
as.character(
glue(
"lifespan ~ county_voting_2000 + {mediator}",
mediator = mediator
)
)
}
analysis.setup <-
analysis.dict %>%
dplyr::filter( role == "mediator" ) %>%
expand_grid( analysis.formulas )
analysis <-
analysis.setup %>%
mutate(
model_formula = pmap_chr(
.l = list(
.f = formula_fun,
mediator = mediator
),
.f = invoke
),
fit = pmap(
.l = list(
.f = lm,
formula = model_formula,
data = list( data )
),
.f = invoke
)
)
Essentially, I can permute either the models, the formulas, or any of the variables based on role into a dataframe of the analyses to run and then execute them. I can also add in a setup to permute model functions too.
It is not obvious to me how to do this kind of thing in tidymodels, but it is a workflow that I run into constantly. Sometimes I want to test multiple formulas, whereas at other times (like this one), I have one general formula I want to test with n variables. At other times I want to do exactly the same thing but with 3-4 outcomes.
It seems more like tidy models is setup to use the same formula on multiple datasets, but I don't have much experience with it, so any thoughts on how to do these kinds of permutation workflows would be great