Suppose I have two samples with the following statistics and I want to determine if there is a difference in their means.
First:
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0 39.0 39.0 38.6 40.0 48.0
Others:
Min. 1st Qu. Median Mean 3rd Qu. Max.
4.00 39.00 39.00 38.51 39.00 50.00
Performing a t.test
in R I get:
t.test(first, other)
Welch Two Sample t-test
data: first and other
t = 1.3771, df = 8981.579, p-value = 0.1685
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.03304794 0.18912248
sample estimates:
mean of x mean of y 38.60095 38.52291
If our $\alpha$ was 0.05 we would not reject the null hypothesis.
But suppose we assume that the means are not the same and the actual difference in the means is 0.078. How would I go about computing the power of this test given an $\alpha$ of 0.05?
I believe I can approximate power by sampling from the distributions and comparing the means and counting the number of times the null hypothesis would be rejected with a given $\alpha$ but I want to confirm that my simulation is correct.
Following @whuber's suggestion from this post I tried computing the power using the following:
first.mean = 38.60095
first.sd = 2.791901
first.count = 4413
other.mean = 38.51326
other.sd = 2.634525
other.count = 4735
z = replicate(10^3, {
first = rnorm(first.count, mean=first.mean, sd=first.sd)
other = rnorm(other.count, mean=other.mean, sd=other.sd)
t.test(first, other)$p.value
})
length(z[z < 0.05])/length(z) # mean(z < 0.05)
I get 0.30 (thanks for catching the typo @John). Is there a way to confirm this analytically?
This is from Allen Downey's ThinkStats book chapter 7 exercise 7. I took a stab at simulating the power and now want to confirm my result analytically if possible.