Running a simulation study and facing an error when capturing the output

[edit: added brms tag]

Start with a short summary of your inquiry.
I am running a simulation and out2 is where I want my data to go. I want to use update function using brms package to avoid compilation every time. I have added the dataset and the code being used

This is where I want the values to be in:
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
Error in object[[name, exact = TRUE]] : subscript out of bounds

example.csv (19.7 KB)

this isn’t answering your question, but I added the brms tag in hopes that someone familiar with brms will answer.

I ran this reproducible example, and got no errors:

# Using your example.csv
mydata1 <- data.table::fread('~/Downloads/example.csv')

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.
dat <- mydata1[mydata1$Sim == 1,]
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, backend = 'cmdstanr')

# This is where I want to do the simulation data analysis.
for (k in seq_len(nsim)){
  dat <- mydata1[mydata1$Sim == k,]
  #baylogi <- try(update(baylogit, newdata = dat, seed=123))
  baylogi <- try(update(baylogit,chains=3, 
                        iter=2000,warmup=1000, thin=200,cores=3, newdata = dat, family = bernoulli(link = "logit"), seed=123))
  
  
  if (inherits(baylogi, "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]))
  }
}

Thanks, so maybe it is just a technical error from my computer.