mendelian-randomizationlisted
Install: claude install-skill choxos/BiostatAgent
# Mendelian Randomization in R
## Overview
Mendelian randomization (MR) methods for causal inference using genetic variants as instrumental variables. Covers instrument selection, two-sample MR, sensitivity analyses, pleiotropy assessment, multivariable MR, and advanced methods for robust causal inference.
## Instrument Selection
### Using TwoSampleMR
```r
library(TwoSampleMR)
# Extract instruments from GWAS database
# IEU Open GWAS Project
exposure_dat <- extract_instruments(
outcomes = "ieu-a-2", # GWAS ID for exposure
p1 = 5e-8, # Genome-wide significance
clump = TRUE, # LD clumping
r2 = 0.001, # LD threshold
kb = 10000 # Clumping window (kb)
)
# View extracted SNPs
head(exposure_dat)
# Manual instrument selection from summary statistics
exposure_dat <- read_exposure_data(
filename = "exposure_gwas.txt",
sep = "\t",
snp_col = "SNP",
beta_col = "BETA",
se_col = "SE",
effect_allele_col = "A1",
other_allele_col = "A2",
pval_col = "P",
eaf_col = "EAF"
) |>
filter(pval.exposure < 5e-8)
# Clump instruments
exposure_dat <- clump_data(exposure_dat, clump_r2 = 0.001)
```
### F-statistic Calculation
```r
# F-statistic for instrument strength
# Rule of thumb: F > 10 indicates strong instruments
calculate_f_stat <- function(beta, se, n) {
r2 <- (beta^2) / (beta^2 + se^2 * n) # Approximate R²
k <- 1 # Number of instruments (per SNP)
f_stat <- (r2 * (n - k - 1)) / ((1 - r2)