New to Stan: Having trouble fitting basic treatment response data with brm

I am new to stan and R and I was given simple example data to fit using brm. I have a dataframe of 500 rows and 3 columns. One column is treatment labeled 1, 2, 3, 4, or 5, a second column with a different mean for each treatment, and a third column with a response value with a mean around treatment mean with a sd of 3.
there are 100 different response value for each treatment 1 - 5 and 5 different means for each treatment.



data <- tibble::tibble(treatment = as.factor(1:5)) %>% # piping tibble(treatment) into mutate. tibble constructs a df
  dplyr::mutate( # allows you to manipulate dataframe
    treatment_mean = rnorm( # finding the treatment mean
      n = 5,
      mean = 10,
      sd = 1)) %>% # piping treatment mean into rowwise
  dplyr::rowwise() %>% # piping rowwise into do 
  dplyr::do({ # do applies a function to each group (in this case rowwise)
    treatment_params <- . # the df
    tibble::tibble( # constructing a df
      treatment = treatment_params$treatment, # putting treatment into the df
      treatment_mean = treatment_params$treatment_mean, # treatment_mean into the df
      value = rnorm( # response value
        n = 100, # using 100 random Gaussian distribution values around the mean
        mean = treatment_params$treatment_mean,
        sd = 3)) # with a sd between 0 & 1 (default)
  }) %>%
  dplyr::ungroup() # removes grouping done by group_by 



This is my model:

model_w_prior <- brms::brm(
  formula = treatment_mean ~ 0 + treatment, 
  data = data,
  prior = c(
    brms::prior(normal(10,3), 
                class = "b",
                lb = 0), 
    brms::prior(normal(3,0.5),
                class = "sigma"
                ) ),
  iter=8000,
  control=list(
      adapt_delta=0.99,
      max_treedepth=12)
)

I get all of these errors:

Warning: There were 168 divergent transitions after warmup.

Warning: There were 1164 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 12.

Warning: There were 4 chains where the estimated Bayesian Fraction of Missing Information was low.

Warning: Examine the pairs() plot to diagnose sampling problems

Warning: The largest R-hat is 3.15, indicating chains have not mixed.
Running the chains for more iterations may help.

Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help.

Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help.


This is the output I receive:

image


here is the pairs plot:


I have spent an exhausting amount of hours reading documentation and journals and tried increasing iterations, warmup, adapt_delta, treedepth, changing my priors to make them more or less informative, messing with the formula, changing my data (increasing and decreasing the number of samples, increasing and decreasing sd, increasing and decreasing mean). None of these seem to improve my model by very much. Any suggestions are appreciated!

(This is toy data to practice with so if there is something wrong with the data that is making this difficult, I am all ears for what that might be.)

Maybe I am confused at your intent, but don’t you want to model the response value (“value”), not “treatment_mean”?

The following worked very quickly ( < 1 second) for me, with the default priors and no changes to default control settings. I’d also stick with far fewer iterations.

model_w_prior <- brm(bf(value ~ 0 + treatment), 
                     data = data,
                     iter = 1000,
                     warmup = 500,
                     chains = 4,
                     cores = 4,
                     backend = "cmdstanr")

> summary(model_w_prior)

Family: gaussian 
  Links: mu = identity; sigma = identity 
Formula: value ~ 0 + treatment 
   Data: data (Number of observations: 500) 
Samples: 4 chains, each with iter = 1000; warmup = 500; thin = 1;
         total post-warmup samples = 2000

Population-Level Effects: 
           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
treatment1     9.74      0.30     9.19    10.30 1.00     2682     1573
treatment2     9.57      0.29     9.02    10.13 1.00     3202     1768
treatment3    10.75      0.28    10.20    11.31 1.00     2622     1546
treatment4    11.77      0.31    11.18    12.36 1.00     3092     1545
treatment5     9.57      0.30     8.98    10.17 1.00     2857     1745

Family Specific Parameters: 
      Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     2.98      0.09     2.80     3.18 1.00     2946     1644

Samples were drawn using sample(hmc). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).