library(dplyr)
library(mosaic)
library(ggplot2)
library(readr)
# Set seed value
set.seed(76)
# Note you will need to change this line to whatever loads the data for you:
grades <- read_csv("../PS/grades.csv")
Both the following get the job done, but when using the boxplot:
ggplot(data=grades, aes(x=even_vs_odd, y=final)) +
geom_boxplot()
ggplot(data=grades, aes(x=final)) +
geom_histogram(binwidth = 0.25) +
facet_wrap(~even_vs_odd)
No clear “slam-dunk” winner in my opinion! But really? # of letters in your last name?!?
When demonstrating that the long code and the wrapper function code do the same thing when randomization is involved, we need to set the seed value to get replicable random results. 76 was an arbitrary choice of seed. Choose your favorite number.
# The true observed difference in averages i.e. the observed test statistic
observed_diff <- mean(final ~ even_vs_odd, data=grades) %>% diff()
# Simulate the null distribution of the test statistic:
simulations <- do(10000) * mean(final ~ shuffle(even_vs_odd), data=grades)
simulations <- simulations %>%
as_data_frame() %>%
mutate(difference=odd-even)
# Compare what we observed (red) to what happens if we assume no difference:
ggplot(data=simulations , aes(x=difference)) +
geom_histogram() +
geom_vline(xintercept = observed_diff, col="red") +
geom_vline(xintercept = 0, linetype="dashed") +
labs(x="Difference: Odd Avg - Even Avg")
Observing a difference in means of -0.073 still seems somewhat plausible. Also, not where it is centered: 0 i.e. no difference!
The p-value is the probability of observing a test statistic just as or more extreme than the one observed. In our case since \(H_A: \mu_{odd} - \mu_{even} \neq 0\): i.e. there is a difference, we can have either
simulations %>%
mutate(more_extreme_left = difference <= observed_diff) %>%
summarise(more_extreme_left = sum(more_extreme_left))
## # A tibble: 1 × 1
## more_extreme_left
## <int>
## 1 1248
There are 1248 values less than the observed difference of -0.073. So the p-value is
\[ \frac{2 \times 1248 + 1}{10000 + 1} = 0.250 \] Notes:
We can only falsify the null hypothesis, never prove that it’s true.
Analogy: Criminal justice system has two possible verdicts: