Two sample \(t\) test example using nycflights13 flights data

Chester Ismay

2019-11-18

Note: The type argument in generate() is automatically filled based on the entries for specify() and hypothesize(). It can be removed throughout the examples that follow. It is left in to reiterate the type of generation process being performed.

Data preparation

library(nycflights13)
library(dplyr)
library(stringr)
library(infer)
set.seed(2017)
fli_small <- flights %>% 
  sample_n(size = 500) %>% 
  mutate(half_year = case_when(
    between(month, 1, 6) ~ "h1",
    between(month, 7, 12) ~ "h2"
  )) %>% 
  mutate(day_hour = case_when(
    between(hour, 1, 12) ~ "morning",
    between(hour, 13, 24) ~ "not morning"
  )) %>% 
  select(arr_delay, dep_delay, half_year, 
         day_hour, origin, carrier)

One numerical variable, one categorical (2 levels)

Calculate observed statistic

The recommended approach is to use specify() %>% calculate():

## Warning: Removed 9 rows containing missing values.
The observed \(t\) statistic is
stat
0.2766

.

Or using t_test in infer

The observed \(t\) statistic is 0.2766.

Or using another shortcut function in infer:

The observed \(t\) statistic is 0.2766.

Randomization approach to t-statistic

## Warning: Removed 9 rows containing missing values.

Calculate the randomization-based \(p\)-value

p_value
0.806

Theoretical distribution

## Warning: Removed 9 rows containing missing values.
## Warning: Check to make sure the conditions have been met for the
## theoretical method. {infer} currently does not check these for you.

Overlay appropriate \(t\) distribution on top of permuted t-statistics

## Warning: Check to make sure the conditions have been met for the
## theoretical method. {infer} currently does not check these for you.

Compute theoretical p-value

## [1] 0.7822