How can I use for loop to my brms models?

I want to fit a brms on 75 dependent variable (node_1 ~ node_75), and save the each results in different vector (fit1 ~ fit 75).

My models like this:

fit1<-brm(formula=node_1~type+(1|GD),
family = gaussian(link = “identity”),
data=data,chains=4,
cores=8, iter=4000,
warmup=2000, thin=1,backend = “cmdstanr”)

and my data here
DF.RData (2.1 KB)

Is there any quick way in brms?

You can’t do this in brms but you can use base R to build the formula from a string using brmsformula.
Then you can use the current formula to fit the model. A quick sketch would look something like this:

fit_list <- vector(type="list", length = 75)

for(i in seq_len(75)){
    fit_list[i] <- brm(formula=brmsformula(paste0("node_", i, "type+(1|GD)")),
        family = gaussian(link = “identity”),
        data=data,chains=4,
        cores=8, iter=4000,
        warmup=2000, thin=1,backend = “cmdstanr”)
}
3 Likes

Mr. scholz

Thank your very much, it looks uesful.

But I got error when I run the fist code,

fit_list ← vector(type=“list”, length = 75)
Error in vector(type = “list”, length = 75) : unused argument (type = “list”)

The type in the code means my Independent variable (type)?

I didn’t test the code so there might be errors in there from me misremembering syntax and not simply work as a copy-paste example.
The parameter for the vector is indeed mode not type.
It tells the vector what you plan on storing in it. For more details see the vector documentation.

Mr. scholz

When I ran the secend code, I got above error, I looked up information about the error on the Internet,

But there is no way to fix it.

for(i in seq_len(75)){

  • fit_list[i] ← brm(formula<-brmsformula(paste0(“node_”, i, “type+(1|ground_diameter)”)),
  •                  family = gaussian(link = "identity"),
    
  •                  data=data_anhui_IAA,chains=4,
    
  •                  cores=8, iter=4000,
    
  •                  warmup=2000, thin=1,backend = "cmdstanr")
    
  • }
    Error in class(ff) ← “formula” : attempt to set an attribute on NULL

Please fix your code block. You can use the preview function to see how your answer looks.

You can wrap your code in ``` above and below or use the code button in the editor.
And a code block can hold multiple lines. Allowing a reader to easily read and copy your code.

Again, the code was not meant to be copy-pastable. Just to give you an idea of how you might be able to solve the problem. From a glance, I would say that you are using an arrow where an equal sign should be in the brm call
formula = brmsformula...

Just as a small comment, you can edit replies using the pencil button.

And as a matter of forum/question etiquette, you might find it useful to write down questions and think about them a second time before posting them. I do this all the time as just precisely stating the question sometimes helps me to answer it. And it might prevent you from posting and deleting replies over and over again (and sending me an email notification each time with it).

Mr. scholz

I changed some of your code, and it worked finally.

for(i in seq_len(64)){
                      fit_list_anhui_IAA[[i]]<- brm(formula=brmsformula(paste0("node_", i, "~type+(1|ground_diameter)")),
                        family = Gamma(),
                       data=data_anhui_IAA,
                        )   

I don’t know how to use the functions in this forum, I’m very sorry if I offended you.