pymare.estimators.StanMetaRegression
- class StanMetaRegression(tau_prior_scale=None, **sampling_kwargs)[source]
Bases:
BaseEstimatorBayesian meta-regression estimator using Stan.
- Parameters:
tau_prior_scale (None or
float, optional) – Scale of the half-normal prior on tau, the between-group standard deviation. When None (default), it is set tomax(std(y), sqrt(mean(v))), the larger of the observed spread of the estimates and the typical sampling standard deviation.**sampling_kwargs – Optional keyword arguments to pass on to CmdStanPy’s sampler (e.g.,
iter_samplingfor the number of post-warmup draws per chain,chains,seed,adapt_delta).
Notes
The model is

where
is the known sampling standard deviation
of observation
and
is its group. This is the random-effects
meta-analysis model of the Stan User’s Guide [1] with that guide’s stated
extension to observation-level predictors. The reported tau2is
, the between-group variance, matching what every other
PyMARE estimator reports under that name.thetais given a non-centered parameterization (theta = tau * theta_rawwiththeta_rawstandard normal). The centered form produces the funnel geometry that dominates divergences in hierarchical models with few groups, which is this estimator’s principal use case.
gets a half-normal prior, weakly informative per Stan’s
recommendations [2] for models with few groups. Its scale is derived from the
data rather than fixed, since a fixed scale would be crushingly informative on
data measured in thousands and vacuous on data measured in thousandths.The default scale is
max(std(y), sqrt(mean(v))).
cannot
plausibly exceed the spread of the estimates, and should not be presumed
smaller than a typical standard error, so the larger of the two never asserts
that
is small when either quantity says otherwise. Erring large
is deliberate: too small a scale costs coverage, too large costs only
precision in
. validation/stanrecords the measurements behind that choice. Passtau_prior_scaleto override it.
keeps Stan’s implicit improper uniform prior, so under a
diffuse prior on
the posterior means agree with
VarianceBasedLikelihoodEstimatoratmethod="ML".The Stan program is compiled on first use and cached beside the installed source, so the cost is paid once per installation rather than per fit.
References
Changed in version 0.0.11:
The backend moved from PyStan 3 to CmdStanPy. PyStan’s sampler argument names (
num_samples,num_warmup,num_chains,num_thin) are rejected with a message naming their replacements.tau2is now the between-group variance rather than its square root, and sampling variances are converted to standard deviations before being passed to Stan. Both were wrong before, so posterior estimates change.groupsaccepts scalar labels of any type, not only integers in1..k, andfit_dataset()now passesdataset.grather than dropping it.cinow sets the width of the reported credible interval. It was previously accepted and ignored.
- compile(force=False)[source]
Compile the Stan model.
- Parameters:
force (
bool, optional) – Whether to recompile even when an up-to-date executable already exists. Default = False.- Returns:
The instance, so that
compile()can be chained.- Return type:
Notes
Called by
fit()when needed, so it never has to be called directly. Calling it in advance is worthwhile when the same estimator will be fitted to several datasets, because the compiled executable does not depend on the data.The executable is written beside the installed
.stanfile, where CmdStanPy finds and reuses it on later runs. If that directory is not writable – a read-onlysite-packages, for instance – it falls back to~/.pymare/stanand warns once.
- fit(y, v, X, groups=None)[source]
Run the Stan sampler and return results.
- Parameters:
y (
numpy.ndarrayof shape (K,)) – 1d array of observation-level estimatesv (
numpy.ndarrayof shape (K,)) – 1d array of observation-level variancesX (
numpy.ndarrayof shape (K[, P])) – 1d or 2d array containing observation-level predictors (including intercept); has dimensions K x P, where K is the number of observations and P is the number of predictor variables.groups (None or array-like of shape (K,), optional) – One scalar label per observation, identifying the groups of observations in the y/v/X inputs. Labels may be strings, integers or any other hashable that numpy stores as a single element, and need not be consecutive; they are encoded internally in order of first occurrence by
encode_groups(). Composite labels such as tuples are not accepted, because numpy reads a sequence of them as a 2-dimensional array. When None (default), each observation in the inputs is treated as a separate group.
- Returns:
The fitted instance.
- Return type:
- Warns:
UserWarning – If the sampler reported divergent transitions. Divergences mean the sampler could not explore part of the posterior, so the reported means and intervals may be biased; refitting with a larger
adapt_deltais the usual remedy.
Notes
This estimator supports (simple) hierarchical models. When multiple observations belong to at least one common sampling unit, the groups argument can specify the nesting structure (i.e., which rows in y, v, and X belong to each group).
The raw CmdStanPy fit is kept on
self.result_, so its diagnostics remain reachable –est.result_.diagnose()reports R-hat, effective sample size, E-BFMI and treedepth alongside divergences.Changed in version 0.0.5:
groupsaccepts arbitrary hashable labels, and passing a numpy array no longer raises.
- fit_dataset(dataset, *args, **kwargs)[source]
Apply the current estimator to the passed Dataset container.
A convenience interface that wraps fit() and automatically aligns the variables held in a Dataset with the required arguments.
- Parameters:
dataset (
Dataset) – A PyMARE Dataset instance holding the data.*args – Optional positional arguments to pass onto the
fit()method.**kwargs – Optional keyword arguments to pass onto the
fit()method.
- get_v(dataset)[source]
Get the variances, or an estimate thereof, from the given Dataset.
- Parameters:
dataset (
Dataset) – The dataset to use to retrieve/estimate v.- Returns:
2-dimensional array of variances/variance estimates.
- Return type:
Notes
This is equivalent to directly accessing
dataset.vwhen variances are present, but affords a way of estimating v from sample size (n) for any estimator that implicitly estimates a sigma^2 parameter.