Dumping data in json file by cmdstanpy does not see a variable to be declared

Dear all,

When I dump the data to the file, it says that the variable N does not exist:

stan_data = dict({
    'N': df_.shape[0],
    'onset_day': np.array(df_.發病日期.values, dtype='int'),
    'report_day': np.array(df_.確診日期.values, dtype='int'),
    'truncation_day': truncation_day,
    'M': 150,
    'xmax': 13.0,
    'xmin': -2.0
})
stan_data_file = os.path.join(standirname, 'Data.R')
cmdstan.write_stan_json(stan_data_file, stan_data)

The error:

RuntimeError: Error during sampling: Exception: variable does not exist; processing stage=data initialization; variable name=N; base type=int 

I assume I need to declare the type directly (I remember I had a similar issue in the past). I also tried to write int(df_.shape[0]) following Difficulty passing data to PyStan: variable name=N; dims declared=(); dims found=(1) - #5 by ahartikainen, but result is the same

Interestingly, when I do the same with pystan (v.2.19.1.1), everything works

import pystan as stan
stan.misc.stan_rdump(stan_data, stan_data_file)

Thank you in advance and my apologies if I am wrong somewhere.

Best,
Andrei

Can you open the json file and check how it looks like? (And you have defined N in your data block?

Edit. Can you change your datafile ending to .json?

1 Like

The json file looks quite normal:

{"N":562,"onset_day":[13,10,12,10,14,14,7,15,14,15,14,15,16,18,17,14,17,19,19,19,10,19,17,18,16,20,17,10,15,18,21,21,20,21,21,21,21,21,21,21,22,21,21,22,20,20,21,21,23,23,23,20,23,23,24,20,23,20,24,24,24,23,23,25,25,24,24,23,22,26,26,23,25,25,26,27,27,27,22,25,27,25,27,27,19,20,21,21....

but when I changed the ending from Data.R to Data.json, it worked :/

1 Like