Skip to content

Commit 88ed813

Browse files
authored
Merge pull request #13 from ISCAM4/speedup
Faster plotting
2 parents fcf5de0 + bed5aaf commit 88ed813

78 files changed

Lines changed: 6738 additions & 296 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

R/binomial.R

Lines changed: 187 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ iscambinomnorm <- function(k, n, prob, direction, verbose = TRUE) {
4545
)
4646
normmean <- n * prob
4747
normsd <- sqrt(n * prob * (1 - prob))
48-
normseq <- seq(0, n, 0.001)
48+
normseq <- seq(0, n, by = max(0.001, abs(n - 0) / 2000))
4949
lines(normseq, dnorm(normseq, normmean, normsd), col = "grey")
5050
if (direction %in% c("below", "above")) {
5151
lower_tail <- direction == "below"
@@ -150,10 +150,14 @@ iscambinomnorm <- function(k, n, prob, direction, verbose = TRUE) {
150150
showprob <- format(this.prob, digits = 4)
151151
showprob2 <- format(normprob, digits = 4)
152152
showprob3 <- format(normprob2, digits = 4)
153-
probseq1 <- seq(0, k1, 0.001)
154-
probseq2 <- seq(k2, n, 0.001)
155-
withcorrect <- seq(0, k1 + 0.5, 0.001)
156-
withcorrect2 <- seq(k2 - 0.5, n, 0.001)
153+
probseq1 <- seq(0, k1, by = max(0.001, abs(k1 - 0) / 2000))
154+
probseq2 <- seq(k2, n, by = max(0.001, abs(n - k2) / 2000))
155+
withcorrect <- seq(0, k1 + 0.5, by = max(0.001, abs(k1 + 0.5 - 0) / 2000))
156+
withcorrect2 <- seq(
157+
k2 - 0.5,
158+
n,
159+
by = max(0.001, abs(n - (k2 - 0.5)) / 2000)
160+
)
157161
polygon(
158162
c(withcorrect, k1 + 0.5, 0),
159163
c(dnorm(withcorrect, normmean, normsd), 0, 0),
@@ -255,6 +259,7 @@ iscambinompower <- function(
255259
return(invisible())
256260
}
257261

262+
thisx <- 0:n
258263
minx <- max(
259264
0,
260265
min(
@@ -274,14 +279,19 @@ iscambinompower <- function(
274279
on.exit(par(old), add = TRUE)
275280
old <- par(mar = c(4, 3, 2, 2))
276281
on.exit(par(old), add = TRUE)
277-
.iscam_plot_binom_distribution(
278-
n = n,
279-
prob = prob1,
282+
plot(
283+
thisx,
284+
dbinom(thisx, size = n, prob1),
285+
xlab = "",
286+
ylab = " ",
287+
type = "h",
280288
xlim = c(minx, maxx),
281-
x_axis_label = "Number of Successes",
282-
y_axis_label = "Probability",
283-
add_title = FALSE
289+
panel.first = grid(),
290+
lwd = 2
284291
)
292+
abline(h = 0, col = "gray")
293+
mtext(side = 1, line = 2, "Number of Successes")
294+
mtext(side = 2, line = 2, "Probability")
285295

286296
if (alternative == "less") {
287297
rr <- qbinom(LOS, n, prob1) - 1
@@ -359,14 +369,19 @@ iscambinompower <- function(
359369
title(newtitle)
360370

361371
if (!is.null(prob2)) {
362-
.iscam_plot_binom_distribution(
363-
n = n,
364-
prob = prob2,
372+
plot(
373+
thisx,
374+
dbinom(thisx, size = n, prob2),
375+
xlab = " ",
376+
ylab = " ",
377+
type = "h",
365378
xlim = c(minx, maxx),
366-
x_axis_label = "Number of Successes",
367-
y_axis_label = "Probability",
368-
add_title = FALSE
379+
lwd = 2,
380+
panel.first = grid()
369381
)
382+
abline(h = 0, col = "gray")
383+
mtext(side = 1, line = 2, "Number of Successes")
384+
mtext(side = 2, line = 2, "Probability")
370385
myy2 <- dbinom(floor(n * prob2), n, prob2) / 2
371386
if (alternative == "less") {
372387
this.prob2 <- pbinom(rr, n, prob2)
@@ -465,56 +480,59 @@ iscambinomprob <- function(k, n, prob, lower.tail, verbose = TRUE) {
465480

466481
old <- par(mar = c(4, 3, 2, 2))
467482
on.exit(par(old), add = TRUE)
468-
limits <- .iscam_binom_limits(n, prob, include_upper = k + 1)
469-
minx <- limits[1]
470-
maxx <- limits[2]
483+
thisx <- 0:n
484+
minx <- max(0, n * prob - 4 * sqrt(prob * (1 - prob) * n))
485+
maxx <- min(n, n * prob + 4 * sqrt(prob * (1 - prob) * n))
486+
maxx <- max(k + 1, maxx)
471487
myy <- dbinom(floor(n * prob), n, prob)
472-
.iscam_plot_binom_distribution(
473-
n = n,
474-
prob = prob,
488+
plot(
489+
thisx,
490+
dbinom(thisx, size = n, prob),
491+
xlab = " ",
492+
ylab = " ",
493+
type = "h",
475494
xlim = c(minx, maxx),
476-
x_axis_label = "Number of Successes"
477-
)
478-
479-
tail_out <- .iscam_discrete_tail_probability(
480-
k = k,
481-
lower.tail = lower.tail,
482-
cdf_lower = function(x) pbinom(x, n, prob),
483-
cdf_upper = function(x) 1 - pbinom(x - 1, n, prob),
484-
digits = 4
485-
)
486-
this.prob <- tail_out$prob
487-
showprob <- tail_out$showprob
488-
.iscam_draw_discrete_tail_spikes(
489-
k = k,
490-
upper = n,
491-
lower.tail = lower.tail,
492-
pmf_fn = function(x) dbinom(x, size = n, prob),
493-
col = "red",
495+
panel.first = grid(),
494496
lwd = 2
495497
)
496-
x_text <- if (lower.tail) {
497-
(minx + n * prob) / 4
498+
abline(h = 0, col = "gray")
499+
500+
if (lower.tail) {
501+
this.prob <- pbinom(k, n, prob)
502+
showprob <- format(this.prob, digits = 4)
503+
lines(0:k, dbinom(0:k, size = n, prob), col = "red", type = "h", lwd = 2)
504+
text(
505+
(minx + n * prob) / 4,
506+
myy,
507+
labels = bquote(atop(P(X <= .(k)), "=" ~ .(showprob))),
508+
pos = 1,
509+
col = "red"
510+
)
511+
if (verbose) {
512+
cat("Probability", k, "and below =", this.prob, "\n")
513+
}
498514
} else {
499-
(maxx + n * prob) * 9 / 16
515+
this.prob <- 1 - pbinom(k - 1, n, prob)
516+
showprob <- format(this.prob, digits = 4)
517+
lines(k:n, dbinom(k:n, size = n, prob), col = "red", type = "h", lwd = 2)
518+
text(
519+
(maxx + n * prob) * 9 / 16,
520+
myy,
521+
labels = bquote(atop(P(X >= .(k)), "=" ~ .(showprob))),
522+
pos = 1,
523+
col = "red"
524+
)
525+
if (verbose) {
526+
cat("Probability", k, "and above =", this.prob, "\n")
527+
}
500528
}
501-
text(
502-
x_text,
503-
myy,
504-
labels = .iscam_discrete_tail_label(
505-
k = k,
506-
showprob = showprob,
507-
lower.tail = lower.tail
508-
),
509-
pos = 1,
510-
col = "red"
511-
)
512-
.iscam_print_discrete_tail_probability(
513-
verbose = verbose,
514-
k = k,
515-
prob = this.prob,
516-
lower.tail = lower.tail
529+
newtitle <- substitute(
530+
paste("Binomial (", n == x1, ", ", pi == x2, ")", ),
531+
list(x1 = n, x2 = prob)
517532
)
533+
title(newtitle)
534+
mtext(side = 1, line = 2, "Number of Successes")
535+
mtext(side = 2, line = 2, "Probability")
518536

519537
invisible(this.prob)
520538
}
@@ -580,26 +598,80 @@ iscambinomtest <- function(
580598
observed <- .iscam_as_count(observed, n)
581599
pvalue <- NULL
582600
if (!is.null(hypothesized)) {
583-
limits <- .iscam_binom_limits(n, hypothesized, include_upper = observed + 1)
584-
minx <- limits[1]
585-
maxx <- limits[2]
601+
minx <- max(
602+
0,
603+
n * hypothesized - 4 * sqrt(hypothesized * (1 - hypothesized) * n)
604+
)
605+
maxx <- min(
606+
n,
607+
n * hypothesized + 4 * sqrt(hypothesized * (1 - hypothesized) * n)
608+
)
609+
maxx <- max(observed + 1, maxx)
586610
myy <- max(dbinom(floor(n * hypothesized), n, hypothesized)) * 0.9
587-
.iscam_plot_binom_distribution(
588-
n = n,
589-
prob = hypothesized,
611+
x <- 0:n
612+
plot(
613+
x,
614+
dbinom(x, size = n, prob = hypothesized),
615+
xlab = "",
616+
ylab = " ",
617+
type = "h",
590618
xlim = c(minx, maxx),
591-
x_axis_label = "Number of Successes"
592-
)
593-
pvalue <- .iscam_plot_exact_binom_region(
594-
observed = observed,
595-
n = n,
596-
hypothesized = hypothesized,
597-
alternative = alternative,
598-
minx = minx,
599-
maxx = maxx,
600-
myy = myy
619+
panel.first = grid(),
620+
lwd = 2
601621
)
622+
title(.iscam_binom_title(n, hypothesized))
623+
mtext(side = 1, line = 2, "Number of Successes")
624+
mtext(side = 2, line = 2, "Probability")
625+
626+
if (alternative == "less") {
627+
pvalue <- pbinom(observed, size = n, prob = hypothesized, TRUE)
628+
lines(
629+
0:observed,
630+
dbinom(0:observed, size = n, prob = hypothesized),
631+
col = "red",
632+
type = "h",
633+
lwd = 2
634+
)
635+
text(
636+
minx,
637+
myy,
638+
labels = paste("p-value:", signif(pvalue, 4)),
639+
pos = 4,
640+
col = "red"
641+
)
642+
} else if (alternative == "greater") {
643+
pvalue <- pbinom(observed - 1, size = n, prob = hypothesized, FALSE)
644+
lines(
645+
observed:n,
646+
dbinom(observed:n, size = n, prob = hypothesized),
647+
col = "red",
648+
type = "h",
649+
lwd = 2
650+
)
651+
text(
652+
maxx,
653+
myy,
654+
labels = paste("p-value:", signif(pvalue, 4)),
655+
pos = 2,
656+
col = "red"
657+
)
658+
} else {
659+
xvals <- 0:n
660+
probabilities <- dbinom(xvals, size = n, prob = hypothesized)
661+
cutoff <- dbinom(observed, size = n, prob = hypothesized) + 1e-05
662+
keep <- probabilities <= cutoff
663+
pvalue <- sum(probabilities[keep])
664+
lines(xvals[keep], probabilities[keep], col = "red", type = "h", lwd = 2)
665+
text(
666+
minx,
667+
myy,
668+
labels = paste("two-sided p-value:\n", signif(pvalue, 4)),
669+
pos = 4,
670+
col = "red"
671+
)
672+
}
602673
pvalue <- signif(pvalue, 5)
674+
abline(h = 0, col = "gray")
603675
abline(v = 0, col = "gray")
604676
}
605677
if (verbose) {
@@ -620,15 +692,15 @@ iscambinomtest <- function(
620692
}
621693

622694
if (!is.null(hypothesized)) {
623-
.iscam_print_hypotheses(
624-
verbose = verbose,
625-
null_name = "pi",
626-
alt_name = "pi",
627-
hypothesized = hypothesized,
628-
alternative = alternative,
629-
include_not_equal = FALSE
630-
)
631695
if (verbose) {
696+
cat(paste("Null hypothesis : pi =", hypothesized, sep = " "), "\n")
697+
}
698+
altname <- switch(alternative, less = "<", greater = ">", two.sided = "<>")
699+
if (verbose) {
700+
cat(
701+
paste("Alternative hypothesis: pi", altname, hypothesized, sep = " "),
702+
"\n"
703+
)
632704
cat(paste("p-value:", pvalue, sep = " "), "\n")
633705
}
634706
}
@@ -637,18 +709,26 @@ iscambinomtest <- function(
637709
if (!is.null(conf.level)) {
638710
conf.level <- .iscam_normalize_conf_levels(conf.level)
639711
for (k in seq_along(conf.level)) {
640-
ci_bounds <- signif(.iscam_binom_ci(observed, n, conf.level[k]), 5)
641-
CINT <- c(ci_bounds["lower"], ",", ci_bounds["upper"])
712+
alpha <- (1 - conf.level[k]) / 2
713+
lower1[k] <- if (observed == 0) {
714+
0
715+
} else {
716+
qbeta(alpha, observed, n - observed + 1)
717+
}
718+
upper1[k] <- if (observed == n) {
719+
1
720+
} else {
721+
qbeta(1 - alpha, observed + 1, n - observed)
722+
}
642723
if (verbose) {
724+
CINT <- c(signif(lower1[k], 5), ",", signif(upper1[k], 5))
643725
cat(
644726
100 * conf.level[k],
645727
"% Confidence interval for pi: (",
646728
CINT,
647729
") \n"
648730
)
649731
}
650-
lower1[k] <- as.numeric(CINT[1])
651-
upper1[k] <- as.numeric(CINT[3])
652732
}
653733
}
654734
old <- par(mar = c(4, 2, 1.5, 0.5), mfrow = c(3, 1))
@@ -748,16 +828,28 @@ iscaminvbinom <- function(alpha, n, prob, lower.tail, verbose = TRUE) {
748828
old <- par(mar = c(4, 3, 2, 2))
749829
on.exit(par(old), add = TRUE)
750830

751-
limits <- .iscam_binom_limits(n, prob)
752-
minx <- limits[1]
753-
maxx <- limits[2]
831+
thisx <- 0:n
832+
minx <- max(0, n * prob - 4 * sqrt(prob * (1 - prob) * n))
833+
maxx <- min(n, n * prob + 4 * sqrt(prob * (1 - prob) * n))
754834
myy <- dbinom(floor(n * prob), n, prob) / 2
755-
.iscam_plot_binom_distribution(
756-
n = n,
757-
prob = prob,
835+
plot(
836+
thisx,
837+
dbinom(thisx, size = n, prob),
838+
xlab = " ",
839+
ylab = " ",
840+
type = "h",
758841
xlim = c(minx, maxx),
759-
x_axis_label = "X = Number of Successes"
842+
panel.first = grid(),
843+
lwd = 2
760844
)
845+
abline(h = 0, col = "gray")
846+
newtitle <- substitute(
847+
paste("Binomial (", n == x1, ", ", pi == x2, ")", ),
848+
list(x1 = n, x2 = prob)
849+
)
850+
title(newtitle)
851+
mtext(side = 1, line = 2, "X = Number of Successes")
852+
mtext(side = 2, line = 2, "Probability")
761853
if (lower.tail) {
762854
answer <- qbinom(alpha, n, prob, lower.tail) - 1
763855
actualprob <- format(pbinom(answer, n, prob, lower.tail), digits = 4)

R/chisqprob.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ iscamchisqprob <- function(xval, df, verbose = TRUE) {
2525

2626
minx <- 0
2727
maxx <- max(20, xval, df)
28-
this_x <- seq(minx, maxx, 0.001)
28+
this_x <- seq(minx, maxx, by = max(0.001, abs(maxx - minx) / 2000))
2929
.iscam_plot_continuous_distribution(
3030
x = this_x,
3131
density_y = dchisq(this_x, df),

0 commit comments

Comments
 (0)