Skip to content
Open
Show file tree
Hide file tree
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
193 changes: 193 additions & 0 deletions .ipynb_checkpoints/7.05_Lab_T-tests_p_values-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ee314908",
"metadata": {},
"source": [
"# Lab | Inferential statistics - T-test & P-value"
]
},
{
"cell_type": "markdown",
"id": "515aa25e",
"metadata": {},
"source": [
"## 1. We will have another simple example on two sample t test (pooled- when the variances are equal). But this time this is a one sided t-test\n",
"\n",
"In a packing plant, a machine packs cartons with jars. It is supposed that a new machine will pack faster on the average than the machine currently used. To test that hypothesis, the times it takes each machine to pack ten cartons are recorded. The results, in seconds, are shown in the tables in the file `files_for_lab/machine.txt`.\n",
"Assume that there is sufficient evidence to conduct the t test, does the data provide sufficient evidence to show if one machine is better than the other\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c4797fc7",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import math\n",
"from scipy.stats import ttest_ind\n",
"import scipy.stats as stats\n",
"import statistics\n",
"import statsmodels.stats\n",
"import warnings\n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "markdown",
"id": "8353cb13-89e6-48dc-ab8a-80dff039c275",
"metadata": {},
"source": [
"### 1.1 Hypothesis\n",
"\n",
"H0: the average packing time of the new machine is equal to the old machine \n",
"HA: the average packing time of the new machine is significantly different from the old machine \n",
"\n",
"### 1.2 Test selection (one tailed vs. two tailed)\n",
"\n",
"Here I'm deciding to apply a two tailed test to reflect that the average packing time of the new machine could be higher of lower than the old maschine\n",
"\n",
"### 1.3 Selection of test statistics\n",
"\n",
"By the information given it's a t test in this case\n",
"\n",
"### 1.4 Level of significance\n",
"\n",
"0.05\n",
"\n",
"### 1.5. Calculation of the test statistic\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "f67948d1-816d-48bf-b2af-d5617a190db9",
"metadata": {},
"outputs": [],
"source": [
"machine_old = [42.7, 43.6, 43.8, 43.3, 42.5, 43.5, 43.1, 41.7, 44, 44.1]\n",
"machine_new = [42.1, 41, 41.3, 41.8, 42.4, 42.8, 43.2, 42.3, 41.8, 42.7]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "82229424-84ed-4213-9648-4dc4b0065025",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Ttest_indResult(statistic=3.3972307061176026, pvalue=0.0032111425007745158)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ttest_ind(machine_old, machine_new)"
]
},
{
"cell_type": "markdown",
"id": "e97bc2c3-13ce-4166-95e1-60c2b96c1a0a",
"metadata": {},
"source": [
"### 1.6 comparing with critical value "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e7ba89dd-621a-4169-8311-11e10a71bcd7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.10092204024096"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dof = (len(machine_old) - 1) + (len(machine_new) - 1)\n",
"\n",
"t_critical = stats.t.ppf(q=1-.05/2,df=dof)\n",
"t_critical"
]
},
{
"cell_type": "markdown",
"id": "1e806e19-61ec-4809-ac42-93a82b419e0d",
"metadata": {},
"source": [
"### 1.7 Conclusion\n",
"\n",
"Since the p value 0.003 is smaller than the confidence interval (0.05) and the test statistic 3.397 is bigger than the critical value for t (2.101) we can reject the null hypothesis and conclude that there is a sinificant difference in the average packing time of the new maschine compared to the old maschine."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ee15546-047e-48f8-8c89-bd4a84c05520",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "501614ae",
"metadata": {},
"source": [
"## 2. An additional problem (not mandatory): In this case we can't assume that the population variances are equal. Hence in this case we cannot pool the variances.\n",
" Independent random samples of 17 sophomores and 13 juniors attending a large university yield the following data on grade point averages. Data is provided in the file `files_for_lab/student_gpa.txt`.\n",
" At the 5% significance level, do the data provide sufficient evidence to conclude that the mean GPAs of sophomores and juniors at the university differ?\n",
"\n",
" Test statistics can be calculated as: [link to the image - Test statistics calculation for Unpooled Variance Case](https://education-team-2020.s3-eu-west-1.amazonaws.com/data-analytics/7.04/7.04-unpooled_variances.png)\n",
"\n",
" Degrees of freedom is `(n1-1)+(n2-1)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3925f57e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python3Brew",
"language": "python",
"name": "python3brew"
},
"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.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
76 changes: 0 additions & 76 deletions .ipynb_checkpoints/Untitled-checkpoint.ipynb

This file was deleted.

Loading