'How to use update in a simulation study using the brms package

Can someone help me to correct the error I am getting, see link below.

This text appears on "https://discourse.mc-stan.org/t/running-a-simulation-study-and-facing-an-error-when-capturing-the-output/26115?u=lovemore"

Here is the code

library(brms)
nsim = 5
out2 <- data.frame(Sim=NA, sample= NA,beta0.bay=NA, beta1.bay=NA, beta2.bay=NA, sd0.bay=NA, sd1.bay=NA, sd2.bay=NA, Lbeta0=NA, Lbeta1=NA,Lbeta2=NA, 
                   Ubeta0=NA, Ubeta1=NA, Ubeta2=NA)

This is the first model for compilation.
baylogit <- brm(formula = y ~ x1 +  x2, prior = c(set_prior("normal(0,1000)", class ="b")), chains=3, 
                iter=5000,warmup=1000, thin=200,cores=3, data = dat, family = bernoulli(link = "logit"),
                seed=123, file="fits/baylogit")

This is where I want to do the simulation data analysis.
for(k in 1:nsim){
      dat <- mydata1[mydata1$Sim == k,]
      baylogi <- try(update(baylogit,chains=3, 
                            iter=2000,warmup=1000, thin=200,cores=3, newdata = dat, family = bernoulli(link = "logit"), seed=123))
      
      if(class(baylogi)[1] == "try-error"){ 
        out2[k,1] <- k
        out2[k,3:14] <- NA
      } else { 
        out2[k,1]<- k
        out2[k,2]<- nrow(dat)      
        out2[k,3:5]<- as.numeric((fixef(baylogi)[,1]))
        out2[k,6:8]<- as.numeric((fixef(baylogi)[,2]))
        out2[k,9:11]<- as.numeric((fixef(baylogi)[,3]))
        out2[k,12:14]<- as.numeric((fixef(baylogi)[,4]))
      }
    }

I am running into the error.
Error in object[[name, exact = TRUE]] : subscript out of bounds
Error in object[[name, exact = TRUE]] : subscript out of bounds
Error in object[[name, exact = TRUE]] : subscript out of bounds
Error in object[[name, exact = TRUE]] : subscript out of bounds

I am running a simulation study and I want Stan to compile once utilizing the update function however I am getting that error messagelink with the data and explanation. The data is on the link



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source