Learn R Programming

Ecfun (version 0.3-2)

confint.var: Confidence interval for sample variance or standard deviation

Description

Computes the standard normal (i.e., chi-square) confidence intervals for a sample variance or standard deviation.

Usage

# S3 method for var
confint(object, parm, level=0.95, ...) 
# S3 method for sd
confint(object, parm, level=0.95, ...)

Value

a matrix with columns "lower" and "upper",

nrow = the longest of the lengths of object, parm, and level, and an attribute "level".

Arguments

object

a numeric vector possibly with a df or df.residuals attribute assumed to represent a sample variance, possibly computed as root mean square of residuals from a model.

parm

degrees of freedom in the estimated variance or standard deviation.

level

the confidence level required

...

optional arguments not used.

Author

Spencer Graves

Details

1. If object is not numeric, throw an error.

2. If parm is missing, look for an attribute of object starting with df. If present, use that for parm. If parm is absent or not numeric, throw an error.

3. replicate object, parm, and level to the same length. Issue a warning if the longest is not a multiple of the others.

4. alph2 <- (1-level)/2

5. Qntls <- cbind(lower=qchisq(alph2, parm, lower=FALSE), upper=qchisq(alph2, parm))

6. CI <- (object*parm/Qntls)

7. attr(CI, 'level') <- Level

7. return(CI)

References

Wikipedia, "Standard deviation", accessed 2016-07-06.

See Also

cor.test, VarCI

Examples

Run this code
##
## 1.  simple examples 
##
(CI.v <- confint.var(c(1,1,4), c(1, 9, 9))) 
(CI.s <- confint.sd(c(1,1,2), c(1, 9, 9))) 

# Compare with the examples on Wikipedia
stopifnot(
all.equal(CI.s, sqrt(CI.v))
)

WikipEx <- t(matrix(c(0.45, 31.9, 0.69, 1.83, 1.38, 3.66), 
                  nrow=2))
colnames(WikipEx) <- c('lower', 'upper')

(dCI <- (CI.s-WikipEx))
#Confirm within 2-digit roundoff
stopifnot(
max(abs(dCI))<0.0102
)

##
## 2.  test df attributes
##
v <- c(1,1,4)
attr(v, 'df.') <- c(1, 9, 9)
class(v) <- 'var'
vCI <- confint(v)

# check 
stopifnot(
all.equal(vCI, CI.v)
)

s <- sqrt(v)
class(s) <- 'sd'
sCI <- confint(s)

# check 
stopifnot(
all.equal(sCI, CI.s)
)

Run the code above in your browser using DataLab