l18- Linear Regressions, Two-Sample T-Tests, and Categorical Predictors in Biological Data

Course Resources and Announcements

  • BIOL 209 Learn Site Resources: Several short videos (2–5 minutes) covering key statistical concepts are available in the resources section. Students are encouraged to email biology209canterbury.ac.nz to request specific topics for future videos.

  • Assessment and Extra Credit: There are multiple opportunities for extra credit located under the assessment tab. These include:

    • Writing original test questions.

    • Contributing to a collaborative glossary of terms.

    • Adding R function descriptions to the class Padlet.

    • An extra credit quiz later in the semester will be available to replace the second-lowest quiz grade.

  • Practice and Communication: Additional practice problems are added periodically to the Learn site. Students should check the announcements section and their university emails for updates on new materials.

Introduction to Categorical Predictors in Regressions

  • Segue into Categorical Variables: The current module transitions from standard regressions into analyzing data where the predictor (xx) variable is categorical rather than continuous.

  • Ordinary Least Squares (OLS) and Categorical Data: Although regressions typically predict a response (yy) from a continuous numeric xx, it is possible to fit an OLS regression to categorical data.

    • Assumptions: Normality assumptions in OLS pertain primarily to residuals (the yy variable); there is no requirement for the predictor (xx) to be normally distributed.

    • Clustering: Fitting a line to categorical data results in "funky" assumption plots because the data points are clustered at specific x-values (e.g., specific age groups or locations).

    • Interpretation: In an OLS model with a binary categorical predictor, the intercept reflects the mean of the first group, and the slope reflects the difference between the mean of the second group and the mean of the first group.

Case Study: Spiny Lobster Biomass in Marine Protected Areas (MPAs)

  • Research Context: The study evaluates lobster biomass in two different MPAs on the North Island of New Zealand:

    • Lee Marine Protected Area: A well-established site, 21 years old at the time of the study.

    • Cathedral Cove Marine Protected Area: A newer site, only 3 years old at the time of the study.

  • Research Question: Is the biomass of spiny lobsters significantly different based on the age of the MPA?

  • Data Variables: The dataset (lobsterbiomass2mpa.csv) contains:

    • site: Character variable for the location.

    • MPA age: Numeric version of the location variable (3 vs. 21).

    • biomass: Response variable.

    • log transformed biomass: Used for analysis to meet normality requirements.

The Two-Sample T-Test: Theoretical Framework

  • Definition: A two-sample t-test is used to determine if the means of two distinct groups are significantly different. It is essentially a specialized form of OLS regression for categorical predictors where the focus is directly on the comparison of means (y1y_1 vs. y2y_2).

  • Null Hypothesis (H0H_0): The two samples come from populations with the same mean (i.e., they are drawn from the same original population).

    • Mathematical expression: yˉ1yˉ2=0\bar{y}_1 - \bar{y}_2 = 0

  • Alternative Hypothesis (HaH_a): The two samples come from populations with different means.

  • The T-Statistic Formula:     t=yˉ1yˉ2sp1n1+1n2t = \frac{\bar{y}_1 - \bar{y}_2}{s_p \sqrt{\frac{1}{n_1} + \frac{1}{n_2}}}     Where:

    • yˉ1\bar{y}_1 and yˉ2\bar{y}_2 are sample means.

    • sps_p is the pooled standard deviation.

    • n1n_1 and n2n_2 are sample sizes.

  • Degrees of Freedom (dfdf):     df=n1+n22df = n_1 + n_2 - 2     For example, if both sites have 50 samples, df=50+502=98df = 50 + 50 - 2 = 98.

Assumptions of the Two-Sample T-Test

  1. Continuous Data: The response variable must be continuous.

  2. Normality: The data within each group must be normally distributed.

  3. Equal Variances (Homoscedasticity): The variance should be similar across both groups.

  4. Independence: Samples must be independent of one another.

Implementation in R: Exploratory Data Analysis

  • Loading Data: lobsters <- read.csv("lobsterbiomass2mpa.csv", stringsAsFactors = TRUE).

  • Summarizing Means by Group: The tapply() function is used to calculate statistics for specific groups.

    • Example: tapply(lobsters$log_biomass, lobsters$age, mean)

  • Boxplots: boxplot(log_biomass ~ age, data = lobsters) provides a quick visual of medians and interquartile ranges (IQR).

    • Note: While t-tests compare means, boxplots are useful for checking overlap in distributions. Non-overlapping IQRs suggest potentially significant differences.

  • Checking Variances: Use tapply(lobsters$log_biomass, lobsters$age, var) to compare group variances.

Testing for Equality of Variances (Fisher's F-test)

  • Fisher’s F-test: Used to statistically compare two variances (s12s_1^2 and s22s_2^2).

  • F-Ratio: F=larger variancesmaller varianceF = \frac{\text{larger variance}}{\text{smaller variance}}

  • R Implementation: var.test(biomass ~ age, data = lobsters)

    • If the resulting p-value is extremely small (e.g., p < 2.2 \times 10^{-16}), the null hypothesis that variances are equal is rejected.

    • In the lobster dataset, the age 3 variance (0.670.67) was much larger than the age 21 variance (0.00950.0095), necessitating a correction.

Welch’s Correction for Unequal Variances

  • Purpose: When the assumption of equal variance is violated, Welch’s (or Satterthwaite) approximation is applied.

  • Mechanism: The test statistic remains the same, but the degrees of freedom are adjusted based on the variances and sample sizes of the two groups.

  • R Implementation: Within the t.test() function, set the argument var.equal = FALSE (which is the default).

    • Example: t.test(log_biomass ~ age, data = lobsters, var.equal = FALSE)

  • Identifying Welch's in Output: Resulting degrees of freedom (dfdf) will typically be non-integer values (e.g., 62.6166962.61669).

Hypothesis Testing and Probability Functions in R

  • pt() vs. qt():

    • pt(): Provides the probability (p-value) for a given test statistic (tt).

    • qt(): Provides the critical t-value for a given probability (alpha level).

  • One-tailed vs. Two-tailed Tests:

    • A two-tailed test checks if means are "different" (multiply one-tail probability by 2).

    • A one-tailed test checks if one mean is specifically "less" or "greater" than the other.

    • R argument: alternative = "less" or alternative = "greater".

Questions & Discussion

Echo Pollock Question 1: Interpretation of T-test output

  • Question: Consider output where the mean biomass is different in new vs. old MPAs. What is the correct interpretation?

  • Answer: The samples represent populations drawn from different distributions. One cannot claim there is NO variance in the groups, nor does it mean every single individual from the old MPA is larger than every individual in the new MPA; rather, the overall population means are distinct.

Echo Pollock Question 2: Difference in Means from Linear Model

  • Question: In an OLS output, the intercept is 3.513.51 and the slope is 0.482610.48261. What is the difference in means?

  • Answer: The difference in means is exactly the slope: 0.482610.48261. To find the mean of the second group, you add the slope to the intercept (3.51+0.483.993.51 + 0.48 \approx 3.99).

Echo Pollock Question 3: Calculating P-values manually

  • Question: Given a test statistic of 15.01615-15.01615 and df=62.61669df = 62.61669, which code tests the null hypothesis for a two-tailed test?

  • Answer: 2 * pt(-15.01615, 62.61669).

    • Explanation: pt() gives the cumulative probability in the lower tail. Since it is a two-tailed test, we multiply by 2. Using the difference in means (as in pt(-0.48, ...) is incorrect because the function requires the standardized test statistic (tt), not the raw difference.