diff --git a/your-code/main.ipynb b/your-code/main.ipynb
index b2b6f8d..0a439b4 100644
--- a/your-code/main.ipynb
+++ b/your-code/main.ipynb
@@ -1,279 +1,1181 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Before your start:\n",
- "- Read the README.md file\n",
- "- Comment as much as you can and use the resources (README.md file)\n",
- "- Happy learning!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# import numpy and pandas\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Challenge 1 - Exploring the Data\n",
- "\n",
- "In this challenge, we will examine all salaries of employees of the City of Chicago. We will start by loading the dataset and examining its contents."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Examine the `salaries` dataset using the `head` function below."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We see from looking at the `head` function that there is quite a bit of missing data. Let's examine how much missing data is in each column. Produce this output in the cell below"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let's also look at the count of hourly vs. salaried employees. Write the code in the cell below"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "What this information indicates is that the table contains information about two types of employees - salaried and hourly. Some columns apply only to one type of employee while other columns only apply to another kind. This is why there are so many missing values. Therefore, we will not do anything to handle the missing values."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "There are different departments in the city. List all departments and the count of employees in each department."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Challenge 2 - Hypothesis Tests\n",
- "\n",
- "In this section of the lab, we will test whether the hourly wage of all hourly workers is significantly different from $30/hr. Import the correct one sample test function from scipy and perform the hypothesis test for a 95% two sided confidence interval."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We are also curious about salaries in the police force. The chief of police in Chicago claimed in a press briefing that salaries this year are higher than last year's mean of $86000/year a year for all salaried employees. Test this one sided hypothesis using a 95% confidence interval.\n",
- "\n",
- "Hint: A one tailed test has a p-value that is half of the two tailed p-value. If our hypothesis is greater than, then to reject, the test statistic must also be positive."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Using the `crosstab` function, find the department that has the most hourly workers. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "The workers from the department with the most hourly workers have complained that their hourly wage is less than $35/hour. Using a one sample t-test, test this one-sided hypothesis at the 95% confidence level."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Challenge 3: To practice - Constructing Confidence Intervals\n",
- "\n",
- "While testing our hypothesis is a great way to gather empirical evidence for accepting or rejecting the hypothesis, another way to gather evidence is by creating a confidence interval. A confidence interval gives us information about the true mean of the population. So for a 95% confidence interval, we are 95% sure that the mean of the population is within the confidence interval. \n",
- ").\n",
- "\n",
- "To read more about confidence intervals, click [here](https://en.wikipedia.org/wiki/Confidence_interval).\n",
- "\n",
- "\n",
- "In the cell below, we will construct a 95% confidence interval for the mean hourly wage of all hourly workers. \n",
- "\n",
- "The confidence interval is computed in SciPy using the `t.interval` function. You can read more about this function [here](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.t.html).\n",
- "\n",
- "To compute the confidence interval of the hourly wage, use the 0.95 for the confidence level, number of rows - 1 for degrees of freedom, the mean of the sample for the location parameter and the standard error for the scale. The standard error can be computed using [this](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.sem.html) function in SciPy."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Now construct the 95% confidence interval for all salaried employeed in the police in the cell below."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Bonus Challenge - Hypothesis Tests of Proportions\n",
- "\n",
- "Another type of one sample test is a hypothesis test of proportions. In this test, we examine whether the proportion of a group in our sample is significantly different than a fraction. \n",
- "\n",
- "You can read more about one sample proportion tests [here](http://sphweb.bumc.bu.edu/otlt/MPH-Modules/BS/SAS/SAS6-CategoricalData/SAS6-CategoricalData2.html).\n",
- "\n",
- "In the cell below, use the `proportions_ztest` function from `statsmodels` to perform a hypothesis test that will determine whether the number of hourly workers in the City of Chicago is significantly different from 25% at the 95% confidence level."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n",
- "\n"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "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.7.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
-}
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Before your start:\n",
+ "- Read the README.md file\n",
+ "- Comment as much as you can and use the resources (README.md file)\n",
+ "- Happy learning!"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# import numpy and pandas\n",
+ "import pandas as pd\n",
+ "import numpy as np\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Challenge 1 - Exploring the Data\n",
+ "\n",
+ "In this challenge, we will examine all salaries of employees of the City of Chicago. We will start by loading the dataset and examining its contents."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "data = pd.read_csv('Current_Employee_Names__Salaries__and_Position_Titles.csv')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Examine the `salaries` dataset using the `head` function below."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Name | \n",
+ " Job Titles | \n",
+ " Department | \n",
+ " Full or Part-Time | \n",
+ " Salary or Hourly | \n",
+ " Typical Hours | \n",
+ " Annual Salary | \n",
+ " Hourly Rate | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " AARON, JEFFERY M | \n",
+ " SERGEANT | \n",
+ " POLICE | \n",
+ " F | \n",
+ " Salary | \n",
+ " NaN | \n",
+ " 101442.0 | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " AARON, KARINA | \n",
+ " POLICE OFFICER (ASSIGNED AS DETECTIVE) | \n",
+ " POLICE | \n",
+ " F | \n",
+ " Salary | \n",
+ " NaN | \n",
+ " 94122.0 | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " AARON, KIMBERLEI R | \n",
+ " CHIEF CONTRACT EXPEDITER | \n",
+ " GENERAL SERVICES | \n",
+ " F | \n",
+ " Salary | \n",
+ " NaN | \n",
+ " 101592.0 | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " ABAD JR, VICENTE M | \n",
+ " CIVIL ENGINEER IV | \n",
+ " WATER MGMNT | \n",
+ " F | \n",
+ " Salary | \n",
+ " NaN | \n",
+ " 110064.0 | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " ABASCAL, REECE E | \n",
+ " TRAFFIC CONTROL AIDE-HOURLY | \n",
+ " OEMC | \n",
+ " P | \n",
+ " Hourly | \n",
+ " 20.0 | \n",
+ " NaN | \n",
+ " 19.86 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Name Job Titles \\\n",
+ "0 AARON, JEFFERY M SERGEANT \n",
+ "1 AARON, KARINA POLICE OFFICER (ASSIGNED AS DETECTIVE) \n",
+ "2 AARON, KIMBERLEI R CHIEF CONTRACT EXPEDITER \n",
+ "3 ABAD JR, VICENTE M CIVIL ENGINEER IV \n",
+ "4 ABASCAL, REECE E TRAFFIC CONTROL AIDE-HOURLY \n",
+ "\n",
+ " Department Full or Part-Time Salary or Hourly Typical Hours \\\n",
+ "0 POLICE F Salary NaN \n",
+ "1 POLICE F Salary NaN \n",
+ "2 GENERAL SERVICES F Salary NaN \n",
+ "3 WATER MGMNT F Salary NaN \n",
+ "4 OEMC P Hourly 20.0 \n",
+ "\n",
+ " Annual Salary Hourly Rate \n",
+ "0 101442.0 NaN \n",
+ "1 94122.0 NaN \n",
+ "2 101592.0 NaN \n",
+ "3 110064.0 NaN \n",
+ "4 NaN 19.86 "
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "data.head()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We see from looking at the `head` function that there is quite a bit of missing data. Let's examine how much missing data is in each column. Produce this output in the cell below"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "RangeIndex: 33183 entries, 0 to 33182\n",
+ "Data columns (total 8 columns):\n",
+ " # Column Non-Null Count Dtype \n",
+ "--- ------ -------------- ----- \n",
+ " 0 Name 33183 non-null object \n",
+ " 1 Job Titles 33183 non-null object \n",
+ " 2 Department 33183 non-null object \n",
+ " 3 Full or Part-Time 33183 non-null object \n",
+ " 4 Salary or Hourly 33183 non-null object \n",
+ " 5 Typical Hours 8022 non-null float64\n",
+ " 6 Annual Salary 25161 non-null float64\n",
+ " 7 Hourly Rate 8022 non-null float64\n",
+ "dtypes: float64(3), object(5)\n",
+ "memory usage: 2.0+ MB\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "data.info()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Let's also look at the count of hourly vs. salaried employees. Write the code in the cell below"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Name | \n",
+ "
\n",
+ " \n",
+ " | Salary or Hourly | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Hourly | \n",
+ " 8022 | \n",
+ "
\n",
+ " \n",
+ " | Salary | \n",
+ " 25161 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Name\n",
+ "Salary or Hourly \n",
+ "Hourly 8022\n",
+ "Salary 25161"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "data.groupby('Salary or Hourly').agg({'Name': 'count'}).head()\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "What this information indicates is that the table contains information about two types of employees - salaried and hourly. Some columns apply only to one type of employee while other columns only apply to another kind. This is why there are so many missing values. Therefore, we will not do anything to handle the missing values."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "There are different departments in the city. List all departments and the count of employees in each department."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Name | \n",
+ "
\n",
+ " \n",
+ " | Department | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | ADMIN HEARNG | \n",
+ " 39 | \n",
+ "
\n",
+ " \n",
+ " | ANIMAL CONTRL | \n",
+ " 81 | \n",
+ "
\n",
+ " \n",
+ " | AVIATION | \n",
+ " 1629 | \n",
+ "
\n",
+ " \n",
+ " | BOARD OF ELECTION | \n",
+ " 107 | \n",
+ "
\n",
+ " \n",
+ " | BOARD OF ETHICS | \n",
+ " 8 | \n",
+ "
\n",
+ " \n",
+ " | BUDGET & MGMT | \n",
+ " 46 | \n",
+ "
\n",
+ " \n",
+ " | BUILDINGS | \n",
+ " 269 | \n",
+ "
\n",
+ " \n",
+ " | BUSINESS AFFAIRS | \n",
+ " 171 | \n",
+ "
\n",
+ " \n",
+ " | CITY CLERK | \n",
+ " 84 | \n",
+ "
\n",
+ " \n",
+ " | CITY COUNCIL | \n",
+ " 411 | \n",
+ "
\n",
+ " \n",
+ " | COMMUNITY DEVELOPMENT | \n",
+ " 207 | \n",
+ "
\n",
+ " \n",
+ " | COPA | \n",
+ " 116 | \n",
+ "
\n",
+ " \n",
+ " | CULTURAL AFFAIRS | \n",
+ " 65 | \n",
+ "
\n",
+ " \n",
+ " | DISABILITIES | \n",
+ " 28 | \n",
+ "
\n",
+ " \n",
+ " | DoIT | \n",
+ " 99 | \n",
+ "
\n",
+ " \n",
+ " | FAMILY & SUPPORT | \n",
+ " 615 | \n",
+ "
\n",
+ " \n",
+ " | FINANCE | \n",
+ " 560 | \n",
+ "
\n",
+ " \n",
+ " | FIRE | \n",
+ " 4641 | \n",
+ "
\n",
+ " \n",
+ " | GENERAL SERVICES | \n",
+ " 980 | \n",
+ "
\n",
+ " \n",
+ " | HEALTH | \n",
+ " 488 | \n",
+ "
\n",
+ " \n",
+ " | HUMAN RELATIONS | \n",
+ " 16 | \n",
+ "
\n",
+ " \n",
+ " | HUMAN RESOURCES | \n",
+ " 79 | \n",
+ "
\n",
+ " \n",
+ " | INSPECTOR GEN | \n",
+ " 87 | \n",
+ "
\n",
+ " \n",
+ " | LAW | \n",
+ " 407 | \n",
+ "
\n",
+ " \n",
+ " | LICENSE APPL COMM | \n",
+ " 1 | \n",
+ "
\n",
+ " \n",
+ " | MAYOR'S OFFICE | \n",
+ " 85 | \n",
+ "
\n",
+ " \n",
+ " | OEMC | \n",
+ " 2102 | \n",
+ "
\n",
+ " \n",
+ " | POLICE | \n",
+ " 13414 | \n",
+ "
\n",
+ " \n",
+ " | POLICE BOARD | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | PROCUREMENT | \n",
+ " 92 | \n",
+ "
\n",
+ " \n",
+ " | PUBLIC LIBRARY | \n",
+ " 1015 | \n",
+ "
\n",
+ " \n",
+ " | STREETS & SAN | \n",
+ " 2198 | \n",
+ "
\n",
+ " \n",
+ " | TRANSPORTN | \n",
+ " 1140 | \n",
+ "
\n",
+ " \n",
+ " | TREASURER | \n",
+ " 22 | \n",
+ "
\n",
+ " \n",
+ " | WATER MGMNT | \n",
+ " 1879 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Name\n",
+ "Department \n",
+ "ADMIN HEARNG 39\n",
+ "ANIMAL CONTRL 81\n",
+ "AVIATION 1629\n",
+ "BOARD OF ELECTION 107\n",
+ "BOARD OF ETHICS 8\n",
+ "BUDGET & MGMT 46\n",
+ "BUILDINGS 269\n",
+ "BUSINESS AFFAIRS 171\n",
+ "CITY CLERK 84\n",
+ "CITY COUNCIL 411\n",
+ "COMMUNITY DEVELOPMENT 207\n",
+ "COPA 116\n",
+ "CULTURAL AFFAIRS 65\n",
+ "DISABILITIES 28\n",
+ "DoIT 99\n",
+ "FAMILY & SUPPORT 615\n",
+ "FINANCE 560\n",
+ "FIRE 4641\n",
+ "GENERAL SERVICES 980\n",
+ "HEALTH 488\n",
+ "HUMAN RELATIONS 16\n",
+ "HUMAN RESOURCES 79\n",
+ "INSPECTOR GEN 87\n",
+ "LAW 407\n",
+ "LICENSE APPL COMM 1\n",
+ "MAYOR'S OFFICE 85\n",
+ "OEMC 2102\n",
+ "POLICE 13414\n",
+ "POLICE BOARD 2\n",
+ "PROCUREMENT 92\n",
+ "PUBLIC LIBRARY 1015\n",
+ "STREETS & SAN 2198\n",
+ "TRANSPORTN 1140\n",
+ "TREASURER 22\n",
+ "WATER MGMNT 1879"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "data.groupby('Department').agg({'Name': 'count'}).head(100)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Challenge 2 - Hypothesis Tests\n",
+ "\n",
+ "In this section of the lab, we will test whether the hourly wage of all hourly workers is significantly different from $30/hr. Import the correct one sample test function from scipy and perform the hypothesis test for a 95% two sided confidence interval."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "4 19.86\n",
+ "6 46.10\n",
+ "7 35.60\n",
+ "10 2.65\n",
+ "18 17.68\n",
+ "Name: Hourly Rate, dtype: float64"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "from scipy.stats import ttest_1samp\n",
+ "dataset = data[data['Salary or Hourly'] == 'Hourly']['Hourly Rate']\n",
+ "dataset.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "TtestResult(statistic=20.6198057854942, pvalue=4.3230240486229894e-92, df=8021)"
+ ]
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "test_result = ttest_1samp(dataset,30.00)\n",
+ "test_result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Since P value of 2.1615120243114947e-92 is less than 0.05 then we reject the null hypothesis.\n"
+ ]
+ }
+ ],
+ "source": [
+ "p_value = test_result[1]/2\n",
+ "print(f'Since P value of {p_value} is less than 0.05 then we reject the null hypothesis.')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "We are also curious about salaries in the police force. The chief of police in Chicago claimed in a press briefing that salaries this year are higher than last year's mean of $86000/year a year for all salaried employees. Test this one sided hypothesis using a 95% confidence interval.\n",
+ "\n",
+ "Hint: A one tailed test has a p-value that is half of the two tailed p-value. If our hypothesis is greater than, then to reject, the test statistic must also be positive."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 50,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0 101442.0\n",
+ "1 94122.0\n",
+ "9 93354.0\n",
+ "11 84054.0\n",
+ "12 87006.0\n",
+ "Name: Annual Salary, dtype: float64"
+ ]
+ },
+ "execution_count": 50,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "police_data = data[(data['Department'] == 'POLICE') & (data['Salary or Hourly'] == 'Salary')]['Annual Salary']\n",
+ "police_data.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 52,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "TtestResult(statistic=3.081997005712994, pvalue=0.0020603403550965155, df=13403)"
+ ]
+ },
+ "execution_count": 52,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "police_test_result = ttest_1samp(police_data, 86000.00)\n",
+ "police_test_result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.0010301701775482577"
+ ]
+ },
+ "execution_count": 33,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "p_value = police_test_result[1]/2\n",
+ "p_value"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Since P value of 0.0010301701775482577 is less than 0.05 and our statistics is positive then we reject the null hypothesis\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(f'Since P value of {p_value} is less than 0.05 and our statistics is positive then we reject the null hypothesis')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Using the `crosstab` function, find the department that has the most hourly workers. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | Salary or Hourly | \n",
+ " Hourly | \n",
+ " Salary | \n",
+ "
\n",
+ " \n",
+ " | Department | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | ADMIN HEARNG | \n",
+ " 0 | \n",
+ " 39 | \n",
+ "
\n",
+ " \n",
+ " | POLICE BOARD | \n",
+ " 0 | \n",
+ " 2 | \n",
+ "
\n",
+ " \n",
+ " | BOARD OF ELECTION | \n",
+ " 0 | \n",
+ " 107 | \n",
+ "
\n",
+ " \n",
+ " | BOARD OF ETHICS | \n",
+ " 0 | \n",
+ " 8 | \n",
+ "
\n",
+ " \n",
+ " | LICENSE APPL COMM | \n",
+ " 0 | \n",
+ " 1 | \n",
+ "
\n",
+ " \n",
+ " | BUILDINGS | \n",
+ " 0 | \n",
+ " 269 | \n",
+ "
\n",
+ " \n",
+ " | INSPECTOR GEN | \n",
+ " 0 | \n",
+ " 87 | \n",
+ "
\n",
+ " \n",
+ " | CITY CLERK | \n",
+ " 0 | \n",
+ " 84 | \n",
+ "
\n",
+ " \n",
+ " | HUMAN RELATIONS | \n",
+ " 0 | \n",
+ " 16 | \n",
+ "
\n",
+ " \n",
+ " | TREASURER | \n",
+ " 0 | \n",
+ " 22 | \n",
+ "
\n",
+ " \n",
+ " | COPA | \n",
+ " 0 | \n",
+ " 116 | \n",
+ "
\n",
+ " \n",
+ " | DoIT | \n",
+ " 0 | \n",
+ " 99 | \n",
+ "
\n",
+ " \n",
+ " | DISABILITIES | \n",
+ " 0 | \n",
+ " 28 | \n",
+ "
\n",
+ " \n",
+ " | PROCUREMENT | \n",
+ " 2 | \n",
+ " 90 | \n",
+ "
\n",
+ " \n",
+ " | FIRE | \n",
+ " 2 | \n",
+ " 4639 | \n",
+ "
\n",
+ " \n",
+ " | BUDGET & MGMT | \n",
+ " 2 | \n",
+ " 44 | \n",
+ "
\n",
+ " \n",
+ " | HEALTH | \n",
+ " 3 | \n",
+ " 485 | \n",
+ "
\n",
+ " \n",
+ " | COMMUNITY DEVELOPMENT | \n",
+ " 4 | \n",
+ " 203 | \n",
+ "
\n",
+ " \n",
+ " | HUMAN RESOURCES | \n",
+ " 4 | \n",
+ " 75 | \n",
+ "
\n",
+ " \n",
+ " | BUSINESS AFFAIRS | \n",
+ " 7 | \n",
+ " 164 | \n",
+ "
\n",
+ " \n",
+ " | CULTURAL AFFAIRS | \n",
+ " 7 | \n",
+ " 58 | \n",
+ "
\n",
+ " \n",
+ " | MAYOR'S OFFICE | \n",
+ " 8 | \n",
+ " 77 | \n",
+ "
\n",
+ " \n",
+ " | POLICE | \n",
+ " 10 | \n",
+ " 13404 | \n",
+ "
\n",
+ " \n",
+ " | ANIMAL CONTRL | \n",
+ " 19 | \n",
+ " 62 | \n",
+ "
\n",
+ " \n",
+ " | LAW | \n",
+ " 40 | \n",
+ " 367 | \n",
+ "
\n",
+ " \n",
+ " | FINANCE | \n",
+ " 44 | \n",
+ " 516 | \n",
+ "
\n",
+ " \n",
+ " | CITY COUNCIL | \n",
+ " 64 | \n",
+ " 347 | \n",
+ "
\n",
+ " \n",
+ " | FAMILY & SUPPORT | \n",
+ " 287 | \n",
+ " 328 | \n",
+ "
\n",
+ " \n",
+ " | PUBLIC LIBRARY | \n",
+ " 299 | \n",
+ " 716 | \n",
+ "
\n",
+ " \n",
+ " | TRANSPORTN | \n",
+ " 725 | \n",
+ " 415 | \n",
+ "
\n",
+ " \n",
+ " | GENERAL SERVICES | \n",
+ " 765 | \n",
+ " 215 | \n",
+ "
\n",
+ " \n",
+ " | AVIATION | \n",
+ " 1082 | \n",
+ " 547 | \n",
+ "
\n",
+ " \n",
+ " | OEMC | \n",
+ " 1273 | \n",
+ " 829 | \n",
+ "
\n",
+ " \n",
+ " | WATER MGMNT | \n",
+ " 1513 | \n",
+ " 366 | \n",
+ "
\n",
+ " \n",
+ " | STREETS & SAN | \n",
+ " 1862 | \n",
+ " 336 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "Salary or Hourly Hourly Salary\n",
+ "Department \n",
+ "ADMIN HEARNG 0 39\n",
+ "POLICE BOARD 0 2\n",
+ "BOARD OF ELECTION 0 107\n",
+ "BOARD OF ETHICS 0 8\n",
+ "LICENSE APPL COMM 0 1\n",
+ "BUILDINGS 0 269\n",
+ "INSPECTOR GEN 0 87\n",
+ "CITY CLERK 0 84\n",
+ "HUMAN RELATIONS 0 16\n",
+ "TREASURER 0 22\n",
+ "COPA 0 116\n",
+ "DoIT 0 99\n",
+ "DISABILITIES 0 28\n",
+ "PROCUREMENT 2 90\n",
+ "FIRE 2 4639\n",
+ "BUDGET & MGMT 2 44\n",
+ "HEALTH 3 485\n",
+ "COMMUNITY DEVELOPMENT 4 203\n",
+ "HUMAN RESOURCES 4 75\n",
+ "BUSINESS AFFAIRS 7 164\n",
+ "CULTURAL AFFAIRS 7 58\n",
+ "MAYOR'S OFFICE 8 77\n",
+ "POLICE 10 13404\n",
+ "ANIMAL CONTRL 19 62\n",
+ "LAW 40 367\n",
+ "FINANCE 44 516\n",
+ "CITY COUNCIL 64 347\n",
+ "FAMILY & SUPPORT 287 328\n",
+ "PUBLIC LIBRARY 299 716\n",
+ "TRANSPORTN 725 415\n",
+ "GENERAL SERVICES 765 215\n",
+ "AVIATION 1082 547\n",
+ "OEMC 1273 829\n",
+ "WATER MGMNT 1513 366\n",
+ "STREETS & SAN 1862 336"
+ ]
+ },
+ "execution_count": 40,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "pd.crosstab(data['Department'], data['Salary or Hourly'] ).sort_values('Hourly')\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "The workers from the department with the most hourly workers have complained that their hourly wage is less than $35/hour. Using a one sample t-test, test this one-sided hypothesis at the 95% confidence level."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "TtestResult(statistic=-9.567447887848152, pvalue=3.3378530564707717e-21, df=1861)"
+ ]
+ },
+ "execution_count": 42,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "hourly_ds = data[(data['Department'] == 'STREETS & SAN') & (data['Salary or Hourly'] == 'Hourly')]['Hourly Rate']\n",
+ "h_tr = ttest_1samp(hourly_ds, 35.00)\n",
+ "h_tr"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 43,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'Considering the pvalue is smaller than 0.05 and the statistics is negative, then we reject the null hypothesis'"
+ ]
+ },
+ "execution_count": 43,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "\"\"\"Considering the pvalue is smaller than 0.05 and the statistics is negative, then we reject the null hypothesis\"\"\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Challenge 3: To practice - Constructing Confidence Intervals\n",
+ "\n",
+ "While testing our hypothesis is a great way to gather empirical evidence for accepting or rejecting the hypothesis, another way to gather evidence is by creating a confidence interval. A confidence interval gives us information about the true mean of the population. So for a 95% confidence interval, we are 95% sure that the mean of the population is within the confidence interval. \n",
+ ").\n",
+ "\n",
+ "To read more about confidence intervals, click [here](https://en.wikipedia.org/wiki/Confidence_interval).\n",
+ "\n",
+ "\n",
+ "In the cell below, we will construct a 95% confidence interval for the mean hourly wage of all hourly workers. \n",
+ "\n",
+ "The confidence interval is computed in SciPy using the `t.interval` function. You can read more about this function [here](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.t.html).\n",
+ "\n",
+ "To compute the confidence interval of the hourly wage, use the 0.95 for the confidence level, number of rows - 1 for degrees of freedom, the mean of the sample for the location parameter and the standard error for the scale. The standard error can be computed using [this](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.sem.html) function in SciPy."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 48,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.1352368565101596"
+ ]
+ },
+ "execution_count": 48,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "hourly_rates = data[data['Salary or Hourly'] == 'Hourly']['Hourly Rate']\n",
+ "from scipy import stats\n",
+ "er = stats.sem(hourly_rates)\n",
+ "er"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 49,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(32.52345834488425, 33.05365708767623)"
+ ]
+ },
+ "execution_count": 49,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "stats.t.interval(0.95,hourly_rates.shape[0]-1, loc=hourly_rates.mean(), scale=er)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now construct the 95% confidence interval for all salaried employeed in the police in the cell below."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 53,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "157.82445675052244"
+ ]
+ },
+ "execution_count": 53,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "p_er = stats.sem(police_data)\n",
+ "p_er"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 54,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(86177.05631531784, 86795.77269094894)"
+ ]
+ },
+ "execution_count": 54,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "stats.t.interval(0.95,police_data.shape[0]-1, loc=police_data.mean(), scale=p_er)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Bonus Challenge - Hypothesis Tests of Proportions\n",
+ "\n",
+ "Another type of one sample test is a hypothesis test of proportions. In this test, we examine whether the proportion of a group in our sample is significantly different than a fraction. \n",
+ "\n",
+ "You can read more about one sample proportion tests [here](http://sphweb.bumc.bu.edu/otlt/MPH-Modules/BS/SAS/SAS6-CategoricalData/SAS6-CategoricalData2.html).\n",
+ "\n",
+ "In the cell below, use the `proportions_ztest` function from `statsmodels` to perform a hypothesis test that will determine whether the number of hourly workers in the City of Chicago is significantly different from 25% at the 95% confidence level."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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.10.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}