BIOL 235 Biostatistics Study Guide Chapters 11-12
Introduction to ANOVA in R (Lecture 28)
Core Concepts of ANOVA:
ANOVA stands for Analysis of Variance.
It is used to compare the means of three or more independent groups simultaneously to avoid inflating the Type I error rate that occurs with multiple t-tests.
For example, with 10 groups, there would be 45 pairwise t-tests; ANOVA collapses this into a single global test.
Hypotheses for ANOVA:
Null Hypothesis (): (all population means are equal).
Alternative Hypothesis (): At least one differs from at least one other .
Key R Functions for Fitting Models:
fit <- aov(y ~ group, data = df): Fits a basic one-way ANOVA model.summary(fit): Prints the ANOVA table containing degrees of freedom (df), Sum of Squares (SS), Mean Squares (MS), F-statistic (F value), and p-value ().
Data Preparation (Stacking):
The
aov()function requires data in "long" format (one column for numeric values, one for group labels).Wide format data (individual columns per group) must be converted using the
stack()function.Syntax example:
stacked <- stack(list(groupA = dfgroupB, groupC = dfvalues: Contains all numeric observations.stackedne, wa=cornnw, ba=cornco))fit <- aov(values ~ ind, data = stacked)summary(fit)Note: The left side of the
~is the numeric response; the right side is the categorical grouping variable.
MS(between), MS(within), and the F Statistic (Lecture 29)
Reading the ANOVA Table Output:
Df (Degrees of Freedom):
Treatment (Between groups):
Residuals (Within groups):
Total: (where is total sample size and is number of groups).
Sum Sq (Sum of Squares - SS):
Sum Squares Between:
Sum Squares Within:
.
Mean Sq (Mean Square - MS): Calculated as .
F value: The F-statistic is the ratio of Signal to Noise.
Pr(>F): The p-value. Reject if .
Signal-to-Noise Framework:
MS(between): Measures the spread of group means from the grand mean (The SIGNAL). how far spread out are the means
MS(within): Measures the spread of individuals within groups, also known as pooled variance (The NOISE). Measures how far apart individual data points are from each other.
F-statistic Interpetation:
: No evidence groups differ significantly
: Strong evidence at least one group mean differs significantly
Checking Assumptions and Two-Way ANOVA (Lecture 30)
Assumptions of ANOVA:
Independent Random Samples: Determined by study design; cannot be tested statistically.
Normality of Residuals: Checked using
plot(fit)to view the Normal Q-Q plot. Points should follow the diagonal line.Equal Variances (Homoscedasticity): Checked using the Residuals vs Fitted plot. Look for an even spread around zero with no "fan" or "cone" shape.
Two-Way ANOVA Syntax:
aov(y ~ A + B, data = df): Main effects only (no interaction term).aov(y ~ A * B, data = df): Includes main effects AND the interaction term ().
Workflow for Two-Way ANOVA:
Step 1: Fit the model with the interaction term (
*).Step 2: Check if the interaction is significant. If YES, stop and report it.
Step 3: If the interaction is NOT significant, re-fit the model using only main effects (
+) and report those results.
Interaction Plots:
Parallel Lines: Indicate no interaction between variables.
Crossing/Diverging Lines: Indicate a significant interaction is present.
Linear Regression and Correlation in R (Lecture 31)
Usage Criteria: Used when both the predictor () and the response () are numeric variables.
Core Functions:
plot(x, y): Generates a scatterplot. Essential first step.cor(x, y): Returns the correlation coefficient ().cor.test(x, y): Full correlation test including , t-statistic, p-value, and 95% Confidence Interval for . This is the preferred function for formal testing.fit <- lm(y ~ x, data = df): Fits the least-squares regression line.summary(fit): Provides coefficients (), Standard Error, t-values, p-values, , and .abline(fit): Adds the regression line to the scatterplot.influence.measures(fit): Flags points that shift the model drastically.predict(fit, newdata=data.frame(x=val)): Evaluates the model at a specific value.
Mathematical Components of the Regression Line:
Equation:
Slope (): . Represents the average change in for a 1-unit increase in .
Intercept (): .
The line always passes through the point .
Residual Standard Error (): Typical vertical distance from points to the line ().
Coefficient of Determination (): The fraction of variance in explained by .
Correlation Coefficient ():
Measures strength and direction of the linear relationship.
Ranges from to .
does not mean no relationship; it means no linear relationship.
Linear Regression Inference and Diagnostics (Lecture 32)
Standard Error of the Slope ():
Formula:
Logic: Larger spread in () leads to smaller and better precision. Larger residual noise () leads to larger and less precision.
Inference on :
(no linear relationship).
(linear relationship exists).
t-statistic for slope: .
Degrees of Freedom: .
95% CI for : .
Interpretation of Residual Plots:
Starry Sky (Random Scatter): Model is appropriate.
U-shape/Arch: Indicates non-linearity (curvilinearity).
Fan/Cone Shape: Indicates heteroscedasticity (violated equal variance).
Outliers and Influence:
Low leverage: Points near but off the trend. Primarily shift the intercept.
High leverage: Points extreme in space.
Influential: Points that significantly change the slope and . Check with
influence.measures()and look for an asterisk (*) in theinfcolumn or points crossing Cook's distance lines at 0.5 or 1.
Data Scaling and Transformation:
Interpolation: Predicting within the observed range (reliable).
Extrapolation: Predicting outside the observed range (error-prone; avoid).
Exponential Trends: Apply
log(y)transformation to achieve linearity for regression fitting.
Final Review and Decision Framework (Lecture 33)
Sutter's Four-Step Process:
Define the question.
Know your data (variable counts, types, paired/unpaired).
LOOK AT THE DATA (graphs first).
Use the right tool.
Master Test Choice Guide:
Numeric vs None (1 group): Mean + CI using t-distribution.
Numeric vs Categorical (2 groups, independent): t-test or Wilcoxon.
Numeric vs Categorical (2 groups, paired): Paired t-test.
Numeric vs Categorical (3+ groups): One-way ANOVA.
Numeric vs 2 Categorical variables: Two-way ANOVA.
Numeric vs Numeric (Linear):
cor.test()andlm().Categorical (1 var): Wilson CI (
septilde()).Categorical vs Categorical (2 vars):
chisq.test()orfisher.test().50+ Numeric variables: Principal Component Analysis (PCA).
Principal Component Analysis (PCA):
Used to reduce dimensionality in complex datasets.
PC1: Axis of maximum variance.
PC2: Perpendicular to PC1, captures the next largest variance.
Factor Loadings: Correlations between original variables and each PC.
Rabbit Example: PC1 represents body SIZE, while PC2 represents body THICKNESS.
R Implementation:
pca_result <- prcomp(df, scale. = TRUE). Always usescale. = TRUE.
Exam Workflows (Checklists)
Linear Regression Cookbook Flow:
State both hypotheses ( vs ).
Check scatterplot for linearity and outliers.
Analyze 4 diagnostic plots:
Residuals vs Fitted: Want horizontal line/starry sky (tests linearity).
Q-Q Plot: Want straight diagonal line (tests normality of residuals).
Scale-Location: Want straight line with no peaks/valleys (tests equal variance).
Residuals vs Leverage: Check Cook's distance (tests influential outliers).
If plots pass, use
summary(fit). State and p-value.Interpret slope: "For every 1-unit increase in X, Y changes by units on average."
Run
cor.test(x, y)and report , t-stat, p-value, and 95% CI.Make final statement: Reject/Fail to reject .
ANOVA Cookbook Flow:
State hypotheses ( vs ).
Visualize data with a boxplot (stack data if it is in wide format first).
Fit model with
aov()and check two diagnostic plots (Q-Q for normality and Scale-Location for equal variance).Read table via
summary(fit); report F-statistic and p-value.Make final conclusion regarding the group means.
Reading
influence.measures()Output:cook.d: How much the point shifts fitted values.hat: Leverage (extremeness in X-space). High leverage threshold is .inf: Asterisk means the point is flagged by R as potentially influential.