0

While trying bayesian approach on regression models using "rstanarm", duplicate group specific terms are not allowed. Is there any solution for this?

Formula = "SleepTime ~ 1 + WorkingHours + (1 + WorkingHours | JobClass) + Tenure + (1 + Tenure | JobClass)"

bayesian = stan_lmer(Formula, data = data_model)

Error in check_reTrms(group) : rstanarm does not permit formulas with duplicate group-specific terms. In this case JobClass is used as a grouping factor multiple times and (Intercept) is included multiple times. Consider using || or -1 in your formulas to prevent this from happening.

1 Answers1

2

By

Consider using || or -1 in your formulas to prevent this from happening.

it means that

SleepTime ~ 1 + WorkingHours + Tenure + (1 + WorkingHours | JobClass) + (-1 + Tenure | JobClass)

or

SleepTime ~ 1 + WorkingHours + Tenure + (1 + WorkingHours + Tenure || JobClass)

would be valid formulas that do not have two (unidentified) intercept-deviation parameters for each level of JobClass.

Ben Goodrich
  • 4,870
  • 1
  • 19
  • 18
  • What is the difference between both the formulas you mentioned? – Mukund Komati May 16 '20 at 18:48
  • 1
    The latter assumes that the covariance matrix among the deviations in the parameters by level of `JobClass` is diagonal. – Ben Goodrich May 16 '20 at 20:32
  • How can I find the coefficients and predicted SleepTime in the rstanarm similar to lme4 package? – Mukund Komati May 21 '20 at 15:16
  • The `fixef`, `ranef`, and `coef` functions work the same way as in lme4 but are evaluated at the posterior medians. You need to do `as.matrix`, `as.data.frame`, etc. to get the raw draws in order to communicate the uncertainty. In addition, there is `posterior_predict`, which works like `predict` does in the lme4 context, except that it produces draws from the posterior predictive distribution rather than (draws from the posterior distribution of) the conditional expectation. – Ben Goodrich May 21 '20 at 20:03
  • What does bayesian[["fitted.values"]] return? Is it the median of the posterior_predict distribution for each data point? (Here, bayesian is a stanreg object) – Mukund Komati May 22 '20 at 07:36
  • No, it is the linear predictor implied by the median coefficients. It isn't very useful. – Ben Goodrich May 22 '20 at 13:11
  • @BenGoodrich I know this thread is old, but I was using it to help with the same problem I encountered. You mention that the the equation with `||` assumes the covariance matrix is diagonal, which I understand, but for the first equation which uses the `(-1+ Tenure | JobClass)` what does the `-1` here say about assumptions of the covariance matrix? Or specifically what is the difference in assumptions between `(-1 + Tenure | JobClass)` and `(1 + Tenure | JobClass)` ? Thanks!!!! – colebrookson Jul 22 '23 at 18:02