I have an R function that provides the 95% confidence Interval for the *ncp* (non-centrality parameter) of a t distribution.

Via simulation in R, is it possible to show that in the long-run the CIs from this R function capture a given *TRUE ncp* (here "2" same as input *t*) 95% of the time? 

(I appreciate any ideas as to how to do this)

```
CI.ncp <- function(t, N){

  f <- function (ncp, alpha, q, df) {  
abs(suppressWarnings(pt(q = t, df = N - 1, ncp, lower.tail = FALSE)) - alpha) }

sapply(c(0.025, 0.975),
function(x) optim(1, f, alpha = x, q = t, df = N - 1, control = list(reltol = (.Machine$double.eps)))[[1]]) }

#Example of Use:
CI.ncp(t = 2, N = 20) # gives: -0.08293755  4.03548862 

#(in the long-run 95% of the time, "2" is contained within these
# two numbers, how to show this in R?)

```

Here is what I have tried with no success:

```
fun <- function(t = 2, N = 20){

  ncp = rt(1, N - 1, t)
  CI.ncp(t = 2, N = 20)
  mean(ncp <= 2 & 2 <= ncp )
   }

 R <- 1000
 sim <- t(replicate(R, fun()))
 coverage <- mean(sim[,1] <= 2 & 2 <= sim[,2])

```