1 | LCO.CI(n, level, dp)
|
n |
|
level |
|
dp |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (n, level, dp)
{
iter <- 10^(dp + 1)
p <- seq(0, 0.5, 1/iter)
cpf.matrix <- matrix(NA, ncol = 3, nrow = iter + 1)
colnames(cpf.matrix) <- c("p", "low", "upp")
for (i in 1:(iter/2 + 1)) {
p <- (i - 1)/iter
bin <- dbinom(0:n, n, p)
x <- 0:n
pmf <- cbind(x, bin)
pmf <- pmf[order(-pmf[, 2], pmf[, 1]), ]
pmf <- data.frame(pmf)
m.row <- min(which((cumsum(pmf[, 2]) >= level) == TRUE))
low.val <- min(pmf[1:m.row, ][, 1])
upp.val <- max(pmf[1:m.row, ][, 1])
cpf.matrix[i, ] <- c(p, low.val, upp.val)
if (i != iter/2 + 1) {
n.p <- 1 - p
n.low <- n - upp.val
n.upp <- n - low.val
cpf.matrix[iter + 2 - i, ] <- c(n.p, n.low, n.upp)
}
}
diff.l <- c(99, diff(cpf.matrix[, 2], differences = 1))
if (min(diff.l) == -1) {
for (i in which(diff.l == -1)) {
j <- min(which(diff.l == 1)[which(diff.l == 1) >
i])
new.low <- cpf.matrix[j, 2]
new.upp <- cpf.matrix[j, 3]
cpf.matrix[i:(j - 1), 2] <- new.low
cpf.matrix[i:(j - 1), 3] <- new.upp
}
pointer.1 <- iter - (j - 1) + 2
pointer.2 <- iter - i + 2
cpf.matrix[pointer.1:pointer.2, 2] <- n - new.upp
cpf.matrix[pointer.1:pointer.2, 3] <- n - new.low
}
ci.matrix <- matrix(NA, ncol = 3, nrow = n + 1)
rownames(ci.matrix) <- c(rep("", nrow(ci.matrix)))
colnames(ci.matrix) <- c("x", "lower", "upper")
if (n%%2 == 1)
x.limit <- n%/%2
if (n%%2 == 0)
x.limit <- n/2
for (x in 0:x.limit) {
num.row <- nrow(cpf.matrix[(cpf.matrix[, 2] <= x & x <=
cpf.matrix[, 3]), ])
low.lim <- round(cpf.matrix[(cpf.matrix[, 2] <= x & x <=
cpf.matrix[, 3]), ][1, 1], digits = dp)
upp.lim <- round(cpf.matrix[(cpf.matrix[, 2] <= x & x <=
cpf.matrix[, 3]), ][num.row, 1], digits = dp)
ci.matrix[x + 1, ] <- c(x, low.lim, upp.lim)
n.x <- n - x
n.low.lim <- 1 - upp.lim
n.upp.lim <- 1 - low.lim
ci.matrix[n.x + 1, ] <- c(n.x, n.low.lim, n.upp.lim)
}
heading <- matrix(NA, ncol = 1, nrow = 1)
heading[1, 1] <- paste("LCO Confidence Intervals for n = ",
n, " and Level = ", level, sep = "")
rownames(heading) <- c("")
colnames(heading) <- c("")
ci.matrix
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.