llprevr <- function(p, yes=c(0), no=c(0)) { sumcheck <- sum(yes) + sum(no) if (sumcheck == 0) stop("Data must be entered for yes or no") if (sum(yes) == 0) { llmck <- sum(no) * log(1 - p) } else { llmck <- sum(log(1 - (1 - p)^yes)) + sum(no) * log(1 - p) } llmck } dprev <- function(yes=c(0),no=c(0),disp='y',conf=.95) { sumcheck <- sum(yes) + sum(no) if (sumcheck == 0) stop("Data must be entered for yes or no") if (sum(yes) == 0) { ucl <- 1 -exp(-qchisq(conf,1)/(2*sum(no)) ) result <- c(0., 0., ucl) if (disp == 'y') print("Lower 95% limit, MLE, Upper 95% limit = ") result } else if (sum(no) == 0) { tfct <- function(p) { sum(log(1 - (1 - p)^yes)) + qchisq(conf,1)/2 } lcl <- uniroot(tfct, interval = c(0.0001, 1.)) result <- c(lcl$root, 1., 1.) if (disp == 'y') print("Lower 95% limit, MLE, Upper 95% limit = ") result } else { #print(yes) ; #print(no) llpmax <- optimize(llprevr, c(0., 1.), maximum = TRUE, yes = yes, no = no ) #print(llpmax) mval <- llprevr(llpmax$maximum,yes = yes, no = no) tfct <- function(p) { llprevr(p, yes, no) - (mval - qchisq(conf,1)/2) } lcl <- uniroot(tfct, interval = c(0.00000001, llpmax$maximum)) ucl <- uniroot(tfct, interval = c(llpmax$maximum, 0.99999999)) result <- c(lcl$root, llpmax$maximum, ucl$root) if (disp == 'y') print("Lower 95% limit, MLE, Upper 95% limit = ") result } }