By the end of today you will…
Download this application exercise by pasting the code below into your console
download.file("https://sta101.github.io/static/appex/ae12.Rmd",
destfile = "ae12.rmd")
library(tidyverse)
library(tidymodels)
Last time we asked if a coin was fair. We let \(p\) be the probability of landing heads. Fundamentally, we were interested in whether or not \(p = 0.5\). This was our null hypothesis:
\[ H_0: p = .5 \]
and our alternative, was that the coin was biased heads: \[ H_A: p > 0.5 \]
In one example our data consisted of 30 coin flips and 26 heads. The proportion of heads that we observed, \(\hat{p} = 26/30 = .87\).
Do these 30 coin flips give us enough evidence to reject the null in favor of the alternative?
To answer this question, we computed the p-value: \(Pr(\hat{p} \geq .87 | H_0 \text{ true})\). In words, the probability that our statistic of interest, (\(\hat{p}\)), is greater than or equal to what we saw given that the null is true.
Notice that the p-value is defined by three things:
We compared the p-value to some pre-defined cutoff, \(\alpha\). In our example we set our cutoff at \(\alpha = 0.05\). If p-value \(< \alpha\), we reject the null. If p-value \(> \alpha\), we fail to reject the null.
coin_flips = read_csv("https://sta101.github.io/static/appex/data/coin_flips.csv")
## Rows: 30 Columns: 1
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): one_flip
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
set.seed(2022)
null_dist =
coin_flips %>%
specify(response = one_flip, success = "H") %>%
hypothesize(null = "point", p = 0.5) %>%
generate(reps = 10000, type = "draw") %>%
calculate(stat = "prop")
obs_stat = 26/30
visualize(null_dist) +
shade_p_value(obs_stat, direction = "right")
null_dist %>%
get_p_value(obs_stat, direction = "right")
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.0001
p-value of \(1 \times 10^{-4}\) < 0.05. We reject the null that the coin is fair.
In general, if we want to test proportion we can do so this way.
push_pull = read_csv("https://sta101.github.io/static/appex/data/push_pull.csv")
push_pull %>%
slice(1:3, 24:26)
## # A tibble: 6 × 7
## participant_id age push1 push2 pull1 pull2 training
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 1 41 41 45 16 17 density
## 2 2 32 35 44 9 11 density
## 3 3 44 33 38 10 11 density
## 4 24 36 31 60 9 15 gtg
## 5 25 50 35 42 9 12 gtg
## 6 26 34 23 39 9 13 gtg
The push_pull
dataset comes from a “mini study” by mountain
tactical institute.
26 individuals completed 1 of 2 exercise regiments for 3.5 weeks to increase their pushups and pullups. Codebook below:
participant_id
: unique identifier for each
participantage
: age of participantpush1/2
: push-ups at beginning and end of program
respectivelypull1/2
: pull-ups at beginning and end of program
respectivelytraining
: which training protocol the individual
participated inpush_pull = push_pull %>%
mutate(
pct_push_inc = (push2 / push1 ) - 1,
pct_pull_inc = (pull2 / pull1) - 1)
Hypothesis: “Most people who train consistently will see at least a 15% increase in push-ups over a 3.5 week training period.”
Breaking it down:
What’s the null?
“will see at least a 15% increase”. Each person either increases by 15% over 3.5 weeks or does not. This is our binary outcome.
create a new column called over_15pct
that tells you
whether or not an individual achieved at least a 15% increase in
push-ups
# code here
Write the null and alternative in mathematical notation.
What is the observed statistic? Compute and write it in mathematical notation.
Next, simulate under the null and compute the p-value. State your conclusion with \(\alpha = 0.05\). As a bonus, visualize the null distribution and shade in the p-value.
Hypothesis: “The mean age of push-up/pull-up training participants is greater than 30”.
Let’s investigate this hypothesis with a significance level \(\alpha = 0.01\).
Write down the null and alternative hypotheses in words and mathematical notation
What is the observed statistic? Write it in mathematical notation.
Bootstrapping does the following…
# find observed statistic
obs_mean_age = push_pull %>%
drop_na(age) %>%
summarize(meanAge = mean(age)) %>%
pull()
# subtract observed_mean - desired_mean from age
age_and_null = push_pull %>%
select(age) %>%
drop_na(age) %>%
mutate(nullAge = age - (obs_mean_age - 30))
# show data frame
age_and_null
## # A tibble: 25 × 2
## age nullAge
## <dbl> <dbl>
## 1 41 35.8
## 2 32 26.8
## 3 44 38.8
## 4 37 31.8
## 5 37 31.8
## 6 21 15.8
## 7 33 27.8
## 8 38 32.8
## 9 49 43.8
## 10 33 27.8
## # … with 15 more rows
# show the means of each column
age_and_null %>%
summarize(meanAge = mean(age),
mean_nullAge = mean(nullAge))
## # A tibble: 1 × 2
## meanAge mean_nullAge
## <dbl> <dbl>
## 1 35.2 30
If we take bootstrap samples from this new nullAge
column, we are sampling from data with the same variability as our
original data, but a different mean. This is a nice way to explore the
null!
set.seed(3)
# simulate null
null_dist = push_pull %>%
specify(response = age) %>%
hypothesize(null = "point", mu = 30) %>%
generate(reps = 10000, type = "bootstrap") %>%
calculate(stat = "mean")
# get observed statistic
obs_stat = obs_mean_age
p_value = null_dist %>%
get_p_value(obs_stat, direction = "right")
p_value
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0.0001
Say we are interested in the performance of trainees at this particular facility and the sample is representative of the population.
Hypothesis: The median number of pull-ups trainees can perform is less than 20 even after training for 3.5 weeks.
Write down the null and alternative hypothesis in mathematical notation.
Write down the observed statistic. Simulate under the null and compute the p-value. Finally, visualize and interpret the p-value in context.
Two exercise regimes:
We want to know, is the average pull-up percent increase of a gtg trainee significantly different than a density trainee?
Fundamentally, does the categorical variable training
affect the average percentage increase in pull-ups?
State the null hypothesis:
\[ \mu_d = \mu_{gtg} \]
\[ H_0: \mu_d - \mu_{gtg} = 0 \]
What we want to do to simulate data under this null:
random_training = sample(push_pull$training, replace = FALSE)
push_pull %>%
select(pct_pull_inc) %>%
mutate(random_training = random_training)
# code here
Simulating under the null and computing the p-value:
sim_num = 10000
set.seed(1)
# simulate null
null_dist = push_pull %>%
specify(response = pct_pull_inc, explanatory = training) %>%
hypothesize(null = "independence") %>%
generate(reps = sim_num, type = "permute") %>%
calculate(stat = "diff in means", order = c("density", "gtg"))
# observed statistic
obs_stat = .196 - .489
# visualize / get p
visualize(null_dist) +
shade_p_value(obs_stat, direction = "both")
Compute the p-value and state your conclusion with \(\alpha = 0.05\)
generate()
optionsdraw
bootstrap
permute