AI summary by TwinMind
Design Matrix Examples in R, Clearly Explained!!!
StatQuest with Josh Starmer
8m 19s
Introduction and Context
- StatQuest tutorial on implementing General Linear Models in R
- Brought by UNC Chapel Hill Genetics Department
- Complements previous StatQuest on design matrices (Part 3)
- Focus: translating theoretical examples into R code
Example 1: Control vs Mutant Mice Analysis
- Research Question: Statistical difference between control and mutant mice sizes
- Data: Mouse weights and size measurements
- Model Equation: y = control intercept + mutant offset + slope
- Fits two separate lines (control mice, mutant mice)
R Implementation Steps
- Create labels for control mice, then mutant mice
- Enter weight data for control mice, then mutant mice
- Enter size measurements for control mice, then mutant mice
- Construct design matrix using
model.matrix()- Formula:
~ type + weight(intercept assumed by default) - Optional step but recommended for verification
- Formula:
Design Matrix Structure
- Column 1: Multiplied by control intercept (on for all measurements)
- Column 2: Multiplied by mutant offset (on only for mutant values)
- Column 3: Multiplied by slope (weight parameter)
Linear Model Results
- Function:
lm()performs least squares fit and statistics - Outputs:
- Residuals calculation
- Multiple R-squared (for complex equations)
- Adjusted R-squared (adjusted for number of parameters)
- Multiple p-values for different comparisons
P-value Interpretations
- Overall model p-value: Compares fancy equation vs simplest (y = overall mean)
- Weight parameter p-value: Importance of weight term in equation
- Compares full model vs model without slope
- Equivalent to comparing fancy equation vs normal t-test
- Mouse type p-value: Importance of mouse type information
- Compares full model vs simple linear regression without type
Example 2: Lab Batch Effect Control
- Research Question: Compare experiments from two different labs
- Challenge: Batch effect requiring statistical control
R Implementation
- Create labels for Lab A data, then Lab B data
- Create labels for control/mutant data within each lab
- Enter all expression values
- Optional: examine design matrix structure
Design Matrix Structure
- Column 1: Lab A control mean (on for every measurement)
- Column 2: Lab B offset (on only for Lab B data)
- Column 3: Difference between mutant and control means
Critical P-value Selection
- Bottom right p-value: NOT what we need
- Compares fancy equation vs y = overall mean
- Too simple comparison, doesn't test control vs mutant difference
- Correct p-value: Tests removal of mouse type term from equation
- Located elsewhere in output summary
- Tests whether fancy equation minus type term still fits well
Key Learning Points
- Bottom right p-value isn't always the desired test
- Must identify which comparison matches research question
- Linear models function performs multiple simultaneous tests
- Important to understand which specific test addresses your hypothesis
