question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

plot_ppc - KeyError: "['y'] var names are not present in dataset"

See original GitHub issue

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:

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:

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!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
amukh18commented, Jan 14, 2020

Hello. I’d like to work on this issue!

0reactions
amukh18commented, Jan 15, 2020

I understand. I will make the changes accordingly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting an error I'm not Familiar with Trying to plot the ppc
I'm getting two errors when trying to use arviz to plot_ppc. ... KeyError: 'var names: "[\'y\'] are not present" in dataset'.
Read more >
Getting KeyError when setting dataset column to variable with ...
If you want to use filtering with a boolean-mask, you need to use loc , and if you just want a column you...
Read more >
Python KeyError Exceptions and How to Handle Them
In this tutorial, you'll learn how to handle Python KeyError exceptions. They are often caused by a bad key lookup in a dictionary,...
Read more >
How to Fix KeyError Exceptions in Python - Rollbar
When an item in a dictionary is accessed using a key, and the key is not found within the set of keys of...
Read more >
What is KeyError in Python? Dictionary and Handling Them
Here I am trying to access a key called “D” which is not present in the dictionary. Hence, the error is thrown as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found