View source: R/stat_samplesize.R
| stat_samplesize | R Documentation |
Compute the minimum sample size required to achieve a specified statistical power for detecting a given effect size. Supports multiple test types including t-tests, ANOVA, proportion tests, correlation tests, and chi-square tests.
stat_samplesize(
power = 0.8,
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
)
power |
Numeric. Target statistical power (probability of correctly rejecting the null hypothesis). Default: 0.8 (80%). |
effect_size |
Numeric. Effect size appropriate for the test:
|
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". |
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 sample size curve plot? Default: TRUE. |
plot_range |
Numeric vector of length 2. Range of effect sizes for the 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. |
Sample size estimation is a critical step in research planning. This function calculates the minimum number of participants needed to achieve a specified statistical power (typically 0.8 or 80%) for detecting an effect of a given size.
The function uses the pwr package for all calculations, ensuring accurate results based on well-established statistical theory. Sample sizes are always rounded up to the nearest integer.
An object of class stat_samplesize_result containing:
Required sample size (per group for t-tests and ANOVA)
Total sample size across all groups
Target statistical power
Effect size used in the calculation
Significance level
Type of statistical test
ggplot2 object showing the sample size curve (if plot = TRUE)
List with interpretation and recommendations
When plot = TRUE, a sample size curve is generated showing how required
sample size changes with effect size. The curve helps visualize:
The current required sample size (marked with a red point)
Reference lines for small, medium, and large effects
How detecting smaller effects requires larger samples
Sample sizes are calculated per group for t-tests and ANOVA
Consider adding 10-15% to account for potential dropout
Very small effect sizes may require impractically large samples
stat_power for calculating statistical power.
## Not run:
# Example 1: Sample size for a two-sample t-test
result <- stat_samplesize(
power = 0.8,
effect_size = 0.5,
test = "t.test",
type = "two.sample"
)
print(result)
plot(result)
# Example 2: Sample size for ANOVA with 3 groups
stat_samplesize(
power = 0.8,
effect_size = 0.25,
test = "anova",
k = 3
)
# Example 3: Sample size for correlation test
stat_samplesize(
power = 0.9,
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.