Hello guys,
I have a hierarchical switchpoint model built and the convergence looks great, incorporating individual and group level model with my “mean” parameters.
However, I am now stuck at adding a new multilevel comparison. My data consists of body length measurement with respect to Age. I would like to explore if any sex effect has on the switchpoint.
How should I add in the multilevel effect…something like pm.Binomial() …? for instance I am testing between Male and Female, say I have df.Sex with 2 levels - Male and Female encoded with 0 and 1.
And any suggestion if I am adding multinomial groups? such as country difference, for instance USA, Canada and Britain, encoded with 0,1,2?
Many thanks!!
with pm.Model() as model:
# Switchpoint
mean_switchpoint = pm.Normal(‘mean_switchpoint’, 4.0, sd=2)
switchpoint = pm.TruncatedNormal(‘switchpoint’, mean_switchpoint, sd=2., lower=0., upper=10., shape=n_sample)# Hyperpriors for intercept & slope intercept_pre_mean = pm.Normal('intercept_pre_mean', 17.0, sd=5.0) intercept_post_mean = pm.Normal('intercept_post_mean', 13.0, sd = 5.0 ) slope_pre_mean = pm.TruncatedNormal('slope_pre_mean', 0, sd=2, upper=0.) slope_post_mean = pm.Normal('slope_post_mean', 0, sd=2) # Priors for pre- and post-switch intercepts and slopes intercept_pre = pm.Normal('intercept_pre', mu = intercept_pre_mean, sd= 2.0, shape=n_sample) intercept_post = pm.Normal('intercept_post', mu = intercept_post_mean, sd = 2.0, shape=n_sample) slope_pre = pm.Normal('slope_pre', slope_pre_mean, sd=2, shape=n_sample) slope_post = pm.Normal('slope_post', slope_post_mean, sd=2, shape=n_sample) intercept = pm.math.switch(switchpoint[id_idx] >= df.Age, intercept_pre[id_idx], intercept_post[id_idx]) x_coeff = pm.math.switch(switchpoint[id_dx] >= df.Age, slope_pre[id_idx], slope_post[sousa_idx]) likelihood = pm.Normal('likelihood', mu = intercept + x_coeff * df.Age, observed=df.length)
pm.model_to_graphviz(model)