Skip to content

Instantly share code, notes, and snippets.

@mcohen01
Created November 9, 2017 12:50
Show Gist options
  • Save mcohen01/62719b8b6254b1dcad15ab2be48ea75d to your computer and use it in GitHub Desktop.
Save mcohen01/62719b8b6254b1dcad15ab2be48ea75d to your computer and use it in GitHub Desktop.
library(ggplot2)
observations <- 5
mu = 10
trials <- 100
confidence.interval <- function(threshold = .975, variance = 4) {
x <- rnorm(observations, mean = mu, sd = sqrt(variance))
df <- length(x) - 1
SE <- sd(x) / sqrt(length(x))
c(mean(x) - qt(threshold, df) * SE,
mean(x) + qt(threshold, df) * SE)
}
p <- ggplot()
for (i in seq(trials)) {
ci <- confidence.interval()
if (ci[1] > mu | ci[2] < mu)
col <- 'red'
else
col <- 'green'
p <- p + geom_line(aes_string(c(i, i), ci), colour=col)
}
p + xlab('x') + ylab('interval')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment