Using for loops to run univariate models


I would like to fit a handful of univariate models using brm. My first thought was to use a for loop, where i corresponds to the columns in my data frame containing the different predictor variables. Y1-Y5 are proportions summing to 1.

bind ← function(…) cbind(…)

ncores = parallel::detectCores() - 1

models ← list()

for (i in 1:12) {
models[ i ] ← brms::brm(bind(Y1, Y2, Y3, Y4, Y5) ~ DF[ , i] ,
data = DF, iter = 1000, chains = 4, cores = ncores, family = “dirichlet”)

The above code gives the error:
Error in is(sexpr, “try-error”) :
argument “sexpr” is missing, with no default

I found this post: Argument "sexpr" is missing, with no default, pointing out that brm does not like predictor variables specified in this manner.

Any thoughts on how to automate the implementation of models where everything except the predictor variable changes?


Can you split it into two pieces, like take a subset of your dataframe, rename the covariate you want to use to some standard name, and then make the brms call?

my_df_subset = ...; // Take a subset of DF, rename the covariate you want to use "covariate"

models[i] <- brms::brm(bind(Y1, Y2, Y3, Y4, Y5) ~ covariate,
                       data = my_df_subset, iter = 1000, chains = 4, cores = ncores, family = “dirichlet”)

@bbbales2 Thank you for the suggestion. This approach will work and I believe I would not even have to make the subsets of the data but rather just call the name of the variable I am interested in from the full dataset.

I was hoping to find a more iterative way though in which I could run the code, and when it was finished, have all the models accessible without having to manually change the predictor and/or data frame for each model.

1 Like

Ooo, okay, yeah the only way I know how to do it is by creating the new data frames.