R/nonparametric_test.R

Defines functions nonparametric_test

Documented in nonparametric_test

#' Non-Parametric test for independence for an A/B test
#'
#'This program will calculate the response rate for a test vs control experiment and conduct a chi-squared statistical test
#'for independence to determine if there is a significant difference in response rates between the test and control groups.
#'
#' @param experiment_name a character expression for the name of the campaign or experiment
#' @param n_test an integer variable for the number of test subjects
#' @param n_control an integer variable for the number of control subjects
#' @param n_test_responders an integer variable for the number of test subjects who responded
#' @param n_control_responders an integer variable for the number of control subjects who responded
#' @return A response table based on the input parameters, a proportions table, a mosaic plot of the results and the results of a chi-squared test for independence.
#' @author Dean Marchiori
#' @details
#' This function accepts the numbers of subjects in a campaign or experiment, along with the responders. It is designed to handle
#' just the case of a two-way contingency table and will use the null hypothesis that the test and control groups are independent across
#' the responders and non-responders. The test for independence will be via the Pearson's Chi-Squared Test for Independence.
#' @export
#'


nonparametric_test <- function(experiment_name, n_test, n_control, n_test_responders, n_control_responders) {

  #define parameters
  n_test_nonresp <- (n_test - n_test_responders)
  n_control_nonresp <- (n_control -n_control_responders)

  #creates a matrix to display results
  results <- matrix(c(n_test_responders, n_control_responders, n_test_nonresp, n_control_nonresp), nrow = 2, ncol = 2)
  rownames(results) <- c("Test", "Control")
  colnames(results) <- c("Responder","Non-Responder")

  #table
  results <- as.table(results)

  #proportions
  proportions <- prop.table(results,1)

  #performs chi-square test
  chisqresult <- chisq.test(results,correct = FALSE)

  #prints all results
  print("Table of Responses")
  print(results)
  print("Table of Responses - %")
  print(proportions)
  print(chisqresult)

  #plot
  mosaicplot(results, color = c("Dark Blue","light blue"), main= paste(experiment_name," - Response Testing"), xlab= "Cell",ylab= "Reponse")

}
deanmarchiori/UpliftTestR documentation built on May 12, 2017, 9:53 p.m.