Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 208 additions & 15 deletions Solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,191 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "39c6f717",
"execution_count": 7,
"id": "5013f01b",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t statistic is: 4.761904761904759\n"
]
}
],
"source": [
"from statistics import math\n",
"\n",
"# Step 1) H0: μ = 120 mm Hg (population mean = sample mean)\n",
"# Step 2) H1: μ != 120 mm Hg\n",
" \n",
"# Step 3) a = 0.05 (significance level)\n",
"\n",
"# Step 4) Calculate the test statistic and corresponding p-value\n",
"\n",
"# [Sample size > 30] -> Z test or t test\n",
"# [Unknown pop. variance] -> t test\n",
"\n",
"sample_mean = 130.1\n",
"pop_mean = 120\n",
"sample_stddev = 21.21\n",
"n = 100\n",
"\n",
"t_statistic = (sample_mean - pop_mean)/(sample_stddev/math.sqrt(n))\n",
"print(\"t statistic is: \", t_statistic)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "80d12266",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"We fail to reject H0 (population mean = sample mean) and we accept H1\n"
]
}
],
"source": [
"# Step 5) Drawing a conclusion.\n",
"# Comparing our statistic with the t test one\n",
"# t test: DF = 100 and a = 0.05 -> 1.984\n",
"\n",
"if (t_statistic > 1.984) == True:\n",
" print('We fail to reject H0 (population mean = sample mean) and we accept H1')"
]
},
{
"cell_type": "markdown",
"id": "17c9988a",
"id": "f87e6842",
"metadata": {},
"source": [
"### 1.2 Write down all the steps followed for setting up the test."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b09ce149",
"cell_type": "markdown",
"id": "ff5acebe",
"metadata": {},
"outputs": [],
"source": []
"source": [
"1. Specify the Null Hypothesis\n",
"2. Specify the Alternative Hypothesis\n",
"3. Set the Significance Level (a)\n",
"4. Calculate the Test Statistic and Corresponding P-Value\n",
"5. Drawing a Conclusion\n",
"\n",
"https://www.nedarc.org/statisticalhelp/advancedstatisticaltopics/hypothesisTesting.html"
]
},
{
"cell_type": "markdown",
"id": "a486c007",
"metadata": {},
"source": [
"Step 1: Specify the Null Hypothesis\n",
"The null hypothesis (H0) is a statement of no effect, relationship, or difference between two or more groups or factors. In research studies, a researcher is usually interested in disproving the null hypothesis.\n",
"\n",
"Examples:\n",
"There is no difference in intubation rates across ages 0 to 5 years.\n",
"The intervention and control groups have the same survival rate (or, the intervention does not improve survival rate).\n",
"There is no association between injury type and whether or not the patient received an IV in the prehospital setting."
]
},
{
"cell_type": "markdown",
"id": "316a89e3",
"metadata": {},
"source": [
"Step 2: Specify the Alternative Hypothesis\n",
"The alternative hypothesis (H1) is the statement that there is an effect or difference. This is usually the hypothesis the researcher is interested in proving. The alternative hypothesis can be one-sided (only provides one direction, e.g., lower) or two-sided. We often use two-sided tests even when our true hypothesis is one-sided because it requires more evidence against the null hypothesis to accept the alternative hypothesis.\n",
"\n",
"Examples:\n",
"The intubation success rate differs with the age of the patient being treated (two-sided).\n",
"The time to resuscitation from cardiac arrest is lower for the intervention group than for the control (one-sided).\n",
"There is an association between injury type and whether or not the patient received an IV in the prehospital setting (two sided)."
]
},
{
"cell_type": "markdown",
"id": "7d31f7cb",
"metadata": {},
"source": [
"Step 3: Set the Significance Level (a)\n",
"The significance level (denoted by the Greek letter alpha— a) is generally set at 0.05. This means that there is a 5% chance that you will accept your alternative hypothesis when your null hypothesis is actually true. The smaller the significance level, the greater the burden of proof needed to reject the null hypothesis, or in other words, to support the alternative hypothesis."
]
},
{
"cell_type": "markdown",
"id": "5a1d4c48",
"metadata": {},
"source": [
"Step 4: Calculate the Test Statistic and Corresponding P-Value\n",
"In another section we present some basic test statistics to evaluate a hypothesis. Hypothesis testing generally uses a test statistic that compares groups or examines associations between variables. When describing a single sample without establishing relationships between variables, a confidence interval is commonly used.\n",
"\n",
"The p-value describes the probability of obtaining a sample statistic as or more extreme by chance alone if your null hypothesis is true. This p-value is determined based on the result of your test statistic. Your conclusions about the hypothesis are based on your p-value and your significance level. \n",
"\n",
"Example:\n",
"P-value = 0.01 This will happen 1 in 100 times by pure chance if your null hypothesis is true. Not likely to happen strictly by chance.\n",
"Example:\n",
"P-value = 0.75 This will happen 75 in 100 times by pure chance if your null hypothesis is true. Very likely to occur strictly by chance.\n",
"\n",
"Cautions About P-Values\n",
"Caution SignYour sample size directly impacts your p-value. Large sample sizes produce small p-values even when differences between groups are not meaningful. You should always verify the practical relevance of your results. On the other hand, a sample size that is too small can result in a failure to identify a difference when one truly exists. \n",
"\n",
"Plan your sample size ahead of time so that you have enough information from your sample to show a meaningful relationship or difference if one exists. See calculating a sample size for more information.\n",
"\n",
"Example:\n",
"Average ages were significantly different between the two groups (16.2 years vs. 16.7 years; p = 0.01; n=1,000). Is this an important difference? Probably not, but the large sample size has resulted in a small p-value.\n",
"\n",
"Example:\n",
"Average ages were not significantly different between the two groups (10.4 years vs. 16.7 years; p = 0.40, n=10). Is this an important difference? It could be, but because the sample size is small, we can't determine for sure if this is a true difference or just happened due to the natural variability in age within these two groups.\n",
"If you do a large number of tests to evaluate a hypothesis (called multiple testing), then you need to control for this in your designation of the significance level or calculation of the p-value. For example, if three outcomes measure the effectiveness of a drug or other intervention, you will have to adjust for these three analyses."
]
},
{
"cell_type": "markdown",
"id": "b243b92c",
"metadata": {},
"source": [
"Step 5: Drawing a Conclusion\n",
"\n",
"P-value <= significance level (a) => Reject your null hypothesis in favor of your alternative hypothesis. Your result is statistically significant.\n",
"\n",
"P-value > significance level (a) => Fail to reject your null hypothesis. Your result is not statistically significant.\n",
"\n",
"Hypothesis testing is not set up so that you can absolutely prove a null hypothesis. Therefore, when you do not find evidence against the null hypothesis, you fail to reject the null hypothesis. When you do find strong enough evidence against the null hypothesis, you reject the null hypothesis. Your conclusions also translate into a statement about your alternative hypothesis. When presenting the results of a hypothesis test, include the descriptive statistics in your conclusions as well. Report exact p-values rather than a certain range. For example, \"The intubation rate differed significantly by patient age with younger patients have a lower rate of successful intubation (p=0.02).\" Here are two more examples with the conclusion stated in several different ways.\n",
"\n",
"Example:\n",
"\n",
"H0: There is no difference in survival between the intervention and control group.\n",
"\n",
"H1: There is a difference in survival between the intervention and control group.\n",
"\n",
"a = 0.05; 20% increase in survival for the intervention group; p-value = 0.002\n",
"\n",
"Conclusion:\n",
"\n",
"Reject the null hypothesis in favor of the alternative hypothesis.\n",
"The difference in survival between the intervention and control group was statistically significant.\n",
"There was a 20% increase in survival for the intervention group compared to control (p=0.001).\n",
"\n",
"Example:\n",
"\n",
"H0: There is no difference in survival between the intervention and control group.\n",
"\n",
"H1: There is a difference in survival between the intervention and control group.\n",
"\n",
"a = 0.05; 5% increase in survival between the intervention and control group; p-value = 0.20\n",
"\n",
"Conclusion:\n",
"\n",
"Fail to reject the null hypothesis.\n",
"The difference in survival between the intervention and control group was not statistically significant.\n",
"There was no significant increase in survival for the intervention group compared to control (p=0.20)."
]
},
{
"cell_type": "markdown",
Expand All @@ -64,13 +228,34 @@
"### 1.3 Calculate the test statistic by hand and also code it in Python. It should be 4.76190. We will take a look at how to make decisions based on this calculated value."
]
},
{
"cell_type": "markdown",
"id": "91955a31",
"metadata": {},
"source": [
"- Made on item 1.1, variable t_statistic"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97f73e20",
"execution_count": 12,
"id": "3591bd7c",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"4.761904761904759"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t_statistic"
]
},
{
"cell_type": "markdown",
Expand All @@ -79,6 +264,14 @@
"source": [
"## 2. If you finished the previous question, please go through the code for principal_component_analysis_example provided in the files_for_lab folder ."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9bf667b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -97,7 +290,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.8.8"
}
},
"nbformat": 4,
Expand Down