Inferential: t-test, ANOVA, Regression

S25 · Chapter 13

Today’s Agenda

  • The Twitch job, part 3: the Inferencing Data worksheet
  • What a hypothesis test actually asks
  • How your variables choose the test
  • How to read a p-value, and what it does not tell you
  • Effect size, and why a big sample makes significance and size come apart

Where We Left Off

  • In the example data, gaming channels averaged 28.49 characters per message
  • Non-gaming channels averaged 33.70, a gap of about five characters
  • The histogram showed the gap, and could not say whether the gap is real or chance

Today we settle that question. There are no formulas to memorize, only questions to ask.

Real, Important, or Chance

  • Non-gaming chat averaged 33.70 characters per message, and gaming chat averaged 28.49
  • The example data holds 34,762 messages
  • Which is the best description of the five-character gap?
    • A. Real and important
    • B. Real, but too small to matter
    • C. Possibly just chance
    • D. Impossible to judge from two averages

The Twitch Job, Part 3

  • The Inferencing Data worksheet is part 3 of the Twitch job
  • Four hypotheses first, written before any code
  • Then four tests: chi-square, t-test, ANOVA, and regression
  • Every test is reported with its effect size

Get the Worksheet

What a Test Actually Asks

Split a group of people in half at random and the two halves’ average heights still differ a little. Samples wobble.

  • We measured 34,762 example messages to learn about chat in general
    • The sample is the means to an end, and the population is the point
  • The honest question is not whether there is a gap in the sample
    • It is whether chance alone is a convincing explanation for that gap

The Skeptic’s Move

Null hypothesis: the claim that there is no real difference or relationship, only chance. Example: gaming and non-gaming messages have the same average length.

  • A test starts from the opposite of what you suspect
    • Then it asks: if that were true, how surprising is the gap we saw?
  • Testing a coin works the same way
    • Assume the coin is fair, flip it a hundred times, and ask how often a fair coin looks this lopsided
  • If the answer is “almost never,” the fair-coin assumption is hard to keep

The Variables Choose the Test

  • A numeric outcome compared across two groups: the t-test
  • A numeric outcome compared across three or more groups: ANOVA
  • Two categorical variables: chi-square, chisq.test()
  • A numeric outcome and a numeric or dummy-coded predictor: linear regression, such as message length by hour of the day
  • The example: message length is a count, and gaming status has two values, so the t-test

Two Terms From That List

t-test: a test of whether the averages of two groups differ by more than chance would explain. Example: whether gaming and non-gaming chat messages differ in average length.

Categorical variable: a variable whose values are categories rather than measured amounts. Example: gaming status, gaming or non-gaming.

Two Terms From That List (cont.)

  • A test applied to the wrong kind of data produces a meaningless number
  • Unsure? Write your choice down, then click the AURA Lab R Companion icon on VS Code’s left edge
    • Under Test, click Check my test choice
    • It says whether the choice fits these rules, and it does not choose for you

Exercise: State the Null

  • The research question asks whether gaming and non-gaming channels differ in message length
  • What is the null hypothesis for this question, in one sentence?

Running the t-test

  • The R Companion writes this same code: in its panel, under Test, Welch t-test with Cohen’s d
  • cohens_d() comes from the effectsize package, loaded in the worksheet’s settings block
analysis %>%
  filter(!is.na(message_length), !is.na(is_gaming)) %>%
  group_by(is_gaming) %>%
  summarize(n = n(),
            mean = mean(message_length),
            sd = sd(message_length))

t.test(message_length ~ is_gaming, data = analysis)
cohens_d(message_length ~ is_gaming, data = analysis)

Reading the Output

# A tibble: 2 × 4
  is_gaming     n  mean    sd
  <lgl>     <int> <dbl> <dbl>
1 FALSE      3457  33.7  60.7
2 TRUE      31305  28.5  38.5
  • FALSE is non-gaming and TRUE is gaming, in the order R sorts them
  • n is how many messages each group has; mean is the average length, 33.70 against 28.49, a gap of 5.21
  • sd is the spread: 60.68 against 38.47, so the two groups do not vary by the same amount
  • Older worksheets called run_t_test(); v2v keeps it as a deprecated shortcut, and this is the code you write

Reading the Output (cont.)

    Welch Two Sample t-test

data:  message_length by is_gaming
t = 4.9418, df = 3768.7, p-value = 8.077e-07
alternative hypothesis: true difference in means between group FALSE and group TRUE is not equal to 0
95 percent confidence interval:
 3.144345 7.280115
sample estimates:
mean in group FALSE  mean in group TRUE
           33.70234            28.49011

What the p-value Means

p-value: how likely a gap at least this big would be if the null hypothesis were true. Below .05 is called statistically significant.

  • If the two groups truly had the same mean length,
    • a gap this large would appear in fewer than one sample in a thousand
    • so the data are surprising if nothing is going on
  • A p-value below .05 counts as statistically significant, by convention
    • It is a convention, not a law: .049 and .051 are the same strength of evidence

What the p-value Does Not Mean

  • It is not the probability that the null hypothesis is true
  • It is not the probability that your result is a fluke
  • It is not a measure of how large the difference is
  • It is not a measure of whether the difference matters

A small p says the gap is probably not zero. It says nothing about the gap’s size.

Reading the t-test Line

  • It says Welch: the ordinary t-test assumes equal spread, and 60.68 against 38.47 is not equal, so R drops that assumption
  • t = 4.9418 measures the gap in units of its own uncertainty
    • Its sign shows direction only: list the groups the other way around and t is -4.94
  • df = 3768.7 is the Welch degrees of freedom, computed from both groups, so not a round number
  • p-value = 8.077e-07 is scientific notation for 0.0000008077, reported as p < .001
  • The 95 percent confidence interval says the true gap most likely sits between 3.14 and 7.28 characters

How Big Is the Difference

Effect size: how big a difference is, on a scale that does not grow with the sample. Example: Cohen’s d of -0.13 for gaming against non-gaming message length.

  • To report size you need an effect size
    • For a gap between two means, the effect size is Cohen’s d
    • It states the gap in units of the natural variation in the data

Reading the Effect Size

Cohen's d |       95% CI
------------------------
0.13      | [0.09, 0.16]

- Estimated using pooled SD.
  • Cohen’s d measures the gap against how much message lengths vary: 0.13, with an interval from 0.09 to 0.16
  • By Cohen’s benchmarks (0.2 small, 0.5 medium, 0.8 large), 0.13 does not reach small: negligible
  • d follows the group order too: 0.13 with non-gaming first, as printed, and -0.13 with gaming first, as reported
  • Missing values detected. NAs dropped. is a warning, not an error: the unlabeled messages were left out

A Difference Can Be Real and Trivial at Once

  • Five characters is fifteen percent of the non-gaming mean
    • Against standard deviations of 38 and 61 characters, five is a ripple
  • The gap was significant because the sample was large: 34,762 messages
    • A large enough sample makes almost any gap significant

Significance and size are different questions, and big samples pull them apart.

Describe the Gap Again

  • Non-gaming chat averaged 33.70 characters per message, and gaming chat averaged 28.49
  • The test reported p < .001, and the effect size is Cohen’s d = 0.13
  • Which is the best description of the five-character gap now?
    • A. Real and important
    • B. Real, but too small to matter
    • C. Possibly just chance
    • D. Impossible to judge from two averages

More Than Two Groups: ANOVA

four_games <- analysis %>%
  filter(main_game == "Fortnite" |
         main_game == "Hearthstone" |
         main_game == "Just Chatting" |
         main_game == "League of Legends")

fit_anova <- aov(message_length ~ main_game,
                 data = four_games)
summary(fit_anova)
eta_squared(fit_anova)
  • main_game is the channel’s usual category; four means, so no t-test
  • Four groups two at a time is six tests, each with its own false-positive chance; ANOVA runs one
  • aov() fits, summary() prints the table, eta_squared() adds the effect size

Reading the Output

               Df   Sum Sq Mean Sq F value Pr(>F)
main_game       3   547281  182427   92.26 <2e-16 ***
Residuals   13429 26552407    1977
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  • Df is 3 for the groups (four minus one) and 13429 for the messages left over
  • F value compares the variation between the four means against the variation within the groups; if all four shared one mean, F would sit near one. Here it is 92.26
  • Pr(>F) is the p-value: <2e-16 means smaller than R will print, so p < .001
  • The *** is R’s star code for how small the p-value is; the Signif. codes line is its key

Reading the Effect Size

For one-way between subjects designs, partial eta squared is equivalent
  to eta squared. Returning eta squared.
# Effect Size for ANOVA

Parameter | Eta2 |       95% CI
-------------------------------
main_game | 0.02 | [0.02, 1.00]

- One-sided CIs: upper bound fixed at [1.00].
  • The effect size for ANOVA is eta-squared, here 0.02: game category accounts for about two percent of the variation in message length
  • The difference is real, and small
  • The upper bound of 1.00 is the package’s one-sided convention, not a finding; the first two lines are a note, not an error

Report This Result

  • The ANOVA compared message length across four game categories
  • It reported F = 92.26 and p < .001
  • Its effect size, eta-squared, is 0.02
  • In one sentence, how would you report this result to a reader?
  • Check your sentence: the model in Chapter 13

From a Difference to a Model

fit_lm <- lm(message_length ~ is_gaming, data = analysis)
summary(fit_lm)
  • lm() fits the model and summary() prints it; the Coefficients table is the part to read
  • (Intercept), 33.70, is the non-gaming mean, the baseline
  • is_gamingTRUE, -5.21, is the slope: R made gaming status a dummy variable (0 for non-gaming, 1 for gaming), so the slope is exactly the gap the t-test found
  • A regression is a baseline plus an adjustment

From a Difference to a Model (cont.)

Call:
lm(formula = message_length ~ is_gaming, data = analysis)

Residuals:
   Min     1Q Median     3Q    Max
-32.70 -21.49 -12.49   6.30 471.51
  • Call repeats the model you asked for
  • Residuals summarize how far each message falls from its group’s mean; they are not the part to read

From a Difference to a Model (cont.)

Coefficients:
              Estimate Std. Error t value Pr(>|t|)
(Intercept)    33.7023     0.7010  48.077  < 2e-16 ***
is_gamingTRUE  -5.2122     0.7387  -7.056 1.75e-12 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  • Estimate is the number to report on each row: the baseline, then the adjustment
  • Pr(>|t|) is each row’s p-value; 1.75e-12 reads as p < .001

From a Difference to a Model (cont.)

Residual standard error: 41.22 on 34760 degrees of freedom
  (505 observations deleted due to missingness)
Multiple R-squared:  0.00143,   Adjusted R-squared:  0.001401
F-statistic: 49.79 on 1 and 34760 DF,  p-value: 1.747e-12
  • Multiple R-squared: 0.00143 says gaming status accounts for about a tenth of a percent of the variation
  • 505 observations deleted are the unlabeled messages
  • The R Companion’s Linear regression (in its panel, under Test) writes these lines with predictor, a number (the hour of the day), as in your worksheet

Checkpoint

You should now be able to:

  • Name the test your question calls for, and say why
  • Run t.test() and cohens_d() and read the means, the difference, p, and d
  • Say what a p-value does and does not claim
  • Report a significant result that is negligible in size, without inflating it
  • Say that aov() handles three or more groups, and that lm() generalizes both tests

Before Next Time

  • Due Friday, November 20, 11:59 PM: Describing Data in R, 75 points
  • Due Friday, December 4, 11:59 PM: Inferencing Data in R, 100 points
  • Thursday is a lab: you fill the Inferencing Data worksheet with help in the room
    • Write its four hypotheses before Thursday
    • Check that data/twitch_analysis.RDS is in your workspace’s data folder
  • Step by step: Run a Test and Check Your Test Choice, with its walkthrough video
  • Re-read the Chapter 13 section on significance against importance
  • Thursday the White Paper (250 points) is assigned

Questions, Comments, or Concerns?