Comprehensive Biostatistics Notes: Hypothesis Testing, Error Analysis, Variance Tests, and Mean Comparisons

Foundations of Statistical Hypothesis Testing and Error Analysis

  • Conceptual Framework of Hypothesis Testing:

    • Statistical testing translates an initial observation or empirical question into two mutually exclusive mathematical assertions:

      • Null Hypothesis (H0H_0): States that there is no true difference, effect, or relationship in the population (e.g., population means are equal: μ1=μ2\mu_1 = \mu_2; or the sample distribution conforms to a theoretical distribution).

      • Alternative Hypothesis / Working Hypothesis (HAH_A): States that a genuine difference, effect, or deviation exists in the population (e.g., μ1≠μ2\mu_1 \neq \mu_2).

  • Empirical data collected from samples are transformed into a single test statistic (such as tt, FF, WW, or DD), which quantifies the deviation of the empirical observations from what is expected under H0H_0.

  • A pre-defined significance level (α\alpha) is established prior to data evaluation (conventionally α=0.05\alpha = 0.05 or 5%5\%).

  • The pp-Value:

    • Verbatim Definition: The probability of obtaining a test statistic equal to or more extreme than the observed value, assuming that the null hypothesis (H0H_0) is strictly true.

    • Decision Rules:

      • If p≤αp \le \alpha: The observed data are considered highly improbable under the assumption that H0H_0 is true. The null hypothesis H0H_0 is rejected in favor of HAH_A (statistically significant result).

      • If p>αp > \alpha: The observed data are compatible with random variation under H0H_0. The null hypothesis H0H_0 is not rejected (fail to reject H0H_0).

Statistical testing decision flowchart
  • Decision Matrix and Types of Statistical Errors:

    • Statistical decisions rely entirely on probabilities derived from sample data, creating an inherent risk of erroneous conclusions:

      • True State: H0H_0 is True:

        • Test Decision: Do not reject H0H_0: Correct decision. Probability = 1−α1 - \alpha.

        • Test Decision: Reject H0H_0: Type I Error (Fehler 1. Art / False Positive). The test falsely detects an effect or difference when none exists in reality. Probability = α\alpha.

      • True State: H0H_0 is False:

        • Test Decision: Do not reject H0H_0: Type II Error (Fehler 2. Art / False Negative). The test fails to detect a genuine effect or difference. Probability = β\beta.

        • Test Decision: Reject H0H_0: Correct decision. Probability = 1−β1 - \beta (known as the Statistical Power or Teststärke of the test).

    • Terminology Mapping:

      • False Positive: The test falsely indicates a positive response/effect; H0H_0 is rejected despite being true (corresponds exactly to a Type I error).

      • False Negative: The test falsely indicates no response/effect; H0H_0 is accepted/retained despite being false (corresponds exactly to a Type II error).

  • Real-World Consequences of Errors and the Base Rate Fallacy:

    • Tests do not provide absolute certainty. When conducting mass screening for rare events, the overwhelming majority of positive test results can be false positives even when test error rates appear very low.

    • Case Study 1: Schoolwide Scarlet Fever Screening (5%5\% Error Rates):

      • Population: Mass testing of 20,00020{,}000 first-grade students.

      • Prevalence: 0.5%0.5\% of students are infected (100100 sick children, 19,90019{,}900 healthy children).

      • Type I Error Rate (α\alpha): 5%5\% (95%95\% specificity).

      • Type II Error Rate (β\beta): 5%5\% (95%95\% sensitivity / power).

      • Test Outcomes for Sick Cohort (100100 children):

        • True Positives: 100×0.95=95100 \times 0.95 = 95 children test positive.

        • False Negatives: 100×0.05=5100 \times 0.05 = 5 children test falsely negative.

      • Test Outcomes for Healthy Cohort (19,90019{,}900 children):

        • True Negatives: 19,900×0.95=18,90519{,}900 \times 0.95 = 18{,}905 children test correctly negative.

        • False Positives: 19,900×0.05=99519{,}900 \times 0.05 = 995 children test falsely positive.

      • Total Positive Tests: 95+995=1,09095 + 995 = 1{,}090 children test positive.

      • Positive Predictive Value (PPV):             PPV=951090≈8.3%\text{PPV} = \frac{95}{1090} \approx 8.3\%

      • Implication: Only 8.3%8.3\% of all individuals who test positive actually have scarlet fever; over 91.7%91.7\% of positive findings are false alarms.

    • Case Study 2: Scarlet Fever Screening with Highly Accurate Tests (1%1\% Error Rates):

      • Parameters: 20,00020{,}000 students; 100100 sick (0.5%0.5\%), 19,90019{,}900 healthy (99.5%99.5\%); α=1%\alpha = 1\%, β=1%\beta = 1\%.

      • Sick children: 9999 test positive, 11 tests false negative.

      • Healthy children: 19,70119{,}701 test negative, 199199 test false positive.

      • Total positive tests: 99+199=29899 + 199 = 298.

      • False Positive Proportion: 199298≈67.7%\frac{199}{298} \approx 67.7\%.

      • Implication: Even with an exceptional test accuracy of 99%99\%, more than two-thirds (67.7%67.7\%) of all positive test results remain false positives due to low base prevalence.

    • Case Study 3: COVID-19 Antigen Rapid Mass Testing:

      • Test parameters: Sensitivity = 80%80\% (β=0.20\beta = 0.20), Specificity = 98%98\% (α=0.02\alpha = 0.02).

      • Scenario A (Low Prevalence: 55 in 10,00010{,}000 inflected):

        • Of 55 infected: 44 True Positive, 11 False Negative.

        • Of 9,9959{,}995 uninfected: 200200 False Positive (9,995×0.02≈2009{,}995 \times 0.02 \approx 200), 9,7959{,}795 True Negative.

        • Total positive results = 204204. Only 44 out of 204204 (≈1.96%\approx 1.96\%) are truly infected.

      • Scenario B (High Prevalence: 1,0001{,}000 in 10,00010{,}000 infected):

        • Of 1,0001{,}000 infected: 800800 True Positive, 200200 False Negative.

        • Of 9,0009{,}000 uninfected: 180180 False Positive (9,000×0.02=1809{,}000 \times 0.02 = 180), 8,8208{,}820 True Negative.

        • Total positive results = 980980. Truly infected positives = 800800 out of 980980 (≈81.6%\approx 81.6\%).

COVID antigen rapid test probability tree comparing low and high infection prevalence

Verification of Distributional Assumptions: Testing for Normality

  • Role of Normality Testing:

    • Parametric inferential procedures (including the FF-test, Student's tt-test, and Welch's tt-test) rely on the fundamental assumption that sample data originate from a normally distributed population.

    • Critical Prerequisite Rule: If either of two datasets being compared violates the assumption of a normal distribution, parametric tests (FF-test, Student's tt-test, Welch's tt-test) must not be applied.

    • Hypotheses for Normality Tests:

      • H0H_0: The sample distribution does not differ significantly from a normal distribution (Distribution=Normal\text{Distribution} = \text{Normal}).

      • HAH_A: The sample distribution differs significantly from a normal distribution (Distribution≠Normal\text{Distribution} \neq \text{Normal}).

    • Decision Criteria:

      • p>αp > \alpha: Fail to reject H0H_0. Data are assumed to be normally distributed.

      • p≤αp \le \alpha: Reject H0H_0. Data are significantly non-normal.

  • Methods for Assessing Normality:

    • Visual Assessment Methods:

      • Histogram: Allows rapid visual inspection for symmetry, unimodality, and pronounced skewness or heavy tails.

      • Quantile-Quantile Plot (Q-Q Plot): Plots empirical sample quantiles against theoretical normal distribution quantiles. If the data are normally distributed, the data points fall directly along the linear reference line. S-shaped curves, systematic arcs, or deviations at the extremes reveal skewness and kurtosis.

    • Inferential Testing Methods:

      • Kolmogorov-Smirnov Test (KS-Test): Compares the cumulative empirical distribution function of the sample against the cumulative distribution function of a theoretical normal distribution. Measures maximum distance DD.

      • Shapiro-Wilk Test: Evaluates the correlation between sample order statistics and expected normal values, comparing variance components to compute test statistic WW. Highly recommended for small to moderate sample sizes.

Empirical histogram of sample dataNormal Q-Q Plot illustrating systematic departure from the theoretical reference line
  • Contradictions Among Testing Procedures:

    • Statistical tests for normality can yield conflicting results when applied to identical datasets, demonstrating why visual validation must accompany analytical testing:

      • One-Sample Kolmogorov-Smirnov Test on mydata:

        • Statistic: D=0.0791D = 0.0791

        • pp-value = 0.16370.1637

        • Formal Conclusion: p>0.05p > 0.05, failing to reject H0H_0. The test indicates that the data distribution does not deviate significantly from a normal distribution.

      • Shapiro-Wilk Normality Test on mydata:

        • Statistic: W=0.9258W = 0.9258

        • pp-value = 1.598×10−81.598 \times 10^{-8}

        • Formal Conclusion: p≤0.05p \le 0.05, soundly rejecting H0H_0. The test indicates that the data deviate significantly from normality.

      • Resolution via Visual Inspection: The normal Q-Q plot of mydata clearly displays substantial non-linear tail deviations, and the histogram demonstrates pronounced right-skewness. Despite the non-significant KS-test result, the dataset is objectively non-normal, confirming the Shapiro-Wilk test result.

  • Standards for Rigorous Reporting of Results:

    • A complete and scientific presentation of test results must include:

      1. A precise verbal description of the biological or empirical outcome.

      2. The exact formal test applied.

      3. The numerical test statistic (DD, WW, FF, tt, etc.) including associated degrees of freedom (dfdf).

      4. The exact pp-value reported with precision (avoiding vague thresholds such as p<0.05p < 0.05 or p>0.05p > 0.05 unless values are extremely small, e.g., p=1.6×10−8p = 1.6 \times 10^{-8}).

    • Reporting Examples:

      • Significant Non-Normality: The distribution of the data deviates significantly from a normal distribution (Shapiro-Wilk test, W=0.9258W = 0.9258, p=1.6×10−8p = 1.6 \times 10^{-8}).

      • Non-Significant Deviation: The distribution of the data does not deviate significantly from a normal distribution (KS-test, D=0.0791D = 0.0791, p=0.16p = 0.16).

      • Visual Finding: Visual inspection using a Q-Q plot revealed that the data are not normally distributed.

    • Standards for Graphical Figures: All axes must have descriptive text labels including measurement units (e.g., parts per hundred million [pphm\text{pphm}], grams [g\text{g}]). Formal publications and theses require legends and standalone figure captions.

Testing for Equality of Variances (Homoscedasticity)

  • Overview and Significance:

    • Homoscedasticity refers to the assumption that two or more compared groups exhibit equal population variances (σ12=σ22\sigma_1^2 = \sigma_2^2).

    • Evaluating variance equality determines whether standard parametric comparisons (such as Student's tt-test) are legitimate, or whether adjustments (Welch's tt-test) or non-parametric alternatives must be employed.

Boxplot comparing ozone concentrations across Garden A, Garden B, and Garden C
  • Empirical Dataset: Ozone Concentrations in Nurseries:

    • Study design: Three commercial garden centers (gartenA, gartenB, gartenC) measured ozone concentrations across 2020 distinct time points each (N=60N = 60 total observations). Ozone is recorded in parts per hundred million (pphm\text{pphm}).

    • R Data Ingestion and Summary Commands: R gaerten <- read.table("05_gaerten.txt", header = TRUE) tapply(gaertenozon,gaertenozon, gaertenstandort, var) tapply(gaertenozon,gaertenozon, gaertenstandort, mean) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    • Summary Statistics:

      • Garden A: Mean = 5.40 pphm5.40\,\text{pphm}, Variance = 5.078500 pphm25.078500\,\text{pphm}^2

      • Garden B: Mean = 3.15 pphm3.15\,\text{pphm}, Variance = 1.413100 pphm21.413100\,\text{pphm}^2

      • Garden C: Mean = 5.15 pphm5.15\,\text{pphm}, Variance = 1.623666 pphm21.623666\,\text{pphm}^2

    • Empirical Comparison: Garden A and Garden C possess nearly identical means (5.405.40 vs 5.155.15), but the variance of Garden A is more than three times higher than that of Garden C (5.085.08 vs 1.621.62).

  • Fisher's F-Test for Two Variances (Parametric):

    • Strict Requirement: Both datasets must be normally distributed.

    • Hypotheses:

      • H0H_0: σ12=σ22\sigma_1^2 = \sigma_2^2 (variance ratio equals 11\,: σ12σ22=1\frac{\sigma_1^2}{\sigma_2^2} = 1).

      • HAH_A: σ12≠σ22\sigma_1^2 \neq \sigma_2^2 (variance ratio differs from 11\,: σ12σ22≠1\frac{\sigma_1^2}{\sigma_2^2} \neq 1).

  • Test Statistic Calculation:     F=s12s22=var(Sample 1)var(Sample 2)F = \frac{s_1^2}{s_2^2} = \frac{\text{var}(\text{Sample 1})}{\text{var}(\text{Sample 2})}

  • For Garden A versus Garden C:     F=5.0785001.623666≈3.128F = \frac{5.078500}{1.623666} \approx 3.128

  • Interpretation and Directional Symmetry:

    • If the ratio is inverted (FC/A=1.6236665.078500=0.3197F_{C/A} = \frac{1.623666}{5.078500} = 0.3197), the resulting statistical probability is completely identical.

    • Cumulative probability with df1=19,df2=19df_1 = 19, df_2 = 19: P(X≤3.13)=0.9916459P(X \le 3.13) = 0.9916459.

    • Upper-tail probability: P(X>3.13)=1−0.9916459=0.0083541P(X > 3.13) = 1 - 0.9916459 = 0.0083541.

    • Lower-tail probability for inverted ratio: P(X≤0.32)=0.0083541P(X \le 0.32) = 0.0083541.

    • Two-sided pp-value: 2×0.0083541=0.0167082≈0.016772 \times 0.0083541 = 0.0167082 \approx 0.01677.

Cumulative F-distribution function showing rejection threshold for variance ratio
  • Influence of Degrees of Freedom on the F-Distribution:

    • Degrees of freedom for each group are determined by sample size: df1=n1−1df_1 = n_1 - 1 and df2=n2−1df_2 = n_2 - 1.

    • Sample Size Effect: With increasing sample size (NN), the cumulative FF-distribution curve becomes markedly steeper around F=1F = 1.

      • With few data points (e.g., df=20,20df = 20, 20), variance estimates are uncertain; an FF-value of 1.51.5 yields a cumulative probability of approximately 0.800.80 (p=1−0.80=0.20p = 1 - 0.80 = 0.20), failing to reach significance.

      • With large sample sizes (e.g., df=100,100df = 100, 100), variance estimates are highly accurate; an FF-value of 1.51.5 yields a cumulative probability of approximately 0.980.98 (p=1−0.98=0.02p = 1 - 0.98 = 0.02), which is statistically significant.

    • Principle: As sample size increases, smaller deviations of the variance ratio from 11 are required to reject the null hypothesis of equal variances.

Cumulative probability of the F-distribution comparing small versus large degrees of freedom
  • Step-by-Step F-Test Implementation in R:

    1. Data loading and subgroup creation:R gaerten <- read.table("05_gaerten.txt", header = TRUE) gA <- subset(x = gaerten, standort == "gartenA") gB <- subset(x = gaerten, standort == "gartenB") gC <- subset(x = gaerten, standort == "gartenC") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  1. Normality confirmation via Shapiro-Wilk test: R shapiro.test(gAozon) # W = 0.9879, p = 0.9941 shapiro.test(gBozon) shapiro.test(gC$ozon) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;         All pp-values are greater than 0.050.05; normality assumption is satisfied.

  2. Execution of the variance test: R var.test(gAozon,gCozon, gCozon) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    • R Output Details:

      • F=3.128F = 3.128

      • num df=19\text{num } df = 19, denom df=19\text{denom } df = 19

      • pp-value = 0.016770.01677

      • 95%95\% Confidence Interval: [1.238105,7.902767][1.238105, 7.902767]

      • Sample estimate (ratio of variances): 3.1280113.128011

    • Formal Results Presentation:

      • Garden A vs Garden C: The variances of the ozone values measured in Garden A and Garden C differ significantly (FF-test, F19,19=3.13F_{19,19} = 3.13, p=0.017p = 0.017).

      • Garden A vs Garden B: The variances of the ozone values measured in Garden A and Garden B differ significantly (FF-test, F19,19=3.59F_{19,19} = 3.59, p=0.0077p = 0.0077).

      • Garden B vs Garden C: The variances of the ozone values measured in Garden B and Garden C do not differ significantly (FF-test, F19,19=0.87F_{19,19} = 0.87, p=0.76p = 0.76).

  • Levene's Test for Homogeneity of Variances (Non-Parametric / Robust):

    • Indications: Used when data deviate from a normal distribution. While the FF-test possesses greater statistical power for purely normal data, it is sensitive to violations of normality. Levene's test provides a robust assessment of homoscedasticity across two or more groups (k≥2k \ge 2).

    • Mathematical Formulation:

      • Calculate the absolute deviation of each data point XijX_{ij} from its group mean Xˉi\bar{X}_i (or group median):             Zij=∣Xij−Xˉi∣Z_{ij} = |X_{ij} - \bar{X}_i|

      • Compute the average deviation within each individual group ii:             Zˉi=1ni∑j=1niZij\bar{Z}_i = \frac{1}{n_i} \sum_{j=1}^{n_i} Z_{ij}

      • Compute the grand mean of absolute deviations across all observations:             Zˉ=1N∑i=1k∑j=1niZij\bar{Z} = \frac{1}{N} \sum_{i=1}^k \sum_{j=1}^{n_i} Z_{ij}             where N=∑i=1kniN = \sum_{i=1}^k n_i.

      • Calculate the test statistic WW, which approximately follows an FF-distribution:             W=N−kk−1⋅∑i=1kni(Zˉi−Zˉ)2∑i=1k∑j=1ni(Zij−Zˉi)2≈Fk−1,N−kW = \frac{N - k}{k - 1} \cdot \frac{\sum_{i=1}^k n_i (\bar{Z}_i - \bar{Z})^2}{\sum_{i=1}^k \sum_{j=1}^{n_i} (Z_{ij} - \bar{Z}_i)^2} \approx F_{k-1, N-k}

      • Degrees of Freedom:

        • Numerator degrees of freedom: df1=k−1df_1 = k - 1

        • Denominator degrees of freedom: df2=N−kdf_2 = N - k

    • Median-Centered Levene Test (Brown-Forsythe): Modern implementations replace group means with group medians (Zij=∣Xij−X~i∣Z_{ij} = |X_{ij} - \tilde{X}_i|) to enhance robustness against extreme outliers.

    • R Implementation via the car Package:R install.packages("car") library("car") gAC <- subset(gaerten, standort == "gartenA" | standort == "gartenC") leveneTest(gACozon,gACozon, gACstandort) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    • R Output Details:

      • Center: median

      • df1=1df_1 = 1, df2=38df_2 = 38

      • F value=4.2444F\text{ value} = 4.2444

      • Pr⁡(>F)=0.04628\Pr(>F) = 0.04628

    • Formal Presentation of Levene Result: The variances of the ozone values measured in Garden A and Garden C differ significantly (Levene's test, F1,38=4.24F_{1,38} = 4.24, p=0.046p = 0.046).

  • Excursus: Logical Operators in R Data Subsetting:

    • The | operator represents logical OR (at least one condition must hold true).

    • The & operator represents logical AND (both conditions must simultaneously hold true).

    • Behavioral Analysis:

      • standort == "gartenA" & standort == "gartenC": Produces an empty subset (empty set) because no row can contain both character strings in the same column.

      • standort == "gartenA" | ozon == 2.06: Extracts every row belonging to Garden A plus entry 21 (which has an ozone level of 2.062.06).

      • standort == "gartenB" & ozon == 2.06: Extracts exclusively entry 21 (which simultaneously satisfies both conditions).

Testing Differences Between Two Independent Means

  • Methodological Decision Tree for Group Mean Comparisons:

    1. Assess Normality: Check both datasets using KS-tests, Shapiro-Wilk tests, or Q-Q plots.

      • If normal: proceed to step 2.

      • If non-normal: proceed directly to non-parametric testing via the Wilcoxon Rank-Sum Test (Mann-Whitney U-Test).

    2. Assess Homoscedasticity (Equal Variance):

      • Test using the parametric FF-test (or robust Levene-test).

      • If variances are equal (p>0.05p > 0.05): proceed to Student's tt-test.

      • If variances are unequal (p≤0.05p \le 0.05): proceed to Welch's tt-test.

  • Student's Two-Sample t-Test (Parametric, Homoscedastic):

    • Historical Context: Developed by William Sealy Gosset (1876–1937), an Oxford-trained mathematician and chemist employed by the Guinness Brewery in Dublin. Because corporate policy prohibited staff from publishing proprietary brewing discoveries, Gosset published the distribution under the pseudonym "Student" in 1908.

Portrait of William Sealy Gosset
  • Research Case Study: Blue Tit Body Mass: Empirical observation indicates geographical variation in male blue tit (Cyanistes caeruleus) body mass between Hamburg and Berlin.

    • Hamburg Sample: nA=60n_A = 60, mean mass xˉA=11.35683 g≈11.36 g\bar{x}_A = 11.35683\,\text{g} \approx 11.36\,\text{g}

    • Berlin Sample: nB=60n_B = 60, mean mass xˉB=11.48717 g≈11.49 g\bar{x}_B = 11.48717\,\text{g} \approx 11.49\,\text{g}

    • Hypotheses:

      • H0H_0: μA=μB\mu_A = \mu_B (mean body mass does not differ significantly between cities).

      • HAH_A: μA≠μB\mu_A \neq \mu_B (mean body mass differs significantly between cities).

  • Mathematical Definition of the t-Statistic:     ts=xˉA−xˉBs⋅1nA+1nBt_s = \frac{\bar{x}_A - \bar{x}_B}{s \cdot \sqrt{\frac{1}{n_A} + \frac{1}{n_B}}}     where ss represents the pooled common standard deviation of both samples, and the denominator represents the standard error of the difference between two sample means.

Scatterplot of individual male blue tit body weights in Hamburg versus Berlin
  • Component Breakdown:

    • Numerator: Observed difference in sample means (xˉA−xˉB=11.357−11.487≈−0.1 g\bar{x}_A - \bar{x}_B = 11.357 - 11.487 \approx -0.1\,\text{g}).

    • Denominator: Combined standard error:         s⋅160+160=0.085 gs \cdot \sqrt{\frac{1}{60} + \frac{1}{60}} = 0.085\,\text{g}

    • Calculated tt-statistic:         ts=−0.1 g0.085 g=−1.5t_s = -\frac{0.1\,\text{g}}{0.085\,\text{g}} = -1.5

  • Degrees of Freedom:     df=nA+nB−2=60+60−2=118df = n_A + n_B - 2 = 60 + 60 - 2 = 118

Student t-distribution density curve showing two-sided rejection areas
  • One-Sided versus Two-Sided Testing:

    • Two-Sided (Nondirectional) Test: Applied when no prior theoretical or physiological basis predicts which mean will be larger. Critical rejection areas are placed in both tails (ts≥1.5t_s \ge 1.5 and ts≤−1.5t_s \le -1.5).

    • One-Sided (Directional) Test: Applied when explicit prior knowledge dictates that an effect can occur in only one direction (e.g., comparing running speeds of hares versus snails; biological constraints preclude snails from being faster). The entire rejection area (α\alpha) is concentrated in a single tail.

  • Execution in R: R meisen <- read.table("05_Blaumeisen.txt", header = TRUE) m_hh <- subset(meisen, ort == "hamburg") m_b <- subset(meisen, ort == "berlin") t.test(m_hhgewicht,mbgewicht, m_bgewicht, var.equal = TRUE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    • R Output Results:

      • t=−1.5009t = -1.5009, df=118df = 118, pp-value = 0.13610.1361

      • 95%95\% Confidence Interval: [−0.30229670,0.04163003][-0.30229670, 0.04163003]

      • Mean of xx (Hamburg) = 11.3568311.35683, Mean of yy (Berlin) = 11.4871711.48717

    • Formal Presentation of t-Test Result: The means of the blue tit body weights in Hamburg (11.36 g11.36\,\text{g}) and Berlin (11.49 g11.49\,\text{g}) do not differ significantly (tt-test, t=−1.5t = -1.5, df=118df = 118, p=0.14p = 0.14).

  • Welch's t-Test (Parametric, Heteroscedastic):

    • Purpose: Used when sample distributions are normal but group variances are unequal (σ12≠σ22\sigma_1^2 \neq \sigma_2^2).

  • Mathematical Adjustment: Discards the pooled standard deviation ss in favor of separate group sample variances:     ts=xˉA−xˉBsA2nA+sB2nBt_s = \frac{\bar{x}_A - \bar{x}_B}{\sqrt{\frac{s_A^2}{n_A} + \frac{s_B^2}{n_B}}}

  • Degrees of Freedom: Corrected using the Welch-Satterthwaite equation, which typically yields a non-integer, reduced degrees of freedom.

  • Execution in R: Implemented by specifying var.equal = FALSE (which is the default in R): R t.test(m_hhgewicht,mbgewicht, m_bgewicht, var.equal = FALSE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    • R Output Results: t=−1.5009t = -1.5009, df=117.99df = 117.99, pp-value = 0.13610.1361. Because the tit dataset exhibited equal variances, the Welch correction yields an identical conclusion with a minor adjustment to degrees of freedom (117.99117.99 vs 118118).

  • Wilcoxon Rank-Sum Test / Mann-Whitney U-Test (Non-Parametric):

    • Indications: Non-parametric equivalent of the independent two-sample tt-test. Applied when data violate normality assumptions.

    • Core Principle: Evaluates the relative ranks of data points rather than their raw metric values. Focuses on ordinal positions when all pooled observations are arranged in ascending order.

    • Step-by-Step Calculation Procedure:

      1. Pool both independent samples (k=2k = 2) containing nAn_A and nBn_B observations into a combined dataset of size N=nA+nBN = n_A + n_B.

      2. Sort all NN values in ascending order, retaining the group identity of each observation.

      3. Assign ranks RiR_i from 11 (smallest value) to NN (largest value). For tied values, assign the average of the ranks they occupy.

      4. Calculate the rank sum for each group:             Uk=∑j=1nkRk,jU_k = \sum_{j=1}^{n_k} R_{k, j}

      5. Compute the Wilcoxon test statistic WkW_k for each sample:             Wk=Uk−nk(nk+1)2W_k = U_k - \frac{n_k (n_k + 1)}{2}

      6. The smaller of the computed WW values is compared against critical values tabulated for sample sizes nAn_A and nBn_B.

    • Execution in R:R wilcox.test(m_hhgewicht,mbgewicht, m_bgewicht) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  • R Output Results: W=1494.5W = 1494.5, pp-value = 0.10940.1094.

  • Formal Presentation of Wilcoxon Result: The means of the blue tit body weights in Hamburg (11.36 g11.36\,\text{g}) and Berlin (11.49 g11.49\,\text{g}) do not differ significantly (Wilcoxon rank-sum test, W=1494.5W = 1494.5, p=0.11p = 0.11).

Analysis of Paired Samples and Advanced Median Comparison

  • Principles of Paired (Dependent) Experimental Designs:

    • Definition: A paired design occurs when each data point in sample A is uniquely and non-randomly linked to a specific data point in sample B (e.g., repeated measures on the same individual, split-plot agricultural experiments, or spatial upstream/downstream matching).

    • Conceptual Difference:

      • Unpaired Test: Compares overall group means (ignoring individual correspondences). High background variance between experimental units can obscure genuine differences.

      • Paired Test: Subtracts each paired observation to create a single vector of paired differences (xdiff=xA−xBx_{\text{diff}} = x_A - x_B), testing whether the mean difference (xˉA−B\bar{x}_{A-B}) differs significantly from zero.

  • Case Study: Aquatic Invertebrate Diversity Around Wastewater Plants:

    • Study System: Abundance of aquatic macroinvertebrates recorded across 2020 different rivers at two sampling stations per river: upstream (up, before plant) and downstream (down, after plant).

    • Data Characteristics: River-to-river biodiversity varies dramatically across natural landscapes (some rivers support only 55 to 1010 species, while others support 1515 to 2020). However, downstream counts are systematically and consistently higher than upstream counts within nearly every river.

Invertebrate species count comparing upstream and downstream stations across riversDifference plot displaying paired upstream minus downstream invertebrate counts
  • Paired Student's t-Test:

    • Mathematical Formula:         ts=xˉA−BsA−B⋅1nA−Bt_s = \frac{\bar{x}_{A-B}}{s_{A-B} \cdot \sqrt{\frac{1}{n_{A-B}}}}         where xˉA−B\bar{x}_{A-B} is the mean of the paired differences, sA−Bs_{A-B} is the sample standard deviation of the paired differences, and nA−Bn_{A-B} is the number of paired differences.

    • Comparing Statistical Outcomes in R:

      • Incorrect Unpaired Test:R t.test(streamsup,streamsup, streamsdown, var.equal = TRUE) # Output: p-value = 0.6231 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;             Conclusion: No statistically significant difference. The substantial variance among different rivers masks the local treatment effect.

  • Correct Paired Test: R t.test(streamsup,streamsup, streamsdown, paired = TRUE, var.equal = TRUE) # Output: p-value = 3.162e-07 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;             Conclusion: Highly statistically significant difference (p=3.162×10−7p = 3.162 \times 10^{-7}). Accounting for river-specific pairings eliminates inter-river variance and detects the true biological impact of the facility.

  • Paired Wilcoxon Signed-Rank Test (Non-Parametric):

    • Indications: Used for paired/matched samples when the differences (xA−xBx_A - x_B) violate normality.

    • Calculation Procedure:

      1. Compute the raw differences for every matched pair: Gdiff,i=xA,i−xB,iG_{\text{diff}, i} = x_{A, i} - x_{B, i}.

      2. Take the absolute values of these differences: ∣Gdiff,i∣|G_{\text{diff}, i}|.

      3. Rank the absolute differences from lowest to highest: Ri=rank(∣Gdiff,i∣)R_i = \text{rank}(|G_{\text{diff}, i}|).

      4. Calculate separate rank sums for pairs with positive versus negative original differences:             R+=∑Gdiff>0Ri,R−=∑Gdiff≤0RiR^+ = \sum_{G_{\text{diff}} > 0} R_i, \quad R^- = \sum_{G_{\text{diff}} \le 0} R_i

      5. The test statistic is based on the minimum rank sum (min⁡(R+,R−)\min(R^+, R^-)).

    • Comparing Statistical Outcomes in R:

      • Unpaired Wilcoxon Test:R wilcox.test(streamsup,streamsup, streamsdown) # Output: p-value = 0.6205 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  • Paired Wilcoxon Signed-Rank Test: R wilcox.test(streamsup,streamsup, streamsdown, paired = TRUE) # Output: p-value = 5.722e-06 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * Conclusion: The paired Wilcoxon test detects a significant effect (p=5.722×10−6p = 5.722 \times 10^{-6}), whereas the unpaired version fails entirely (p=0.6205p = 0.6205).

  • Visual Median Comparison via Notched Boxplots:

    • Methodology: Adding notches to standard boxplots provides a graphical test for median differences: R boxplot(data = meisen, gewicht ~ ort, notch = TRUE, xlab = "Stadt", ylab = "Gewicht (g)") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  • Interpretation Rules:

    • The notch width illustrates an approximate 95%95\% confidence interval around the sample median.

    • Overlapping Notches: Strong visual indicator that population medians do not differ significantly (e.g., comparing Berlin and Hamburg tit weights).

    • Non-Overlapping Notches: Strong visual indicator that population medians differ significantly (e.g., comparing Turku tit weights to Berlin or Hamburg).

Notched boxplot comparing median blue tit body weights in Berlin, Hamburg, and Turku

Summary of Biostatistical Testing Procedures

  • Comprehensive Testing Matrix:

    • Assessing Normality (One Sample vs Theoretical Normal):

      • Quantitative Tests: Shapiro-Wilk test (preferred for smaller NN), Kolmogorov-Smirnov test.

      • Visual Assessment: Normal Q-Q plots (quantiles vs theoretical line), histograms.

    • Assessing Homoscedasticity (Equality of Variances):

      • Normal Data: Fisher's FF-test (var.test()). Ratio of variances F=s12s22F = \frac{s_1^2}{s_2^2}.

      • Non-Normal Data: Levene's test (leveneTest() in library car). Compares group absolute deviations to overall absolute deviations.

    • Comparing Means / Medians (Two Groups):

      • Normal Data + Equal Variances (Independent): Student's Two-Sample tt-test (t.test(..., var.equal = TRUE)). Compares difference between group means to the pooled standard error.

      • Normal Data + Unequal Variances (Independent): Welch's tt-test (t.test(..., var.equal = FALSE)). Employs unpooled standard errors and adjusted degrees of freedom.

      • Non-Normal Data (Independent): Wilcoxon Rank-Sum Test / Mann-Whitney U-Test (wilcox.test()). Transforms observations into ranks and sums ranks per group.

      • Normal Data (Paired/Matched): Paired Student's tt-test (t.test(..., paired = TRUE)). Analyzes the vector of pairwise differences against zero.

      • Non-Normal Data (Paired/Matched): Paired Wilcoxon Signed-Rank Test (wilcox.test(..., paired = TRUE)). Analyzes ranked absolute paired differences separated by sign (R+,R−R^+, R^-).

      • Visual Assessment of Medians: Notched boxplots (boxplot(..., notch = TRUE)). Evaluates confidence intervals of medians via notch overlap.