-
Notifications
You must be signed in to change notification settings - Fork 15
[WIP] Add section to logistic regression on computing marginal risk differences #706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Claude
wants to merge
6
commits into
main
Choose a base branch
from
claude/add-logistic-regression-marginal-risk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f2bc4f
WIP: marginal risk differences (squashed)
claude 07e1d46
Merge remote-tracking branch 'origin/main' into claude/add-logistic-r…
claude 0c6ae9a
Address four nits on marginal-RD section
claude b18aec4
Merge branch 'main' into claude/add-logistic-regression-marginal-risk
d-morrison 95926fd
Merge remote-tracking branch 'origin/main' into claude/add-logistic-r…
claude a477ff8
Address review: teal color, RR collapsibility wording, notes slide
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| :::{#exm-collapsibility} | ||
|
|
||
| #### Collapsibility: numerical illustration | ||
|
|
||
| Consider a hypothetical scenario with two strata | ||
| ($Z = 0$: low-risk, $Z = 1$: high-risk). | ||
| We make two simplifying assumptions: | ||
|
|
||
| 1. **No confounding** ($A \perp Z$): | ||
| exposure is independent of the covariate, | ||
| so $\p(Z = z \mid A = a) = \p(Z = z)$ for all $a$ and $z$. | ||
| 2. **Equal stratum sizes** ($\p(Z = 0) = \p(Z = 1) = \tfrac{1}{2}$): | ||
| both strata have the same marginal probability. | ||
|
|
||
| Together, these imply | ||
| $\p(Z = z \mid A = a) = \tfrac{1}{2}$ for all $a$ and $z$, | ||
| so the simple (equal-weight) average of stratum-specific effects | ||
| equals both the observed marginal risk $\pi(a)$ and the causal marginal risk $\pi_a$: | ||
|
|
||
| ```{r} | ||
| #| label: collapsibility-example | ||
| #| code-fold: true | ||
|
|
||
| strata <- tibble::tibble( | ||
| stratum = c("Z = 0 (low risk)", "Z = 1 (high risk)"), | ||
| pi_0 = c(0.05, 0.30), | ||
| pi_1 = c(0.10, 0.50) | ||
| ) |> | ||
| dplyr::mutate( | ||
| RD = pi_1 - pi_0, | ||
| RR = pi_1 / pi_0, | ||
| OR = (pi_1 / (1 - pi_1)) / (pi_0 / (1 - pi_0)) | ||
| ) | ||
|
|
||
| pi1_marg <- mean(strata$pi_1) | ||
| pi0_marg <- mean(strata$pi_0) | ||
|
|
||
| tibble::tibble( | ||
| Measure = c("Risk difference", "Risk ratio", "Odds ratio"), | ||
| Marginal = c( | ||
| pi1_marg - pi0_marg, | ||
| pi1_marg / pi0_marg, | ||
| (pi1_marg / (1 - pi1_marg)) / (pi0_marg / (1 - pi0_marg)) | ||
| ), | ||
| `Avg. conditional` = c( | ||
| mean(strata$RD), mean(strata$RR), mean(strata$OR) | ||
| ), | ||
| `Marginal = avg. conditional?` = c("Yes", "No", "No") | ||
| ) |> | ||
| knitr::kable(digits = 3) | ||
| ``` | ||
|
|
||
| Even with no confounding, the marginal RR and marginal OR both differ from | ||
| the average of their conditional counterparts, | ||
| while the marginal RD equals the average conditional RD exactly. | ||
|
|
||
| Non-collapsibility is distinct from effect-measure modification. | ||
| Here the stratum-specific effects also vary across strata | ||
| (risk ratios $2.00$ vs. $1.67$, odds ratios $2.11$ vs. $2.33$), | ||
| but that variation is not what drives the discrepancy: | ||
| even if the conditional odds ratio were held *constant* across strata, | ||
| the marginal odds ratio would generally still differ from it | ||
| (and lie closer to the null), | ||
| whereas a constant conditional risk difference | ||
| always reproduces the marginal risk difference. | ||
|
|
||
| ::: | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
|
|
||
| ### Objections to odds ratios | ||
| ## Objections to odds ratios | ||
|
|
||
| {{< include _subfiles/logistic-regression/_sec_OR_objections.qmd >}} | ||
|
|
||
| ### Deriving risk ratios and risk differences from logistic regression models | ||
| ## Deriving risk ratios and risk differences from logistic regression models | ||
|
|
||
| {{< include _subfiles/logistic-regression/_sec-logistic-RR-RD.qmd >}} | ||
|
|
||
| ### Other link functions for Bernoulli outcomes | ||
| ## Other link functions for Bernoulli outcomes | ||
|
|
||
| {{< include _subfiles/logistic-regression/_sec-non-logistic-bernoulli-models.qmd >}} | ||
|
|
||
| ### Quasibinomial | ||
| ## Quasibinomial | ||
|
|
||
| See [Hua Zhou](https://hua-zhou.github.io/)'s [lecture notes](https://ucla-biostat-200c-2020spring.github.io/slides/04-binomial/binomial.html#:~:text=0.05%20%27.%27%200.1%20%27%20%27%201-,Quasi%2Dbinomial,-Another%20way%20to) |
51 changes: 51 additions & 0 deletions
51
_subfiles/logistic-regression/_sec-bootstrap-boot-package.qmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| The [`boot`](https://cran.r-project.org/package=boot) package provides a more streamlined interface for bootstrap inference. | ||
| Here's how to compute the same confidence interval using `boot::boot()`: | ||
|
|
||
| ```{r} | ||
| #| label: boot-package-example | ||
| #| code-fold: show | ||
| #| eval: false | ||
|
|
||
| library(boot) | ||
|
|
||
| statistic_fn <- function(data, indices) { | ||
| boot_data <- data[indices, ] | ||
|
|
||
| boot_fit <- glm( | ||
| chd69_binary ~ dibpat + age, | ||
| data = boot_data, | ||
| family = binomial(link = "logit") | ||
| ) | ||
|
|
||
| compute_marginal_rd( | ||
| model = boot_fit, | ||
| data = boot_data, | ||
| exposure_var = "dibpat", | ||
| exposed_level = "Type A", | ||
| unexposed_level = "Type B" | ||
| ) | ||
| } | ||
|
|
||
| set.seed(20260512) | ||
| boot_results <- boot( | ||
| data = wcgs_clean, | ||
| statistic = statistic_fn, | ||
| # Keep the rendered example fast; increase to 2000+ for final analyses. | ||
| R = 300 | ||
| ) | ||
|
|
||
| boot_results | ||
|
|
||
| boot.ci(boot_results, type = c("perc", "bca")) | ||
| ``` | ||
|
|
||
| The [`boot`](https://cran.r-project.org/package=boot) package provides several types of confidence intervals, | ||
| including the percentile method (`perc`) | ||
| and the bias-corrected and accelerated (BCa) method (`bca`), | ||
| which can provide better coverage in some situations. | ||
| The BCa method is more demanding than the percentile method: | ||
| its bias-correction and acceleration estimates are unstable | ||
| at the illustrative `R = 300` used here | ||
| (and can warn or fail outright), | ||
| so use a substantially larger `R` (at least 2000) | ||
| before relying on `bca` intervals. |
42 changes: 42 additions & 0 deletions
42
_subfiles/logistic-regression/_sec-bootstrap-inference.qmd
|
d-morrison marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| Adapted from | ||
| [@vittinghoff2e, Section 3.6, p. 62], | ||
| [@hastie2009esl2e, Section 7.11, p. 249], | ||
| and | ||
| [@james2021islr2e, Chapter 5, Section 5.2, p. 209]. | ||
|
|
||
| The bootstrap is a resampling method | ||
| that allows us to estimate the sampling distribution | ||
| of a statistic without making strong parametric assumptions. | ||
| For an introduction to the bootstrap, | ||
| see [Bootstrap Confidence Intervals](basic-statistical-methods.qmd#sec-bootstrap-ci). | ||
|
|
||
| #### Bootstrap algorithm | ||
|
|
||
| To construct a bootstrap confidence interval | ||
| for a marginal risk difference: | ||
|
|
||
| 1. For $b = 1, \ldots, B$ (e.g., $B = 1000$): | ||
| a. Draw a bootstrap sample of size $n$ with replacement from the original data | ||
| b. Fit the logistic regression model to the bootstrap sample | ||
| c. Compute the marginal risk difference from the fitted model | ||
| 2. The bootstrap distribution of the $B$ risk difference estimates | ||
| approximates the sampling distribution | ||
| 3. Construct a confidence interval using the percentile method | ||
| (e.g., the 2.5th and 97.5th percentiles for a 95% CI) | ||
|
|
||
| The bootstrap standard error is the standard deviation | ||
| of the bootstrap distribution. | ||
|
|
||
| {{< slidebreak >}} | ||
|
|
||
| :::{#exm-wcgs-marginal-rd} | ||
|
|
||
| #### Example: CHD risk and behavioral pattern | ||
|
|
||
| {{< include _subfiles/logistic-regression/_sec-wcgs-bootstrap-example.qmd >}} | ||
|
|
||
| ::: | ||
|
|
||
| #### Alternative: Using the boot package | ||
|
|
||
| {{< include _subfiles/logistic-regression/_sec-bootstrap-boot-package.qmd >}} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.