--- title: "power" author: "Prof. Eric A. Suess" format: html: self-contained: true --- ## Compute the power of a two sample t-test and effect size. The power of a statistical test is the probability that the test will reject a false null hypothesis. In other words, the power of a test is the probability of correctly rejecting the null hypothesis when it is false. The power of a test is equal to 1 minus the probability of a Type II error, or the probability of not rejecting the null hypothesis when it is false. The power of a test is affected by the significance level. As the significance level increases (say from 0.01 to 0.05), the power of the test increases. The power of a test is also affected by the sample size. As the sample size increases, the power of the test increases. The power of a test is also affected by the effect size. As the effect size increases, the power of the test increases. The power of a two sample t-test can be computed using the `power.t.test()` function. The `power.t.test()` function has four arguments: `n`, `delta`, `sd`, and `sig.level`. The `n` argument is the sample size. The `delta` argument is the effect size. The `sd` argument is the standard deviation. The `sig.level` argument is the significance level. The `power.t.test()` function returns a list with the following components: `n`, `delta`, `sd`, `sig.level`, `power`, and `alternative`. The `n` component is the sample size. The `delta` component is the effect size. The `sd` component is the standard deviation. The `sig.level` component is the significance level. The `power` component is the power of the test. The `alternative` component is the alternative hypothesis. The `power.t.test()` function can be used to compute the power of a two sample t-test. For example, suppose we wish to compute the power of a two sample t-test with a sample size of 10, an effect size of 0.5, a standard deviation of 1, and a significance level of 0.05. The power of the test is 0.312. ```{r} library(stats) power.t.test(n = 10, delta = 0.5, sd = 1, sig.level = 0.05) ``` The `power.t.test()` function can also be used to compute the power of a two sample t-test with a sample size of 10, an effect size of 0.5, a standard deviation of 1, and a significance level of 0.01. The power of the test is 0.231. ```{r} power.t.test(n = 10, delta = 0.5, sd = 1, sig.level = 0.01) ``` Visualize the power of a two sample t-test with a sample size of 10, an effect size of 0.5, a standard deviation of 1, and a significance level of 0.05. ```{r} library(pwr) pwr.t.test(n = 10, d = 1.5, sig.level = 0.05, type = "two.sample", alternative = "two.sided") ``` Visualize the two normal distributions with a sample size of 10, an effect size of 0.5, a standard deviation of 1, and a significance level of 0.05. ```{r} curve(dnorm(x, mean = 0, sd = 1), from = -4, to = 4, xlab = "Effect Size", ylab = "Power", main = "Power of a Two Sample t-Test", col = "blue", lwd = 2) curve(dnorm(x, mean = 1.5, sd = 1), from = -4, to = 4, xlab = "Effect Size", ylab = "Power", main = "Power of a Two Sample t-Test", col = "red", lwd = 2, add = TRUE) abline(v = 0, col = "black", lwd = 2) abline(v = 1.5, col = "black", lwd = 2) ``` Visualize the power of a two sample t-test with a sample size of 10, an effect size of 0.5, a standard deviation of 1, and a significance level of 0.01. ```{r} curve(pwr.t.test(n = 10, d = x, sig.level = 0.05, type = "two.sample", alternative = "two.sided")$power, from = -4, to = 4, xlab = "Effect Size", ylab = "Power", main = "Power of a Two Sample t-Test", col = "blue", lwd = 2) abline(h = 0.89, col = "red", lwd = 2) abline(v = 1.5, col = "red", lwd = 2) text(0, 0.8, "Power = 0.89", pos = 4, col = "red") text(0, 0.6, "Effect Size = 1.5", pos = 4, col = "red") ``` Using the `pwrss` library, we can compute the power of a two sample t-test with a sample size of 10, an effect size of 1.5, a standard deviation of 1, and a significance level of 0.05. ```{r} library(pwrss) pwr.t2n.test(n1 = 10, n2 = 10, d = 1.5, sig.level = 0.05, alternative = "two.sided") ``` Using the `pwrss` library, we can compute the sample size of a two sample t-test with a power of 0.90, an effect size of 1.5, a standard deviation of 1, and a significance level of 0.05. ```{r} library(pwrss) pwrss.t.2means(mu1 = 0, mu2 = 1.5, power = 0.90, alternative = "not equal") ```