varwci {ConfIntVariance}R Documentation

varwci

Description

Surround the univariate variance estimator of the function var with a confidence interval, not assuming normality

Usage

varwci(x, conf.level=0.95)

Arguments

x

A one-dimensional numeric vector

conf.level

The confidence level for the confidence interval. Defaults to 0.95

Value

Returns a vector with two entries: the lower and the upper bound of the confidence interval, and the following attributes:

point.estimator

The usual sample variance at the center of the interval

conf.level

The confidence level used

var.SampleVariance

The estimated variance of the sample variance

Warning

On very small sample sizes, the result is NA because there is insufficient information on the variance estimation

Note

The underlying theory is that of U-statistics. See Hoeffding 1948.

Author(s)

Mathias Fuchs

References

http://dx.doi.org/10.1080/15598608.2016.1158675 and https://mathiasfuchs.de/b3.html

Examples

##
## Example: throwing a dice
## 

# throw a dice 100 times
s <- sample(6, 100, replace=TRUE)

# the standard point estimator for the variance
print(var(s))

# contains the true value 2.9166 with a probability of 95 percent.
print(varwci(s))

##
## Check the coverage probability of the confidence interval
##

                                        # True quantities that do not depend on n
trueMeanOfDice <- mean(1:6)
trueVarianceOfDice <- mean((1:6)^2) - trueMeanOfDice^2

## see package description for more details
                                        # number of times we draw a
                                        # sample and compute a confidence interval
N <- 1e4
trueValueCovered <- rep(NA, N)
for (i in 1:N) {
    if (i %% 1e3 == 0) print(i)
                                        # throw a dice 100 times
    x <- sample(6, 100, replace=TRUE)
                                        # compute our confidence interval
    ci <- varwci(x)
                                        # We know that the true variance
                                        # of the dice is 91/6 - 49/4 = 2.916666...
                                        # did the confidence interval contain the correct value?
    trueValueCovered[i] <- (trueVarianceOfDice > ci[1] && trueVarianceOfDice < ci[2])
}

                                        # Result of simulation study: should be close to 0.95
print(mean(trueValueCovered))

[Package ConfIntVariance version 1.0.2 Index]