# This is R code for computing sample # sizes for 2-sample t-tests. # # It is posted as power2t.R # Specity the Type I error level alpha <- .05 # Specify the power of the test power <- .90 # Compute Type II error level beta <- 1 - power # Specify difference in means to be detected # divided by the standard deviation for one # observation d <- 0.8 # Specify the ration of sample sizes n1/n2 ratio <- 1.0 # Computations for a two sided alternative n2 <- ((1+(1/ratio))*(qnorm(alpha/2) + qnorm(beta))^2)/(d*d); n1 <- ratio*n2; # Round up to next largest interger n2 <- ceiling(n2) n1 <- ceiling(n1) # Compute degrees of freedom v <- n1+n2-2 n2 <- ((1+(1/ratio))*(qt(alpha/2,v) + qt(beta,v))^2)/(d*d) n1 <- ratio*n2 n2 <- ceiling(n2) n1 <- ceiling(n1) cat("Sample sizes for two sample t-tests", "\n \n Two-sided alternative", "\n \n alpha=",alpha, "\n power=", power, "\n scaled difference=",d, "\n ratio of sample sizes=", ratio, "\n n1=",n1, "\n n2=",n2) # Compute sample sizes for a one-sided two-sample t-test n2 <- ((1+(1/ratio))*(qnorm(alpha) + qnorm(beta))^2)/(d*d) n1 <- ratio*n2 n2 <- ceiling(n2) n1 <- ceiling(n1) v <- n1+n2-2 n2 <- ((1+(1/ratio))*(qt(alpha,v) + qt(beta,v))^2)/(d*d) n1 <- ratio*n2 n2 <- ceiling(n2) n1 <- ceiling(n1) cat("Sample sizes for two sample t-tests", "\n \n One-sided alternative", "\n \n alpha=",alpha, "\n power=", power, "\n scaled difference=",d, "\n ratio of sample sizes=", ratio, "\n n1=",n1, "\n n2=",n2)