How to calculate power with welch t-test formula

This is based on a two-tailed T-test when the two groups have different mean and variance. 

power_cal_welch_t_test <- function
(mean_control,mean_treatment,
sample_size_control,sample_size_treatment,
sd_control,sd_treatment)
{
  m2 <- mean_treatment
  m1 <- mean_control
  n1 <- sample_size_control
  n2 <- sample_size_treatment
  s1 <- sd_control
  s2 <- sd_treatment
  true_mean <- m2-m1
  se_pool <- sqrt(sd1*sd1/n1+sd2*sd2/n2)
## Below is the welch t-test degree of freedom
  degree_of_up <- (sd1*sd1/n1 + sd2*sd2/n2)^2
  degree_of_buttom <- (sd1*sd1)^2/(n1*n1*(n1-1)) + (sd2*sd2)^2/(n2*n2*(n2-1))
  degree_of <- degree_of_up/degree_of_buttom
 ## End of calculating degree of freedom

  t_critical <- abs(qt(0.025,df=degree_of))
  margin_error <- t_critical*se_pool

  t_left <- (-true_mean+margin_error)/se_pool
  t_right <- (-true_mean-margin_error)/se_pool
  power_val <- 1 - pt(t_left, degree_of) + pt(t_right, degree_of)
  ## pt is the specicial function in R to calculate the t-distribution value
  return(power_val)
}

Comments