Hi all, I’m working on some hierarchical regressions but I have a few groups which only have one member. I’m considering different variances for each group, but if a group only has one member, it doesn’t have a variance.
To generate the individual level variables, I loop over each individual and define the variable with an if statement depending on whether the group has one or multiple members. Full code is attached for an example with made up data, and here’s a snippet with the model.
with pm.Model() as model:
#Baseline intercept
a0 = pm.Normal('baseline',mu=0.,sd=5.)
#group level predictors
a_group = hierarchical_normal('group',5)
mu_individual = a_group[groups]
sigma_indiv_upp = pm.HalfNormal('sigma_individual_upper', sd=1.)
sigma_indiv = pm.HalfNormal('sigma_individual',sd=sigma_indiv_upp,shape=4)
a_individual = []
for i in range(nIndiv):
if i in skip:
a_individual.append(pm.Deterministic('individual_{}'.format(i), mu_individual[i]))
else:
delta = pm.Normal('delta_individual_{}'.format(i), 0., 1.)
a_individual.append(pm.Deterministic('individual_{}'.format(i), mu_individual[i] + delta * sigma_indiv[groups[i]]))
a_individual = tt.as_tensor_variable(a_individual)
eta = a0 + a_individual[indivIdx]
p = pm.math.sigmoid(eta)
likelihood = pm.Binomial("obs",N,p,observed=obs)
This seems to work fine, but then the individual coefficients don’t get treated like vectors as they would if I had generated them with the shape argument set. So when I do a traceplot for example, I get separate subplots for each of the individual coefficients rather than having all of them on one subplot.
Is there a better way to define the model, or if not, is there a way to make the individual coefficients behave like a vector for plotting purposes? Thanks
toyExample.py (2.9 KB)