Example R programs and commands 6. Use, power and sample size in Student t tests # All lines preceded by the "#" character are my comments. # All other left-justified lines are my input. # All other indented lines are the R program output. # Perform a one-sample, two-tailed t-test of H_0: mu=1.7 on data vector x: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); t.test(x,mu=1.7) One Sample t-test data: x t = 2.3334, df = 11, p-value = 0.03963 alternative hypothesis: true mean is not equal to 1.7 95 percent confidence interval: 1.750127 3.416539 sample estimates: mean of x 2.583333 # Perform a one-sample, one-tailed t-test of H_A: mu>1.7 on data vector x: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); t.test(x,mu=1.7, alternative="greater") One Sample t-test data: x t = 2.3334, df = 11, p-value = 0.01982 alternative hypothesis: true mean is greater than 1.7 95 percent confidence interval: 1.903482 Inf sample estimates: mean of x 2.583333 # Perform a two-sample, two-tailed t-test of H_0:mu_x=mu_y on data vectors x,y # of unequal length, assuming both populations have the same variance: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); y<-c(3,6,4,5,7,5,6); t.test(x,y, var.equal=TRUE) Two Sample t-test data: x and y t = -4.0666, df = 17, p-value = 0.0008027 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -3.887451 -1.231596 sample estimates: mean of x mean of y 2.583333 5.142857 # ...and using Welch's correction for not assuming equal variance: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); y<-c(3,6,4,5,7,5,6); t.test(x,y, var.equal=FALSE) Welch Two Sample t-test data: x and y t = -4.0378, df = 12.415, p-value = 0.001540 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -3.935537 -1.183511 sample estimates: mean of x mean of y 2.583333 5.142857 # Perform a two-sample, one-tailed t-test of H_A:mu_x-mu_y < -1.5 on data vectors x,y # of unequal length, assuming both populations have the same variance: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); y<-c(3,6,4,5,7,5,6); t.test(x,y, mu= -1.5, alternative="less", var.equal=TRUE) Two Sample t-test data: x and y t = -1.6834, df = 17, p-value = 0.05529 alternative hypothesis: true difference in means is less than -1.5 95 percent confidence interval: -Inf -1.464607 sample estimates: mean of x mean of y 2.583333 5.142857 # ...and using Welch's correction for not assuming equal variance: t.test(x,y, mu= -1.5,alternative="less", var.equal=FALSE) Welch Two Sample t-test data: x and y t = -1.6715, df = 12.415, p-value = 0.05981 alternative hypothesis: true difference in means is less than -1.5 95 percent confidence interval: -Inf -1.432897 sample estimates: mean of x mean of y 2.583333 5.142857 # Perform a paired-sample, one-tailed t-test of H_A:mu_d < -1.5 on data vectors x,y # of equal length: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); y<-c(3,6,4,2,6,5,5,7,5,6,4,6); t.test(x,y, mu= -1.5, paired=TRUE, alternative="less") Paired t-test data: x and y t = -2.011, df = 11, p-value = 0.03474 alternative hypothesis: true difference in means is less than -1.5 95 percent confidence interval: -Inf -1.589141 sample estimates: mean of the differences -2.333333 # Perform a paired-sample, two-tailed t-test of H_0:mu_d = 0 on data vectors x,y # of equal length: x<-c(1,4,2,2,3,1,5,3,4,2,1,3); y<-c(3,6,4,2,6,5,5,7,5,6,4,6); t.test(x,y, paired=TRUE) # mu=0 and alternative="two.sided" are defaults Paired t-test data: x and y t = -5.6308, df = 11, p-value = 0.0001531 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -3.245395 -1.421272 sample estimates: mean of the differences -2.333333 # Find the power in a two-tailed two-sample Student t test with n1=n2=n=33 # samples per population, a true difference |mu1-mu2|=1.7, equal standard # deviations s1=s2=2.1, using a Type I error probability of alpha=0.05: power.t.test(n=33,delta=1.7,sd=2.1, sig.level=0.05, power=NULL) Two-sample t test power calculation n = 33 delta = 1.7 sd = 2.1 sig.level = 0.05 power = 0.8994822 alternative = two.sided NOTE: n is number in *each* group # Find the minimum sample size, if the desired power level is 95%: power.t.test(n=NULL,delta=1.7,sd=2.1, sig.level=0.05, power=0.95) Two-sample t test power calculation n = 40.64486 delta = 1.7 sd = 2.1 sig.level = 0.05 power = 0.95 alternative = two.sided NOTE: n is number in *each* group # Find the minimum detectable difference with 50 samples, same distribution # parameters, alpha=0.05 and power=0.95: power.t.test(n=50,delta=NULL,sd=2.1, sig.level=0.05, power=0.95) Two-sample t test power calculation n = 50 delta = 1.529104 sd = 2.1 sig.level = 0.05 power = 0.95 alternative = two.sided NOTE: n is number in *each* group # Given d, find the the number n of samples needed from an N(mu,sigma) # probability space so that the average m of the n samples makes a 95% # confidence interval [m-d,m+d] for mu: variance <- 12.8; sigma <- sqrt(variance) # example variance and its s.d. conf <- 0.95; sig <- 1-conf # 95% confidence is significance level 5% err <- 2.0; # allowed error; we want to estimate mu within err power.t.test(n=NULL, # this is computed and returned sig.level=sig, # 1 - confidence delta=err, # half-width of conf. interval sd=sigma, # estimated standard deviation power=0.50, # fixed value! type="one.sample") # required flag for non-default # This "magic" value power=0.50 is what it takes to get the right value of n