我有一个函数可以计算给定样本大小的特定置信区间所需的最小简单大小,以及基于给定置信水平的给定误差范围。
x<-c(1,-2,3,-4,5,-6,7,-8,9,-8,9,-8,7,-6,5,-4,3,-2,1,-0,1,-2,3,-4,5,-6,7,-8,9,-0,1,-2,3,-4,5,-6,7,-8,9,0,1,-2,3,-4,5,-6,7,-8,9)
T <-sum( x > 0 )/length(x)
sample.size = function(c.lev, margin=T,
c.interval=.1, population) {
z.val = qnorm(T+c.lev/ 200)
ss = (z.val^2 * margin * (1-margin))/c.interval^2
p.ss = round((ss/(1 + ((ss-1)/population))), digits=0)
METHOD = paste("Recommended sample size for a population of ",
population, " at a ", c.lev,
"% confidence level", sep = "")
structure(list(Population = population,
"Confidence level" = c.lev,
"Margin of error" = c.interval,
"Response distribution" = margin,
"Recommended sample size" = p.ss,
method = METHOD),
class = "power.htest")
}
e<-sample.size(y, , , length(x))
我想循环e
获取y
(置信度)的范围并选择产生最高置信度的值,按100从0到0排序并选择(停止循环)在产生y
并选择该值的y
值之前的置信度e[[5]]!= NaN
值。
谢谢