# Masking missing values of predictors

**URL:** https://discourse.pymc.io/t/masking-missing-values-of-predictors/5424
**Category:** Questions
**Created:** [July 9, 2020, 5:35pm UTC](https://discourse.pymc.io/t/masking-missing-values-of-predictors/5424 "2020-07-09T17:35:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nico](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nico/32/2545_2.png) [@nico](https://discourse.pymc.io/u/nico)
#### Post date: [July 9, 2020, 5:35pm UTC](https://discourse.pymc.io/t/masking-missing-values-of-predictors/5424/1 "2020-07-09T17:35:57Z")

</div>

Dear PyMC3 Community,

I am looking for someone that worked with missing data for both predictors (Xi) and y\_obs.  
My understanding is that there is no need to do the imputation beforehand, e.g. as part of a preprocessing data analysis pipeline. Hence, this can be written in a Bayesian way directly.  
I would like to model a Bernoulli classification based on X1 and X2 that contain missing values.  
If I excluded the missing value, I could run the model but if I want to keep the missing values, I get into bugs.

I would highly appreciate any advice in regards to this.

Please find below the script.

Thank you very much in advance

```
x_missing = np.isnan(x_train)
X_train = np.ma.masked_array(x_train, mask=x_missing)
#y_train.shape is (97, 1)
#X_train.shape is (97, 2)
X_shape = len(x_missing)
with pm.Model() as model:
  #Define priors
  beta = pm.Normal ('beta', 0, 10) 

  #Imputation of X missing values
  Xmu = pm.Normal('Xmu', 0, 1, shape=X_shape)
  X_modeled = pm.Normal('X', mu=Xmu, sd=10, observed=X_train)

  #Define likelihood
  lp = pm.Deterministic('lp', pm.math.dot(X_modeled, beta))

  #Define posterior
  y_obs = pm.Bernoulli('y_obs', p=lp, observed=y_train)      

  #Inference
  trace = pm.sample()
```

---

<div class="post-metadata">

### Author: ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)
#### Post date: [July 10, 2020, 1:13pm UTC](https://discourse.pymc.io/t/masking-missing-values-of-predictors/5424/2 "2020-07-10T13:13:25Z")

</div>

You will need to make sure Xmu is broadcastable to X\_train  
for example:

```auto
Xmu = pm.Normal('Xmu', 0, 1, shape=(X_shape, 1))

```

or

```auto
Xmu = pm.Normal('Xmu', 0, 1, shape=(X_shape, 2))

```

---

<div class="post-metadata">

### Author: ![nico](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nico/32/2545_2.png) [@nico](https://discourse.pymc.io/u/nico)
#### Post date: [July 10, 2020, 2:02pm UTC](https://discourse.pymc.io/t/masking-missing-values-of-predictors/5424/3 "2020-07-10T14:02:05Z")

</div>

Hi Junpeng,

Thank you very much for your message.

I also had this thought that the issue might be related to the shape, but I still get bad initial energy when running the Model().

The X matrix has X1 and X2, both with different numbers of missing values. Do you think I’m missing something somewhere in this regards?

Also, even though it might sound stupid, should the x\_missing contain the actual missing values or the boolean numpy transformation which to be masked as the latent variable?

---

<div class="post-metadata">

### Author: ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)
#### Post date: [July 10, 2020, 2:46pm UTC](https://discourse.pymc.io/t/masking-missing-values-of-predictors/5424/4 "2020-07-10T14:46:58Z")

</div>

the generated `x_missing` will contain fill-in value for the masked latent variable. The bad initial energy problem you can search for some suggested solution on the discourse.
