# Covariance matrix reconstruction in a stan\_lmer model

**URL:** <https://discourse.mc-stan.org/t/covariance-matrix-reconstruction-in-a-stan-lmer-model/40943>\
**Category:** Modeling\
**Tags:** rstanarm\
**Created:** [February 24, 2026, 8:13pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-reconstruction-in-a-stan-lmer-model/40943 "2026-02-24T20:13:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophe-rd](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/christophe-rd/32/20608_2.png) [@christophe-rd](https://discourse.mc-stan.org/u/christophe-rd)\
**Post date:** [February 24, 2026, 8:13pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-reconstruction-in-a-stan-lmer-model/40943/1 "2026-02-24T20:13:59Z")

</div>

Hi all! I have a question regarding a 2-level hierarchical model I’m fitting. I want to estimate a slope and an intercept from the same group: `spp` (species) with the following model:

```no-highlight
stan_lmer(
  y ~
    (1|site_fac) +
    (gdd | spp_fac),
  data = emp,
  chains = 4,
  iter = 4000,
  cores = 4
)

```

When I fit my data, and return the random effects, I get this:

```no-highlight
> ranef(fitlmer_partialpooling3)
$site_fac
  (Intercept)
1 -0.2326
2 -0.3119
3 0.0955
4 0.4740

$spp_fac
  (Intercept) gdd
1 0.0007 0.02289
2 -0.0102 -0.04361
3 -0.0021 -0.00521
4 0.0111 0.07815

with conditional variances for “site_fac” “spp_fac” 

```

Now, I believe rstanarm applies a covariance matrix for the parameters estimated from the same group for models with `stan_glmer` and `stan_lmer`. My question is: is this covariance matrix already applied internally by rstanarm, or do I need to apply it manually to the full posterior distribution of my parameters?

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [February 24, 2026, 8:57pm UTC](https://discourse.mc-stan.org/t/covariance-matrix-reconstruction-in-a-stan-lmer-model/40943/2 "2026-02-24T20:57:54Z")

</div>

> [@christophe-rd](#):
>
> My question is: is this covariance matrix already applied internally by rstanarm, or do I need to apply it manually to the full posterior distribution of my parameters?

It’s already used internally, no need for you to do anything extra. In `stan_lmer`, a term like `(gdd | spp_fac)` means the per-species intercept and slope deviations are modeled jointly as a correlated multivariate normal with a covariance matrix. That covariance is estimated when fitting the model, so there’s nothing you need to reconstruct or apply after fitting the model. The posterior distribution already reflects the estimated correlation/covariance.

---

<div class="post-metadata">

**Author:** ![christophe-rd](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/christophe-rd/32/20608_2.png) [@christophe-rd](https://discourse.mc-stan.org/u/christophe-rd)\
**Post date:** [February 28, 2026, 1:08am UTC](https://discourse.mc-stan.org/t/covariance-matrix-reconstruction-in-a-stan-lmer-model/40943/3 "2026-02-28T01:08:13Z")

</div>

@jonah thank you very much! This is very helpful
