# Az.plot\_ppc() and coords

**URL:** <https://discourse.pymc.io/t/az-plot-ppc-and-coords/13020>\
**Category:** v5\
**Tags:** arviz\
**Created:** [September 29, 2023, 12:14pm UTC](https://discourse.pymc.io/t/az-plot-ppc-and-coords/13020 "2023-09-29T12:14:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nn\_Nnn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nn_nnn/32/3790_2.png) [@Nn\_Nnn](https://discourse.pymc.io/u/Nn_Nnn)\
**Post date:** [September 29, 2023, 12:14pm UTC](https://discourse.pymc.io/t/az-plot-ppc-and-coords/13020/1 "2023-09-29T12:14:46Z")

</div>

Runing on PyMC v5.3.0  
Runing on arviz v0.15.1  
Hi,  
Hopefully somebody can see how to solve what seems like a simple problem of coords and ppc,s. which is to use az.plot\_ppc(idata,coords) to plot ppcs for separate groups.

The problem appears solved in this example from : [arviz.plot\_ppc — ArviZ 0.16.1 documentation](https://python.arviz.org/en/stable/api/generated/arviz.plot_ppc.html) for getting az.plot\_ppc() on indidual groups.

```auto
idata_radon = az.load_arviz_data('radon')
az.plot_ppc(idata_radon, data_pairs={"y":"y"},num_pp_samples=20)
obs_county = idata_radon.posterior["County"][df1.constant_data["county_idx"]]
idata_radon = idata_radon.assign_coords(obs_id=obs_county, groups="observed_vars")
az.plot_ppc(idata_radon, coords={'obs_id': ['ANOKA', 'BELTRAMI']}, flatten=[],num_pp_samples=20) 

```

this 5 lines of code work perfectly for the radon idata.

The example I am working on is from [Coordinates in PyMC & InferenceData Objects – Christian Luhmann – Python, math, etc. (cluhmann.github.io)](https://cluhmann.github.io/inferencedata/) which is explaining the use of coords in pymc.

This is the adapted code:

```auto
# CREAT SOME KNOWN BLOOD_PRESSURE DATA (BP) FOR TWO GROUPS (MALES AND FEMALES)

rng = np.random.default_rng(101010)

group_size = 100
systolic_f = np.random.normal(loc=123, scale=5, size=group_size)
systolic_f =rng.normal(loc=120, scale=2, size=group_size)#123
systolic_m = rng.normal(loc=150, scale=2, size=group_size)#127

systolic = np.hstack((systolic_f, systolic_m))
gender = (['female'] * group_size) + (['male'] * group_size)

df = pd.DataFrame({'bp':systolic, 'gender':gender})

df.reset_index()
df.head()

cat=pd.Categorical(df['gender'])

with pm.Model(coords = {'gender':['female','male']}) as labeled_model:
    mu = pm.Normal('mu', 140, sigma=15, dims='gender')
    
    likelihood = pm.Normal('likelihood', 
                           mu=mu[cat.codes],
                           sigma=5, 
                           observed=df['bp'])
    idata_coords=pm.sample(chains=2)
    idata_coords.extend(pm.sample_posterior_predictive(idata_coords))
az.plot_ppc(idata_coords,num_pp_samples=20) 

```

this line works but to get the ppc plot for the ‘male’ and ‘female’ groups separately does not work!

```auto
az.plot_ppc(idata_coords,coords={'gender':['female','male']}) 

```

This last line of code does not work. Could anyone tell me how to write the last line of code using az.plot\_ppc() and the coords to get plot predictions for the individual groups as appears to have been achieved in the Radon example above. Thank you in advance for examining code.

---

<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:** [September 30, 2023, 9:06am UTC](https://discourse.pymc.io/t/az-plot-ppc-and-coords/13020/2 "2023-09-30T09:06:09Z")

</div>

The ppc plot does not use the `mu` variable but the `likelihood` one, which is the one that can be compared to the observations. And `likelihood` has no information about any dimensions or coordinates, so it can’t know which observations correspond to which gender.

This is also what happens in the radon example in plot\_ppc docstring. We need to assign the coordinate values of what we want before calling plot\_ppc. You’ll have to follow the same steps, but the `obs_county` won’t be an array with the county that correspond to each observation but the genders of each observation, in your case I think `cat.categories`
