Calculate expected confidence bands or prediction intreval with normal or t sampling distribution
Source:R/utils.R
meanProbs.Rd
The generic function meanProbs
produces expected confidence bands
under either the t distribution or the normal sampling
distribution. This uses qnorm()
or qt()
with the mean and
standard deviation.
Usage
meanProbs(x, ...)
# Default S3 method
meanProbs(
x,
probs = seq(0, 1, 0.25),
na.rm = FALSE,
names = TRUE,
useT = TRUE,
onlyProbs = TRUE,
pred = FALSE,
n = 0L,
...
)
Arguments
- x
numeric vector whose mean and probability based confidence values are wanted, NA and NaN values are not allowed in numeric vectors unless ‘na.rm’ is ‘TRUE’.
- ...
Arguments passed to default method, allows many different methods to be applied.
- probs
numeric vector of probabilities with values in the interval from 0 to 1 .
- na.rm
logical; if true, any NA and NaN's are removed from
x
before the quantiles are computed.- names
logical; if true, the result has a names attribute.
- useT
logical; if true, use the t-distribution to calculate the confidence-based estimates. If false use the normal distribution to calculate the confidence based estimates.
- onlyProbs
logical; if true, only return the probability based confidence interval estimates, otherwise return
- pred
logical; if true use the prediction interval instead of the confidence interval
- n
integer/integerish; this is the n used to calculate the prediction or confidence interval. When
n=0
(default) use the number of non-NA
observations.
Value
By default the return has the probabilities as names (if
named) with the points where the expected distribution are
located given the sampling mean and standard deviation. If
onlyProbs=FALSE
then it would prepend mean, variance, standard
deviation, minimum, maximum and number of non-NA observations.
Details
For a single probability, p, it uses either:
mean + qt(p, df=n)*sd/sqrt(n)
or
mean + qnorm(p)*sd/sqrt(n)
The smallest observation corresponds to a probability of 0 and the largest to a probability of 1 and the mean corresponds to 0.5.
The mean and standard deviation of the sample is calculated based on Welford's method for a single pass.
This is meant to perform in the same way as quantile()
so it can
be a drop in replacement for code using quantile()
but using
distributional assumptions.
Examples
quantile(x<- rnorm(1001))
#> 0% 25% 50% 75% 100%
#> -3.43144297 -0.64931989 0.02790862 0.74082214 3.21812718
meanProbs(x)
#> 0% 25% 50% 75% 100%
#> -3.43144297 0.01587258 0.03751493 0.05915729 3.21812718
# Can get some extra statistics if you request onlyProbs=FALSE
meanProbs(x, onlyProbs=FALSE)
#> mean var sd min max
#> 0.03751493 1.02985610 1.01481826 -3.43144297 3.21812718
#> n 0% 25% 50% 75%
#> 1001.00000000 -3.43144297 0.01587258 0.03751493 0.05915729
#> 100%
#> 3.21812718
x[2] <- NA_real_
meanProbs(x, onlyProbs=FALSE)
#> mean var sd min max n 0% 25% 50% 75% 100%
#> NA NA NA NA NA NA NA NA NA NA NA
quantile(x<- rnorm(42))
#> 0% 25% 50% 75% 100%
#> -2.16834112 -0.97726328 -0.05486986 0.46855652 1.72420071
meanProbs(x)
#> 0% 25% 50% 75% 100%
#> -2.1683411 -0.3386530 -0.2264151 -0.1141771 1.7242007
meanProbs(x, useT=FALSE)
#> 0% 25% 50% 75% 100%
#> -2.1683411 -0.3376584 -0.2264151 -0.1151718 1.7242007