I’ve been working through the Frailty and Survival Regression Models example, which includes an accelerated failure time (AFT) model. I’d like to extend this model for my own use case with an unpooled dimension. However, the AFT model already uses an index to split censored/uncensored data. I’m unsure how to add an index that refers to the unpooled groups. Below I’ve taken my best guess at how to do this but it is invalid.
idx = pd.Categorical(group_values, categories=unique_groups).codes
coords = {
"intervals": intervals,
"group": unique_groups,
"group_flat": group_values,
"preds": [
"sentiment",
"intention",
"Male",
"Low",
"Medium",
"Finance",
"Health",
"Law",
"Public/Government",
"Sales/Marketing",
],
}
X = retention_df[
[
"sentiment",
"intention",
"Male",
"Low",
"Medium",
"Finance",
"Health",
"Law",
"Public/Government",
"Sales/Marketing",
]
].copy()
y = retention_df["month"].values
cens = retention_df.left.values == 0.0
def weibull_lccdf(x, alpha, beta):
"""Log complementary cdf of Weibull distribution."""
return -((x / beta) ** alpha)
with pm.Model(coords=coords, check_bounds=False) as aft_model:
idx = pm.MutableData("idx", floor_measure, dims="group_flat")
X_data = pm.MutableData("X_data_obs", X)
beta = pm.Normal("beta", 0.0, 1, dims="preds")
mu = pm.Normal("mu", 0, 1, dims="groups")
s = pm.HalfNormal("s", 5.0, dims="groups")
eta = pm.Deterministic("eta", pm.math.dot(beta, X_data.T))
reg = pm.Deterministic("reg", pt.exp(-(mu + eta) / s))
y_obs = pm.Weibull("y_obs", beta=reg[~cens][idx], alpha=s[idx], observed=y[~cens], dims="groups_flat")
y_cens = pm.Potential("y_cens", weibull_lccdf(y[cens], alpha=s, beta=reg[cens]))