# Loo for hierarchical model

**URL:** <https://discourse.mc-stan.org/t/loo-for-hierarchical-model/39158>\
**Category:** Modeling\
**Tags:** loo, cmdstanr\
**Created:** [March 26, 2025, 9:20am UTC](https://discourse.mc-stan.org/t/loo-for-hierarchical-model/39158 "2025-03-26T09:20:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgajdos](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@tgajdos](https://discourse.mc-stan.org/u/tgajdos)\
**Post date:** [March 26, 2025, 9:20am UTC](https://discourse.mc-stan.org/t/loo-for-hierarchical-model/39158/1 "2025-03-26T09:20:50Z")

</div>

Hello everyone,

I have a question concerning loo for hierarchical models. I want to do a trial-level loo, but the number of trials differ across subjects. Therefore, the array of log\_lok contains missing or default values. How will loo take care of these?

have a good day.

---

<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:** [March 26, 2025, 6:48pm UTC](https://discourse.mc-stan.org/t/loo-for-hierarchical-model/39158/2 "2025-03-26T18:48:18Z")

</div>

The loo package will throw an error if the `log_lik` object contains NA values, but I don’t think you need to have missing/NA values here. It sounds like you’re creating a `log_lik` object where each subject has the same number of entries (the maximum number of trials). Instead, if you want to do leave-one-trial-out CV (that’s what it sounds like you want to do, but correct me if I’m wrong) you can just have one entry per observed trial. For example, if subject A has 3 trials and subject B has two trials, you would just put those five trials next to each other in the `log_lik` object. You don’t need a placeholder/missing value for a third trial for subject B just because subject A has three trials.

---

<div class="post-metadata">

**Author:** ![tgajdos](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@tgajdos](https://discourse.mc-stan.org/u/tgajdos)\
**Post date:** [March 27, 2025, 8:39am UTC](https://discourse.mc-stan.org/t/loo-for-hierarchical-model/39158/3 "2025-03-27T08:39:23Z")

</div>

Very clear! Thank you very much for the insight.
