R/varCI.R

Defines functions varCI

Documented in varCI

#' Confidence interval for the population variance
#'
#' @description
#' A function 'varCI()' is used to calculate a confidence interval for the population variance
#' when given a numerical vector
#'
#' @param x    a numeric vector
#' @param conf.level   a confidence level, default is \code{0.95}; the user can change to \code{0.90} or otherwise
#'
#' @importFrom stats qchisq sd
#'
#' @returns a confidence interval of the population variance
#' @export
#'
#' @references
#' Rattanalertnusorn, A. (2024). R and its application (3rd ed.). TPN press.
#' <https://www.researchgate.net/publication/371944275_porkaermxarlaeakarprayuktchingan_R_and_its_applications>.
#'
#' @examples
#' heigth <- c(155.5, 165.5, 170, 164.5, 180, 162, 173, 158.5, 168.5, 175, 164.5, 167)
#' varCI(x=heigth,conf.level = 0.90)
#'
varCI <- function(x,conf.level=0.95){
  n <- length(x)
  s <- sd(x)
  ci <- conf.level
  alpdiv2 <- (1-ci)/2
  v <- n-1
  q <- qchisq(c(alpdiv2,1-alpdiv2),df=v)
  vCI <- c( (n-1)*(s^2)/q[2], (n-1)*(s^2)/q[1] )
  return(vCI)
}

Try the Mychisq package in your browser

Any scripts or data that you put into this service are public.

Mychisq documentation built on June 22, 2026, 9:08 a.m.