Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created August 21, 2019 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarddmorey/94e1cac1bd20a116747d3728ad8a8862 to your computer and use it in GitHub Desktop.
Save richarddmorey/94e1cac1bd20a116747d3728ad8a8862 to your computer and use it in GitHub Desktop.
CI "precision" reversal
desired_precision = .6
N1 = 5
N2 = 6
# Function to compute CDFs of "precisions" (CI widths)
SS_func = function(W, n, alpha = .05){
n * (n - 1) * W^2 / (4 * qt(1-alpha/2, n - 1)^2)
}
# Show theoretical CDFs
curve( pchisq(SS_func(x, N1), N1 - 1), 0, 6, 256, col = "blue")
curve( pchisq(SS_func(x, N2), N2 - 1), 0, 6, 256, col = "red", add = TRUE)
# Confirm through simulation
widths_n1 = replicate(100000, {
diff(t.test(rnorm(N1))$conf.int)
})
widths_n2 = replicate(100000, {
diff(t.test(rnorm(N2))$conf.int)
})
## Add simulations to plots
## Should exactly overlay theoretical curves
plot(ecdf(widths_n1), add = TRUE, col = "blue", lwd = 2)
plot(ecdf(widths_n2), add = TRUE, col = "red", lwd = 2)
# Zoom in
curve( pchisq(SS_func(x, N1), N1 - 1), 0, 2*desired_precision, 256, col = "blue")
curve( pchisq(SS_func(x, N2), N2 - 1), 0, 2*desired_precision, 256, col = "red", add = TRUE)
abline(v = desired_precision)
## Notice that the blue line (smaller N) is *above* the red line (larger N) at
## the desired precision, indicating that there is a greater probability that
## the "precision" (CI width) is smaller than the desired precision is
## *larger* for for the smaller N.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment