Repository: arviz-devs/arviz

Issue:
plot_ppc - KeyError: "['y'] var names are not present in dataset"
Hi, I've had troubles using `plot_ppc` in a project I'm working on, so I tried to reproduce it using the quickstart pystan example. This is the code I'm using:

```python
schools_code = """
data {
  int<lower=0> J;
  real y[J];
  real<lower=0> sigma[J];
}

parameters {
  real mu;
  real<lower=0> tau;
  real theta[J];
}

model {
  mu ~ normal(0, 5);
  tau ~ cauchy(0, 5);
  theta ~ normal(mu, tau);
  y ~ normal(theta, sigma);
}
generated quantities {
    vector[J] log_lik;
    vector[J] y_hat;
    for (j in 1:J) {
        log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);
        y_hat[j] = normal_rng(theta[j], sigma[j]);
    }
}
"""

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

sm = pystan.StanModel(model_code=schools_code, verbose=False)
fit = sm.sampling(data=schools_dat, iter=1000, chains=4)

data = az.from_pystan(posterior=fit,
                      posterior_predictive='y_hat',
                      observed_data=['y'],
                      log_likelihood='log_lik',
                      coords={'school': schools},
                      dims={'theta': ['school'], 'y': ['school'], 'log_lik': ['school'], 'y_hat': ['school'], 'theta_tilde': ['school']})

az.plot_ppc(data)
```

This raises:

```python
KeyError: "['y'] var names are not present in dataset"
```

which is exactly the kind of error I got in my own project... I guess I'm doing something wrong, here, but I can't figure out what it is.

Any help would be appreciated... thanks!


Discussion:
In docstring

```
data_pairs : dict
        Dictionary containing relations between observed data and posterior predictive data.
        Dictionary structure:
        Key = data var_name
        Value = posterior predictive var_name
        For example, `data_pairs = {'y' : 'y_hat'}`
        If None, it will assume that the observed data and the posterior
        predictive data have the same variable name.
```
Thanks!

Just a suggestion - perhaps it's worth adding this to the example:
https://arviz-devs.github.io/arviz/examples/plot_ppc.html

I guess most users will start from looking at the example, and since the y/y_pred scenario is probably pretty common, it might help...
Thanks for the suggestion. I think it is a good idea!
Hello. I'd like to work on this issue!
@OriolAbril I am a bit confused about what I should do. When modifying the documentation for plot_ppc, as per this issue, do I directly add the code from the PyStan quickstart(with a citation) and display the error?Do I also add any additional information? 
I would use the argument `data_pairs` in one of the examples of the example gallery and in one of the examples in the docstring. I think it will be enough. 

With the inference data used to generate the docs it is not really necessary to use this argument, but it being shown can help the usage of `plot_ppc` with pystan objects (where `data_pairs` is needed).
Ok thank you for the clarification! 
@OriolAbril I was trying to work with the first example. I understand that I have to extract data from the `observed_data` and `predictive_posterior` `xarray.Dataset` objects in the `InferenceData` object but I am not sure which attribute I should use. Could you please help me?
The code in the example gallery [plot_ppc example](https://arviz-devs.github.io/arviz/examples/matplotlib/mpl_plot_ppc.html) calls it using `az.plot_ppc(data, alpha=0.03, figsize=(12, 6), textsize=14)`, this can be changed to `az.plot_ppc(data, data_pairs={"obs": "obs"}, alpha=0.03, figsize=(12, 6), textsize=14)` because in this case the variables in observed data and posterior predictive have the same name. 
I understand. I will make the changes accordingly.

Resolving patch:
diff --git a/arviz/plots/ppcplot.py b/arviz/plots/ppcplot.py
--- a/arviz/plots/ppcplot.py
+++ b/arviz/plots/ppcplot.py
@@ -62,8 +62,8 @@ def plot_ppc(
     data_pairs : dict
         Dictionary containing relations between observed data and posterior/prior predictive data.
         Dictionary structure:
-        Key = data var_name
-        Value = posterior/prior predictive var_name
+            key = data var_name
+            value = posterior/prior predictive var_name
         For example, `data_pairs = {'y' : 'y_hat'}`
         If None, it will assume that the observed data and the posterior/prior
         predictive data have the same variable name.
@@ -128,6 +128,7 @@ def plot_ppc(
         >>> import arviz as az
         >>> data = az.load_arviz_data('radon')
         >>> az.plot_ppc(data,data_pairs={"obs":"obs"})
+        >>> #az.plot_ppc(data,data_pairs={"obs":"obs_hat"})
 
     Plot the overlay with empirical CDFs.
 


Test patch: