Last active
December 7, 2018 18:09
-
-
Save rudeboybert/b5dde565a50df08571dabb6ec53ab58e to your computer and use it in GitHub Desktop.
Permutation hypothesis test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(infer) | |
africa_results <- | |
"https://rudeboybert.github.io/SDS220/static/PS/africa_results.csv" %>% | |
read_csv() %>% | |
select(priming_number, how_many_countries) | |
View(africa_results) | |
# What actually happened! Observed difference | |
obs_diff <- africa_results %>% | |
specify(formula = how_many_countries ~ priming_number) %>% | |
calculate(stat = "diff in means", | |
order = c("Primed with 14 countries", "Primed with 94 countries")) | |
obs_diff | |
# Shuffle! | |
africa_results %>% | |
specify(formula = how_many_countries ~ priming_number) %>% | |
hypothesize(null = "independence") %>% | |
generate(reps = 1) | |
# Build null distribution | |
null_distribution <- africa_results %>% | |
specify(formula = how_many_countries ~ priming_number) %>% | |
hypothesize(null = "independence") %>% | |
generate(reps = 1000) %>% | |
calculate(stat = "diff in means", order = c("Primed with 14 countries", "Primed with 94 countries")) | |
# Plot! | |
ggplot(null_distribution, aes(x=stat)) + | |
geom_histogram() | |
null_distribution %>% | |
visualize(obs_stat = obs_diff, direction = "both") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment