Skip to content

Commit bbeebe5

Browse files
authored
Bacteria (#101)
* add bacteria example * update references * update references
1 parent 26042c0 commit bbeebe5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

Chapters/Sensitivity_checks.qmd

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,60 @@ Overall, the power-scaling sensitivity analysis on the adjusted prior shows that
279279

280280
### Bacteria treatment
281281

282+
Now we discuss and example of power-scaling sensitivity analysis for hierarchical models. The main motivation for this example is to show that for certain models we should selectively power-scaled the priors. To illustrate this, consider two forms of prior, a non-hierarchical prior with two independent parameters $p(\theta)$ and $p(\phi)$ and a hierarchical prior of the form $p(\theta \mid \psi) p(\psi)$. In the first case, the appropriate power-scaling for the prior is $p(\theta)^{\alpha} p(\phi)^{\alpha}$. This is what we did in the previous example. In the second case, for the hierarchical model, we only want to power-scale the top level prior, that is, $p(\theta) p(\phi)^{\alpha}$.
283+
284+
For this example we are going to use the bacteria data set [@venables_2002].
285+
286+
```{python}
287+
bacteria = pd.read_csv("../data/bacteria.csv")
288+
bacteria["y"] = bacteria["y"].astype("category").cat.codes
289+
bacteria["ID"] = bacteria["ID"].astype("category").cat.codes
290+
bacteria["trtDrugP"] = bacteria["trt"] == "drug+"
291+
bacteria["trtDrug"] = bacteria["trt"] == "drug"
292+
K = len(bacteria["ID"].unique())
293+
```
294+
295+
Let's start by fitting a hierarchical model. The model is as follows:
296+
297+
::: {.panel-tabset}
298+
## PyMC
299+
300+
```{python}
301+
with pm.Model() as model:
302+
μ = pm.Normal('μ', mu=0, sigma=10)
303+
β_week = pm.Normal('β_week', mu=0, sigma=10)
304+
β_trtDrug = pm.Normal('β_trtDrug', mu=0, sigma=10)
305+
β_trtDrugP = pm.Normal('β_trtDrugP', mu=0, sigma=10)
306+
307+
σ = pm.HalfNormal('σ', sigma=5)
308+
b_Intercept = pm.Normal('b_Intercept', mu=0, sigma=σ, shape=K)
309+
310+
theta = μ + b_Intercept[bacteria.ID] + β_week * bacteria.week + β_trtDrug * bacteria.trtDrug + β_trtDrugP * bacteria.trtDrugP
311+
312+
y_obs = pm.Bernoulli('y_obs', logit_p=theta, observed=bacteria.y)
313+
314+
idata = pm.sample()
315+
pm.compute_log_prior(idata, var_names=["μ", "β_week", "β_trtDrug", "β_trtDrugP", "σ"])
316+
pm.compute_log_likelihood(idata)
317+
```
318+
319+
## PyStan
320+
321+
``` {.python}
322+
## coming soon
323+
```
324+
:::
325+
326+
From the power-scaling sensitivity analysis perspective the key element in the previous code-block is that we are specifying the variables we want to use for the prior-powerscaling
327+
`var_names=["μ", "β_week", "β_trtDrug", "β_trtDrugP", "σ"]` i.e. we are omitting the `b_Intercept` variable. This is because we are only interested in power-scaling the top level prior. There are two way to specify the variables for power-scaling, the first is to use the `var_names` argument when computing the log_prior and/or log_likelihood, as we just did. The second is to use the `prior_varnames` and `likelihood_varnames` arguments in the `psense`-related functions.
328+
329+
Let's compute sensitivity diagnostics for all variables except `~b_Intercept`, if we want to check the sensitivity of all of them we can do it. The key point with hierarchical models is to not power-scale the lower level priors.
330+
331+
```{python}
332+
azs.psense_summary(dt, var_names=["~b_Intercept"])
333+
```
334+
We see that everything looks fine. If you like to get potentials issues you could try running the model again with a prior like `σ = pm.HalfNormal('σ', sigma=1)`.
335+
282336

283337
## Interpreting sensitivity diagnostics: Summary
284338

references.bib

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,16 @@ @InProceedings{nguyen_2015
462462
publisher="Springer International Publishing",
463463
address="Cham",
464464
pages="173--189",
465+
}
466+
467+
@book{venables_2002,
468+
address = {New York},
469+
edition = {4th edition},
470+
title = {Modern {Applied} {Statistics} with {S}},
471+
isbn = {978-0-387-95457-8},
472+
language = {English},
473+
publisher = {Springer},
474+
author = {Venables, W. N. and Ripley, B. D.},
475+
month = aug,
476+
year = {2002},
465477
}

0 commit comments

Comments
 (0)