R/sigmatest.R

Defines functions sigmatest

Documented in sigmatest

#' Hypothesis testing for the population variance
#'
#' @description
#' The function 'sigmatest()' is used to test the hypothesis about the population variance
#'
#' @param x            a numeric vector
#' @param sigma        a population standard deviation under the null hypothesis
#' @param alternative  an alternative hypothesis, default is "two.sided"; the user can change to "greater" or "less"
#' @param conf.level   a confidence level for hypothesis testing, default is \code{0.95}; the user can change to \code{0.90} or otherwise
#' @param verbose      a special variable is used for protected unexpected print output; the default is TRUE, do NOT change
#'
#' @importFrom stats pchisq qchisq var
#'
#' @returns output of one sample Chi-squared test for 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)
#' sigmatest(x=heigth, sigma = 3, alternative = "two.sided", conf.level = 0.95)
#'
sigmatest <- function(x, sigma, alternative = c("two.sided"), conf.level = 0.95,verbose = TRUE){

  if(verbose){
    n <- length(x)
    v <- var(x)
    alpha <- 1-conf.level
    sigZero <- sigma
    X_squared <- ((n-1)*v)/(sigZero^2)
    dfchi <- n-1
    if(alternative==c("two.sided")){
      C_value <- c(qchisq(alpha/2,df=dfchi),qchisq(1-alpha/2,df=dfchi))
      P_value<-  pchisq(X_squared,df=dfchi,lower.tail = FALSE)*2
    } else if(alternative==c("greater")){
      C_value <- qchisq(1-alpha,df=dfchi)
      P_value<-  pchisq(X_squared,df=dfchi,lower.tail = FALSE)
    }else if(alternative==c("less")){
      C_value <- qchisq(alpha,df=dfchi)
      P_value<-  1-pchisq(X_squared,df=dfchi,lower.tail = FALSE)
    }else { warning("Error in the specified alternative argument") }

    printoutput <- function(alternative,verbose = TRUE){
      if(verbose){
        cat("\t\t One sample Chi-squared test for variance\n")
        cat("\n",paste("data:","x"),"\n")
        if (alternative==c("two.sided")){
          cat("X-squared = ",signif(X_squared,4)," ","df = ",dfchi," ",
              "Two critical-values = ",paste(signif(C_value[1],4),",",signif(C_value[2],4))," ",
              "p-value = ",signif(P_value,4),"\n")
          cat("Alternative hypothesis: true variance is not equal to ", sigZero^2,"\n")
        }else if(alternative==c("greater")){
          cat("X-squared = ",signif(X_squared,4)," ","df = ",dfchi," ",
              "One critical-value = ",signif(C_value,4)," ",
              "p-value = ",signif(P_value,4),"\n")
          cat("Alternative hypothesis: true variance is greater than ", sigZero^2,"\n")
        }else if(alternative==c("less")){
          cat("X-squared = ",signif(X_squared,4)," ","df = ",dfchi," ",
              "One critical-value = ",signif(C_value,4)," ",
              "p-value = ",signif(P_value,4),"\n")
          cat("Alternative hypothesis: true variance is less than ", sigZero^2,"\n")
        }else { warning("Error in the specified alternative argument") }
        cat("sample estimated variance = ",signif(v,8),"\n\n")
      }
    }
  }
  printoutput(alternative)
  result_list=list(data=x,n=n,Chisq_val=signif(X_squared,4),DF=dfchi,Critical_value=signif(C_value,4),
                   P_value=signif(P_value,4),Alter_hypothesis=alternative,Sample_var=signif(v,8))
  cat("*** List of variables for the Chi-squared test of variance ***\n")
  return(result_list)
}

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.