R/z.test.R

#' z.test function
#'
#R does not have a z-test function built-in, so Professor Reuning-Scherer created one to match the t.test() function.
#The function takes four arguments: your data (as a vector of values),
#the true standard deviation,  your chosen level of confidence,
#and the type of hypothesis test ("less", "greater", or "two.sided").
#'@param dataVector Your data, as a vector of values.
#'@param sigma The true standard deviation.
#'@param confidence The chosen level of confidence; defaults to 95%.
#'
#'@keywords z.test
#'@export
z.test <- function(dataVector, sigma, confidence = .95, alternative = "two.sided"){
  names(sigma) <- "sigma" #Create a name for your sigma argument to enhance readability of output
  n <- length(dataVector) #Get n
  se <- sigma/sqrt(length(dataVector)); names(se) <- "se" #Compute and name standard error
  myMean <- mean(dataVector) #Get the sample mean
  #Set up conditional reporting based the tail choice of the user...
  if(alternative == "less"){
    criticalZ <- qnorm((1-confidence), lower.tail = T); names(criticalZ) <- "z-value" #Compute critical z-value used to evaluate statistical significance. One-tailed, testing for greater than or equal to.
    CI95 <- paste("(-Inf, ", round(myMean+criticalZ*se,4),")", sep=""); names(CI95) <- "CI_95%" #Compute and name a confidence interval per page 382
  } else if (alternative == "greater"){
    criticalZ <- qnorm((1-confidence), lower.tail = F); names(criticalZ) <- "z-value" #Compute critical z-value used to evaluate statistical significance. One-tailed, testing for less than or equal to.
    CI95 <- paste("(", round(myMean-criticalZ*se,4),", Inf)", sep=""); names(CI95) <- "CI_95%" #Compute and name a confidence interval per page 382
  } else if (alternative=="two.sided"){
    criticalZ <- qnorm((1-confidence)/2, lower.tail = F); names(criticalZ) <- "z-value" #Compute critical z-value used to evaluate statistical significance. Two-tailed, testing for any kind of difference.
    CI95 <- paste("(",round(myMean-criticalZ*se,4),", ", round(myMean+criticalZ*se,4),")", sep=""); names(CI95) <- "CI_95%" #Compute and name a confidence interval per page 382
  }
  return(data.frame("v35", n, sigma, se, myMean, criticalZ, CI95)) #Return from the function a printout of the useful information
}
18kimn/yalestats documentation built on May 9, 2019, 2:17 a.m.