|
12 | 12 | #' @export |
13 | 13 | #' |
14 | 14 | #' @examples |
15 | | -#' binomnorm(10, 20, 0.5, "two.sided") |
16 | | -binomnorm <- function(k, n, prob, direction) { |
| 15 | +#' iscambinomnorm(k = 10, n = 20, prob = 0.5, direction = "two.sided") |
| 16 | +iscambinomnorm <- function(k, n, prob, direction) { |
17 | 17 | withr::local_par(mar = c(5, 3, 1, 1)) |
18 | 18 |
|
19 | 19 | thisx <- 0:n |
@@ -221,7 +221,14 @@ binomnorm <- function(k, n, prob, direction) { |
221 | 221 | #' @export |
222 | 222 | #' |
223 | 223 | #' @examples |
224 | | -binompower <- function(LOS, n, prob1, alternative, prob2 = NULL) { |
| 224 | +#' iscambinompower(LOS = 0.05, n = 20, prob1 = 0.5, alternative = "less") |
| 225 | +#' |
| 226 | +#' iscambinompower(LOS = 0.05, n = 20, prob1 = 0.5, alternative = "greater", prob2 = 0.75) |
| 227 | +#' |
| 228 | +#' iscambinompower(LOS = 0.10, n = 30, prob1 = 0.4, alternative = "two.sided") |
| 229 | +#' |
| 230 | +#' iscambinompower(LOS = 0.10, n = 30, prob1 = 0.4, alternative = "two.sided", prob2 = 0.2) |
| 231 | +iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) { |
225 | 232 | thisx <- 0:n |
226 | 233 | minx <- max( |
227 | 234 | 0, |
@@ -423,14 +430,14 @@ binompower <- function(LOS, n, prob1, alternative, prob2 = NULL) { |
423 | 430 | #' @export |
424 | 431 | #' |
425 | 432 | #' @examples |
426 | | -binomprob <- function(k, n, prob, lower.tail) { |
427 | | - Description <- "iscambinomprob(k, n, prob, lower.tail) \n This function calculates tail probabilities from the binomial distribution.\r |
428 | | -k is the number of successes of interest (must be integer), n and prob are the number of trials and success probability \n lower.tail is a Boolean for finding the probability above (FALSE) or below (TRUE) the inputted value (inclusive)" |
429 | | - # TODO Stop if probability is wrong |
430 | | - |
431 | | - if (as.character(k) == "?") { |
432 | | - stop(Description) |
| 433 | +#' iscambinomprob(k = 5, n = 20, prob = 0.4, lower.tail = TRUE) |
| 434 | +#' iscambinomprob(k = 15, n = 30, prob = 0.3, lower.tail = FALSE) |
| 435 | +#' iscambinomprob(k = 22, n = 25, prob = 0.9, lower.tail = TRUE) |
| 436 | +iscambinomprob <- function(k, n, prob, lower.tail) { |
| 437 | + if (prob < 0 || prob > 1) { |
| 438 | + stop("Error: `prob` (probability) must be a numeric value between 0 and 1.") |
433 | 439 | } |
| 440 | + |
434 | 441 | withr::local_par(mar = c(4, 3, 2, 2)) |
435 | 442 | thisx <- 0:n |
436 | 443 | minx <- max(0, n * prob - 4 * sqrt(prob * (1 - prob) * n)) |
@@ -499,24 +506,43 @@ k is the number of successes of interest (must be integer), n and prob are the n |
499 | 506 | #' @param alternative "less", "greater", or "two.sided" |
500 | 507 | #' @param conf.level Confidence level for a two-sided confidence interval. |
501 | 508 | #' |
502 | | -#' @return P-value along with a plot of the binomial distribution and/or |
503 | | -#' binomial confidence interval. |
| 509 | +#' @return a list of the p-value along with lower and upper bound for the calculated confidence interval. |
504 | 510 | #' @export |
505 | 511 | #' |
506 | 512 | #' @examples |
507 | | -binomtest <- function( |
| 513 | +#' |
| 514 | +#' iscambinomtest( |
| 515 | +#' observed = 17, |
| 516 | +#' n = 25, |
| 517 | +#' hypothesized = 0.5, |
| 518 | +#' alternative = "greater" |
| 519 | +#' ) |
| 520 | +#' |
| 521 | +#' iscambinomtest( |
| 522 | +#' observed = 12, |
| 523 | +#' n = 80, |
| 524 | +#' hypothesized = 0.10, |
| 525 | +#' alternative = "two.sided", |
| 526 | +#' conf.level = 0.95 |
| 527 | +#' ) |
| 528 | +#' |
| 529 | +#' iscambinomtest( |
| 530 | +#' observed = 0.14, |
| 531 | +#' n = 100, |
| 532 | +#' hypothesized = 0.20, |
| 533 | +#' alternative = "less" |
| 534 | +#' ) |
| 535 | +#' |
| 536 | +#' iscambinomtest(observed = 17, n = 25, conf.level = 0.95) |
| 537 | +#' |
| 538 | +#' iscambinomtest(observed = 12, n = 80, conf.level = c(0.90, 0.95, 0.99)) |
| 539 | +iscambinomtest <- function( |
508 | 540 | observed, |
509 | 541 | n, |
510 | 542 | hypothesized = NULL, |
511 | 543 | alternative, |
512 | 544 | conf.level = NULL |
513 | 545 | ) { |
514 | | - # TODO Better documentation that takes into account all parts of description |
515 | | - Description <- "iscambinomtest(observed, n, hypothesized=NULL, alternative, conf.level=NULL) \n This function performs an exact binomial test and graphs the binomial distribution and/or binomial confidence interval.\n Input the observed number of successes or sample proportion (assumed if value less than one),\n Input n = the sample size and the hypothesized probability of success \n Optional: Input the hypothesized probability of success and form of alternative (\"less\", \"greater\", or \"two.sided\") \n Optional: Input a confidence level (one or more values) for a two-sided confidence interval.\n " |
516 | | - |
517 | | - if (as.character(observed) == "?") { |
518 | | - stop(Description) |
519 | | - } |
520 | 546 | withr::local_par(mar = c(4, 3, 2, 2)) |
521 | 547 |
|
522 | 548 | if (observed < 1) { |
@@ -759,12 +785,12 @@ binomtest <- function( |
759 | 785 | #' @export |
760 | 786 | #' |
761 | 787 | #' @examples |
762 | | -invbinom <- function(alpha, n, prob, lower.tail) { |
763 | | - Description <- "iscaminvbinom(alpha, n, prob, lower.tail) \n This function calculates the binomial quantile of a specified probability. \n Input the desired probability and the parameters of the binomial distribution. \n Specify whether you want this is an upper tail (FALSE) or lower tail (TRUE) \n The integer that achieves at most the stated probability will be returned." |
764 | | - |
765 | | - if (as.character(alpha) == "?") { |
766 | | - stop(Description) |
767 | | - } |
| 788 | +#' iscaminvbinom(alpha = 0.05, n = 30, prob = 0.5, lower.tail = TRUE) |
| 789 | +#' |
| 790 | +#' iscaminvbinom(alpha = 0.05, n = 30, prob = 0.5, lower.tail = FALSE) |
| 791 | +#' |
| 792 | +#' iscaminvbinom(alpha = 0.01, n = 60, prob = 0.10, lower.tail = FALSE) |
| 793 | +iscaminvbinom <- function(alpha, n, prob, lower.tail) { |
768 | 794 | withr::local_par(mar = c(4, 3, 2, 2)) |
769 | 795 |
|
770 | 796 | thisx <- 0:n |
|
0 commit comments