mardi 8 août 2017

Simulation for Confidence interval in R

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 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




Aucun commentaire:

Enregistrer un commentaire