R/deltamad_A.R

Defines functions deltamad_A

Documented in deltamad_A

#' Generate the mean of the contaminated group, the standard deviation of the contaminated group, and delta MAD for one randomly distributed data set against a normal data set contaminated with another normal data set
#'
#' Generates the mean of the contaminated group, the standard deviation of the contaminated group, and delta MAD effect size for one randomly distributed data set against a normal data set contaminated with another normal data set with defined characteristics (number of subjects, percent of subjects with x mean and y standard deviation, percent of subjects with z mean and a standard deviation, mean of the first data set, x mean, z mean, standard deviation of the second data set, y standard deviation, and a standard deviation)
#'
#' @param num0 number of subjects
#' @param num1 percent/decimal of subjects for the first group
#' @param num2 percent/decimal of subjects for the second group
#' @param bef mean of the original group
#' @param aft1 mean for the first group
#' @param aft2 mean for the second group
#' @param stdev0 standard deviation of the original group
#' @param stdev1 standard deviation for the first group
#' @param stdev2 standard deviation mean for the second group
#'
#' @return vector with mean of the contaminated group, standard deviation of the contaminated group, and delta MAD
#' @importFrom stats rnorm sd
#' @export
#'
#' @examples
#' deltamad_A(75,0.8,0.2,0,1,0.5,1,1,2)
deltamad_A <- function(num0,num1,num2,bef,aft1,aft2,stdev0,stdev1,stdev2) {

  # makes sure that the contaminated group's percentages
  # add up to 100% because you can't have 2 groups adding
  # to say 70% or 135% of the total subjects
  if((num1+num2) == 1){

    # simulating before
    # creates a normal distribution of user input number of subjects,
    # mean, and standard deviation
    before <- rnorm(num0, bef, stdev0)

    # simulating after/contaminated groups
    # creates a normal distribution of user input number of subjects,
    # mean, and standard deviation
    # also takes into account percentage of contamination
    # (user input percentage of the first group, percentage of the second group adding to 100%)
    after1 <- rnorm(num0*num1, aft1, stdev1)
    after2 <- rnorm(num0*num2, aft2, stdev2)

    # combines distributions into one group
    after <- c(after1, after2)

    # finds the mean and standard deviation of the contaminated group
    meanaft <- mean(after)
    sdaft <- sd(after)

  # denominator of the equation to get MAD pooled
  # takes the number of subjects minus 1 multiplied by the median absolute difference
  # of the after group and then takes the number of subjects minus 1 multiplied by
  # the median absolute difference of the before group and then adds them together and
  # then divides by the number of subjects added to itself minus 2
  MADpool <- (((num0-1)*mad(after))+((num0-1)*mad(before)))/((num0+num0)-2)

  # calculating delta MAD
  # takes the after group's median, and takes the before group's median
  # subtracts the after group's median from the after group's median
  # divides by the pooled median absolute difference found above
  deltaMAD <- (abs((median(after))-(median(before)))/MADpool)

  # creates a vector of calculated delta MAD, mean of the after
  # group, and standard deviation of the after group
  rtrn <- c(meanaft, sdaft, deltaMAD)

  # returns the vector
  return(rtrn)
  } else {
    x = "Num1 and Num2 must sum to 100% or 1.00."
    print(x)
  }
}
dakthomps00/researchfunctions documentation built on March 19, 2022, 10:50 a.m.