R/z.test.onesample.simple.R

Defines functions z.test.onesample.simple

Documented in z.test.onesample.simple

#' One Sample Z Test for Mean  
#' 
#' Calculate one sample z test for mean.
#'
#' @param x Vector - sample values to be used for mean calculation.
#' @param sample.mean Scalar - sample mean to be tested.
#' @param known.population.variance Scalar - population variance.
#' @param sample.size Scalar - sample size.
#' @param null.hypothesis.mean Scalar - null hypothesis mean value, sample.mean is tested against this value.
#' @param alternative The alternative hypothesis to use for the test computation.
#' @param conf.level The confidence level for this test, between 0 and 1.
#' @param finite.population.N Scalar - the population size if finite population adjustment needs to be made.
#'
#' @aliases z.test.onesample
#'
#' @return The results of the statistical test.
z.test.onesample.simple<-function(sample.mean
                                  ,known.population.variance = 1
                                  ,sample.size
                                  ,null.hypothesis.mean = 0
                                  ,alternative = c("two.sided","less","greater")
                                  ,conf.level = 0.95
                                  ,finite.population.N = NA
) {
  validate.htest.alternative(alternative = alternative)
  se.est  <- sqrt(known.population.variance/sample.size)
  
  if (!is.na(finite.population.N)) {
    se.est <- se.est * sqrt((finite.population.N-sample.size)/(finite.population.N-1))
  }
  
  z       <- (sample.mean-null.hypothesis.mean)/se.est
  cv      <- qnorm(conf.level+(1-conf.level)/2)
  
  z.upper <- sample.mean + cv*se.est
  z.lower <- sample.mean - cv*se.est
  
  p.value <- if (alternative[1] == "two.sided") {
    tmp<-pnorm(z)
    min(tmp,1-tmp)*2
  } else if (alternative[1] == "greater") {
    pnorm(z,lower.tail = FALSE)
  } else if (alternative[1] == "less") {
    pnorm(z,lower.tail = TRUE)
  } else {
    NA
  }
  
  pow <- power.mean.z.onesample(sample.size = sample.size
                               ,effect.size = sample.mean-null.hypothesis.mean
                               ,variance = known.population.variance
                               ,alpha = 1-conf.level
                               ,alternative = alternative
                               ,details = F)
  
  retval<-list(data.name   = "sample mean, population variance, sample size",
               statistic   = z, 
               estimate    = c(sample.mean = sample.mean 
                               ,sample.size = sample.size
                               ,se.est = se.est
                               ,power = pow
                               ),
               parameter   = null.hypothesis.mean,
               p.value     = p.value,
               null.value  = null.hypothesis.mean,
               alternative = alternative[1],
               method      = "One-Sample Z Test For Means",
               conf.int    = c(z.lower,z.upper)
  )
  
  #names(retval$estimate) <- c("sample mean")
  names(retval$statistic) <- "z statistic"
  names(retval$null.value) <- "mean"
  names(retval$parameter) <- "null hypothesis mean"
  attr(retval$conf.int, "conf.level")  <- conf.level
  
  class(retval)<-"htest"
  retval
  
}


#z.test.onesample.simple(sample.mean = .5, known.population.variance = 1, sample.size = 4,null.hypothesis.mean = 0, alternative = "less")
burrm/lolcat documentation built on Sept. 15, 2023, 11:35 a.m.