Confidence intervals

\(\theta\) is fixed and \(C_n\) is random

\[Pr(\theta \in C_n) \ge 0.95\]


p <- .3
X <- rbinom(10, 1, p)
X
##  [1] 0 0 0 0 1 0 1 1 0 1
mean(X)
## [1] 0.4
# computeCI is a user-defined function
computeCI(X)
## [1] 0.09016133 0.70983867

# Another trial
X <- rbinom(10, 1, 0.3)
X
##  [1] 0 0 0 0 0 1 0 0 0 0
mean(X)
## [1] 0.1
computeCI(X)
## [1] -0.08973666  0.28973666

findInterval(.3, c(.1, 4)) == 1
## [1] TRUE
findInterval(.3, c(.35, 4)) == 1
## [1] FALSE
findInterval(.3, computeCI(rbinom(20, 1, p))) == 1
## [1] TRUE
findInterval(.3, computeCI(rbinom(20, 1, p))) == 1
## [1] TRUE

in_int <- replicate(1e5,
  findInterval(.3, computeCI(rbinom(20, 1, p))) == 1)
head(in_int)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE
mean(in_int)
## [1] 0.94742

N <- 10
p_hat_dist <- replicate(1e4, mean(rbinom(N, 1, p)))
head(p_hat_dist)
## [1] 0.0 0.4 0.1 0.6 0.4 0.6

hist(p_hat_dist, 100, xlim=c(0, 1))


N <- 20
p_hat_dist <- replicate(1e4, mean(rbinom(N, 1, p)))
head(p_hat_dist)
## [1] 0.30 0.25 0.25 0.25 0.35 0.25

hist(p_hat_dist, 100, xlim=c(0, 1))


N <- 100
p_hat_dist <- replicate(1e4, mean(rbinom(N, 1, p)))
head(p_hat_dist)
## [1] 0.35 0.26 0.31 0.29 0.29 0.33

hist(p_hat_dist, 100, xlim=c(0, 1))


qnorm(.5)
## [1] 0
qnorm(.975)
## [1] 1.959964
pnorm(0)
## [1] 0.5
pnorm(1.96)
## [1] 0.9750021

computeCI <- function(X) {
  p_hat <- mean(X)
  n <- length(X)
  p_hat + c(-2, 2) * sqrt(p_hat*(1-p_hat)/n)
}