Module 9: Correlation & Simple Linear Regression: Modeling Scientific Relationships
Module 9: Correlation & Simple Linear Regression: Modeling Scientific Relationships
Bivariate association using Pearson (r) and Spearman (rho) correlation, ordinary least squares (OLS) linear regression (Y = beta0 + beta1*X), R-squared goodness-of-fit, p-values, and 4-way residual diagnostic plots.
9. Correlation & Simple Linear Regression: Modeling Scientific Relationships
Scientists frequently examine how two continuous environmental measurements relate. While Correlation evaluates the strength and direction of association between two variables symmetrically, Linear Regression models the functional dependence of a response variable ($Y$) on one or more predictor variables ($X$).
Pearson ($r$) vs Spearman ($ ho$) Correlation:
- Pearson Correlation ($r$): Measures the strength of a strictly linear association between two normally distributed continuous variables. Sensitive to bivariate outliers.
- Spearman Rank Correlation ($ ho$): Non-parametric correlation based on ranked data values. Evaluates monotonic relationships (whether $Y$ increases as $X$ increases, even if non-linear) and tolerates moderate outliers.
Ordinary Least Squares (OLS) Linear Regression
The mathematical model for simple linear regression is: $$Y_i = \beta_0 + \beta_1 X_i + \epsilon_i \quad ext{where } \epsilon_i \sim \mathcal{N}(0, \sigma^2)$$ Where $\beta_0$ is the intercept, $\beta_1$ is the slope coefficient (rate of change in $Y$ per unit change in $X$), and $\epsilon_i$ represents unobserved random error.
The 4 Critical Regression Diagnostic Plots
In R, executing plot(model) produces four mandatory diagnostic plots:
- Residuals vs. Fitted: Evaluates linearity. A horizontal line with points scattered symmetrically around 0 indicates linearity; curved patterns indicate non-linear dynamics.
- Normal Q-Q: Evaluates normality of residuals. Residuals should align along the straight line.
- Scale-Location (Spread-Location): Evaluates homoscedasticity. Points should form an even horizontal band with constant spread.
- Residuals vs. Leverage: Identifies influential observations (Cook's distance $> 0.5$ or $> 1.0$) that exert disproportionate leverage on the regression slope.
Hands-On R Script: Modeling Water Temperature vs. Dissolved Oxygen
library(tidyverse)
# Physical chemistry dictates cold water holds more dissolved oxygen (Henry's Law)
set.seed(42)
temp_celsius <- runif(45, min = 8, max = 28)
# DO decreases roughly 0.25 mg/L per degree Celsius plus natural variation
dissolved_oxygen <- 13.8 - (0.28 * temp_celsius) + rnorm(45, mean = 0, sd = 0.5)
wq_data <- tibble(temp = temp_celsius, do = dissolved_oxygen)
# 1. Pearson Correlation Analysis
cor_test <- cor.test(wq_data$temp, wq_data$do, method = "pearson")
print(cor_test)
# Result: r = -0.96, p < 2.2e-16 (Extremely strong negative association)
# 2. Fit Ordinary Least Squares Linear Model
lm_model <- lm(do ~ temp, data = wq_data)
summary(lm_model)
# 3. Model Interpretation:
# - Intercept (beta0): ~13.8 mg/L (theoretical DO at 0 deg C)
# - Slope (beta1): ~ -0.28 (For every 1 deg C rise in water temp, DO drops by 0.28 mg/L)
# - Multiple R-squared: ~0.92 (92% of variation in DO is explained by temperature)
# 4. Display 4-Way Diagnostic Panels
par(mfrow = c(2, 2))
plot(lm_model)
par(mfrow = c(1, 1))Common Pitfalls & Troubleshooting Checklist
- Java Heap Exhaustion (GATK / Picard): If processes terminate with
java.lang.OutOfMemoryError, set your environment variable explicitly:export _JAVA_OPTIONS="-Xmx16g -Djava.io.tmpdir=/tmp". - Index Timestamp Mismatch: If tools complain that a BAM index is missing or out of date, regenerate with
samtools index -@ 4 sample.bam. - Conda Channel Conflicts: Avoid ABI incompatibilities between bioconda and conda-forge by setting strict priority:
conda config --set channel_priority strict. - Chromosome Naming Contamination: Always verify whether reference FASTA headers use
chr1or1; divergent naming causes silent alignment drops.