Learn R Programming

Ecfun (version 0.2-0)

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, ...)

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.

Value

a matrix with columns "lower" and "upper", nrow = the longest of the lengths of object, parm, and level, and an attribute "level".

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
# NOT RUN {
##
## 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
# }
# NOT RUN {
all.equal(CI.s, sqrt(CI.v))
# }
# NOT RUN {
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
# }
# NOT RUN {
max(abs(dCI))<0.0102
# }
# NOT RUN {
##
## 2.  test df attributes
##
v <- c(1,1,4)
attr(v, 'df.') <- c(1, 9, 9)
class(v) <- 'var'
vCI <- confint(v)

# check 
# }
# NOT RUN {
all.equal(vCI, CI.v)
# }
# NOT RUN {
s <- sqrt(v)
class(s) <- 'sd'
sCI <- confint(s)

# check 
# }
# NOT RUN {
all.equal(sCI, CI.s)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab