Wrapping mi() inside a (centering) function

Hi all,

I’d like to use mi() to impute missing values, but center the predictor around its mean after the imputed values have been predicted (for reasons of interpretation of the intercept).

I’ve tried:

bf(y ~ x + I( mi(z) -mean(mi(z)) ) + bf(z | mi() ~ x)

which produces the error:
“Variables in ‘mi’ terms should also be specified as response variables in the model”

I’ve also tried wrapping mi() around a centering function, as such:

center <- function(x){x-mean(x, na.rm=T)}
bf(y ~ x + center(mi(z))) + bf(z | mi() ~ x)

This actually fits with no errors, but some problems with convergence.
Even when there are no warnings, the estimate for z is distorted and the SE are very large.

Thank you!
(on brms: 2.5.0)

Thanks for reporting this issue. Currently, brms cannot handle that kind of transformations of mi() terms, because all transformations in which mi(.) is wrapped in need to be done within Stan. I am working on a fix for this issue.

Ok, there will be a fix soonish, but not to your particular problem.

If you want to center your variable with missing data, you have to do
it before passing it to brms. That is

df$z <- df$z - mean(df$z, na.rm=TRUE)

Be aware that any function in which mi() is wrapped in, is not actually evaluated in R but in Stan. So what happens exactly depends on the corresponding Stan function.

Ah, I see, that’s good to know.
I’ll center before passing it to mi(). My concern was that once there are imputed values the mean would not be 0 anymore.
Thank you, Paul!

Indeed, it may happen that the variable is no longer centered after imputation. if you want to make sure the variable is centered after imputation, you have to amend the Stan code generated by brms via make_stancode.

Great, thanks much for the answer, Paul!


It’s been a few years, so I wanted to follow up and see whether any progress has been made on the issue of centering before passing a level 1 covariate to mi(). I’d like to make sure the variable is still centered after imputation but I don’t care quiteee enough to spend time figuring out how to amend make_stancode.

If no easy fix is available, I also wonder whether anyone has any thoughts on what would be worse: an inaccurately centered level 1 variables or failing to center level 1 variables, which can result in biased level 1 and 2 slope effects (Rights, Preacher, & Cole, 2020).