{ "cells": [ { "cell_type": "markdown", "id": "216150f3", "metadata": {}, "source": [ "### Example: Power calculations" ] }, { "cell_type": "code", "execution_count": null, "id": "eb55f8b5", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import scipy.stats as stats\n", "import statsmodels.api as sm\n", "\n", "# import package for power calculations:\n", "import statsmodels.stats.power as smp" ] }, { "cell_type": "code", "execution_count": null, "id": "557979a2", "metadata": {}, "outputs": [], "source": [ "# We want to calculate the sample size needed in a new experiment\n", "\n", "# from previous experiments we have a good guess for the sample variation:\n", "sd = 1.62\n", "\n", "# we want to be able to detect a voltage drop of down to 0.5 volts\n", "delta = 1" ] }, { "cell_type": "code", "execution_count": null, "id": "69b1ba56", "metadata": {}, "outputs": [], "source": [ "\n", "# we need some quantiles from the normal distribution (assuming the samples will be big enough for normal distribution assumption to apply)\n", "z_power = stats.norm.ppf(0.80, loc=0, scale = 1)\n", "z_signif = stats.norm.ppf(0.975, loc=0, scale = 1)\n", "\n", "print([z_power, z_signif])" ] }, { "cell_type": "code", "execution_count": null, "id": "9e727ac3", "metadata": {}, "outputs": [], "source": [ "# use formula to calculate n_obs (needed number of observations):\n", "n_obs = (sd/delta*(z_power+z_signif))**2\n", "print(n_obs)" ] }, { "cell_type": "markdown", "id": "a28cb940", "metadata": {}, "source": [ "So we need a sample of 21 observations " ] }, { "cell_type": "code", "execution_count": null, "id": "c4486bde", "metadata": {}, "outputs": [], "source": [ "# we can also use the python function TTestPower().solve_power (for one sample power calculations): \n", "print(smp.TTestPower().solve_power(effect_size=delta/sd, alpha=0.05, power=0.80))" ] }, { "cell_type": "markdown", "id": "87f57a34", "metadata": {}, "source": [ "i.e. 23 observations\n", "\n", "The Python function gives a larger estimate of needed sample size! The formula uses more assumptions than the Python version, so the result from the Python function is a little better/safer." ] }, { "cell_type": "code", "execution_count": null, "id": "d861fd86", "metadata": {}, "outputs": [], "source": [ "# What if we can only make 15 observations?\n", "n = 15\n", "\n", "# calculate the power:\n", "z_power_new = np.sqrt(n*(delta/sd)**2)-z_signif\n", "print(z_power_new)\n", "\n", "power = stats.norm.cdf(z_power_new)\n", "print(power)" ] }, { "cell_type": "code", "execution_count": null, "id": "0195ac82", "metadata": {}, "outputs": [], "source": [ "# we can also use the python function TTestPower().solve_power (for one sample power calculations): \n", "print(smp.TTestPower().solve_power(effect_size=delta/sd, alpha=0.05, nobs=15))" ] }, { "cell_type": "markdown", "id": "39eff535", "metadata": {}, "source": [ "The result is almost the same (but the Python function is better/safer)" ] }, { "cell_type": "markdown", "id": "958515f4", "metadata": {}, "source": [ "### Example: Power calculations, 2 samples" ] }, { "cell_type": "markdown", "id": "ae7c0f10", "metadata": {}, "source": [ "1)\n", "\n", "Find the sample size in a test where:\n", "\n", "power = 0.90 (beta = 0.10)\n", "\n", "n1 = n2 (k = 1)\n", "\n", "We want to be able to detect a difference of 0.4 (effect size is 0.4)\n", "\n", "using alpha = 0.05\n", "\n", "and assuming sigma = 1 in both populations" ] }, { "cell_type": "code", "execution_count": null, "id": "60654430", "metadata": {}, "outputs": [], "source": [ "power = 0.90\n", "k = 1\n", "delta = 0.4\n", "sd = 1\n", "\n", "z_power = stats.norm.ppf(0.90, loc=0, scale = 1)\n", "z_signif = stats.norm.ppf(0.975, loc=0, scale = 1)\n", "\n", "n1 = (k+1) * (sd/delta*(z_signif+z_power))**2\n", "\n", "print(n1)" ] }, { "cell_type": "code", "execution_count": null, "id": "272daa02", "metadata": {}, "outputs": [], "source": [ "# we can also use the python function TTestIndPower().solve_power (for one sample power calculations): \n", "print(smp.TTestIndPower().solve_power(effect_size=delta/sd, alpha=0.05, power=0.90, ratio=k))" ] }, { "cell_type": "markdown", "id": "d68a7d91", "metadata": {}, "source": [ "2)\n", "\n", "Find the power of an experiment where: \n", "\n", "n1 = n2 = 50 (k = 1)\n", "\n", "We want to be able to detect a difference of 20.4\n", "\n", "using alpha = 0.05\n", "\n", "and assuming sigma = 1 in both populations" ] }, { "cell_type": "code", "execution_count": null, "id": "1afa5306", "metadata": {}, "outputs": [], "source": [ "n1 = 50\n", "n2 = 50\n", "k = 1\n", "delta = 0.4\n", "sd = 1\n", "z_signif = stats.norm.ppf(0.975, loc=0, scale = 1)\n", "\n", "z = np.sqrt(n1/(k+1)*delta**2/sd**2) - z_signif\n", "power = stats.norm.cdf(z)\n", "\n", "print(power)" ] }, { "cell_type": "code", "execution_count": null, "id": "a702585d", "metadata": {}, "outputs": [], "source": [ "# we can also use the python function TTestIndPower().solve_power (for one sample power calculations): \n", "print(smp.TTestIndPower().solve_power(effect_size=delta/sd, alpha=0.05, nobs1=50, ratio=k))" ] } ], "metadata": { "kernelspec": { "display_name": "Pernille_Env_python_311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.13" } }, "nbformat": 4, "nbformat_minor": 5 }