Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.