Hi
I’m trying to make the example (A Primer on Bayesian Methods for Multilevel Modeling — PyMC3 3.11.5 documentation) for hierarchical models work, but I’m struggling to introduce new betas (b_2, from data_var_2) efficiently for other variables from the data that I want to add without adding them manually (b_2,sigma_b_2, adjusting formula for theta).
coords = {"Level": ["Basement", "Floor"], "obs_id": np.arange(floor.size)}
with pm.Model(coords=coords) as varying_intercept_slope:
floor_idx = pm.Data("floor_idx", floor, dims="obs_id")
#data_2_idx = pm.Data("data_2_idx", data_var_2, dims="obs_id")
county_idx = pm.Data("county_idx", county, dims="obs_id")
### Hyperpriors:
a = pm.Normal("a", mu=0.0, sigma=5.0)
sigma_a = pm.Exponential("sigma_a", 1.0)
#a_2 = pm.Normal("a_2", mu=0.0, sigma=5.0)
#sigma_a_2 = pm.Exponential("sigma_a_2", 1.0)
b = pm.Normal("b", mu=0.0, sigma=1.0)
sigma_b = pm.Exponential("sigma_b", 0.5)
# b_2 = pm.Normal("b_2", mu=0.0, sigma=1.0)
# sigma_b_2 = pm.Exponential("sigma_b_2", 0.5)
### Varying intercepts:
za_county = pm.Normal("za_county", mu=0.0, sigma=1.0, dims="County")
# za_2_county = pm.Normal("za_2_county", mu=0.0, sigma=1.0, dims="County")
# Varying slopes:
zb_county = pm.Normal("zb_county", mu=0.0, sigma=1.0, dims="County")
#zb_2_county = pm.Normal("zb_2_county", mu=0.0, sigma=1.0, dims="County")
### Expected value per county:
theta = (a + za_county[county_idx] * sigma_a) + (b + zb_county[county_idx] * sigma_b) * floor
#+ (a_2 + za_2_county[county_idx] * sigma_a_2)+ (b_2 + zb_2_county[county_idx] * sigma_b_2) * data_var_2
### Model error:
sigma = pm.Exponential("sigma", 1.0)
y = pm.Normal("y", theta, sigma=sigma, observed=log_radon, dims="obs_id")
varying_intercept_slope_idata = pm.sample(
2000, tune=2000, target_accept=0.99, random_seed=RANDOM_SEED, return_inferencedata=True
)
Can this be handled via some shape parameters or do I need to create a special xarray like described here? Coordinates in PyMC & InferenceData Objects – Christian Luhmann – Python, math, etc.