R/ResultsStandardizeR.R

Defines functions ResultsStandardizeR

Documented in ResultsStandardizeR

#'Calculate cohen's d, variance, and standard errors.
#'
#'\code{ResutsStandardizeR} Converts any of the following statistics into an
#'estimate of Cohen's d s, variance, and standard error: difference in means,
#'difference in differences, t-tests, F-tests, and regression coefficients into
#'an estimated standard effect size and measure of uncertainty. The function
#'requires an effect statistic, an estimate of the standard deviation of the
#'dependent variable, an N for the control group, and an N for the treatment
#'group. Note: t-tests, F-tests, and standardized regression coefficients do not
#'require a standard deviation.
#'
#'All calculations taken from Cooper, Hedges, and Valentine (2009)
#'@seealso Cooper, Hedges, and Valentine (2009), "The Handbook of Research
#'  Synthesis and Meta-Analysis, Second Edition", chapter 12.
#'
#'@param eff_type Write the effect type you are converting. Current options are
#'  "d_i_d" (difference-in-differences), "d_i_m" (difference-in-means),
#'  "st_reg_coef" (standardized regression coefficient), "reg_coef" (regression
#'  coefficient), t_test" (t-test), and "f-test" (f-test). Note: you must
#'  include quotes.
#'@param u_s_d Unstandardized effect size.
#'@param pop_sd Estimate of the population standard deviation for the dependent
#'  variable. Not necessary if the effect size is "st_reg_coef" (assumed to be
#'  1), "t_test" or "f_test."
#'@param n_t Number of subjects in the treatment group.
#'@param n_c Number of subjects in the control group.
#'
#'@return results_table is a dataframe that displays cohen's d, variance, and
#'  standard error.
#'
#'@examples
#' ```ResultsStandardizeR(eff_type = "reg_coef",
#'  u_s_d = .366, pop_sd = 1.074,
#'  n_t = 35, n_c = 1036)```
#' ```ResultsStandardizeR(eff_type = "d_i_d" ,
#'  u_s_d = (111.1 - 92.7) - (102.9 - 99.4) ,
#'  ctrl_sd = 28.28, n_t = 15, n_c = 15)```
#'@export
ResultsStandardizeR <- function(eff_type, u_s_d, pop_sd, n_t, n_c,
                                write_to_csv = NULL, append = NULL){
  # difference in differences
  if (eff_type == "d_i_d"){
    d <- round(u_s_d / pop_sd, digits = 3)
  }
  # difference in means
  else if (eff_type == "d_i_m"){
    d <- round(u_s_d / pop_sd, digits = 3)
  }
  # standardized regression coefficient
  else if (eff_type == "st_reg_coef"){
    d <- u_s_d
  }

  # regression coefficient
  else if (eff_type == "reg_coef"){
    d <- round(u_s_d / pop_sd, digits = 3)
  }

  # t test
  else if (eff_type == "t_test"){
    d <- round(u_s_d * sqrt( (n_t + n_c ) / (n_t * n_c) ), digits = 3)
  }
  # f_test
  else if (eff_type == "f_test"){
    d <- round(sqrt( ( u_s_d * (n_t + n_c) ) / (n_t * n_c) ), digits = 3)
  }
  # Next, compute variance of the estimated effect size

  ust_var_d <- (((n_t + n_c)
                 / (n_t * n_c))
                +
                  ((d ^ 2) / (2 * (n_t + n_c)) )
  )
  # Apply hedge's g correction
  hedge_g <- 1 - (3
                  /
                    (4 * (n_t + n_c - 2 ) - 1))

  var_d <- round((hedge_g ^ 2) * ust_var_d, digits = 3)

  # standard error is the square root of variance
  st_err_g <- round(sqrt(var_d), digits = 3)

  # print everything out
  results <- c(d, var_d, st_err_g)

  col_names <- c("Standardized Effect (Cohen's D)",
                 "Variance of D", "Standard Error of D")

  results_table <- data.frame(col_names, results)

  return(results_table)
}
setgree/ResultsStandardizeR documentation built on June 2, 2020, 11:48 a.m.