| stat_power | R Documentation |
Compute the statistical power (probability of correctly rejecting the null hypothesis) for a given sample size, effect size, and significance level. Supports multiple test types including t-tests, ANOVA, proportion tests, correlation tests, and chi-square tests.
stat_power(
n,
effect_size,
test = c("t.test", "anova", "proportion", "correlation", "chisq"),
type = c("two.sample", "one.sample", "paired"),
alternative = c("two.sided", "less", "greater"),
alpha = 0.05,
k = NULL,
df = NULL,
plot = TRUE,
plot_range = NULL,
palette = "qual_vivid",
verbose = TRUE
)
n |
Integer. Sample size. Interpretation depends on test type:
|
effect_size |
Numeric. Effect size appropriate for the test (must be positive):
|
test |
Character. Type of statistical test: "t.test" (default), "anova", "proportion", "correlation", or "chisq". |
type |
Character. For t-tests only: "two.sample" (default), "one.sample", or "paired". |
alternative |
Character. Direction of alternative hypothesis: "two.sided" (default), "less", or "greater". Note: Only applicable to t-tests, proportion tests, and correlation tests. Ignored for ANOVA and chi-square tests (which are inherently non-directional). |
alpha |
Numeric. Significance level (Type I error rate). Default: 0.05. |
k |
Integer. Number of groups (required for ANOVA). |
df |
Integer. Degrees of freedom (required for chi-square tests). |
plot |
Logical. Generate a power curve plot? Default: TRUE. |
plot_range |
Numeric vector of length 2. Range of sample sizes for the power curve. If NULL (default), automatically determined. |
palette |
Character. evanverse palette name for the plot. Default: "qual_vivid". |
verbose |
Logical. Print detailed diagnostic information? Default: TRUE. |
Statistical power is the probability of correctly rejecting the null hypothesis when it is false (i.e., detecting a true effect). Conventionally, a power of 0.8 (80%) is considered adequate, though higher power (0.9 or 0.95) may be desirable in some contexts.
The function uses the pwr package for all power calculations, ensuring accurate results based on well-established statistical theory.
An object of class stat_power_result containing:
The calculated statistical power (probability of detecting the effect)
Sample size used in the calculation
Effect size used in the calculation
Significance level
Type of statistical test
Subtype for t-tests (e.g., "two.sample", "one.sample", "paired"); NULL for other tests
Direction of alternative hypothesis used in the test
Number of groups (for ANOVA); NULL for other tests
Degrees of freedom (for chi-square tests); NULL for other tests
ggplot2 object showing the power curve (if plot = TRUE); NULL otherwise
Raw result object from the pwr package function
List with interpretation and recommendation text
POSIXct timestamp of when the calculation was performed
Proportion Test: Uses pwr.p.test, which is for one-sample
proportion tests (testing a single proportion against a hypothesized value).
For two-sample proportion tests, consider using specialized tools or packages.
The effect_size parameter uses Cohen's h, which quantifies the difference
between two proportions. In one-sample settings, Cohen's h is computed from the
observed proportion p and the null hypothesis proportion p0.
When plot = TRUE, a power curve is generated showing how statistical
power changes with sample size. The curve helps visualize:
The current power level (marked with a red point)
The conventional 0.8 power threshold (red dashed line)
How increasing sample size affects power
## Not run:
# Example 1: Power for a two-sample t-test
result <- stat_power(
n = 30,
effect_size = 0.5,
test = "t.test",
type = "two.sample"
)
print(result)
plot(result)
# Example 2: Power for ANOVA with 3 groups
stat_power(
n = 25,
effect_size = 0.25,
test = "anova",
k = 3
)
# Example 3: Power for correlation test
stat_power(
n = 50,
effect_size = 0.3,
test = "correlation"
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.