Skip to content

Commit 43f4d4e

Browse files
committed
further tightening of writing
Signed-off-by: Nathaniel <[email protected]>
1 parent 64fa8fd commit 43f4d4e

File tree

2 files changed

+64
-28
lines changed

2 files changed

+64
-28
lines changed

examples/case_studies/bayesian_sem_workflow.ipynb

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
"id": "72588976-efc3-4adc-bec2-bc5b6ac4b7e1",
2323
"metadata": {},
2424
"source": [
25-
"This case study builds on themes of {ref}`contemporary Bayesian workflow <bayesian_workflow>` and {ref}`Structural Equation Modelling <cfa_sem_notebook>`. Both are broad topics, already somewhat covered within the PyMC examples site, but here we wish to draw out some of the key points when applying the Bayesian workflow to Structural equation models. The iterative and expansionary strategies of model development for SEMs provide an independent motivation for the recommendations of {cite:p}`gelman2020bayesian` stemming from the SEM literature broadly but {cite:p}`kline2023principles` in particular. \n",
25+
"This case study extends the themes of {ref}`contemporary Bayesian workflow <bayesian_workflow>` and {ref}`Structural Equation Modelling <cfa_sem_notebook>`.\n",
26+
"While both topics are well represented in the PyMC examples library, our goal here is to show how the principles of the Bayesian workflow can be applied concretely to structural equation models (SEMs). The iterative and expansionary strategies of model development for SEMs provide an independent motivation for the recommendations of {cite:p}`gelman2020bayesian` stemming from the SEM literature broadly but {cite:p}`kline2023principles` in particular. \n",
2627
"\n",
27-
"A secondary motivation is to put SEM modelling with PyMC on firmer ground by detailing different sampling strategies for these complex models; we will cover both conditional and marginal formulations of a SEM model, allowing for the addition of mean-structures and hierarchical effects. These additional components highlight the expressive capacity of this modelling paradigm. \n",
28+
"A further goal is to strengthen the foundation for SEM modeling in PyMC. We demonstrate how to use different sampling strategies, both conditional and marginal formulations, to accommodate mean structures and hierarchical effects. These extensions showcase the flexibility and expressive power of Bayesian SEMs.\n",
2829
"\n",
2930
"### The Bayesian Workflow\n",
3031
"Recall the stages of the Bayesian workflow.\n",
@@ -42,7 +43,7 @@
4243
"- **Decision analysis**: Use the model for predictions or decisions\n",
4344
":::\n",
4445
"\n",
45-
"The Structural equation modelling workflow is similar. \n",
46+
"The structure of the SEM workflow mirrors the Bayesian workflow closely. Each step—from specifying measurement models to refining residual covariances—reflects the same cycle of hypothesis formulation, model testing, and iterative improvement.\n",
4647
"\n",
4748
"### The SEM Workflow\n",
4849
"- __Confirm the Factor Structure__ (CFA):\n",
@@ -349,7 +350,7 @@
349350
"id": "b2a81043",
350351
"metadata": {},
351352
"source": [
352-
"Conveniently, the process of the Bayesian workflow itself involves the constructive thought strategies. At each juncture in model development we must ask ourselves: do i believe this? What assumptions have I made? Is there any visual evidence that my model is well specified? What can i do to improve the model specification? You will see that the end result of the Bayesian workflow is a robust, defensible array of findings. These findings have been derived with care and craft. The process, as the data suggests, leads to sense of satisfaction with a job well done! "
353+
"Interestingly, the Bayesian workflow embodies the same constructive strategies it studies. At each stage, we ask: _Do I believe this model? What assumptions am I making? Is the evidence consistent with my expectations?_ This reflective process , mirroring the “evaluating beliefs and assumptions” mindset, builds models that are not only statistically robust but conceptually grounded. By the end, we arrive at a set of defensible findings which, fittingly, support a genuine sense of satisfaction with a job well done."
353354
]
354355
},
355356
{
@@ -359,6 +360,8 @@
359360
"source": [
360361
"## Mathematical Interlude\n",
361362
"\n",
363+
"Before we turn to implementation, let’s formalize the model mathematically.\n",
364+
"\n",
362365
"In our set up of a Structural Equation Model we have observed variables $y \\in R^{p}$, here (p=12) and $\\eta \\in R^{m}$ latent factors (m=4). The basic structural equation model (SEM) consists of two parts - the measurement model and the structural regressions. \n",
363366
"\n",
364367
"_The Measurement Model_ is the factor-structure we seek to _confirm_ in our analysis. It is called a measurement model because we view the observable metrics as indicators of the thing we actually want to measure. The observable metrics are grouped under a unifying \"factor\" or construct. The idea that each of the indicators are imprecise gauges of the latent factor. The hope is that collectively they provide a better gauge of this hard to measure quantity e.g. satisfaction and well-being. This can be thought of as a data-reduction technique, where we reduce the complex multivariate data set to a smaller collection of inferred features. However, in most SEM applications the factors themselves are of independent interest, not merely a modelling convenience.\n",
@@ -384,7 +387,10 @@
384387
"\n",
385388
"In the structural model we specify how we believe the latent constructs relate to one another. The term $(I - B)^{-1}$ is sometimes called the total effects matrix because it can be expanded as $I + B + B^{2} + B^{3}...$ summing all possible chains of paths in the system. As we'll see the structural and mediating paths between these latent constructs can be additive. This observation allows us to very elegantly derive total, indirect and direct effects within the system. \n",
386389
"\n",
390+
"We can express the SEM in either a conditional or marginal formulation. The conditional form explicitly samples the latent variables, while the marginal form integrates them out of the likelihood.\n",
391+
"\n",
387392
"### Conditional Formulation\n",
393+
"This formulation treats the latent variables as parameters to be sampled directly. This is conceptually straightforward but often computationally demanding for Bayesian samplers.\n",
388394
"\n",
389395
"$$\n",
390396
"\\zeta_i \\sim \\mathcal N(0, \\Psi_{\\zeta}). \n",
@@ -403,9 +409,10 @@
403409
"\n",
404410
"$$\n",
405411
"\n",
406-
"which is just to highlight that the conditional formulation samples the latent variables explicitly, which can be quite demanding for a sampler in the Bayesian setting. \n",
412+
"which highlights that the conditional formulation samples the latent variables explicitly. \n",
407413
"\n",
408414
"### Marginal Formulation\n",
415+
"Here the focus is on deriving the covariance matrix. \n",
409416
"\n",
410417
"$$\\Sigma_{\\mathcal{y}} = \\Psi + \\Lambda(I - B)^{-1}\\Psi_{\\zeta}(I - B)^{T}\\Lambda^{T} $$\n",
411418
"\n",
@@ -425,7 +432,7 @@
425432
"source": [
426433
"## Setting up Utility Functions\n",
427434
"\n",
428-
"For this exercise we will lean on a range of utility functions to build and compare the expansionary sequence. This functions include repeated steps that will be required for any SEM model. \n",
435+
"For this exercise we will lean on a range of utility functions to build and compare the expansionary sequence. These functions include repeated steps that will be required for any SEM model. These functions modularize the model-building process and make it easier to compare successive model expansions.\n",
429436
"\n",
430437
"The most important cases are functions like `make_lambda` to sample and fix the scale of the covariates that contribute to each latent factor. Similarly, we have the `make_B` which samples the parameter values of the path coefficients between the latent constructs, while arranging them in a matrix that can be passed through matrix multiplication routines. Additionally, we have a `make_Psi` function which samples parameter values for particular covariances that gets deployed to encode aspects of the variance in our system not captured by the covariances among the latent factors. These three helper functions determine the structure of the SEM model and variants of each can be used to construct any SEM structure.\n",
431438
"\n",
@@ -641,11 +648,11 @@
641648
"source": [
642649
"## Confirming Factor Structure\n",
643650
"\n",
644-
"First we'll highlight the broad structure of a confirmatory factor model and the types of relations the model encodes. The red dotted arrows here denote covariance relationships among the latent factors. The black arrows denote the effect of the latent constructs on the observable indicator metrics. We've highlighted with red [1] that the first \"factor loading\" is always fixed to 1, so to (a) define the scale of the factor and (b) allow identification of the other factor loadings within that factor. \n",
651+
"In this section, we translate the theoretical structure of a confirmatory factor model (CFA) into a concrete implementation. The diagram below distinguishes between covariance relationships among latent factors (red dotted arrows) and factor loadings linking latent constructs to observed indicators (black arrows). We've highlighted with red [1] that the first \"factor loading\" is always fixed to 1, so to (a) define the scale of the factor and (b) allow identification of the other factor loadings within that factor. \n",
645652
"\n",
646653
"![](cfa_excalidraw.png)\n",
647654
"\n",
648-
"In the model below we sample draws from the latent factors `eta` and relate them to the observables by the matrix computation `pt.dot(eta, Lambda.T)`. This computation reults in a \"psuedo-observation\" matrix which we then feed through our likelihood to calibrate the latent structures against the observed dats. This is the general pattern we'll see in all models below. The covariances (i.e. red arrows) among the latent factors is determined with `chol`."
655+
"In the model below we sample draws from the latent factors `eta` and relate them to the observables by the matrix computation `pt.dot(eta, Lambda.T)`. This computation results in a \"psuedo-observation\" matrix which we then feed through our likelihood to calibrate the latent structures against the observed dats. This is the general pattern we'll see in all models below. The covariances (i.e. red arrows) among the latent factors is determined with `chol`."
649656
]
650657
},
651658
{
@@ -1560,7 +1567,11 @@
15601567
"id": "330d6450",
15611568
"metadata": {},
15621569
"source": [
1563-
"Now for each latent variable (satisfaction, well being, constructive, dysfunctional), we will plot a forest/ridge plot of the posterior distributions of their factor scores `eta` as drawn. Each panel will have a vertical reference line at 0 (since latent scores are typically centered/scaled).These panels visualize the distribution of estimated latent scores across individuals, separated by latent factor. Then we will summarizes posterior estimates of model parameters (factor loadings, regression coefficients, variances, etc.), providing a quick check against identification constraints (like fixed loadings) and effect directions. Finally we will plot the upper-triangle of the residual correlation matrix with a blue–white–red colormap (−1 to +1). This visualizes residual correlations among observed indicators after the SEM structure is accounted for — helping detect model misfit or unexplained associations."
1570+
"Now for each latent variable (satisfaction, well being, constructive, dysfunctional), we will plot a forest/ridge plot of the posterior distributions of their factor scores `eta` as drawn. Each panel will have a vertical reference line at 0 (since latent scores are typically centered/scaled).These panels visualize the distribution of estimated latent scores across individuals. \n",
1571+
"\n",
1572+
"Then we summarizes posterior estimates of model parameters (e.g factor loadings, regression coefficients, variances, etc.), providing a quick check against identification constraints (like fixed loadings) and effect directions. \n",
1573+
"\n",
1574+
"Finally we will plot the upper-triangle of the residual correlation matrix with a blue–white–red colormap (−1 to +1). This visualizes residual correlations among observed indicators after the SEM structure is accounted for — helping detect model misfit or unexplained associations."
15641575
]
15651576
},
15661577
{
@@ -1737,7 +1748,9 @@
17371748
"source": [
17381749
"## Structuring the Latent Relations\n",
17391750
"\n",
1740-
"The next expansionary move in SEM modelling is to consider the relations between the latent constructs. These are generally intended to have a causal interpretation. The constructs are hard to measure precisely but collectively (as a function of multiple indicator variables) we argue they are exhaustively characterised. This is key argument implict in SEM design. Bollen argues as follows:\n",
1751+
"The next expansionary move in SEM modelling is to consider the relations between the latent constructs. These are generally intended to have a causal interpretation. The constructs are hard to measure precisely but collectively (as a function of multiple indicator variables) we argue they are exhaustively characterised. Although each construct is imprecisely measured, together their indicators are assumed to capture the construct’s essential variation. This assumption underpins the causal interpretation of structural paths. \n",
1752+
"\n",
1753+
"Bollen argues as follows:\n",
17411754
"\n",
17421755
"> As I have just explained, we cannot isolate a dependent variable from all influences but a single explanatory variable, so it is impossible to make definitive statements about causes. We replace perfect isolation with pseudo-isolation by assuming that the disturbance (i.e., the composite of all omitted determinants) is uncorrelated with the exogenous variables of an equation. - Bollen in _Structural Equations with Latent Variables_ pg45\n",
17431756
"\n",
@@ -1753,7 +1766,7 @@
17531766
"source": [
17541767
"The isolation or conditional independence of interest is encoded in the model with the sampling of the `gamma` variable. These are drawn from a process that is structuraly divorced from the influence of the exogenous variables. For instance if we have $\\gamma_{cts} \\perp\\!\\!\\!\\perp \\eta_{dtp}$ then the $\\beta_{cts -> dpt}$ coefficient is an unbiased estimate of the direct effect of `CTS` on `DTP` because the remaining variation in $\\eta_{dtp}$ is noise by construction. \n",
17551768
"\n",
1756-
"It is a substantive imposition of theory as you impose the various arrows you want to add to your system. You are making claims of causal influence. Arrows should be added in line with plausible theory, while parameter identification is well supported. In our case we have structured the DAG following the discussion in {cite:p}`vehkalahti2019multivariate` which will allow us to unpick the direct and indirect effects below. In Lavaan syntax the model we want to specify is: \n",
1769+
"Each additional arrow in the structural model thus encodes a substantive theoretical claim about causal influence. You are making claims of causal influence. Arrows should be added in line with plausible theory, while parameter identification is well supported. In our case we have structured the DAG following the discussion in {cite:p}`vehkalahti2019multivariate` which will allow us to unpick the direct and indirect effects below. In Lavaan syntax the model we want to specify is: \n",
17571770
"\n",
17581771
"```\n",
17591772
"# measurement part\n",
@@ -2248,7 +2261,7 @@
22482261
"source": [
22492262
"#### A Sampled B Matrix\n",
22502263
"\n",
2251-
"Now we can sample the B matrix and observe is structure. \n",
2264+
"Now we can sample the B matrix and observe is structure. The sampled $B$ matrix compactly represents the system of regression relationships among latent constructs \n",
22522265
"\n",
22532266
"It's best to see the matrix as encoding target variables in the columns and predictor variables inn the rows. Here we have the set up our matrix so that `satisfaction` is predicted by three variables, but not itself. The values in the first column are sampled coefficient values in the regression: \n",
22542267
"\n",
@@ -2679,7 +2692,7 @@
26792692
"id": "c69afd2c",
26802693
"metadata": {},
26812694
"source": [
2682-
"This compact representation of three separate regressions equations is used to sort out the mutual influences, interactions and distortion our habits of thought have on our job satisfaction. It's true that the relationships between these latent constructs need not be linear and simplistic. There may be messy non-linear relationships between satisfaction and well being, but we are a deliberately abstracting some of that away in favour of a tractable, quantifiable measure of linear effect. This is often a simplification, but parsimony is also a goal of the modelling process. "
2695+
"This compact representation of three separate regressions equations is used to sort out the mutual influences, interactions and distortions our habits of thought have on our job satisfaction. It's true that the relationships between these latent constructs need not be linear and simplistic. There may be messy non-linear relationships between satisfaction and well being, but we are a deliberately abstracting some of that away in favour of a tractable, quantifiable measure of linear effect. This is often a simplification, but parsimony is also a goal of the modelling process. "
26832696
]
26842697
},
26852698
{
@@ -2689,7 +2702,7 @@
26892702
"source": [
26902703
"### Model Diagnostics and Assessment\n",
26912704
"\n",
2692-
"Let's examine our model sense checks. The posterior predictive distributions retain a healthy appearence, the prior allowed for a wide-realisation of values and the posterior has a shrunk the range considerable closer to the standardised 0-mean data. "
2705+
"Let's examine our model sense checks. The posterior predictive distributions retain a healthy appearence. Our prior allowed for a wide-realisation of values, and yet the posterior has a shrunk the range considerably closer to the standardised 0-mean data. "
26932706
]
26942707
},
26952708
{
@@ -6959,6 +6972,12 @@
69596972
"In the end, the value of craft in statistical modeling lies not in improving benchmark metrics, but in the depth of understanding we cultivate through careful communication and justification. The Bayesian workflow reminds us that modeling is not the automation of insight but its deliberate construction. Our workflow is process of listening, revising, and re-articulating until the model speaks clearly. Like any craft, its worth is measured not by throughput but by fidelity: how honestly our structure reflects the world it seeks to describe. Each diagnostic, each posterior check, each refinement of a latent path is a form of attention — a small act of resistance against the flattening logic of metrics and checklists. These are the constructive thought processes that drive job-satisfaction. __To practice modeling as craft is to reclaim pride in knowing what our models say, what they do not say, and what they imply.__ To find, in that attention, the quiet satisfaction of meaningful work.\n"
69606973
]
69616974
},
6975+
{
6976+
"cell_type": "markdown",
6977+
"id": "2d12b3af",
6978+
"metadata": {},
6979+
"source": []
6980+
},
69626981
{
69636982
"attachments": {},
69646983
"cell_type": "markdown",

0 commit comments

Comments
 (0)