R中置信区间的仿真 [英] Simulation for Confidence interval in R
本文介绍了R中置信区间的仿真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个R函数,可为t分布的 ncp (非中心参数)提供95%的置信区间.
I have an R function that provides the 95% confidence Interval for the ncp (non-centrality parameter) of a t distribution.
通过R中的仿真,有可能表明,从长远来看,该R函数的CI会捕获给定的 TRUE ncp (此处的"2"与输入 t )95%的时间?
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])
推荐答案
问题是,我们需要在CI.ncp
中提供从fun
获得的随机ncp
:
The problem is the that we need to feed the random ncp
obtained from the fun
in the CI.ncp
:
fun <- function(t = 2, N = 20){ ;
ncp = rt(1, N - 1, t);
CI.ncp(t = ncp, N = 20);
}
R <- 1e4 ;
sim <- t(replicate(R, fun()));
coverage <- mean(sim[,1] <= 2 & 2 <= sim[,2])
这篇关于R中置信区间的仿真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文