# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/Nckflannery/DS-Unit-1-Sprint-3-Statistical-Tests-and-Experiments/blob/master/module1-statistics-probability-and-inference/LS_DS_131_Statistics_Probability_Assignment.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="cpw-ckzJT9gS" colab_type="text"
# <img align="left" src="https://lever-client-logos.s3.amazonaws.com/864372b1-534c-480e-acd5-9711f850815c-1524247202159.png" width=200>
# <br></br>
# <br></br>
#
# ## *Data Science Unit 1 Sprint 3 Assignment 1*
#
# # Apply the t-test to real data
#
# Your assignment is to determine which issues have "statistically significant" differences between political parties in this [1980s congressional voting data](https://archive.ics.uci.edu/ml/datasets/Congressional+Voting+Records). The data consists of 435 instances (one for each congressperson), a class (democrat or republican), and 16 binary attributes (yes or no for voting for or against certain issues). Be aware - there are missing values!
#
# Your goals:
#
# 1. Load and clean the data (or determine the best method to drop observations when running tests)
# 2. Using hypothesis testing, find an issue that democrats support more than republicans with p < 0.01
# 3. Using hypothesis testing, find an issue that republicans support more than democrats with p < 0.01
# 4. Using hypothesis testing, find an issue where the difference between republicans and democrats has p > 0.1 (i.e. there may not be much of a difference)
#
# Note that this data will involve *2 sample* t-tests, because you're comparing averages across two groups (republicans and democrats) rather than a single group against a null hypothesis.
#
# Stretch goals:
#
# 1. Refactor your code into functions so it's easy to rerun with arbitrary variables
# 2. Apply hypothesis testing to your personal project data (for the purposes of this notebook you can type a summary of the hypothesis you formed and tested)

# + id="afjTZqU0T9gU" colab_type="code" colab={}
### YOUR CODE STARTS HERE
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.stats import ttest_ind, ttest_ind_from_stats, ttest_rel, ttest_1samp

# + [markdown] id="Y8loWeTJpdIw" colab_type="text"
# ## 1.) Load and clean the data

# + id="mrFuQupgnA2k" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="9cfb7ec2-0ebe-44ee-ddfd-c233d5b1a7df"
# !wget 'https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data'

# + id="bOVmWiyLnDzj" colab_type="code" colab={}
# Read in data to df and label columns ###THANKS RYAN!

df = pd.read_csv('house-votes-84.data', names=['party','handicapped-infants',
                          'water-project', 'budget', 'physician-fee-freeze', 
                          'el-salvador-aid', 'religious-groups', 
                          'anti-satellite-ban', 'aid-to-contras', 'mx-missile',
                          'immigration', 'synfuels', 'education', 
                          'right-to-sue','crime','duty-free', 'south-africa'])

# + id="NXgTG2BnnLVY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="59190399-409f-4bb3-e445-61cf90eec5f3"
df.head()

# + id="GEwcIRY5nnM6" colab_type="code" colab={}
# Clean data
df = df.replace({'y':1 , 'n':0, '?':np.nan})

# + id="jAhA9MgBpLT0" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="1a09e832-903a-4113-a0ab-3085dc87096e"
df.head()

# + id="nMC428K6pL7j" colab_type="code" colab={}
dem = df[df.party == 'democrat']

# + id="sQ6TjTITqdDr" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="566df017-7576-4edb-d73e-45bd46ca31cf"
dem.head()

# + id="DvZ97UFvqVPb" colab_type="code" colab={}
rep = df[df.party == 'republican']

# + id="A-i3zTh_qhea" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="824d7241-df41-4891-dd37-0df657ea4673"
rep.head()

# + [markdown] id="VYAFqANIpfxm" colab_type="text"
# ## 2.) Using hypothesis testing, find an issue that Democrats support more than Republicans with p<0.01

# + [markdown] id="DyPpH1NRsIUi" colab_type="text"
# $H_0$ = $\bar{x_1} = \bar{x_2}$ (Democratic support = Republican support)
#
# $H_a$ = $\bar{x_1} \neq \bar{x_2}$ (Democratic support $\neq$ Republican support)
#
# Confidence Level = 99%
#

# + id="HbqDk88PqHmn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="deed181d-c152-40e7-b7d7-eb9831c365b8"
ttest_ind(dem['anti-satellite-ban'], rep['anti-satellite-ban'], nan_policy='omit')

# + [markdown] id="Mc9TZLPGufbs" colab_type="text"
# t-statistic = 12.526
#
# p-value = 8.5e-31
#
# ---
# Conclusion: Due to our p-value < .01, we reject the null hypothesis that Democratic and Republican support of Anti-Satellite-Ban bill are equal. Since we have a large, positive t-statistic, we infer that the mean yes votes for Democrats is greater than the mean yes votes for Republicans 

# + [markdown] id="vJPU2GMD0zvB" colab_type="text"
# ###Verify with .mean()

# + id="F4RWpHhM0tAY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8634c062-9441-4729-b4ab-c4a3186fec2b"
dem['anti-satellite-ban'].mean()

# + id="twckFiBX07RO" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="2422611c-1920-466d-9ca0-6d5da2385f83"
rep['anti-satellite-ban'].mean()

# + [markdown] id="SrGZ_PC4piY0" colab_type="text"
# ## 3.) Using hypothesis testing, find an issue that Republicans support more than Democrats with p<0.01

# + [markdown] id="1nAiCG-J0f0e" colab_type="text"
# $H_0$ = $\bar{x_1} = \bar{x_2}$ (Republican support = Democratic support)
#
# $H_a$ = $\bar{x_1} \neq \bar{x_2}$ (Republican support $\neq$ Democratic support)
#
# Confidence Level = 99%

# + id="tCBPuraAuoE-" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="bb92913b-90da-4c3a-e6d7-81f4b9cafc50"
ttest_ind(rep['crime'], dem['crime'], nan_policy='omit')

# + [markdown] id="cc-me3pL1WTh" colab_type="text"
# t-statistic = 16.342
#
# p-value = 9.95e-47
#
# ---
# Conclusion: Due to our p-value < .01, we reject the null hypothesis that Democratic and Republican support of the Crime bill are equal. Since we have a large, positive t-statistic, we infer that the mean yes votes for Republicans is greater than the mean yes votes for Democrats 

# + [markdown] id="qDo_wGps1pN1" colab_type="text"
# ###Verify with .mean()
#

# + id="LJaXUcLP1s4B" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="bc2181ed-9b20-4074-b04d-e876f07d93b4"
rep['crime'].mean()

# + id="eAy4OozB1vur" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="eb9931e4-5015-46a0-d461-b15d147cbce9"
dem['crime'].mean()

# + [markdown] id="wVDmDaezpiqV" colab_type="text"
# ## 4.) Using hypothesis testing, find an issue where the difference between Republicans and democrats has p>0.1 (i.e. there may not be much of a difference)

# + [markdown] id="13Dd65hw12ks" colab_type="text"
# $H_0$ = $\bar{x_1} = \bar{x_2}$ (Republican support = Democratic support)
#
# $H_a$ = $\bar{x_1} \neq \bar{x_2}$ (Republican support $\neq$ Democratic support)
#
# Confidence Level = 90%

# + id="FJmusuMfphMz" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="658a173f-850d-41f2-d6b7-b9d60ac25b21"
ttest_ind(rep['water-project'], dem['water-project'], nan_policy='omit')

# + [markdown] id="jnuDox6X29mj" colab_type="text"
# t-statistic = 0.0889
#
# p-value = 0.9291
#
# ---
# Conclusion: Due to our p-value > 0.1, we fail to reject the null hypothesis that Democratic and Republican support of the Water Project bill are equal. Since we have a small, positive t-statistic, we infer that the mean yes votes for Republicans is almost equal (slightly greater) to the mean yes votes for Democrats 

# + [markdown] id="O5V6NHzt3r5u" colab_type="text"
# ###Verify with .mean()

# + id="7x0bYKIE2BEH" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6c6106cd-bb37-41e5-f5be-dbe04e90dab2"
dem['water-project'].mean()

# + id="LxPARIz22edn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="f4d1d925-0942-40af-ea3b-230f333a5e39"
rep['water-project'].mean()

# + [markdown] id="oQypNXhS34w6" colab_type="text"
# ##Pre-stretch Goal
#

# + [markdown] id="8EqnqQtcFQWc" colab_type="text"
# ###1 sample t-tests

# + [markdown] id="8gSiRtx63_YV" colab_type="text"
# ####$H_0$ = Democratic Support for Water Project bill = 100%
#
# $H_a$ = Democratic Support for Water Project bill $\neq$ 100%
#
# Confidence Level = 95%

# + id="29s6F1d739FB" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="854adcc6-6735-417b-bd47-d3db71d85e86"
ttest_1samp(dem['water-project'], 1, nan_policy='omit')

# + [markdown] id="fIN7htL6469Q" colab_type="text"
# t-statistic = -15.3628
#
# p-value = 1.8e-37
#
# ---
# Due to our p-value < 0.05, we reject the null hypothesis that the Democrats  unanimously support the bill. The large, negative t-statistic implies that the support is less than 100%

# + id="ByLHBsFY6pnX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="4e66c0be-e8c9-415a-e52c-6f05acc330bd"
dem['water-project'].mean()

# + [markdown] id="AMyJVkWG5f6J" colab_type="text"
# ####$H_0$ = Republican Support for South Africa bill < 20%
#
# $H_a$ = Republican Support for South Africa bill > 20%
#
# Confidence Level = 95%

# + id="4SHBVE024fBs" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="41d0fa7f-847b-4959-99af-c5b3b4699a2a"
ttest_1samp(rep['south-africa'], .2, nan_policy='omit')

# + [markdown] id="enMvRuxi6TPO" colab_type="text"
# t-statistic = 11.610
#
# p-value = 1.9e-22
#
# ---
# Due to our p-value < 0.05, and our t-statistic being a large, positive number, we reject the null hypothesis that Republican support for the South Africa bill was less than 20%

# + id="i0VlXdjd52HB" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="43c6fa42-985a-493f-91dd-ccdedc1d5eda"
rep['south-africa'].mean()

# + [markdown] id="uWWHIiHaFMkm" colab_type="text"
# ###Plotting stuff

# + id="J-nO7dO4FZSk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 323} outputId="c36700c7-09fa-416f-fa87-dc2862d150e9"
ttest_ind(rep, dem, nan_policy='omit')

# + id="edEcQtE1KcDI" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="95a58fc0-21fc-4c62-ba1e-9c29f080b787"
df.head()

# + id="8Cv-mIpJIxnb" colab_type="code" colab={}
df_tidy = df.melt(id_vars='party')

# + id="ZEKSNi9cNJPS" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="8c90550c-9b84-4501-fb2d-949892e370c4"
df_tidy.head()

# + id="Vyg5SbwRUcTp" colab_type="code" colab={}
df_tidy = df_tidy.rename(columns={'variable' : 'Bill', 'value':'Vote'})

# + id="FtKs281zUxho" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="fc65ca70-db80-4057-c778-beca1f140994"
df_tidy.head()

# + id="Xcs8mEXNhf3f" colab_type="code" colab={}
pvalues = [1.61344033e-018, 9.29155682e-001, 2.07034028e-077,
                   1.99426231e-177, 5.60052011e-068, 2.39367225e-020,
                   8.52103302e-031, 2.82471841e-054, 5.03079265e-047,
                   8.33024849e-002, 1.57593223e-015, 1.88342040e-064,
                   1.22785817e-034, 9.95234271e-047, 5.99769717e-032,
                   3.65267436e-011]

# + id="i1dSjDRRMAwA" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 708} outputId="1a2a5ed8-462f-4a1f-92d7-4bf4becac4c2"
sns.set(style="ticks", color_codes=True)
g = sns.catplot(x='party', y='Vote', col='Bill', kind='bar', col_wrap=8, data=df_tidy, 
                ci=None, legend_out=True, aspect = .6)

g.set_axis_labels('', 'Vote').fig.subplots_adjust(wspace=.05, hspace=.07);

# + id="iquhjlMQMvIZ" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered