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, ...)
# S3 method for default
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.403225585 -0.642325341 -0.001837139 0.656154718 2.780013343
meanProbs(x)
#> 0% 25% 50% 75% 100%
#> -3.403225585 -0.023025596 -0.002431956 0.018161685 2.780013343
# Can get some extra statistics if you request onlyProbs=FALSE
meanProbs(x, onlyProbs=FALSE)
#> mean var sd min max
#> -2.431956e-03 9.324674e-01 9.656435e-01 -3.403226e+00 2.780013e+00
#> n 0% 25% 50% 75%
#> 1.001000e+03 -3.403226e+00 -2.302560e-02 -2.431956e-03 1.816168e-02
#> 100%
#> 2.780013e+00
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.1838542 -0.8532155 0.1476959 0.8088171 1.5847511
meanProbs(x)
#> 0% 25% 50% 75% 100%
#> -2.18385418 -0.18647291 -0.07532481 0.03582330 1.58475107
meanProbs(x, useT=FALSE)
#> 0% 25% 50% 75% 100%
#> -2.18385418 -0.18548788 -0.07532481 0.03483827 1.58475107