| Title: | Inference for Samples of Size at Least One |
|---|---|
| Description: | Provides different interval estimates of a location parameter when the sample size is one or more. These include classical methods when n=1, new Bayesian analogues of these classical methods, and extensions of these methods to larger sample sizes in the normal case. Other functions calculate Bayes factors based on t-statistics, and implement the (generalized) inverse normal distribution. These methods are described in Gerard (2026) <doi:10.48550/arXiv.2607.25007>. |
| Authors: | David Gerard [aut, cre] (ORCID: <https://orcid.org/0000-0001-9450-5023>) |
| Maintainer: | David Gerard <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.0.2 |
| Built: | 2026-10-09 10:45:06 UTC |
| Source: | https://github.com/dcgerard/nisone |
Calculates a t-interval using augmented data c(x,A). The multiplier
of this interval bounds the level above level, so these intervals
are typically conservative. This method is very fast for n <= 100
and level %in% c(0.8, 0.9, 0.95, 0.99) because I saved those
multipliers in an internal dataset. But it can be slow (on the order of
a second) for other confidence levels or larger sample sizes.
aug_t(x, A = 0, level = 0.95, wt = 1)aug_t(x, A = 0, level = 0.95, wt = 1)
x |
The vector of data |
A |
The prior mean |
level |
The level of the interval |
wt |
Weight for A. Don't touch this unless you know what you are doing. |
Suppose . Suppose we have
prior value . This provides intervals of the form
where and are the sample mean and
sample standard deviation of the augmented data ,
and is chosen large enough to maintain the confidence level
at all parameter values.
The wt argument allows for more copies of
to be included in the data augmentation. But it doesn't work well with
more data augmentation so you should not set it above 1. Though, you can set
wt to be between 0 and 1 (to have less data augmentation) and this
does seem to work pretty well, but I haven't studied it extensively,
so use at your own risk.
Note that you have to choose before seeing the data. If you choose
it based on the data, then you no longer maintain the confidence level.
The augmented t-interval of the specified level.
David Gerard
set.seed(1) ## A is exactly correct x <- rnorm(10) aug_t(x) stats::t.test(x)$conf.int ## A is very wrong x <- rnorm(10) aug_t(x, A = 100) stats::t.test(x)$conf.intset.seed(1) ## A is exactly correct x <- rnorm(10) aug_t(x) stats::t.test(x)$conf.int ## A is very wrong x <- rnorm(10) aug_t(x, A = 100) stats::t.test(x)$conf.int
Suppose . This gives you credible intervals
of a specified level when we use prior with probability
1, and . See the n1post() for
access to the full posterior distribution.
bci1(x, A = 0, level = 0.95)bci1(x, A = 0, level = 0.95)
x |
a double |
A |
The prior center. |
level |
The level of the credible interval |
The credible interval of the specified level.
David Gerard
n1post(): Functions to access the full posterior distribution.
bci1(x = 1, A = 0)bci1(x = 1, A = 0)
Let . Let ,
, and . Then,
for a fixed value of , this comes up with a
posterior credible interval of where the prior is
or .
The full posterior distribution is generalized inverse normal (implemented
by ginvnorm) with shape parameter , inverse mean of
sum(y) / sum(y^2), and inverse variance 1 / (nu^2 * sum(y^2)).
bcin(x, A = 0, nu = 1, level = 0.95)bcin(x, A = 0, nu = 1, level = 0.95)
x |
a double |
A |
The prior center. |
nu |
The fixed value of nu. |
level |
The level of the credible interval |
The credible interval of the provided level.
David Gerard
ginvnorm(): The generalized inverse normal distribution.
set.seed(1) x <- stats::rnorm(4, mean = 1, sd = 1) bcin(x = x, A = 3)set.seed(1) x <- stats::rnorm(4, mean = 1, sd = 1) bcin(x = x, A = 3)
Given a user-provided prior on the standardized effect size, will calculate the Bayes factor.
bft(t, nu, nd, prior = NULL)bft(t, nu, nd, prior = NULL)
t |
The observed t-statistic. |
nu |
The degrees of freedom of the t-statistic. This should be
|
nd |
Effective sample size. This should be |
prior |
The prior on the standardized effect size, |
Let be the standardized effect size, let be
the variance (assumed equal in two-sample case). In the one-sample case,
, where is the null
value. In the two-sample case, ,
where and are the means of the two-samples. We place
the prior under the null and
under the alternative, for some arbitrary density . Given
this setting, the Bayes factor is a function of the -statistic.
We calculate it via numeric integration.
The Bayes factor to a corresponding t-statistic.
David Gerard
Gronau, Q. F., Ly, A., & Wagenmakers, E. J. (2020). Informed Bayesian t-tests. The American Statistician. doi:10.1080/00031305.2018.1562983
# One sample t, n = 10, t-statistic = 2 bft(t = 2, nu = 10 - 1, nd = 10) # Two sample t, n1 = 10, n2 = 8, t-statistic = 2 bft(t = 2, nu = 10 + 8 - 2, nd = 1 / (1 / 10 + 1 / 8))# One sample t, n = 10, t-statistic = 2 bft(t = 2, nu = 10 - 1, nd = 10) # Two sample t, n1 = 10, n2 = 8, t-statistic = 2 bft(t = 2, nu = 10 + 8 - 2, nd = 1 / (1 / 10 + 1 / 8))
True level of the Bayesian credible interval at a given coefficient of variation
blevel(level, nu)blevel(level, nu)
level |
The credible interval level. |
nu |
(mu - A)/sigma |
The true level of the (1-alpha) credible interval.
David Gerard
blevel(0.95, 0.5) blevel(0.95, 1) blevel(0.95, 1.5)blevel(0.95, 0.5) blevel(0.95, 1) blevel(0.95, 1.5)
When you have one observation, from some symmetric distribution
with center , for a pre-specified , produces
confidence intervals for the center form
(type = "x") or
(type = "ave"). The average intervals have smaller width on
average, and so are the default. We allow to either come from
a normal distribution, a Cauchy, or a uniform. Note that if you use
family = "uniform" then these intervals are valid confidence intervals
for the median/mode for any symmetric unimodal density, which
I think is really amazing.
ci1( x, A = 0, type = c("ave", "x"), family = c("normal", "cauchy", "uniform"), level = 0.95 )ci1( x, A = 0, type = c("ave", "x"), family = c("normal", "cauchy", "uniform"), level = 0.95 )
x |
A vector of single observations. |
A |
Your "prior knowledge" or "augmented data value". |
type |
Either centered at x or the average of x and A |
family |
Either the normal distribution, Cauchy, or uniform. |
level |
The level of the confidence interval. |
A matrix of with 4 columns: the data, the center, the lower bound, the upper bound.
David Gerard
Blachman, N., & Machol, R. (1987). Confidence intervals based on one or more observations. IEEE Transactions on Information Theory, 33(3), 373-382. doi:10.1109/TIT.1987.1057306
ci1(c(1, 2, 10), type = "x") ci1(c(1, 2, 10), type = "ave")ci1(c(1, 2, 10), type = "x") ci1(c(1, 2, 10), type = "ave")
Let . Consider intervals of the form
or . These intervals
define a confidence distribution of . This is the
density of the confidence distribution of
or . Please note that is random
here not . Also note that this confidence distribution does
not exist between the 25th and 75th percentiles.
d_wc(x, center = c("X", "ave"))d_wc(x, center = c("X", "ave"))
x |
The value at which to calculate the density.
Only defined for |
center |
What is the center of the interval? Either |
n=1 confidence density.
David Gerard
xseq <- seq(-10, 10, length.out = 500) dseq <- d_wc(xseq) plot(xseq, dseq, type = "l") dseq <- d_wc(xseq, center = "ave") plot(xseq, dseq, type = "l")xseq <- seq(-10, 10, length.out = 500) dseq <- d_wc(xseq) plot(xseq, dseq, type = "l") dseq <- d_wc(xseq, center = "ave") plot(xseq, dseq, type = "l")
Density, distribution function, quantile function, and random generation for the generalized inverse normal distribution when parameterized by the shape, mean, and standard deviation of the inverse (reciprocal).
dginvnorm(x, alpha, mu = 0, tau = 1, log = FALSE) pginvnorm(q, alpha, mu = 0, tau = 1, lower.tail = TRUE, subdivisions = 500L) qginvnorm(p, alpha, mu = 0, tau = 1) rginvnorm(n, alpha, mu = 0, tau = 1) mginvnorm(alpha, mu = 0, tau = 1) xginvnorm(alpha, mu = 0, tau = 1)dginvnorm(x, alpha, mu = 0, tau = 1, log = FALSE) pginvnorm(q, alpha, mu = 0, tau = 1, lower.tail = TRUE, subdivisions = 500L) qginvnorm(p, alpha, mu = 0, tau = 1) rginvnorm(n, alpha, mu = 0, tau = 1) mginvnorm(alpha, mu = 0, tau = 1) xginvnorm(alpha, mu = 0, tau = 1)
x, q
|
vector of quantiles |
alpha |
vector of shape parameters |
mu |
vector of means of inverse. |
tau |
vector of standard deviations of inverse. |
log |
logical; if |
lower.tail |
logical; if |
subdivisions |
The maximum number of subintervals used in |
p |
vector of probabilities |
n |
sample size |
Either a random sample (rginvnorm),
the density (dginvnorm), the tail
probability (pginvnorm), or the quantile
(qginvnorm) of the inverse normal distribution.
dginvnorm(): Density function.
pginvnorm(): Probability function.
qginvnorm(): Quantile function.
rginvnorm(): Random generation.
mginvnorm(): Mean.
xginvnorm(): Modes.
David Gerard
Robert, C. (1991). Generalized inverse normal distributions. Statistics & Probability Letters, 11(1), 37-41. doi:10.1016/0167-7152(91)90174-P
set.seed(50) samp <- rginvnorm(n = 1000, alpha = 4, mu = 0.5, tau = 1) x <- seq(min(samp), max(samp), length.out = 500) y <- dginvnorm(x = x, alpha = 4, mu = 0.5, tau = 1) modes <- xginvnorm(alpha = 4, mu = 0.5, tau = 1) graphics::hist( samp, freq = FALSE, breaks = 100, xlab = "x", main = "Generalized Inverse Normal Density") graphics::lines(x, y, col = "#E69F00") graphics::abline(v = modes, col = "#56B4E9", lty = 2)set.seed(50) samp <- rginvnorm(n = 1000, alpha = 4, mu = 0.5, tau = 1) x <- seq(min(samp), max(samp), length.out = 500) y <- dginvnorm(x = x, alpha = 4, mu = 0.5, tau = 1) modes <- xginvnorm(alpha = 4, mu = 0.5, tau = 1) graphics::hist( samp, freq = FALSE, breaks = 100, xlab = "x", main = "Generalized Inverse Normal Density") graphics::lines(x, y, col = "#E69F00") graphics::abline(v = modes, col = "#56B4E9", lty = 2)
Let come from some location-scale family
for some standard distribution . You provide ,
and and these functions will provide the density,
distribution, quantile, and random generation for . Convenience
arguments for the normal, Cauchy, and uniform are provided.
didist( x, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), ddist = NULL, log = FALSE, ... ) pidist( q, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), pdist = NULL, lower.tail = TRUE, log.p = FALSE, ... ) qidist( p, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), qdist = NULL, pdist = NULL, ... ) ridist( n, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), rdist = NULL, ... )didist( x, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), ddist = NULL, log = FALSE, ... ) pidist( q, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), pdist = NULL, lower.tail = TRUE, log.p = FALSE, ... ) qidist( p, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), qdist = NULL, pdist = NULL, ... ) ridist( n, center = 0, scale = 1, fam = c("normal", "cauchy", "uniform"), rdist = NULL, ... )
x, q
|
vector of quantiles |
center |
The center parameter |
scale |
The scale parameter |
fam |
One of |
ddist |
Density function of standard distribution |
log, log.p
|
logical; if |
... |
Additional arguments for |
pdist |
Cumulative distribution function of standard distribution. |
lower.tail |
logical; if |
p |
vector of probabilities |
qdist |
Quantile function of standard distribution. |
n |
sample size |
rdist |
Random generation of standard distribution. |
Either a random sample (ridist),
the density (didist), the tail
probability (pidist), or the quantile
(qidist) of the inverse distribution.
didist(): Density function.
pidist(): Distribution function.
qidist(): Quantile function.
ridist(): Random generation.
David Gerard
didist(1, fam = "normal") didist(1, center = 3, scale = 2, ddist = dt, df = 1) pidist(1, fam = "normal") pidist(1, center = 3, scale = 2, pdist = pt, df = 1) qidist(0.1, fam = "normal") qidist(0.1, center = 3, scale = 2, qdist = qt, pdist = pt, df = 1)didist(1, fam = "normal") didist(1, center = 3, scale = 2, ddist = dt, df = 1) pidist(1, fam = "normal") pidist(1, center = 3, scale = 2, pdist = pt, df = 1) qidist(0.1, fam = "normal") qidist(0.1, center = 3, scale = 2, qdist = qt, pdist = pt, df = 1)
Density, distribution function, quantile function, and random generation for the inverse normal distribution when parameterized by the mean and standard deviation of the inverse (reciprocal).
dinvnorm(x, imean = 0, isd = 1, log = FALSE) pinvnorm(q, imean = 0, isd = 1, lower.tail = TRUE, log.p = FALSE) qinvnorm(p, imean = 0, isd = 1) rinvnorm(n, imean = 0, isd = 1)dinvnorm(x, imean = 0, isd = 1, log = FALSE) pinvnorm(q, imean = 0, isd = 1, lower.tail = TRUE, log.p = FALSE) qinvnorm(p, imean = 0, isd = 1) rinvnorm(n, imean = 0, isd = 1)
x, q
|
vector of quantiles |
imean |
vector of means of inverse. |
isd |
vector of standard deviations of inverse. |
log, log.p
|
logical; if |
lower.tail |
logical; if |
p |
vector of probabilities |
n |
sample size |
Either a random sample (rinvnorm),
the density (dinvnorm), the tail
probability (pinvnorm), or the quantile
(qinvnorm) of the inverse normal distribution.
dinvnorm(): Density function.
pinvnorm(): Probability function.
qinvnorm(): Quantile function.
rinvnorm(): Random generation.
David Gerard
Robert, C. (1991). Generalized inverse normal distributions. Statistics & Probability Letters, 11(1), 37-41. doi:10.1016/0167-7152(91)90174-P
x <- seq(-4, 4, length.out = 300) y <- dinvnorm(x = x, imean = 0.1, isd = 1) graphics::plot(x, y, type = "l") p <- pinvnorm(q = x, imean = 0.1, isd = 1) graphics::plot(x, p, type = "l", ylim = c(0, 1)) graphics::abline(h = c(0, 1), lty = 2) qinvnorm(p = c(0.025, 0.5, 0.975), imean = 0.1, isd = 1) s <- rinvnorm(n = 10000, imean = 0.1, isd = 1) stats::quantile(s, c(0.025, 0.5, 0.975))x <- seq(-4, 4, length.out = 300) y <- dinvnorm(x = x, imean = 0.1, isd = 1) graphics::plot(x, y, type = "l") p <- pinvnorm(q = x, imean = 0.1, isd = 1) graphics::plot(x, p, type = "l", ylim = c(0, 1)) graphics::abline(h = c(0, 1), lty = 2) qinvnorm(p = c(0.025, 0.5, 0.975), imean = 0.1, isd = 1) s <- rinvnorm(n = 10000, imean = 0.1, isd = 1) stats::quantile(s, c(0.025, 0.5, 0.975))
If wt is an integer, this is mean(c(x, rep(A, wt))). But we generalize it to non-integer wt as well.
mean_aug(x, A, wt = 1)mean_aug(x, A, wt = 1)
x |
Data |
A |
Prior value |
wt |
Weight for A. Don't touch this unless you know what you are doing. |
The augmented mean
David Gerard
mean_aug(c(1, 2, 3), 4, wt = 10)mean_aug(c(1, 2, 3), 4, wt = 10)
Density, probability, quantile, and random generation from the posterior
distribution of the location parameter when one has observation
and uses a particular prior that results in valid confidence intervals
(asymptotically in the confidence level).
nun1post(fam = c("normal", "cauchy"), ddist = NULL, ...) dn1post( x, A, obs, nu = NULL, fam = c("normal", "cauchy"), ddist = NULL, log = FALSE, ... ) pn1post( q, A, obs, nu = NULL, fam = c("normal", "cauchy"), pdist = NULL, lower.tail = TRUE, log.p = FALSE, ... ) qn1post( p, A, obs, nu = NULL, fam = c("normal", "cauchy"), qdist = NULL, pdist = NULL, ... ) rn1post(n, A, obs, nu = NULL, fam = c("normal", "cauchy"), rdist = NULL, ...)nun1post(fam = c("normal", "cauchy"), ddist = NULL, ...) dn1post( x, A, obs, nu = NULL, fam = c("normal", "cauchy"), ddist = NULL, log = FALSE, ... ) pn1post( q, A, obs, nu = NULL, fam = c("normal", "cauchy"), pdist = NULL, lower.tail = TRUE, log.p = FALSE, ... ) qn1post( p, A, obs, nu = NULL, fam = c("normal", "cauchy"), qdist = NULL, pdist = NULL, ... ) rn1post(n, A, obs, nu = NULL, fam = c("normal", "cauchy"), rdist = NULL, ...)
fam |
One of |
ddist |
Density function of standard distribution |
... |
Additional arguments for |
x, q
|
vector of quantiles |
A |
The prior value. |
obs |
The observed value. This is |
nu |
The prior value of nu. If not known, use |
log, log.p
|
logical; if |
pdist |
Cumulative distribution function of standard distribution. |
lower.tail |
logical; if |
p |
vector of probabilities |
qdist |
Quantile function of standard distribution. |
n |
sample size |
rdist |
Random generation of standard distribution. |
Let have PDF for a
known and symmetric . Let
for a pre-specified . We place prior
and with probability 1.
These functions will calculate the posterior distribution and allow you to interact with it through the distribution, density, quantile, and random generation functions.
density, distribution, quantile, or random values.
nun1post(): Obtains prior value of .
dn1post(): Density function.
pn1post(): Distribution function.
qn1post(): Quantile function.
rn1post(): Random generation.
David Gerard
set.seed(1) # Observe x = 2, assume t with 2 df, prior value is A = 1 nun1post(ddist = dt, df = 2) ## nu should be 1 x <- seq(-10, 10, length.out = 500) y <- dn1post(x = x, A = 1, obs = 2, nu = 1, ddist = dt, df = 2) z <- rn1post(n = 10000, A = 1, obs = 2, nu = 1, rdist = rt, df = 2) z <- z[z >= -10 & z <= 10] graphics::hist(z, freq = FALSE, breaks = 200, border = "grey", col = "grey") graphics::lines(x, y, type = "l", col = "#E69F00") graphics::abline(v = c(1, 2), col = c("#56B4E9", "#009E73"), lty = c(2, 3)) pn1post(q = 2, A = 1, obs = 2, nu = 1, pdist = pt, df = 2) qn1post(p = 0.7113, A = 1, obs = 2, nu = 1, qdist = qt, pdist = pt, df = 2)set.seed(1) # Observe x = 2, assume t with 2 df, prior value is A = 1 nun1post(ddist = dt, df = 2) ## nu should be 1 x <- seq(-10, 10, length.out = 500) y <- dn1post(x = x, A = 1, obs = 2, nu = 1, ddist = dt, df = 2) z <- rn1post(n = 10000, A = 1, obs = 2, nu = 1, rdist = rt, df = 2) z <- z[z >= -10 & z <= 10] graphics::hist(z, freq = FALSE, breaks = 200, border = "grey", col = "grey") graphics::lines(x, y, type = "l", col = "#E69F00") graphics::abline(v = c(1, 2), col = c("#56B4E9", "#009E73"), lty = c(2, 3)) pn1post(q = 2, A = 1, obs = 2, nu = 1, pdist = pt, df = 2) qn1post(p = 0.7113, A = 1, obs = 2, nu = 1, qdist = qt, pdist = pt, df = 2)
Let . Consider intervals of the form
or . These intervals
define a confidence distribution of . This is the
CDF of the confidence distribution of
or . Please note that is random
here not . Also note that this confidence distribution does
not exist between the 25th and 75th percentiles.
p_wc(q, center = c("X", "ave"))p_wc(q, center = c("X", "ave"))
q |
The quantile. Only defined for |
center |
What is the center of the interval? Either |
n=1 confidence distribution CDF.
David Gerard
qseq <- seq(-10, 10, length.out = 100) pseq <- p_wc(qseq) plot(qseq, pseq, type = "l", xlab = "q", ylab = "CDF")qseq <- seq(-10, 10, length.out = 100) pseq <- p_wc(qseq) plot(qseq, pseq, type = "l", xlab = "q", ylab = "CDF")
If wt is an integer, this is var(c(x, rep(A, wt))). But we generalize it to non-integer wt as well.
var_aug(x, A, wt = 1)var_aug(x, A, wt = 1)
x |
Data |
A |
Prior value |
wt |
Weight for A. Don't touch this unless you know what you are doing. |
The augmented variance
David Gerard
var_aug(c(1, 2, 3), 4, wt = 10)var_aug(c(1, 2, 3), 4, wt = 10)
This assumes the random variable X follows a normal distribution.
wc_alpha(width, center = c("X", "ave"))wc_alpha(width, center = c("X", "ave"))
width |
The half-width of the interval, in units of |X|.
This needs to be at least 1 if |
center |
What is the center of the interval? Either |
The worst case probability (a priori) that a confidence interval
of half-width width will not capture the mean.
David Gerard
wc_alpha(width = 2, center = "X") wc_alpha(width = 2, center = "ave")wc_alpha(width = 2, center = "X") wc_alpha(width = 2, center = "ave")
This assumes the random variable X is normal. Intervals are of the form center +/- width * |X|.
wc_cov(width, center = c("X", "ave"))wc_cov(width, center = c("X", "ave"))
width |
The half-width of the interval, in units of |X|.
This needs to be at least 1 if |
center |
What is the center of the interval? Either |
We use A = 0 for calculations, but the worst case COV doesn't change if A is non-zero. So you can think of intervals of the form center +/- width * |X - A| where center is either X or (X + A)/2.
Worst case coefficient of variation .
David Gerard
wc_cov(width = 2, center = "X") wc_cov(width = 2, center = "ave")wc_cov(width = 2, center = "X") wc_cov(width = 2, center = "ave")
confidence intervals are of the form
or . You give
and this function will provide if X follows a Cauchy,
normal, or uniform distribution.
wc_width( alpha, center = c("X", "ave", "approx"), family = c("normal", "cauchy", "uniform") )wc_width( alpha, center = c("X", "ave", "approx"), family = c("normal", "cauchy", "uniform") )
alpha |
The confidence error probability. We produce a (1-alpha)100 percent confidence interval. |
center |
Either X, ave, or the asymptotic width (approx). |
family |
Either the normal, Cauchy, or uniform distribution. |
Returns half-width of worst-case confidence interval in units of |X - A|.
David Gerard
wc_width(alpha = 0.2, center = "X") wc_width(alpha = 0.2, center = "ave") wc_width(alpha = 0.2, center = "approx") wc_width(alpha = 0.05, center = "X") wc_width(alpha = 0.05, center = "ave") wc_width(alpha = 0.05, center = "approx")wc_width(alpha = 0.2, center = "X") wc_width(alpha = 0.2, center = "ave") wc_width(alpha = 0.2, center = "approx") wc_width(alpha = 0.05, center = "X") wc_width(alpha = 0.05, center = "ave") wc_width(alpha = 0.05, center = "approx")