Help with first model in Pystan


I’m trying to learn Stan by slowly implementing a poisson GLM. I’m starting with just getting the mean to sample and I’m getting the following error:

RuntimeError: Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=y; dims declared=(1233565); dims found=(1233565,1)  (in 'unknown file name' at line 4)

Below is my model:

When including Stan code in your post it really helps if you make it as readable as possible by using Stan code chunks (```stan) with clear spacing and indentation. For example, use

liability_code = """
data {
    int<lower=0> N;
    vector[N] y;
parameters {                     //parameters is what we want to infer....we don't know the coefficients

    real mu;
model {
 y ~ poisson(mu);   
stan_model = pystan.stan(model_code=liability_code, data = {'N':1233565, 'y':y}, iter = 1000, chains = 2)

Eventually, I have a data frame of 18 variables that go into this model. I’m trying to show what a Bayesian analysis can do for our team to my boss. Thanks for any help getting me there.

I think your y is N,1 ndarray.

To fix this you can unravel it -->

y = y.ravel()

It’s a pandas dataframe so .ravel() is not an attribute. I also tried y.items but that didn’t work either.

Try doing .to_numpy().ravel(), then. See here:

Thank you.

I did just coerce the values into a list and used those.

y1 = y.iloc[:,0].values.tolist()