Skip to content

Commit fb8e412

Browse files
committed
Added verbose option to suppress printing.
1 parent 0fbf73f commit fb8e412

22 files changed

Lines changed: 536 additions & 286 deletions

R/binomial.R

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
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("\nExact 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
}

R/chisqprob.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#'
66
#' @param xval the value of the chi-square statistic.
77
#' @param df the degrees of freedom.
8+
#' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
89
#'
910
#' @return The upper tail probability for the chi-square distribution, and a
1011
#' plot of the chi-square distribution with the statistic and more extreme
@@ -14,7 +15,7 @@
1415
#'
1516
#' @examples
1617
#' iscamchisqprob(5, 3)
17-
iscamchisqprob <- function(xval, df) {
18+
iscamchisqprob <- function(xval, df, verbose = TRUE) {
1819
old <- par(mar = c(4, 4, 2, 1))
1920
on.exit(par(old), add = TRUE)
2021

@@ -50,6 +51,8 @@ iscamchisqprob <- function(xval, df) {
5051
pos = 3
5152
)
5253
title(substitute(paste("Chi-Square (df = ", x, ")"), list(x = df)))
53-
cat(c("probability:", show_prob), "\n")
54+
if (verbose) {
55+
cat(c("probability:", show_prob), "\n")
56+
}
5457
invisible(show_prob)
5558
}

R/hypergeometric.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#' @param n Number of observations in group A
77
#' @param lower.tail Boolean for finding the probability above (FALSE) or
88
#' below (TRUE) the inputted value (inclusive)
9+
#' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
910
#'
1011
#' @return Tail probabilities from the hypergeometric distribution,
1112
#' hypergeometric distribution with normal distribution overlayed with the
@@ -14,7 +15,7 @@
1415
#'
1516
#' @examples
1617
#' iscamhypernorm(1, 20, 5, 10, TRUE)
17-
iscamhypernorm <- function(k, total, succ, n, lower.tail) {
18+
iscamhypernorm <- function(k, total, succ, n, lower.tail, verbose = TRUE) {
1819
# TODO rewrite so that it uses hyperprob and overlay normal?
1920
old <- par(mar = c(4, 4, 2, 1))
2021
on.exit(par(old), add = TRUE)
@@ -132,7 +133,9 @@ iscamhypernorm <- function(k, total, succ, n, lower.tail) {
132133
c("\n normal approx:", showprob2),
133134
c("\n normal approx with continuity:", showprob3)
134135
)
135-
cat(full, "\n")
136+
if (verbose) {
137+
cat(full, "\n")
138+
}
136139
}
137140

138141
#' Hypergeometric p-value and Distribution
@@ -143,6 +146,7 @@ iscamhypernorm <- function(k, total, succ, n, lower.tail) {
143146
#' @param n Number of observations in group A
144147
#' @param lower.tail Boolean for finding the probability above (FALSE) or
145148
#' below (TRUE) the inputted value (inclusive)
149+
#' @param verbose Logical, defaults to `TRUE`. Set to `FALSE` to suppress messages
146150
#'
147151
#' @return Tail probabilities from the hypergeometric distribution,
148152
#' hypergeometric distribution with the observed statistic and more extreme
@@ -152,7 +156,7 @@ iscamhypernorm <- function(k, total, succ, n, lower.tail) {
152156
#'
153157
#' @examples
154158
#' iscamhyperprob(1, 20, 5, 10, TRUE)
155-
iscamhyperprob <- function(k, total, succ, n, lower.tail) {
159+
iscamhyperprob <- function(k, total, succ, n, lower.tail, verbose = TRUE) {
156160
old <- par(mar = c(4, 4, 2, 1))
157161
on.exit(par(old), add = TRUE)
158162

@@ -187,7 +191,9 @@ iscamhyperprob <- function(k, total, succ, n, lower.tail) {
187191
pos = 3,
188192
col = "red"
189193
)
190-
cat("Probability", k, "and below =", this.prob, "\n")
194+
if (verbose) {
195+
cat("Probability", k, "and below =", this.prob, "\n")
196+
}
191197
}
192198
if (!lower.tail) {
193199
this.prob <- 1 - phyper(k - 1, succ, fail, n)
@@ -202,7 +208,9 @@ iscamhyperprob <- function(k, total, succ, n, lower.tail) {
202208
pos = 3,
203209
col = "red"
204210
)
205-
cat("Probability", k, "and above =", this.prob, "\n")
211+
if (verbose) {
212+
cat("Probability", k, "and above =", this.prob, "\n")
213+
}
206214
}
207215
newtitle <- substitute(
208216
paste("Hypergeometric (", N == x1, ", ", M == x2, ",", n == x3, ")"),

0 commit comments

Comments
 (0)