Created
December 11, 2018 18:52
-
-
Save emhart/ca29f6d428b473e9d3040c2b28d72eda to your computer and use it in GitHub Desktop.
Simple fair coin toss code
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(ggplot2) | |
### Imagine a coin is flipped 100 times | |
### You get 60 heads | |
### How can you assess if the coin is fair or not? | |
# Set up the plot | |
binom_density <- dbinom(0:100,100,.5) | |
flips <- data.frame(flip_count = 0:100, density = binom_density) | |
# Make the plot | |
ggplot(flips,aes(x = flip_count, y = density)) + geom_path() + | |
theme_bw() + geom_rect(xmin = 60, xmax = 100, ymin = 0 ,ymax = .09, fill = 'red', alpha = .005) | |
# Get the probability of getting 60 or more heads in 100 flips given a fair coin | |
sum(dbinom(60:100,100,.5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment