File:Interaction between sample size, effect size, and statistical power.svg

Summary

Description
English: Distributions of sample means under the null (θ=0) and alternative hypotheses are shown. The shaded red area represents Type I error (α), held constant at 0.05, while the shaded green area represents statistical power (1-β). As the sample size increases, the distributions narrow, leading to clearer separation between the hypotheses and higher power. Similarly, a larger effect size increases the distance between the distributions, resulting in greater power.

```python import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm

  1. Parameters

alpha = 0.05 # Significance level

  1. Create subplots

xmin, xmax = -0.4, 0.6 ns = [10, 30, 100] thetas = [0.1, 0.3, 0.5] fig, axes = plt.subplots(len(ns), len(thetas), figsize=(12, 10))

  1. Loop through different scenarios

for i, n in enumerate(ns): # Sample sizes

   for j, theta in enumerate(thetas):  # Effect sizes
       # Calculate parameters
       sigma = 1 / np.sqrt(n)
       z_alpha = norm.ppf(1 - alpha)
       threshold = z_alpha * sigma
       power = 1 - norm.cdf(z_alpha - theta / sigma)
       # Generate distributions
       x = np.linspace(xmin, xmax, 1000)
       null_dist = norm.pdf(x, 0, sigma)
       alt_dist = norm.pdf(x, theta, sigma)
       # Plot on the corresponding subplot
       ax = axes[i, j]
ax.plot(x, null_dist, label=rf'$H_0\; (\theta = 0)
) ax.plot(x, alt_dist, label=rf'$H_1\; (\theta = {theta})
Source Own work Edit this at Structured Data on Commons
Author
Category:Media lacking author information

)

       ax.axvline(threshold, color='gray', linestyle='--', label='Threshold')
       ax.fill_between(x, null_dist, where=(x > threshold), color='red', alpha=0.3, label='Significance (α)')
       ax.fill_between(x, alt_dist, where=(x > threshold), color='green', alpha=0.3, label='Power (1-β)')
       # Set subplot title and labels
       ax.set_title(rf'$n={n}, \theta={theta}$ Power$={power:.2f}}})
       ax.set_xlabel('Sample Mean (x̄)')
       ax.set_yticks([])
       ax.set_xlim(xmin, xmax)
       # ax.set_ylabel('Probability Density')
       if i == 0 and j == 0: ax.legend(fontsize='small')

plt.tight_layout() plt.savefig('power_analysis.svg') plt.show() ``` }} |date=2024-07-26 |source=Own work |author=Cosmia Nebula }}

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-4.0#Interaction%20between%20sample%20size,%20effect%20size,%20and%20statistical%20power.svg
Category:Self-published work Category:Media needing categories as of 27 July 2024#Interaction%20between%20sample%20size,%20effect%20size,%20and%20statistical%20power.svg Category:All media needing categories as of 2024#Interaction%20between%20sample%20size,%20effect%20size,%20and%20statistical%20power.svg Category:Created with Matplotlib
Category:All media needing categories as of 2024 Category:CC-BY-SA-4.0 Category:Created with Matplotlib Category:Files with no machine-readable author Category:Media lacking author information Category:Media needing categories as of 27 July 2024 Category:Self-published work