It might be helpful to use brms::make_stancode
to take a look at the actual Stan code that is generated by brms for your model, so that you can clearly see what is happening under the hood.
The error message that you’re encountering involves Cor_1
. I believe this refers to the correlation matrix of the random effects for the first grouping variable. In your case, you only have one grouping variable, namely Patient_ID
. Thus, Cor_1
contains the correlations between each of your random effects (i.e., random intercept and slopes).
This is typically saved in the generated quantities block like so:
corr_matrix[M_1] Cor_1;
Cor_1 = multiply_lower_tri_self_transpose(L_1);
where M_1
is the number of random effects terms (which in your case is 20, with 1 random intercept and 19 random slopes), and L_1
is the estimated Cholesky factor of the correlation matrix.
The error message is telling you that “Cor_1
is not positive definite.”
In principle, if L_1
is a valid Cholesky factor, then multiply_lower_tri_self_transpose
should always produce a valid correlation matrix. So the likely root cause of the problem is with the estimation of L_1
.
You have a relatively large number of random effects, so presumably it’s difficult to estimate their correlations. It’s possible that some draws of L_1
correspond to degenerate or nearly singular matrices.
Do you have enough data to estimate your random effects structure? Note that the default prior on random effects correlation matrices in brms is lkj_corr_cholesky(1)
, which means that a priori all correlation matrices are equally likely (see Details in documentation of brms::set_prior
).
You could simplify things by modelling one or more of the random effects as uncorrelated, by splitting the random effects terms in your model formula. So in pseudocode, changing (1 + x1 + x2 | group)
into this: (1 | group) + (0 + x1 + x2 | group)
would remove the correlations between the random intercept and random slopes, but would keep the correlations between the random slopes, and this: (1 | group) + (0 + x1 | group) + (0 + x2 | group)
would remove all correlations. But I can’t judge whether that actually makes theoretical sense for your data and model.
Hope that helps.