I have an R function called CI.d that produces 95% Confidence Interval for a statistic called d. To generate some random d given (TRUE) d = .5, and sample size = 20, we can use rt(), because t = d * sqrt(N):
N = 20 ; df = N - 1 ; d = .5 ; ncp = d*sqrt(N)
random.d = rt(1e4, df, ncp)/sqrt(N)
Now my question is how can I test that my CI.d function below, relatively provides a 95% coverage for (TRUE) d in the long-run in R?
CI.d <- function(d, N){
df = N - 1 ; t = d*sqrt(N)
f <- function (ncp, alpha, q, df) {
abs(suppressWarnings(pt(q = t, df = df, ncp, lower.tail = FALSE)) - alpha)
}
CI = sapply(c(0.025, 0.975),
function(x) optim(1, f, alpha = x, q = t, df = df, control = list(reltol = (.Machine$double.eps)))[[1]]/sqrt(N))
return(CI)
}
CI.d(d = .5, N = 20)
Aucun commentaire:
Enregistrer un commentaire