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., 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.


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:


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

1 Like