2023-03-14

What is a T-Test?

  • A t-test is a measure in statistics used to compare two means and determine whether the difference between them is significant or not
  • The Null Hypothesis states that there is no significant difference between the two groups, and the difference is due solely to chance.
  • The Alternative Hypothesis states that the difference between the groups is significant enough to suggest that it is not because of chance

Types of Errors

The Null Hypothesis is…

True False
Rejected Type I Error: False Positive Correct!
Not Rejected Correct! Type II Error: False Negative!
  • \(\alpha\) provides the probability of committing a Type I error, and \(\beta\) is the probability of committing a Type II error. The p-value is used in comparison to the \(\alpha\) when determining whether or not the null hypothesis is rejected.
  • Type I Error states that we reject the null hypothesis when it’s actually true, and the Type II Error states that we fail to reject the null hypothesis when it’s actually false.

The rejection region for “Mean of Group A is greater than Mean of Population” is highlighted in red.

Assumptions of the T-Test

  • The samples have normal distributions
  • The samples have equal variance
  • The samples were randomly sampled
  • The samples are independent of each other (exception: paired T-test)

Performing T-Tests

  1. Ensure assumptions are met
  2. Choose the type of t-test
  3. State the null and alternative hypotheses
  4. Select a significance level (Type I Error Level) - Often 5% (\(\alpha\) = 0.05)
  5. Collect the data and calculate the t-statistic
  6. Use the t-statistic to calculate the p-value
  7. Interpret the results to determine whether or not the null hypothesis is rejected

If the p-value is less than the significance level (\(\alpha\)), the null hypothesis is rejected!

Types of T-Tests

There are 3 types of T-Tests:

  • One-sample T-Test, Two-sample T-Test, and Paired T-Test

You must also consider which “side” is evaluated:

  • One-Tailed Test (Left or Right)
    • Determined the probability of difference in a specific direction (example: a new fertilizer will increase the height of cornstalks)
  • Two-Tailed Test
    • Determined the probability of difference in either direction (example: increasing the humidity of the habitat affects lizard growth)

One-sample T-Test

This test is used to evaluate the deviation of one sample mean from the population mean or “normal”

Example: Is the mean heart rate of elephants in the Pittsburgh Zoo different than elephants in the wild?

Formula: \(t = \frac{\bar{x}-\mu_0}{s_\bar{x}}\)

  • \(\bar{x}\) = Sample Mean
  • \(\mu_0\) = Expected Value
  • \(s_\bar{x}\) = Standard Error of the mean

Rejection Graph

The rejection region for “Mean of Group A is greater than Mean of Population” is highlighted in red.

Two-sample T-Test

This test is used to measure the significance of the difference between the means of two sampled groups

Example: Is the mean height of redwood trees in the Carbon Canyon Regional Park different than that of the Humboldt Redwoods State Park?

Two-sample T-Test Formulas:

\(s_p = \sqrt{\frac{(n_1-1)s_1^2 + (n_2-1)s_2^2}{n_1+n_2-2}}\)


\(t = \frac{\bar{x}_1-\bar{x}_2}{s_p\sqrt{\frac{1}{n_1}+\frac{1}{n_2}}}\)


  • \(\bar{x}_a\) & \(\bar{x}_b\) = Sample Means
  • \(n_1\) & \(n_2\) = Sample Sizes
  • \(s_1\) & \(s_2\) = Sample Standard Deviations
  • \(s_p\) = Pooled Standard Deviation

Paired T-Test

This test is used to measure the mean difference between pairs in two populations

Example: Is there a difference between mean wingspans of male and female birds in a pairing? (the birds are paired by partners)

Paired T-Test Formulas:

\(s_\bar{d} = \frac{s}{\sqrt{n}}\)
\(t = \frac{\bar{d}-\mu_0}{s_\bar{d}}\)

  • \(s\) = Standard Deviation of Sample
  • \(n\) = Number of Pairs
  • \(\bar{d}\) = Mean Difference
  • \(\mu_0\) = Mean Difference Under Null Hypothesis (usually 0)
  • \(s_\bar{d}\) = Standard Error of the Mean Difference

Understanding the P-Value

  • The p-value is a calculated value to evaluate observed data to test a null hypothesis
  • The null hypothesis suggests that the difference between the observed and expected values is due to statistical variance while the alternative hypothesis suggests that another variable is causing this deviation.
  • If the alternative hypothesis is accepted, the p-value provides the probability that the null hypothesis was falsely rejected.
    • For example, if we rejected the null hypothesis with a p-value of 0.034, there is a 3.4% chance that the null hypothesis was falsely rejected.

Finding and Interpretting the P-Value

The p-value is found using various methods. By hand, it can be found using a table of p-values correlating to the t-test score and the df of the data. With programming, it can be found using the function “pt”, which calculates the area on a normal distribution function to the left of that t score.

When the t-test is positive, subtract the pt calculation from 1 to get the correct pt value!

If the t-test is two-sided, multiply the correct pt value by 2 to cover both ends.

Each t-test has a corresponding t-test in R!

One-Sample T-Test

Example: The mean annual temperature in the United States is 52.3 Fahrenheit. Is there a strong deviation from this temperature in New Haven, Connecticut?

Manual Calculation:

data("nhtemp")
# Null Hypothesis - The mean temperature in New Haven is
# equal to that of the United States
x = mean(nhtemp)
us_mean = 52.3
s = sd(nhtemp)/sqrt(length(nhtemp))
df_data = length(nhtemp) - 1

t = (x-us_mean)/s
t
## [1] -6.977203
p = 2*pt(t, df_data)
p
## [1] 2.956655e-09

Shortcut Code:

data("nhtemp")

us_mean = 52.3
t.test(nhtemp, mu = us_mean)
## 
##  One Sample t-test
## 
## data:  nhtemp
## t = -6.9772, df = 59, p-value = 2.957e-09
## alternative hypothesis: true mean is not equal to 52.3
## 95 percent confidence interval:
##  50.83306 51.48694
## sample estimates:
## mean of x 
##     51.16

The t-test showed that the mean of the sample is 6.9772 standard deviations below the mean of the US. The null hypothesis is rejected (t = -6.9772, df = 59, p = 2.957e-09).

T-Test Codes

One-Sample T-Test: t.test([data], mu = [X]) (default is mu = 0)

Two-Sample T-Test: t.test([data_a], [data_b])

Paired T-Test: t.test([data_a], [data_b], paired = TRUE)


“alternative” can run the test for one-sided t-tests (greater or less). The default is two-sided

t.test(data_A, data_B, paired = TRUE, alternative = “greater”)

Sample Plot 1: Paired T-Test

The null hypothesis is that the mean extra hours of sleep from Group 2 is equal to that of Group 1. Because the same 10 students were used in both groups, it is a paired t-test.

t.test(sleep$extra[sleep$group == 1], sleep$extra[sleep$group == 2],
       paired = TRUE)
## 
##  Paired t-test
## 
## data:  sleep$extra[sleep$group == 1] and sleep$extra[sleep$group == 2]
## t = -4.0621, df = 9, p-value = 0.002833
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  -2.4598858 -0.7001142
## sample estimates:
## mean difference 
##           -1.58

The t-test showed that the mean difference of the pairs is -4.0621 standard deviations below the mean of the US. There is a 0.2833% chance that the null hypothesis could be falsely rejected. The null hypothesis is rejected (t = -4.0621, df = 9, p = 0.002833).

Sample 2 - One-Sample T-Test

The average eruption time is usually 2.98 minutes. How much does the sample deviate from the expected value?

data("faithful")

eruptions_mean = 3.42
t.test(faithful$eruptions, mu = eruptions_mean)
## 
##  One Sample t-test
## 
## data:  faithful$eruptions
## t = 0.97944, df = 271, p-value = 0.3282
## alternative hypothesis: true mean is not equal to 3.42
## 95 percent confidence interval:
##  3.351534 3.624032
## sample estimates:
## mean of x 
##  3.487783

Results

The t-test showed that the sample deviates from the populaiton by 0.979 standard deviations. There is a 32.82% chance that the null hypothesis could be falsely rejected. The null hypothesis is not rejected (t = 0.97944, df = 271, p = 0.3282).