Statistical modeling

From Tibsfox/gsd-skill-creator · View interactive version

Statistical modeling is an AI agent skill from the GitHub repository Tibsfox/gsd-skill-creator. Regression analysis, ANOVA, generalized linear models, Bayesian methods, and model selection. Covers the full modeling workflow from problem formulation through diagnostics -- linear regression, logistic regression, Poisson regression, mixed-effects models, prior specification, posterior inference,

How to use this skill

Skill file

Statistical Modeling

Statistical modeling is the practice of fitting mathematical structures to data in order to quantify relationships, test hypotheses, and make predictions. Unlike machine learning, which optimizes prediction, statistical modeling privileges interpretability and inference -- understanding why variables relate, not just that they do. Leo Breiman's "two cultures" paper (2001) crystallized this distinction. This skill covers the inferential tradition while acknowledging where the two cultures overlap.

Agent affinity: tukey (EDA and diagnostics), fisher (experimental design and ANOVA), breiman (model comparison)

Concept IDs: data-hypothesis-testing, data-confidence-intervals, data-correlation, data-normal-distribution

The Modeling Workflow

| Stage | Goal | Key operations |

|---|---|---|

| 1. Formulation | Define the question as a model | Specify response variable, predictors, functional form |

| 2. Exploration | Understand data structure | Scatterplots, correlation matrices, distribution checks |

| 3. Fitting | Estimate parameters | OLS, MLE, MCMC, IRLS depending on model class |

| 4. Diagnostics | Check assumptions | Residual plots, Q-Q plots, leverage, VIF |

| 5. Inference | Draw conclusions | Confidence intervals, hypothesis tests, effect sizes |

| 6. Selection | Compare models | AIC, BIC, cross-validation, likelihood ratio tests |

| 7. Communication | Report results | Effect estimates with uncertainty, not just p-values |

Linear Regression

The Model

y = beta_0 + beta_1 * x_1 + beta_2 * x_2 + ... + beta_p * x_p + epsilon

where epsilon ~ N(0, sigma^2) independently. The betas are estimated by ordinary least squares (OLS), minimizing the sum of squared residuals.

Assumptions (LINE)

| Assumption | Check | Violation consequence |

|---|---|---|

| Linearity | Residual vs. fitted plot -- no pattern | Biased estimates, meaningless coefficients |

| Independence | Study design, Durbin-Watson test | Underestimated standard errors, inflated significance |

| Normality of residuals | Q-Q plot, Shapiro-Wilk test | Unreliable confidence intervals and p-values (less critical for large n by CLT) |

| Equal variance (homoscedasticity) | Scale-location plot, Breusch-Pagan test | Inefficient estimates, unreliable standard errors |

Interpretation

Multicollinearity

When predictors are highly correlated, coefficient estimates become unstable. Variance Inflation Factor (VIF) quantifies this: VIF > 5-10 indicates problematic collinearity. Remedies: drop a predictor, combine predictors via PCA, or use regularization (ridge regression).

Logistic Regression

The Model

For binary outcome y in {0, 1}:

log(p / (1 - p)) = beta_0 + beta_1 * x_1 + ... + beta_p * x_p

where p = P(y = 1 | x). The left side is the log-odds (logit). Parameters are estimated by maximum likelihood.

Interpretation

Assumptions

Generalized Linear Models (GLMs)

Logistic regression is one instance of the GLM framework. The general structure:

| Component | Role |

|---|---|

| Random component | Distribution of y (Normal, Binomial, Poisson, Gamma, ...) |

| Systematic component | Linear predictor eta = X * beta |

| Link function | g(mu) = eta, connecting the mean to the linear predictor |

Common GLMs

| Response type | Distribution | Link | Model name |

|---|---|---|---|

| Continuous | Normal | Identity | Linear regression |

| Binary | Binomial | Logit | Logistic regression |

| Count | Poisson | Log | Poisson regression |

| Count (overdispersed) | Negative binomial | Log | Negative binomial regression |

| Positive continuous | Gamma | Log or inverse | Gamma regression |

| Proportion (not 0/1) | Beta | Logit | Beta regression |

Poisson Regression

For count data: log(mu) = beta_0 + beta_1 * x_1 + ... Assumes the mean equals the variance (equidispersion). When variance > mean (overdispersion), use negative binomial or quasi-Poisson. Always check for overdispersion.

Analysis of Variance (ANOVA)

Purpose

ANOVA tests whether group means differ. It is a special case of linear regression where all predictors are categorical.

One-Way ANOVA

Two-Way ANOVA

Adds a second factor and their interaction. The interaction term tests whether the effect of one factor depends on the level of the other. Always plot the interaction (mean response by factor A, colored by factor B) before interpreting the F-test.

ANOVA as Regression

One-way ANOVA with k groups is equivalent to linear regression with k-1 dummy variables. The F-test in ANOVA is the same as the overall F-test in regression. Understanding this equivalence clarifies that ANOVA is not a separate method -- it is regression with categorical predictors.

Bayesian Methods

The Framework

Bayesian inference updates prior beliefs with data to produce posterior beliefs:

posterior is proportional to likelihood times prior

P(theta | data) proportional to P(data | theta) * P(theta)

Prior Specification

| Prior type | When to use | Example |

|---|---|---|

| Informative | Strong domain knowledge | "The effect is between 0.5 and 1.5 based on prior studies" |

| Weakly informative | Some domain knowledge, want to regularize | Normal(0, 10) for regression coefficients -- centered at zero, wide but not flat |

| Non-informative | Want the data to dominate | Uniform or Jeffreys prior. Rarely truly "non-informative" -- all priors carry assumptions |

Posterior Inference

Practical Bayesian Workflow

  1. Specify the model (likelihood + priors).
  2. Fit using MCMC (Stan, PyMC, JAGS) or variational inference.
  3. Check convergence: R-hat < 1.01, effective sample size > 400, no divergent transitions.
  4. Posterior predictive checks.
  5. Report posterior summaries with credible intervals.

Model Selection

Information Criteria

| Criterion | Formula | Interpretation |

|---|---|---|

| AIC | -2 * log-likelihood + 2k | Estimates out-of-sample prediction error. Lower is better. |

| BIC | -2 * log-likelihood + k * log(n) | Penalizes complexity more heavily than AIC. Consistent (selects true model as n -> infinity). |

| WAIC | Bayesian analog of AIC | Uses the full posterior, not point estimates. Preferred for Bayesian models. |

AIC favors prediction accuracy; BIC favors parsimony. When they disagree, consider the goal: prediction -> AIC, explanation -> BIC.

Cross-Validation

Nested vs. Non-Nested Models

Diagnostics Checklist

| Check | Tool | What to look for |

|---|---|---|

| Linearity | Residual vs. fitted plot | No systematic pattern |

| Normality | Q-Q plot | Points on the diagonal line |

| Homoscedasticity | Scale-location plot | Horizontal band, no funnel |

| Independence | Durbin-Watson, ACF plot | DW near 2, no significant autocorrelation |

| Influential points | Cook's distance | No points with Cook's D > 4/n |

| Multicollinearity | VIF | All VIF < 5 |

| Overdispersion (GLM) | Residual deviance / df | Ratio near 1 |

Common Mistakes

| Mistake | Why it fails | Fix |

|---|---|---|

| Interpreting p > 0.05 as "no effect" | Absence of evidence is not evidence of absence | Report effect size with confidence interval |

| Stepwise variable selection | Inflates Type I error, unstable results | Use AIC/BIC or cross-validation |

| Ignoring multicollinearity | Unstable coefficients, misleading significance | Check VIF, consider combining or dropping predictors |

| Extrapolating beyond data range | Model has no support outside observed x-range | State the range of validity explicitly |

| Confusing correlation with causation | Regression coefficients are associations unless the design is experimental | Use causal language only with randomized experiments or strong causal inference methods |

| Reporting R-squared without diagnostics | High R-squared with violated assumptions is meaningless | Always check assumptions before interpreting fit statistics |

Cross-References

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.