# Modeling missing data on outcomes in brms

**URL:** <https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600>\
**Category:** brms\
**Created:** [February 28, 2022, 7:55pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600 "2022-02-28T19:55:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [February 28, 2022, 7:55pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/1 "2022-02-28T19:55:34Z")

</div>

Edit: because I think the way I reformulated this on github might be more clear?  
Maybe I don’t understand how `mi()` works? I’m learning about measurement error models.  
If `x` has missing data and `w` is a proxy, I can do `bf(y ~ mi(x)) + bf(x | mi() ~ w)` .  
I’m trying to understand how comparable this model is to `y ~ MeasError(x,w)` using the R package `mecore.`

`MeasErr(y,w) ~ x` is a valid model. Can `brms` fit a comparable one?

**Original Post Below**

I am working on a modeling problem with missing data on the outcomes. My goal is to fit a bivariate regression.

```nohighlight
    # simulate data
    N <- 100
    x <- rnorm(N, 0, 1)
    y <- x + rnorm(N, 0, 0.5) + 1
    w <- y + rnorm(N, 0, 0.5)
    df <- data.table(x,y,w)

    if(m < N){
        df <- df[sample(nrow(df), 20), y.obs := y]
    } else {
        df <- df[, y.obs := y]
    }

```

If missing data is on the RHS, the following model works great:

```nohighlight
    model <- brm(formula=bf(x ~ mi(y.obs)) + bf(y.obs | mi() ~ w) + set_rescor(FALSE),data=df)

```

But if the missing data is on the LHS, I get an error in fitting the model: `invalid type (list) for variable 'mi(y.obs)'`

```nohighlight
    model <- brm(formula=bf(y.obs | mi() ~ w) + bf(mi(y.obs) ~ x) + set_rescor(FALSE), data=df)

```

`bf(y.obs | mi() ~ w) + bf(mi(y.obs) ~ w) + set_rescor(FALSE)` outputs

```nohighlight
y.obs | mi() ~ w 
mi(y.obs) ~ x

```

Which looks right to me, but I don’t understand why brms is having trouble.

- Operating System: Rocky Linux release 8.4 (Green Obsidian)
- brms Version:2.16.3

Thank you!

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [February 28, 2022, 8:12pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/2 "2022-02-28T20:12:55Z")

</div>

Howdy. You don’t define “mu\_w” and “sd\_w” in your simulation, so I couldn’t run it, but if you are simply trying to impute missing data for the outcome, then you simply write `y.obs | mi() ~ w` and that’s it. If you have a predictor that is also missing, say `w`, then you would write something like:

```nohighlight
y.obs | mi() ~ mi(w)
w | mi() ~ 1

```

if you wanted to impute missings for both the outcome and predictor.

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [February 28, 2022, 8:32pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/3 "2022-02-28T20:32:40Z")

</div>

Thanks for the response!

Sorry about the missing variables. I’ve replaced them with constants.  
I’m trying to impute missing data and fit a linear regression to the imputed data in the same model.

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [February 28, 2022, 8:46pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/4 "2022-02-28T20:46:11Z")

</div>

Yes, with `y.obs | mi() ~ w` you are imputing missing data in the outcome ‘on the fly’ as you fit your model… Maybe I am not understanding what you are trying to do.

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [February 28, 2022, 11:10pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/5 "2022-02-28T23:10:45Z")

</div>

Sorry what i’m doing isn’t super clear.  
The scenario is:  
I want to fit `y ~ x` but `y` has a missing rate. I can measure `w` which a reasonably good approximation of `y`. So I want to impute `y` using `w` and also estimate `y ~ x.`

If we swap `x` and `y` and `x` is the variable with missing data then `bf(y ~ mi(x.obs)) + bf(x.obs | mi() ~ w)` works great. So my question is really about how to fit regression models with missing data on the LHS.

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [March 1, 2022, 6:12pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/6 "2022-03-01T18:12:22Z")

</div>

I just realized a typo in the original question was a source of confusion. It’s corrected now.

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [March 2, 2022, 1:33pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/7 "2022-03-02T13:33:17Z")

</div>

I don’t know if brms can do this unless there is a non-linear formula sort of hack…

---

<div class="post-metadata">

**Author:** ![groceryheist](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/groceryheist/32/1016_2.png) [@groceryheist](https://discourse.mc-stan.org/u/groceryheist)\
**Post date:** [March 3, 2022, 12:35am UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/8 "2022-03-03T00:35:48Z")

</div>

That’s unfortunate. I guess I’ll make a brms feature request.

---

<div class="post-metadata">

**Author:** ![jd\_c](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jd_c/32/16759_2.png) [@jd\_c](https://discourse.mc-stan.org/u/jd_c)\
**Post date:** [March 3, 2022, 1:15pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/9 "2022-03-03T13:15:43Z")

</div>

Maybe ask @paul.buerkner to see if it is already possible. I just don’t know

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [March 3, 2022, 1:24pm UTC](https://discourse.mc-stan.org/t/modeling-missing-data-on-outcomes-in-brms/26600/10 "2022-03-03T13:24:37Z")

</div>

You could use “overimputation” that is imputation while considering measurement error. If you know the measurement error of w with respect to the true (but unknown) y, you can do `w | mi(se_w) ~ x` to achieve your goal.
