2

I have estimated a model using pystan:

import pystan

stan_model = pystan.StanModel('stan_codes/stan_code_1.stan')
samples = stan_model.sampling(data = sdata, iter = 10, chains = 1, seed = 42)
model_results = {'mdata' : model_data, 'sdata': sdata, 'samples' : samples, 'model': stan_model}

Later I want to extract the "stuff" from the samples into a dataframe format. I am using

mdata, sdata, samples, model = [x for x in model_results.values()]
samples.extract().to_dataframe()

But I am getting an error:

 AttributeError: 'collections.OrderedDict' object has no attribute 'to_dataframe'

According to the documentation the samples.extract() should have an attribute to_dataframe(), right ? Am i doing something wrong here ? I am using pystan version 2.18.0.0

quant
  • 4,062
  • 5
  • 29
  • 70

1 Answers1

1

Have you tried the extraction to DataFrame direct from the fit object

Something like ...

stan_model = pystan.StanModel('stan_codes/stan_code_1.stan')
samples = stan_model.sampling(data = sdata, iter = 10, chains = 1, seed = 42)
chains = samples.to_dataframe('parameter name')
Mark Graph
  • 4,969
  • 6
  • 25
  • 37
  • Also, I would strongly urge you to use more than one chain. Four is the recommended number. They are needed for Rhat, the Gelman-Rubin convergence diagnostic. – Mark Graph Oct 13 '18 at 08:39
  • 1
    Actually `samples.to_dataframe()` works (Takes all the parameters) – quant Oct 15 '18 at 10:12