(lecture_13)=

Multilevel Adventures

:::{post} Jan 7, 2024 :tags: statistical rethinking, bayesian inference, multilevel models :category: intermediate :author: Dustin Stansbury :::

This notebook is part of the PyMC port of the Statistical Rethinking 2023 lecture series by Richard McElreath.

Video - Lecture 13 - Multilevel Adventures

Review: Drawing the Bayesian Owl 🦉

  1. Establish Estimand
  2. Build Scientific model(s) (i.e. Causal model), depending on 1.
  3. Use 1 & 2 to build a Statistical Model
  4. Simulate data from from 2 and Validate you can recover from 3
  5. Analyze Real Data using 3.

In real life, it's never a linear path; you are jumping back and forth, iterating on 2-5, much like a branching path/choose your own adventure book.

Multi-level Adventures

Similarly there is no one-size-fits all approach to applying the methods in this course. In order to optimize for success when applying these methods, McElreath suggests a few strategies ("paths") moving forward:

  • Return to the start -- McElreath suggests to return to the beginning of the course, reviewing the material now that you've observed a lion's share of the building blocks.
    • It turns out that the notes presented in this repo are after the Third Pass of the course. I can't recommend strongly enough to take McElreath's advice and review the material from the beginning. It's a lot of material, but I was flabbergasted with how much I had forgotten in the short time between this lecture and the earlier ones. Similarly, I was surprised by how much easier it was to soak up the material the 2nd time around--a real testament to McElreath's outstanding teaching style.
  • Skim & Index -- don't sweat the deatils, just aquaint yourself with the possiblities.
    • One thing that I've found useful is to compile each of the model classes discussed in the course into a "recipe book" or "toolbox" of plug-and-play models that can be reused for different applications

Clusters vs Features

ClustersFeaturestanks⟶survivalstories⟶treatment  effectindividuals⟶average  responsedepartments⟶admission  rate\begin{align*} \textbf{Clusters}& && &\textbf{Features} \\ tanks& &\longrightarrow& &survival \\ stories& &\longrightarrow& & treatment\;effect \\ individuals& &\longrightarrow& & average\;response \\ departments& &\longrightarrow& & admission\;rate \\ \end{align*}
  • Clusters: subgroups in the data (e.g. tanks, participants, stories, departments)
    • Adding clusters is fairly simple
      • requires more index variables; more population priors
  • Features: aspects of the model (i.e. parameters) that vary by cluster (e.g. survival, average response rate, admission rate, etc.)
    • Adding features requires more complexity
      • more parametrers, particularly dimensions in each population prior

Varying effects as confounds

  • Varying effect strategy: using repeat observations and partial pooling to estimate unmeasured features of clusters that have left an imprent on the data
  • Predictive perspective: regularization
  • Causal Perspective: unobserved confounds are terrifying, but leveraging repeat observations give us some hope at more accurate inference

Previous Examples:

Grandparents & Education

  • Neighborhood is a backdoor path confound that blocks mediation analysis of the direct effect of $G$ on $C$
  • but having repeat observations for neighborhoods U allows us to estimate the effects of this confound

Trolley Problem Example

  • Individuals vary on how they react to the response scale, adding noise to our estimatates
  • However, given that each participant has repeat observations, we use the repeats to estimate this noise.
  • Similarly, individual traits may cause sampling bias through an unobserved participation node; we can use mixed effects to help address this sampling bias.

Fixed Effect Approach

  • rather than partial pooling, no pooling.
  • very few benefits to using fixed effects over varying reffects.
    • e.g. less efficient
  • focus on getting the story straight (generative model, causal graph), you can worry about the details of estimator efficiency, etc. later

Practical Difficulties

  • Varying effects models are always a good default, but
    1. how do you use more than one cluster
    2. Predictions is at the level of the hierarchy now, which level do we care about
    3. Sampling efficiency -- e.g. centered/non-centered priors
    4. Group-level confounding -- e.g. Full Luxury Bayes or Mundlak Machines. For details, see the BONUS section of Lecture 12 - Multilevel Models [blocked]

Fertility & Behavior in Bangladesh

  • 1989 Fertility Survey
  • 1924 women, 61 districts
  • Outcome, $C$: contraceptive use (binary variable)
  • Predictors: age, $A$ # of living children $K$, urban/rural location $U$
  • Potential (unobserved) confounds: Family traits, $F$
  • District ID: $D$

Competing causes

Start simple: varying districts

  • Estimand: contraceptive use in each district; partially pooled
  • Model:
    • varying intecept/offset for each district
Ci∼Bernoulli(pi)Contraception use is 0/1 outcomelogit(pi)=αD[i]log odds of C=1αj∼Normal(αˉ,σ)prior for districts; regularizerαˉ∼Normal(0,1)average districtσ∼Exponential(1)std. dev amongst districts\begin{align*} C_i &\sim \text{Bernoulli}(p_i) &\text{Contraception use is 0/1 outcome} \\ \text{logit}(p_i) &= \alpha_{D[i]} &\text{log odds of C=1} \\ \alpha_j &\sim \text{Normal}(\bar \alpha, \sigma) &\text{prior for districts; regularizer} \\ \bar \alpha &\sim \text{Normal}(0, 1) &\text{average district} \\ \sigma &\sim \text{Exponential}(1) &\text{std. dev amongst districts} \end{align*}

District-only model posterior predictions

Studying the posterior graph

Districts with small sample sizes (e.g. district 3) have

  • larger error bars -- exhibiting more uncertainty estimate
  • exhibit more shrinkage
    • posteriors are pulled toward the global mean (dashed line)
    • red circles are far from black circles) b.c. the model is less confident

Districts with large sample sizes (e.g. district 1) have

  • smaller error bars -- more certainty about estimates
  • less shrinkage
    • posteriors are closer to the empirical observations for the district

Districts with no data (e.g. district 49) still have posteriors

  • informed posterior from partial pooling
  • mean is near globla mean
  • error bars larger than for other districts (looks like i may have an indexing bug in my errorbar code--need to look into that)

Varying districs + urban

  • What is the effect of urban living?
  • Beware:
    • district features have potential group-level confounds
    • Total effect of $U$ passes through $K$
    • Do not stratify by $K$ -- it's a collider, that opens up the district-level confound through $D$

Statistical Model

Ci∼Bernoulli(pi)logit(pi)=αD[i]+βD[i]Uiinclude urban effectαj∼Normal(αˉ,σ)regularizing prior for districtαˉ∼Normal(0,1)district average priorσ∼Exponential(1)district std priorβj∼Normal(βˉ,τ)regularizing prior for urban effectβˉ∼Normal(0,1)urban average priorτ∼Exponential(1)urban std prior\begin{align*} C_i &\sim \text{Bernoulli}(p_i) \\ \text{logit}(p_i) &= \alpha_{D[i]} + \beta_{D[i]} U_i &\text{include urban effect}\\ \alpha_j &\sim \text{Normal}(\bar \alpha, \sigma) &\text{regularizing prior for district}\\ \bar \alpha &\sim \text{Normal}(0, 1) &\text{district average prior} \\ \sigma &\sim \text{Exponential}(1) &\text{district std prior} \\ \beta_j &\sim \text{Normal}(\bar \beta, \tau) &\text{regularizing prior for urban effect} \\ \bar \beta &\sim \text{Normal}(0, 1) &\text{urban average prior} \\ \tau &\sim \text{Exponential}(1) &\text{urban std prior} \end{align*}

Fit the district-urban model

We use the non-centered prior version here -- details about non-centered will be discussed later

Summarize the urban-district posterior

Compare posterior predictive for urban/rural form single model fit jointly

Posterior variances

The plots above indicate that urban areas have

  • higher overall rates of contraceptive use
  • The error bars higher variance in urban areas

The plot below re-confirms that variance in contraceptive use is indeed larger in urban areas; the posterior standard deviation parameter for urban areas $\tau$ is larger than the parameter $\sigma$ for rural areas

Urban and Rural Outcomes are positively correlated

A few observations

  • There's more contraceptive use in urban areas than in rural areas--a majority of points are to the left of the vertical line
  • There's a high amount of correlation (cc>0.7) between contraceptive use in rural and urban areas in each district.

We should be able to take advantage of this correlation information to make better estimates. MORE TO COME!

Summary: Multilevel Adventures

  • Clusters: distinct groups in the data
  • Features: aspects of the model (e.g. parameters) that vary by cluster
  • There is useful information transferred across features
    • We can use partial pooling to efficiently estimate features, even in absence of data

Authors

  • Ported to PyMC by Dustin Stansbury (2024)
  • Based on Statistical Rethinking (2023) lectures by Richard McElreath

:::{include} ../page_footer.md :::