66# ' @param n number of trials
77# ' @param prob success probability
88# ' @param direction "above", "below", or "two.sided"
9+ # ' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
910# '
1011# ' @return A plot of the binomial distribution overlayed with the normal approximation
1112# '
1213# ' @export
1314# '
1415# ' @examples
1516# ' iscambinomnorm(k = 10, n = 20, prob = 0.5, direction = "two.sided")
16- iscambinomnorm <- function (k , n , prob , direction ) {
17+ iscambinomnorm <- function (k , n , prob , direction , verbose = TRUE ) {
1718 old <- par(mar = c(5 , 3 , 1 , 1 ))
1819 on.exit(par(old ), add = TRUE )
19-
2020 thisx <- 0 : n
2121 phat <- thisx / n
2222 minx <- max(0 , n * prob - 4 * sqrt(prob * (1 - prob ) * n ))
@@ -200,7 +200,9 @@ iscambinomnorm <- function(k, n, prob, direction) {
200200 c(" \n normal approx:" , showprob2 ),
201201 c(" \n normal approx with continuity:" , showprob3 )
202202 )
203- cat(full , " \n " )
203+ if (verbose ) {
204+ cat(full , " \n " )
205+ }
204206}
205207
206208# ' Rejection Region for Binomial
@@ -214,6 +216,7 @@ iscambinomnorm <- function(k, n, prob, direction) {
214216# ' @param prob1 A numeric value representing the first probability
215217# ' @param alternative "less", "greater", or "two.sided"
216218# ' @param prob2 A numeric value representing the second probability
219+ # ' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
217220# '
218221# ' @return A plot of the binomial distribution with the rejection region highlighted.
219222# '
@@ -227,7 +230,14 @@ iscambinomnorm <- function(k, n, prob, direction) {
227230# ' iscambinompower(LOS = 0.10, n = 30, prob1 = 0.4, alternative = "two.sided")
228231# '
229232# ' iscambinompower(LOS = 0.10, n = 30, prob1 = 0.4, alternative = "two.sided", prob2 = 0.2)
230- iscambinompower <- function (LOS , n , prob1 , alternative , prob2 = NULL ) {
233+ iscambinompower <- function (
234+ LOS ,
235+ n ,
236+ prob1 ,
237+ alternative ,
238+ prob2 = NULL ,
239+ verbose = TRUE
240+ ) {
231241 thisx <- 0 : n
232242 minx <- max(
233243 0 ,
@@ -274,7 +284,9 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
274284 pos = 3 ,
275285 col = " red"
276286 )
277- cat(" Null: Probability" , rr , " and below =" , this.prob1 , " \n " )
287+ if (verbose ) {
288+ cat(" Null: Probability" , rr , " and below =" , this.prob1 , " \n " )
289+ }
278290 } else if (alternative == " greater" ) {
279291 rr <- qbinom(LOS , n , prob1 , FALSE ) + 1
280292 this.prob1 <- 1 - pbinom(rr - 1 , n , prob1 )
@@ -287,7 +299,9 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
287299 pos = 3 ,
288300 col = " red"
289301 )
290- cat(" Null: Probability" , rr , " and above =" , this.prob1 , " \n " )
302+ if (verbose ) {
303+ cat(" Null: Probability" , rr , " and above =" , this.prob1 , " \n " )
304+ }
291305 } else if (alternative == " two.sided" ) {
292306 lowerrr <- qbinom(LOS / 2 , n , prob1 ) - 1
293307 upperrr <- qbinom(LOS / 2 , n , prob1 , FALSE ) + 1
@@ -320,7 +334,9 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
320334 pos = 3 ,
321335 col = " red"
322336 )
323- cat(" Null: Probability in rejection region" , showprob1 , " \n " )
337+ if (verbose ) {
338+ cat(" Null: Probability in rejection region" , showprob1 , " \n " )
339+ }
324340 } else {
325341 stop(" Check input for alternative" )
326342 }
@@ -357,7 +373,9 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
357373 pos = 3 ,
358374 col = " red"
359375 )
360- cat(" Alternative: Probability" , rr , " and below =" , this.prob2 , " \n " )
376+ if (verbose ) {
377+ cat(" Alternative: Probability" , rr , " and below =" , this.prob2 , " \n " )
378+ }
361379 } else if (alternative == " greater" ) {
362380 this.prob2 <- 1 - pbinom(rr - 1 , n , prob2 )
363381 showprob2 <- format(this.prob2 , digits = 4 )
@@ -369,7 +387,9 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
369387 pos = 3 ,
370388 col = " red"
371389 )
372- cat(" Alternative: Probability" , rr , " and above =" , this.prob2 , " \n " )
390+ if (verbose ) {
391+ cat(" Alternative: Probability" , rr , " and above =" , this.prob2 , " \n " )
392+ }
373393 } else if (alternative == " two.sided" ) {
374394 this.prob2 <- pbinom(lowerrr , n , prob2 ) +
375395 pbinom(upperrr - 1 , n , prob2 , FALSE )
@@ -396,7 +416,9 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
396416 pos = 3 ,
397417 col = " red"
398418 )
399- cat(" Alternative: Probability in rejection region" , this.prob2 , " \n " )
419+ if (verbose ) {
420+ cat(" Alternative: Probability in rejection region" , this.prob2 , " \n " )
421+ }
400422 }
401423 newtitle <- substitute(
402424 paste(" Binomial (" , n == x1 , " , " , pi == x2 , " ) - alternative" , ),
@@ -417,6 +439,7 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
417439# ' @param n number of trials.
418440# ' @param prob success probability. Numeric between 0 & 1.
419441# ' @param lower.tail Boolean for finding the probability above (FALSE) or below (TRUE) the inputted value (inclusive)
442+ # ' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
420443# '
421444# ' @return The probability of the binomial distribution along with a graph of the distribution.
422445# ' @export
@@ -425,7 +448,7 @@ iscambinompower <- function(LOS, n, prob1, alternative, prob2 = NULL) {
425448# ' iscambinomprob(k = 5, n = 20, prob = 0.4, lower.tail = TRUE)
426449# ' iscambinomprob(k = 15, n = 30, prob = 0.3, lower.tail = FALSE)
427450# ' iscambinomprob(k = 22, n = 25, prob = 0.9, lower.tail = TRUE)
428- iscambinomprob <- function (k , n , prob , lower.tail ) {
451+ iscambinomprob <- function (k , n , prob , lower.tail , verbose = TRUE ) {
429452 if (prob < 0 || prob > 1 ) {
430453 stop(" Error: `prob` (probability) must be a numeric value between 0 and 1." )
431454 }
@@ -461,7 +484,9 @@ iscambinomprob <- function(k, n, prob, lower.tail) {
461484 pos = 1 ,
462485 col = " red"
463486 )
464- cat(" Probability" , k , " and below =" , this.prob , " \n " )
487+ if (verbose ) {
488+ cat(" Probability" , k , " and below =" , this.prob , " \n " )
489+ }
465490 }
466491 if (! lower.tail ) {
467492 this.prob <- 1 - pbinom(k - 1 , n , prob )
@@ -474,7 +499,9 @@ iscambinomprob <- function(k, n, prob, lower.tail) {
474499 pos = 1 ,
475500 col = " red"
476501 )
477- cat(" Probability" , k , " and above =" , this.prob , " \n " )
502+ if (verbose ) {
503+ cat(" Probability" , k , " and above =" , this.prob , " \n " )
504+ }
478505 }
479506 newtitle <- substitute(
480507 paste(" Binomial (" , n == x1 , " , " , pi == x2 , " )" , ),
@@ -498,6 +525,7 @@ iscambinomprob <- function(k, n, prob, lower.tail) {
498525# ' @param hypothesized hypothesized probability of success.
499526# ' @param alternative "less", "greater", or "two.sided"
500527# ' @param conf.level Confidence level for a two-sided confidence interval.
528+ # ' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
501529# '
502530# ' @return a list of the p-value along with lower and upper bound for the calculated confidence interval.
503531# ' @export
@@ -534,7 +562,8 @@ iscambinomtest <- function(
534562 n ,
535563 hypothesized = NULL ,
536564 alternative ,
537- conf.level = NULL
565+ conf.level = NULL ,
566+ verbose = TRUE
538567) {
539568 old <- par(mar = c(4 , 3 , 2 , 2 ))
540569 on.exit(par(old ), add = TRUE )
@@ -628,27 +657,35 @@ iscambinomtest <- function(
628657 abline(h = 0 , col = " gray" )
629658 abline(v = 0 , col = " gray" )
630659 }
631- cat(" \n " , " Exact Binomial Test\n " , sep = " " , " \n " )
660+ if (verbose ) {
661+ cat(" \n Exact Binomial Test\n\n " )
662+ }
632663 statistic <- signif(observed / n , 4 )
633- cat(paste(
634- " Data: observed successes = " ,
635- observed ,
636- " , sample size = " ,
637- n ,
638- " , sample proportion = " ,
639- statistic ,
640- " \n\n " ,
641- sep = " "
642- ))
664+ if (verbose ) {
665+ cat(paste(
666+ " Data: observed successes = " ,
667+ observed ,
668+ " , sample size = " ,
669+ n ,
670+ " , sample proportion = " ,
671+ statistic ,
672+ " \n\n " ,
673+ sep = " "
674+ ))
675+ }
643676
644677 if (! is.null(hypothesized )) {
645- cat(paste(" Null hypothesis : pi =" , hypothesized , sep = " " ), " \n " )
678+ if (verbose ) {
679+ cat(paste(" Null hypothesis : pi =" , hypothesized , sep = " " ), " \n " )
680+ }
646681 altname <- switch (alternative , less = " <" , greater = " >" , two.sided = " <>" )
647- cat(
648- paste(" Alternative hypothesis: pi" , altname , hypothesized , sep = " " ),
649- " \n "
650- )
651- cat(paste(" p-value:" , pvalue , sep = " " ), " \n " )
682+ if (verbose ) {
683+ cat(
684+ paste(" Alternative hypothesis: pi" , altname , hypothesized , sep = " " ),
685+ " \n "
686+ )
687+ cat(paste(" p-value:" , pvalue , sep = " " ), " \n " )
688+ }
652689 }
653690 p.L <- function (x , alpha ) {
654691 if (x == 0 ) {
@@ -680,7 +717,9 @@ iscambinomtest <- function(
680717 signif(p.U(observed , alpha ), 5 )
681718 )
682719 multconflevel <- 100 * conf.level [k ]
683- cat(multconflevel , " % Confidence interval for pi: (" , CINT , " ) \n " )
720+ if (verbose ) {
721+ cat(multconflevel , " % Confidence interval for pi: (" , CINT , " ) \n " )
722+ }
684723 lower1 [k ] <- as.numeric(CINT [1 ])
685724 upper1 [k ] <- as.numeric(CINT [3 ])
686725 }
@@ -777,6 +816,7 @@ iscambinomtest <- function(
777816# ' @param n The number of trials.
778817# ' @param prob The probability of success.
779818# ' @param lower.tail Boolean for finding the probability above (FALSE) or below (TRUE) the inputted value (inclusive)
819+ # ' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
780820# '
781821# ' @return numeric which achieves at most the stated probability
782822# ' @export
@@ -787,7 +827,7 @@ iscambinomtest <- function(
787827# ' iscaminvbinom(alpha = 0.05, n = 30, prob = 0.5, lower.tail = FALSE)
788828# '
789829# ' iscaminvbinom(alpha = 0.01, n = 60, prob = 0.10, lower.tail = FALSE)
790- iscaminvbinom <- function (alpha , n , prob , lower.tail ) {
830+ iscaminvbinom <- function (alpha , n , prob , lower.tail , verbose = TRUE ) {
791831 old <- par(mar = c(4 , 3 , 2 , 2 ))
792832 on.exit(par(old ), add = TRUE )
793833
@@ -848,13 +888,15 @@ iscaminvbinom <- function(alpha, n, prob, lower.tail) {
848888 col = " red" ,
849889 pos = 3
850890 )
851- cat(
852- " The observation with at most" ,
853- alpha ,
854- " probability at or below is" ,
855- answer ,
856- " \n "
857- )
891+ if (verbose ) {
892+ cat(
893+ " The observation with at most" ,
894+ alpha ,
895+ " probability at or below is" ,
896+ answer ,
897+ " \n "
898+ )
899+ }
858900 }
859901 if (! lower.tail ) {
860902 answer <- qbinom(alpha , n , prob , lower.tail ) + 1
@@ -890,13 +932,15 @@ iscaminvbinom <- function(alpha, n, prob, lower.tail) {
890932 col = " red" ,
891933 pos = 3
892934 )
893- cat(
894- " The observation with at most" ,
895- alpha ,
896- " probability at or above is" ,
897- answer ,
898- " \n "
899- )
935+ if (verbose ) {
936+ cat(
937+ " The observation with at most" ,
938+ alpha ,
939+ " probability at or above is" ,
940+ answer ,
941+ " \n "
942+ )
943+ }
900944 }
901945 answer
902946}
0 commit comments