Hello I am using CmdStanModel using Python.
The model has run well and I have all the summary I need. Here is the code:
LVR_model = CmdStanModel(stan_file=stan_file)
import logging
cmdstanpy_logger = logging.getLogger(“cmdstanpy”)
cmdstanpy_logger.disabled = True
LVR_model.compile()fit_1 = LVR_model.sample(data=LVR_data, chains=4, iter_sampling=200,iter_warmup=200, adapt_delta=0.80, max_treedepth=10)
Fit_Summary.loc[[‘beta_pop’,‘beta_gdp’,‘beta_rate’,‘y_sigma’,‘local_error_sigma’,‘slope_error_sigma’,‘season_error_sigma’,‘nu_y’,‘nu_slope’,‘nu_trend’,‘nu_season’],:
So far so good, but when I am trying to run this idata_from_cmdstanpy = az.from_cmdstanpy(fit_1, posterior_predictive = [“y_predict”])
I am getting the following error:
ValueError Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\cmdstanpy\stanfit\mcmc.py:127, in CmdStanMCMC.getattr(self, attr)
126 try:
→ 127 return self.stan_variable(attr)
128 except ValueError as e:
129 # pylint: disable=raise-missing-from
File ~\anaconda3\lib\site-packages\cmdstanpy\stanfit\mcmc.py:733, in CmdStanMCMC.stan_variable(self, var, inc_warmup)
732 if var not in self._metadata.stan_vars_dims:
→ 733 raise ValueError(
734 f’Unknown variable name: {var}\n’
735 'Available variables are ’
736 + ", ".join(self._metadata.stan_vars_dims)
737 )
738 if self._draws.shape == (0,):
ValueError: Unknown variable name: sample
Available variables are y_sigma, local_error_sigma, slope_error_sigma, season_error_sigma, season, nu, trend_slope, local_trend, nu_y, beta_pop, beta_gdp, beta_rate, nu_slope, nu_trend, nu_season, y_forecast, local_trend_forecast, trend_slope_forecast, season_forecast, y_predict, log_likelihood
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
Input In [57], in <cell line: 1>()
----> 1 idata_from_cmdstanpy = az.from_cmdstanpy(fit_1, posterior_predictive = [“y_predict”])
File ~\anaconda3\lib\site-packages\arviz\data\io_cmdstanpy.py:380, in from_cmdstanpy(posterior, posterior_predictive, predictions, prior, prior_predictive, observed_data, constant_data, predictions_constant_data, log_likelihood, coords, dims)
334 def from_cmdstanpy(
335 posterior=None,
336 *,
(…)
346 dims=None
347 ):
348 “”“Convert CmdStanPy data into an InferenceData object.
349
350 Parameters
(…)
378 InferenceData object
379 “””
→ 380 return CmdStanPyConverter(
381 posterior=posterior,
382 posterior_predictive=posterior_predictive,
383 predictions=predictions,
384 prior=prior,
385 prior_predictive=prior_predictive,
386 observed_data=observed_data,
387 constant_data=constant_data,
388 predictions_constant_data=predictions_constant_data,
389 log_likelihood=log_likelihood,
390 coords=coords,
391 dims=dims,
392 ).to_inference_data()
File ~\anaconda3\lib\site-packages\arviz\data\io_cmdstanpy.py:261, in CmdStanPyConverter.to_inference_data(self)
252 def to_inference_data(self):
253 “”“Convert all available data to an InferenceData object.
254
255 Note that if groups can not be created (i.e., there is no output
, so
256 the posterior
and sample_stats
can not be extracted), then the InferenceData
257 will not have those groups.
258 “””
259 return InferenceData(
260 **{
→ 261 “posterior”: self.posterior_to_xarray(),
262 “sample_stats”: self.sample_stats_to_xarray(),
263 “posterior_predictive”: self.posterior_predictive_to_xarray(),
264 “predictions”: self.predictions_to_xarray(),
265 “prior”: self.prior_to_xarray(),
266 “sample_stats_prior”: self.sample_stats_prior_to_xarray(),
267 “prior_predictive”: self.prior_predictive_to_xarray(),
268 “observed_data”: self.observed_data_to_xarray(),
269 “constant_data”: self.constant_data_to_xarray(),
270 “predictions_constant_data”: self.predictions_constant_data_to_xarray(),
271 “log_likelihood”: self.log_likelihood_to_xarray(),
272 }
273 )
File ~\anaconda3\lib\site-packages\arviz\data\base.py:37, in requires.call..wrapped(cls, *args, **kwargs)
35 if all([getattr(cls, prop_i) is None for prop_i in prop]):
36 return None
—> 37 return func(cls, *args, **kwargs)
File ~\anaconda3\lib\site-packages\arviz\data\io_cmdstanpy.py:100, in CmdStanPyConverter.posterior_to_xarray(self)
93 invalid_cols = (
94 posterior_predictive
95 + predictions
96 + log_likelihood
97 + [col for col in columns if col.endswith(“__”)]
98 )
99 valid_cols = [col for col in columns if col not in invalid_cols]
→ 100 data = _unpack_frame(self.posterior.sample, columns, valid_cols)
101 return dict_to_dataset(data, library=self.cmdstanpy, coords=self.coords, dims=self.dims)
File ~\anaconda3\lib\site-packages\cmdstanpy\stanfit\mcmc.py:130, in CmdStanMCMC.getattr(self, attr)
127 return self.stan_variable(attr)
128 except ValueError as e:
129 # pylint: disable=raise-missing-from
→ 130 raise AttributeError(*e.args)
AttributeError: Unknown variable name: sample
Available variables are y_sigma, local_error_sigma, slope_error_sigma, season_error_sigma, season, nu, trend_slope, local_trend, nu_y, beta_pop, beta_gdp, beta_rate, nu_slope, nu_trend, nu_season, y_forecast, local_trend_forecast, trend_slope_forecast, season_forecast, y_predict, log_likelihood
Can anybody help please?
Thanks
Ants