# "shape mismatch" when new data is set as a predictor for sample\_posterior\_predictive

**URL:** <https://discourse.pymc.io/t/shape-mismatch-when-new-data-is-set-as-a-predictor-for-sample-posterior-predictive/10861>\
**Category:** v5\
**Tags:** prediction\
**Created:** [November 20, 2022, 2:10pm UTC](https://discourse.pymc.io/t/shape-mismatch-when-new-data-is-set-as-a-predictor-for-sample-posterior-predictive/10861 "2022-11-20T14:10:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kengo](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kengo](https://discourse.pymc.io/u/Kengo)\
**Post date:** [November 20, 2022, 2:10pm UTC](https://discourse.pymc.io/t/shape-mismatch-when-new-data-is-set-as-a-predictor-for-sample-posterior-predictive/10861/1 "2022-11-20T14:10:18Z")

</div>

With a simple linear regression, pymc.sample\_posterior\_predictive returns error:

> shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (100,) and arg 1 with shape (20,).

I used 100 data points as predictor for modeling, and set 20 data points as new predictor for predictions. Why does this happen?

MacOS: BigSur (11.7)  
pymc: 4.4.0

Code is something like this:

* * *

```
with pm.Model() as model:

    sigma = pm.HalfCauchy('sigma', beta=10)
    intercept = pm.Normal('intercept', 80, sigma=50)
    beta = pm.Normal('beta', 0, sigma=50)

    X = pm.MutableData('X', data['A'], dims='obs_id') # data['A']'s shape is (100,)
    mu = intercept + X * beta
    
    pm.Normal('y', mu, sigma=sigma, observed=data['y'])
    
    mcmc_result = pm.sample(
        draws=2000,
        chains=4,
        random_seed=123,
    )

new_data = range(11, 31) # this has 20 data points

with model:

    model.set_data('X', new_data, coords={'obs_id': range(len(new_data))})

    y_pred = pm.sample_posterior_predictive(
        mcmc_result,
        var_names=['y'],
        return_inferencedata=True,
        predictions=True,
        random_seed=123,
    )

```

* * *

---

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [November 20, 2022, 6:41pm UTC](https://discourse.pymc.io/t/shape-mismatch-when-new-data-is-set-as-a-predictor-for-sample-posterior-predictive/10861/2 "2022-11-20T18:41:21Z")

</div>

I think you have to use model.set\_dim if you modify the length of the dimension with coordinate values assigned to it.

[Refitting PyMC models with ArviZ — ArviZ dev documentation](https://python.arviz.org/en/latest/user_guide/pymc_refitting.html) is not specifically about this but it does this among other things, it might also be helpful

---

<div class="post-metadata">

**Author:** ![Kengo](https://avatars.discourse-cdn.com/v4/letter/k/5daacb/32.png) [@Kengo](https://discourse.pymc.io/u/Kengo)\
**Post date:** [November 20, 2022, 11:56pm UTC](https://discourse.pymc.io/t/shape-mismatch-when-new-data-is-set-as-a-predictor-for-sample-posterior-predictive/10861/3 "2022-11-20T23:56:22Z")

</div>

@OriolAbril Thank you for your help. I changed my code based on the linked page provided, and it seems to work. In my case, the minimum modification required was just adding the same dimension name to ‘y’.

```
pm.Normal('y', mu, sigma=sigma, observed=data['y'], dims='obs_id')

```
