In [38]:
%matplotlib inline
import geopandas
import pandas as pd
import xlwings
import plotnine
import statsmodels

import statsmodels.formula.api as smf


import matplotlib.pyplot as plt


dnr_counties = geopandas.read_file('/Users/alys/Downloads/County_Boundaries_24K.shp')

#For consistency

dnr_counties["County"] = dnr_counties["COUNTY_NAM"].str.upper()

dnr_counties["County"] = dnr_counties["County"].replace("SAINT CROIX", "ST. CROIX")


#dnr_counties.head()

Initial Data

Registration Data

Get and clean a number of files provided by the state.

In [39]:
registered_2018_wb = pd.read_excel("data/registeredvotersbycounty_11_01_2018_xlsx_77758.xlsx")
registered_2016_wb = pd.read_excel("data/registeredvotersbycounty_xlsx_45404.xlsx") #pretty sure this is 2016??
registered_2014_prior = pd.read_excel("data/voter_registration_summary_statistics_xlsx_11280.xlsx", sheet_name="Voters by County")

Initial Cleaning

In [40]:
registered_2014_prior_long = registered_2014_prior.melt(id_vars="County", var_name="election", value_name="registered_voters")

registered_2014_prior_long.columns
Out[40]:
Index(['County', 'election', 'registered_voters'], dtype='object')
In [41]:
cols = registered_2016_wb.iloc[5]

registered_2016_wb.columns = cols

registered_2016 = registered_2016_wb[["CountyCode", "County", "Registered Voters"]][8:80]
registered_2016.reset_index()

registered_2016 = registered_2016[["County", "Registered Voters"]]
registered_2016["election"] = pd.Series(["11/8/2016"] * 80)
registered_2016.columns = ['County', 'registered_voters', 'election']

registered_2016.head()
Out[41]:
County registered_voters election
8 ADAMS COUNTY 11892 11/8/2016
9 ASHLAND COUNTY 9844 11/8/2016
10 BARRON COUNTY 26333 11/8/2016
11 BAYFIELD COUNTY 11299 11/8/2016
12 BROWN COUNTY 150152 11/8/2016
In [42]:
registered_2018 = registered_2018_wb[["County", "Registered Voters"]][0:72]

registered_2018["election"] = pd.Series(["11/8/2018"] * 79)
registered_2018.columns = ['County', 'registered_voters', 'election']

registered_2018.head()
Out[42]:
County registered_voters election
0 ADAMS COUNTY 11315.0 11/8/2018
1 ASHLAND COUNTY 8840.0 11/8/2018
2 BARRON COUNTY 24472.0 11/8/2018
3 BAYFIELD COUNTY 10819.0 11/8/2018
4 BROWN COUNTY 141443.0 11/8/2018

Merging

In [43]:
registered_all = registered_2018.append(registered_2016) #.sort_values("County")

registered_all = registered_all.append(registered_2014_prior_long)

registered_all.tail()

#registered_all.reset_index()

registered_all[(registered_all['County'] == 'ADAMS COUNTY') & (registered_all['registered_voters'] > 11516)]
Out[43]:
County election registered_voters
8 ADAMS COUNTY 11/8/2016 11892
438 ADAMS COUNTY 05/01/2014 11528
730 ADAMS COUNTY 01/01/2014 11543
803 ADAMS COUNTY 12/02/2013 11558
876 ADAMS COUNTY 11/05/2013 11562
949 ADAMS COUNTY 09/18/2013 11581
1022 ADAMS COUNTY 08/01/2013 11621
1095 ADAMS COUNTY 07/01/2013 11622
1168 ADAMS COUNTY 06/01/2013 11611
1241 ADAMS COUNTY 05/01/2013 12415
1314 ADAMS COUNTY 04/01/2013 12566
1387 ADAMS COUNTY 03/01/2013 12615
1460 ADAMS COUNTY 02/01/2013 12709
1533 ADAMS COUNTY 01/04/2013 12719
1606 ADAMS COUNTY 12/03/2012 12533
1679 ADAMS COUNTY 11/05/2012 12055
1752 ADAMS COUNTY 10/19/2012 12068
1825 ADAMS COUNTY 10/08/2012 12106
1898 ADAMS COUNTY 09/28/2012 12098
1971 ADAMS COUNTY 08/09/2012 12074
2336 ADAMS COUNTY 12/28/2010 12669
2409 ADAMS COUNTY 08/02/2010 12412
2482 ADAMS COUNTY 07/01/2009 12556
In [44]:
registered_midterm_d = registered_2018.merge(registered_all[registered_all['election'] =="11/01/2014"] ,left_on="County", right_on="County", suffixes=("_2018", "_2014"))

registered_midterm_d["midterm_registered_change"] =registered_midterm_d["registered_voters_2018"] - registered_midterm_d["registered_voters_2014"]
registered_midterm_d["County"] = registered_midterm_d["County"].str.replace(" COUNTY", "")

registered_midterm_d.head()
Out[44]:
County registered_voters_2018 election_2018 election_2014 registered_voters_2014 midterm_registered_change
0 ADAMS 11315.0 11/8/2018 11/01/2014 11499 -184
1 ASHLAND 8840.0 11/8/2018 11/01/2014 9767 -927
2 BARRON 24472.0 11/8/2018 11/01/2014 25545 -1073
3 BAYFIELD 10819.0 11/8/2018 11/01/2014 10809 10
4 BROWN 141443.0 11/8/2018 11/01/2014 141915 -472
In [45]:
registered_presidential_d = registered_2016.merge(registered_all[registered_all['election'] =="11/05/2012"] ,left_on="County", right_on="County", suffixes=("_2016", "_2012"))

registered_presidential_d["presidential_registered_change"] =registered_presidential_d["registered_voters_2016"] - registered_presidential_d["registered_voters_2012"]
registered_presidential_d["County"] = registered_presidential_d["County"].str.replace(" COUNTY", "")

registered_presidential_d.head()
Out[45]:
County registered_voters_2016 election_2016 election_2012 registered_voters_2012 presidential_registered_change
0 ADAMS 11892 11/8/2016 11/05/2012 12055 -163
1 ASHLAND 9844 11/8/2016 11/05/2012 10276 -432
2 BARRON 26333 11/8/2016 11/05/2012 26802 -469
3 BAYFIELD 11299 11/8/2016 11/05/2012 11092 207
4 BROWN 150152 11/8/2016 11/05/2012 146310 3842

Vote Totals

In [46]:
votes_2018_wb = pd.read_excel("data/County by County Report-2018 Gen Election-State Constitutional Offices.xlsx", sheet_name=1)
votes_2016_wb = pd.read_excel("data/County by County Report President of the United States Recount_2016.xlsx")
votes_2014_wb = pd.read_excel("data/County by County Report_2014.xlsx", sheet_name=1)
votes_2012_wb = pd.read_excel("data/County by County_11.6.12.xls", sheet_name=1)
WARNING *** file size (526414) not 512 + multiple of sector size (512)
In [47]:
def process_vote_df(vote_df):
    top_label = vote_df.iloc[4]
    bottom_label = vote_df.iloc[5]

    cols = top_label + " (" + bottom_label + ")"

    cols[0] = "County"
    cols[2] = "total_votes"

    vote_df_new = vote_df[6:]
    vote_df_new.columns = cols
    
    vote_df_new["total_votes"].astype('float64')
    
    vote_df_new["County"] = vote_df_new["County"].str.strip()
    return vote_df_new

votes_2018 = process_vote_df(votes_2018_wb)
votes_2016 = process_vote_df(votes_2016_wb)
votes_2014 = process_vote_df(votes_2014_wb)
votes_2012 = process_vote_df(votes_2012_wb)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/ipykernel_launcher.py:15: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  from ipykernel import kernelapp as app

Historic Population

In [48]:
historic_pop_estimates = pd.read_csv("data/PEP_2018_PEPANNRES_with_ann.csv")

#Replace IDs with descriptive ones
descriptive_names = list(historic_pop_estimates.iloc[0,:])
descriptive_names = [name.replace("Population Estimate (as of July 1) -", "") for name in descriptive_names]
historic_pop_estimates.columns = descriptive_names

# Match column names

historic_pop_estimates["County"] = historic_pop_estimates["Geography"].str.replace(" County, Wisconsin", "")



historic_pop_estimates_long = historic_pop_estimates[1:].melt(id_vars=["Id", "Id2", "Geography", "County", "April 1, 2010 - Census", "April 1, 2010 - Estimates Base"],
                                                             var_name="Year", value_name="Population")

historic_pop_estimates_long.head()
Out[48]:
Id Id2 Geography County April 1, 2010 - Census April 1, 2010 - Estimates Base Year Population
0 0500000US55001 55001 Adams County, Wisconsin Adams 20875 20871 2010 20882
1 0500000US55003 55003 Ashland County, Wisconsin Ashland 16157 16157 2010 16143
2 0500000US55005 55005 Barron County, Wisconsin Barron 45870 45867 2010 45810
3 0500000US55007 55007 Bayfield County, Wisconsin Bayfield 15014 15013 2010 15005
4 0500000US55009 55009 Brown County, Wisconsin Brown 248007 248007 2010 248484

Demographic information

In [49]:
demo = pd.read_csv("data/ACS_17_5YR_DP05_with_ann.csv")

demo_col_labels = dict(demo.loc[0])

demo = demo[1:]

demo['County'] = demo['GEO.display-label'].str.split(pat="County", expand=True)[0]
demo['County'] = demo['County'].str.strip()
demo['County'] = demo['County'].str.upper()

demo.head()
Out[49]:
GEO.id GEO.id2 GEO.display-label HC01_VC03 HC02_VC03 HC03_VC03 HC04_VC03 HC01_VC04 HC02_VC04 HC03_VC04 ... HC04_VC113 HC01_VC114 HC02_VC114 HC03_VC114 HC04_VC114 HC01_VC115 HC02_VC115 HC03_VC115 HC04_VC115 County
1 0500000US55001 55001 Adams County, Wisconsin 20111 ***** 20111 (X) 10770 84 53.6 ... (X) 9048 122 53.7 0.5 7789 70 46.3 0.5 ADAMS
2 0500000US55003 55003 Ashland County, Wisconsin 15779 ***** 15779 (X) 7954 64 50.4 ... (X) 6006 40 49.4 0.3 6163 37 50.6 0.3 ASHLAND
3 0500000US55005 55005 Barron County, Wisconsin 45358 ***** 45358 (X) 22759 83 50.2 ... (X) 17395 85 49.6 0.1 17671 66 50.4 0.1 BARRON
4 0500000US55007 55007 Bayfield County, Wisconsin 15004 ***** 15004 (X) 7642 39 50.9 ... (X) 6225 35 50.8 0.3 6021 44 49.2 0.3 BAYFIELD
5 0500000US55009 55009 Brown County, Wisconsin 258004 ***** 258004 (X) 128020 ***** 49.6 ... (X) 91455 530 49.0 0.2 95283 441 51.0 0.2 BROWN

5 rows × 360 columns

In [50]:
demo['percent_poc'] = 100 - demo['HC03_VC99'].astype('float64')  
demo['percent_eligible_vote'] = demo['HC01_VC113'].astype('int') / demo['HC01_VC03'].astype('int')
demo['median_age'] = demo['HC01_VC24'].astype('float64')
demo['voting_population'] = demo['HC01_VC113'].astype('int')

Turnout

Turnout Change Over Time

Now let's calculate the change over time.

In [51]:
def calculate_turnout(df, year):
    return (df["total_votes_" + year] / df["registered_voters_" + year]).astype('float64')


def calculate_turnout2(df, year):
    turnout = (df["total_votes_" + year] / df["registered_voters_" + year]).astype('float64')
    scaled_turnout = turnout*100
    return turnout, scaled_turnout


def calculate_dem_margin(df, nth):
    dem_turnout_name = [col for col in df.columns if "DEM" in col.upper()][nth]
    rep_turnout_name = [col for col in df.columns if "REP" in col.upper()][nth]
    
    overall_total_name = [col for col in df.columns if "total_votes_" in col.lower()][nth]
    
    
    new_series = (df[dem_turnout_name] - df[rep_turnout_name])/df[overall_total_name]
    return new_series.astype('float64')

def calculate_ratio(df, year1, year2):
    ratio = df["Turnout_"+year1] / df["Turnout_"+year2]
    change = (ratio - 1).astype('float64')
    return ratio, change
In [52]:
votes_midterm_d = votes_2018.iloc[:, 0:5].merge(registered_midterm_d, left_on="County", right_on="County")
votes_midterm_d = votes_midterm_d.merge(votes_2014.iloc[:, 0:5], left_on="County", right_on="County", suffixes=("_2018", "_2014"))

votes_midterm_d["Turnout_2018"], votes_midterm_d["Turnout_2018_scaled"] = calculate_turnout2(votes_midterm_d, "2018")
votes_midterm_d["Turnout_2014"], votes_midterm_d["Turnout_2014_scaled"] = calculate_turnout2(votes_midterm_d, "2014")

votes_midterm_d["Turnout_ratio"], votes_midterm_d["Turnout_change"] = calculate_ratio(votes_midterm_d, "2018", "2014")

votes_midterm_d["Democratic_margin_2018"] = calculate_dem_margin(votes_midterm_d, 0)
votes_midterm_d["Democratic_margin_2014"] = calculate_dem_margin(votes_midterm_d, 1)


#votes_midterm_d.head(15)
In [53]:
presidential_voters_d = votes_2016.iloc[:, 0:5].merge(registered_presidential_d, left_on="County", right_on="County")
presidential_voters_d = presidential_voters_d.merge(votes_2012.iloc[:, 0:5], left_on="County", right_on="County", suffixes=("_2016", "_2012"))

presidential_voters_d["Turnout_2016"], presidential_voters_d["Turnout_2016_scaled"] = calculate_turnout2(presidential_voters_d, "2016")
presidential_voters_d["Turnout_2012"], presidential_voters_d["Turnout_2012_scaled"] = calculate_turnout2(presidential_voters_d, "2012")

presidential_voters_d["Turnout_ratio"], presidential_voters_d["Turnout_change"] = calculate_ratio(presidential_voters_d, "2016", "2012")

presidential_voters_d["Democratic_margin_2016"] = calculate_dem_margin(presidential_voters_d, 0)
presidential_voters_d["Democratic_margin_2012"] = calculate_dem_margin(presidential_voters_d, 1)


#presidential_voters_d.head(15)
In [54]:
all_voters_d = presidential_voters_d.merge(votes_midterm_d, left_on="County", right_on="County", suffixes=("_presidential", "_midterm"))

all_voters_d_with_demo = all_voters_d.merge(demo, left_on="County", right_on="County")

County-by-County Maps

In [55]:
def plot_var(df, col, cmap=None):
    global count
    ts = datetime.datetime.now().strftime("%y_%m_%d_%H_%M")
    dnr_counties_midterm = dnr_counties.merge(df, left_on="County", right_on="County")


    fig, ax = plt.subplots(figsize=(12,10), subplot_kw={'aspect':'equal'})
    

    plot = dnr_counties_midterm.plot(column=col,  legend=True, ax=ax, cmap=cmap, label="test")
    #ax.legend(loc=2, prop={'size': 6})
    ax.set_axis_off()
    
    plt.savefig(f"turnout {ts} {count}.png")
    plt.savefig(f"turnout {ts} {count}.svg")
    count += 1
    
    return plot
    
In [56]:
import datetime

count = 0
ts = datetime.datetime.now().strftime("%y_%m_%d_%H_%M")

def plot_scatter(df, xcol, ycol, title=None, x_title=None, y_title=None):
    
    global count

    x = df[xcol] 
    y = df[ycol] 
    x = statsmodels.api.add_constant(x)

    model = statsmodels.api.OLS(y, x)
    results = model.fit()

    #s = results.summary()
    intercept, slope = results.params
    
    font = plotnine.themes.element_text(family="Marcellus", size=16)
    font2 = plotnine.themes.element_text(family="DejaVu Sans", size=9)
    
    plot = (plotnine.ggplot(df, plotnine.aes(x=xcol, y=ycol)) 
     + plotnine.geom_point() + plotnine.theme_light()
     #+ plotnine.geom_label(plotnine.aes(label="County"))
     + plotnine.geom_abline(slope=slope, intercept=intercept)
    + plotnine.theme(text=font, axis_text=font2))
    
    if title is not None:
        plot += plotnine.labels.ggtitle(title=title)
    if x_title is not None:
        plot += plotnine.scale_x_continuous(name=x_title)
    if y_title is not None:
        plot += plotnine.scale_y_continuous(name=y_title)
    
    rsq = ("{:.4f}" if abs(results.rsquared) > .001 else "{:.4e}").format(results.rsquared)
    m = ("{:.4f}" if abs(slope) > .001 else "{:.4e}").format(slope)
    b= ("{:.4f}" if abs(intercept) > .001 else "{:.4e}").format(intercept)
    
    plot.save(f"turnout {ts} {count}", width=7.50, height=6, dpi=150)
    plot.save(f"turnout {ts} {count}.svg")
    count += 1
    
    print(f"R^2 {rsq}; y={m}x + {b}")  
    
    return plot, results
In [57]:
plot_var(votes_midterm_d, "Turnout_change",cmap='Blues')

plt.title("2018 Turnout Change", fontsize=24, fontname="Marcellus")

count += 1

plt.savefig(f"turnout {ts} {count}.png")

plt.show()
In [58]:
plot_var(presidential_voters_d, "Turnout_change",cmap='RdYlBu')
Out[58]:
<matplotlib.axes._subplots.AxesSubplot at 0x13034c748>
In [59]:
dnr_counties_presidential = dnr_counties.merge(presidential_voters_d, how="outer", left_on="County", right_on="County")

#dnr_counties_presidential[]

#[(col, dnr_counties_presidential[col].hasnans) for col in dnr_counties_presidential.columns]

#import numpy as np
#dnr_counties_presidential[dnr_counties_presidential['Turnout_change'].isna()]



votes_midterm_d["County"]
Out[59]:
0           ADAMS
1         ASHLAND
2          BARRON
3        BAYFIELD
4           BROWN
5         BUFFALO
6         BURNETT
7         CALUMET
8        CHIPPEWA
9           CLARK
10       COLUMBIA
11       CRAWFORD
12           DANE
13          DODGE
14           DOOR
15        DOUGLAS
16           DUNN
17     EAU CLAIRE
18       FLORENCE
19    FOND DU LAC
20         FOREST
21          GRANT
22          GREEN
23     GREEN LAKE
24           IOWA
25           IRON
26        JACKSON
27      JEFFERSON
28         JUNEAU
29        KENOSHA
         ...     
42         OCONTO
43         ONEIDA
44      OUTAGAMIE
45        OZAUKEE
46          PEPIN
47         PIERCE
48           POLK
49        PORTAGE
50          PRICE
51         RACINE
52       RICHLAND
53           ROCK
54           RUSK
55           SAUK
56         SAWYER
57        SHAWANO
58      SHEBOYGAN
59      ST. CROIX
60         TAYLOR
61    TREMPEALEAU
62         VERNON
63          VILAS
64       WALWORTH
65       WASHBURN
66     WASHINGTON
67       WAUKESHA
68        WAUPACA
69       WAUSHARA
70      WINNEBAGO
71           WOOD
Name: County, Length: 72, dtype: object

Historic Turnout

In [60]:
historic_data = pd.read_excel("data/Voter Turnout Partisan-NonPartisan Through April 2018.xlsx")

def get_type(val):

    return "Presidential" if (val % 4 == 0) else "Midterm"


historic_data["Type"] = historic_data["Year"].apply(get_type) # (historic_data["Year"] % 4 == 0) 

historic_data
Out[60]:
Year Voting Age Population General Elections Votes General Election Turnout Partisan Primary Votes Partisan Primary Turnout Presidential Primary Votes Presidential Primary Turnout Type
0 2016 4461068 3004051 0.673393 645619 0.144723 2113544.0 0.473775 Presidential
1 2014 4416501 2422040 0.548407 638677 0.144612 NaN NaN Midterm
2 2012 4378741 3071434 0.701442 851572 0.194479 1144351.0 0.261342 Presidential
3 2010 4372347 2171331 0.496605 857027 0.196011 NaN NaN Midterm
4 2008 4330695 2996869 0.692006 388079 0.089611 1511639.0 0.349052 Presidential
5 2006 4260038 2166671 0.508604 615944 0.144587 NaN NaN Midterm
6 2004 4118621 3016288 0.732354 686546 0.166693 992242.0 0.240916 Presidential
7 2002 4060973 1785710 0.439725 913523 0.224952 NaN NaN Midterm
8 2000 3908533 2619184 0.670119 406665 0.104045 866965.0 0.221813 Presidential
9 1998 3853041 1760836 0.456999 497476 0.129113 NaN NaN Midterm
10 1996 3786560 2196169 0.579991 510725 0.134878 932743.0 0.246330 Presidential
11 1994 3777000 1565628 0.414516 484495 0.128275 NaN NaN Midterm
12 1992 3669000 2531114 0.689865 771110 0.210169 1213715.0 0.330803 Presidential
13 1990 3616000 1397727 0.386540 371407 0.102712 NaN NaN Midterm
14 1988 3593000 2191608 0.609966 899912 0.250463 1374076.0 0.382431 Presidential
15 1986 3522000 1526960 0.433549 570075 0.161861 NaN NaN Midterm
16 1984 3487000 2211689 0.634267 405462 0.116278 952099.0 0.273042 Presidential
17 1982 3437000 1580344 0.459803 924789 0.269069 NaN NaN Midterm
18 1980 3375000 2273221 0.673547 557311 0.165129 1540566.0 0.456464 Presidential
19 1978 3272000 1500996 0.458740 703235 0.214925 NaN NaN Midterm
20 1976 3170000 2104175 0.663778 458671 0.144691 1332340.0 0.420297 Presidential
21 1974 3070000 1199495 0.390715 498980 0.162534 NaN NaN Midterm
22 1972 2967000 1852890 0.624499 513851 0.173189 1415128.0 0.476956 Presidential
23 1970 2620000 1343160 0.512656 518145 0.197765 NaN NaN Midterm
24 1968 2560000 1689186 0.659838 478178 0.186788 1223855.0 0.478068 Presidential
25 1966 2512000 1170173 0.465833 501264 0.199548 NaN NaN Midterm
26 1964 2464000 1692204 0.686771 686417 0.278578 1088153.0 0.441621 Presidential
27 1962 2417000 1265900 0.523748 665220 0.275225 NaN NaN Midterm
28 1960 2369000 1729082 0.729878 544539 0.229860 1182160.0 0.499012 Presidential
29 1958 2336000 1202219 0.514649 476161 0.203836 NaN NaN Midterm
30 1956 2309000 1550558 0.671528 666362 0.288593 786497.0 0.340622 Presidential
31 1954 2283000 1158666 0.507519 558033 0.244430 NaN NaN Midterm
32 1952 2257000 1615214 0.715646 877215 0.388664 1018314.0 0.451180 Presidential
33 1950 2230000 1138087 0.510353 564859 0.253300 NaN NaN Midterm
34 1948 2194000 1266097 0.577072 589500 0.268687 699298.0 0.318732 Presidential
In [61]:
font = plotnine.themes.element_text(family="Marcellus", size=16)
font2 = plotnine.themes.element_text(family="DejaVu Sans", size=9)

plot = (plotnine.ggplot(historic_data[historic_data["Year"] > 1978], plotnine.aes(x="Year", y="General Election Turnout", color="Type")) 
 +  plotnine.geom_line() + plotnine.theme_light()
# + plotnine.facet_wrap(facets="Type")
    + plotnine.theme(text=font, axis_text=font2))
In [62]:
#2018 turnout using nonpartisan voting age population figure

turnout_2018 = all_voters_d["total_votes_2018"].sum() / 4469475

new_row = pd.DataFrame([["2018", 4469475, None, turnout_2018, None, None, None, None, "Midterm"]], columns=historic_data.columns)

historic_data_with2018 = historic_data.append(new_row)

historic_data_with2018
Out[62]:
Year Voting Age Population General Elections Votes General Election Turnout Partisan Primary Votes Partisan Primary Turnout Presidential Primary Votes Presidential Primary Turnout Type
0 2016 4461068 3004051 0.673393 645619 0.144723 2.11354e+06 0.473775 Presidential
1 2014 4416501 2422040 0.548407 638677 0.144612 NaN NaN Midterm
2 2012 4378741 3071434 0.701442 851572 0.194479 1.14435e+06 0.261342 Presidential
3 2010 4372347 2171331 0.496605 857027 0.196011 NaN NaN Midterm
4 2008 4330695 2996869 0.692006 388079 0.0896113 1.51164e+06 0.349052 Presidential
5 2006 4260038 2166671 0.508604 615944 0.144587 NaN NaN Midterm
6 2004 4118621 3016288 0.732354 686546 0.166693 992242 0.240916 Presidential
7 2002 4060973 1785710 0.439725 913523 0.224952 NaN NaN Midterm
8 2000 3908533 2619184 0.670119 406665 0.104045 866965 0.221813 Presidential
9 1998 3853041 1760836 0.456999 497476 0.129113 NaN NaN Midterm
10 1996 3786560 2196169 0.579991 510725 0.134878 932743 0.24633 Presidential
11 1994 3777000 1565628 0.414516 484495 0.128275 NaN NaN Midterm
12 1992 3669000 2531114 0.689865 771110 0.210169 1.21372e+06 0.330803 Presidential
13 1990 3616000 1397727 0.386540 371407 0.102712 NaN NaN Midterm
14 1988 3593000 2191608 0.609966 899912 0.250463 1.37408e+06 0.382431 Presidential
15 1986 3522000 1526960 0.433549 570075 0.161861 NaN NaN Midterm
16 1984 3487000 2211689 0.634267 405462 0.116278 952099 0.273042 Presidential
17 1982 3437000 1580344 0.459803 924789 0.269069 NaN NaN Midterm
18 1980 3375000 2273221 0.673547 557311 0.165129 1.54057e+06 0.456464 Presidential
19 1978 3272000 1500996 0.458740 703235 0.214925 NaN NaN Midterm
20 1976 3170000 2104175 0.663778 458671 0.144691 1.33234e+06 0.420297 Presidential
21 1974 3070000 1199495 0.390715 498980 0.162534 NaN NaN Midterm
22 1972 2967000 1852890 0.624499 513851 0.173189 1.41513e+06 0.476956 Presidential
23 1970 2620000 1343160 0.512656 518145 0.197765 NaN NaN Midterm
24 1968 2560000 1689186 0.659838 478178 0.186788 1.22386e+06 0.478068 Presidential
25 1966 2512000 1170173 0.465833 501264 0.199548 NaN NaN Midterm
26 1964 2464000 1692204 0.686771 686417 0.278578 1.08815e+06 0.441621 Presidential
27 1962 2417000 1265900 0.523748 665220 0.275225 NaN NaN Midterm
28 1960 2369000 1729082 0.729878 544539 0.22986 1.18216e+06 0.499012 Presidential
29 1958 2336000 1202219 0.514649 476161 0.203836 NaN NaN Midterm
30 1956 2309000 1550558 0.671528 666362 0.288593 786497 0.340622 Presidential
31 1954 2283000 1158666 0.507519 558033 0.24443 NaN NaN Midterm
32 1952 2257000 1615214 0.715646 877215 0.388664 1.01831e+06 0.45118 Presidential
33 1950 2230000 1138087 0.510353 564859 0.2533 NaN NaN Midterm
34 1948 2194000 1266097 0.577072 589500 0.268687 699298 0.318732 Presidential
0 2018 4469475 None 0.598126 None None None None Midterm
In [63]:
historic_data_with2018["Year"] = historic_data_with2018["Year"].astype('int32')
historic_data_with2018["General Election Turnout Scaled"] = historic_data_with2018["General Election Turnout"] * 100

plot = (plotnine.ggplot(historic_data_with2018, #[historic_data_with2018["Year"] > 1978], 
                 plotnine.aes(x="Year", y="General Election Turnout Scaled", color="Type")) 
 +  plotnine.geom_line() + plotnine.theme_light()
# + plotnine.facet_wrap(facets="Type")
 + plotnine.labels.ggtitle(title="Turnout over time")
        + plotnine.scale_y_continuous(name="General Election Turnout")
    + plotnine.theme(text=font, axis_text=font2, legend_text=font2))


plot.save(f"turnout {ts} {count}", width=7.50, height=6, dpi=150)

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 3
  warn('Filename: {}'.format(filename))
Out[63]:
<ggplot: (318707976)>
In [64]:
pd.Series.astype?
Signature: pd.Series.astype(self, dtype, copy=True, errors='raise', **kwargs)
Docstring:
Cast a pandas object to a specified dtype ``dtype``.

Parameters
----------
dtype : data type, or dict of column name -> data type
    Use a numpy.dtype or Python type to cast entire pandas object to
    the same type. Alternatively, use {col: dtype, ...}, where col is a
    column label and dtype is a numpy.dtype or Python type to cast one
    or more of the DataFrame's columns to column-specific types.
copy : bool, default True
    Return a copy when ``copy=True`` (be very careful setting
    ``copy=False`` as changes to values then may propagate to other
    pandas objects).
errors : {'raise', 'ignore'}, default 'raise'
    Control raising of exceptions on invalid data for provided dtype.

    - ``raise`` : allow exceptions to be raised
    - ``ignore`` : suppress exceptions. On error return original object

    .. versionadded:: 0.20.0

kwargs : keyword arguments to pass on to the constructor

Returns
-------
casted : same type as caller

See Also
--------
to_datetime : Convert argument to datetime.
to_timedelta : Convert argument to timedelta.
to_numeric : Convert argument to a numeric type.
numpy.ndarray.astype : Cast a numpy array to a specified type.

Examples
--------
>>> ser = pd.Series([1, 2], dtype='int32')
>>> ser
0    1
1    2
dtype: int32
>>> ser.astype('int64')
0    1
1    2
dtype: int64

Convert to categorical type:

>>> ser.astype('category')
0    1
1    2
dtype: category
Categories (2, int64): [1, 2]

Convert to ordered categorical type with custom ordering:

>>> cat_dtype = pd.api.types.CategoricalDtype(
...                     categories=[2, 1], ordered=True)
>>> ser.astype(cat_dtype)
0    1
1    2
dtype: category
Categories (2, int64): [2 < 1]

Note that using ``copy=False`` and changing data on a new
pandas object may propagate changes:

>>> s1 = pd.Series([1,2])
>>> s2 = s1.astype('int64', copy=False)
>>> s2[0] = 10
>>> s1  # note that s1[0] has changed too
0    10
1     2
dtype: int64
File:      ~/.virtualenvs/notebooks/lib/python3.7/site-packages/pandas/core/generic.py
Type:      function

Overall Turnout

In [65]:
font = plotnine.themes.element_text(family="Marcellus", size=16)
font2 = plotnine.themes.element_text(family="DejaVu Sans", size=9)

(plotnine.ggplot(all_voters_d, plotnine.aes(x="Turnout_2018")) 
 +  plotnine.geom_histogram(color="grey",fill="none") + plotnine.theme_light()
    + plotnine.theme(text=font, axis_text=font2))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/stats/stat_bin.py:93: UserWarning: 'stat_bin()' using 'bins = 10'. Pick better value with 'binwidth'.
  warn(msg.format(params['bins']))
Out[65]:
<ggplot: (318343039)>
In [66]:
#all_voters_d_with2018

#from matplotlib import pyplot

plot = (plotnine.ggplot(all_voters_d, plotnine.aes(x="Turnout_2018")) 
 +  plotnine.geom_histogram(color="grey",fill="none") + plotnine.theme_light()
 + plotnine.labels.ggtitle(title="Turnout by Number of Counties")
 + plotnine.scale_y_continuous(name="Number of Counties")
 + plotnine.scale_x_continuous(name="2018 Turnout")
    + plotnine.theme(text=font, axis_text=font2, panel_grid_major=plotnine.element_blank(),
                    panel_grid_minor=plotnine.element_blank()))

count += 1
plot.save(f"turnout {ts} {count}.png")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 6.4 x 4.8 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 4.png
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/stats/stat_bin.py:93: UserWarning: 'stat_bin()' using 'bins = 10'. Pick better value with 'binwidth'.
  warn(msg.format(params['bins']))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/stats/stat_bin.py:93: UserWarning: 'stat_bin()' using 'bins = 10'. Pick better value with 'binwidth'.
  warn(msg.format(params['bins']))
Out[66]:
<ggplot: (-9223372036536221482)>
In [67]:
plot_var(all_voters_d, "Turnout_2018")

plt.title("2018 Turnout", fontsize=24, fontname="Marcellus")

count += 1

plt.savefig(f"turnout {ts} {count}.png")

plt.show()
In [68]:
all_voters_d_with_demo["Turnout_2018_bin"] =pd.cut(all_voters_d_with_demo["Turnout_2018"], 10)

all_voters_d_with_demo.groupby("Turnout_2018_bin").agg({'County': pd.Series.count, 'voting_population': sum})
Out[68]:
County voting_population
Turnout_2018_bin
(0.659, 0.681] 1.0 3035
(0.681, 0.702] NaN 0
(0.702, 0.724] NaN 0
(0.724, 0.746] 2.0 10767
(0.746, 0.767] 7.0 192314
(0.767, 0.789] 17.0 1008384
(0.789, 0.81] 10.0 311005
(0.81, 0.832] 20.0 1294196
(0.832, 0.853] 9.0 594814
(0.853, 0.875] 6.0 914005
In [69]:
all_voters_d["total_votes_2014"].sum() /all_voters_d["registered_voters_2014"].sum()
Out[69]:
0.7083448314167902
In [70]:
all_voters_d["total_votes_2014"].sum()
Out[70]:
2410314
In [71]:
all_voters_d["total_votes_2018"].sum() / 4469475 #/ all_voters_d_with_demo["HC01_VC113"].sum()
Out[71]:
0.5981257306506916

Factors That Influenced Turnout

In [72]:
(plotnine.ggplot(votes_midterm_d, plotnine.aes(x="Turnout_change", y="Democratic_margin_2018")) 
 + plotnine.geom_point() + plotnine.theme_light()
 #+ plotnine.geom_label(plotnine.aes(label="County"))
 
)


plot, summary = plot_scatter(all_voters_d_with_demo, 
                             'Democratic_margin_2018', "Turnout_change_midterm", 
                             title="Turnout change compared to Democratic margin",
                             x_title="Democratic margin in 2018",
                            y_title="Turnout change between 2014 and 2018"
                            )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 6
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 6.svg
  warn('Filename: {}'.format(filename))
R^2 0.2040; y=0.1230x + 0.1635
Out[72]:
<ggplot: (-9223372036540446460)>
In [73]:
(plotnine.ggplot(presidential_voters_d, plotnine.aes(x="Turnout_change", y="Democratic_margin_2016")) 
 + plotnine.geom_point() + plotnine.theme_light()
 #+ plotnine.geom_label(plotnine.aes(label="County"))
 
)

plot, summary = plot_scatter(all_voters_d_with_demo, 
                              'Democratic_margin_2016', "Turnout_change_presidential",
                             title="Turnout change in 2016 compared to Democratic margin",
                             x_title='Democratic margin 2016', y_title="Turnout change")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 7
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 7.svg
  warn('Filename: {}'.format(filename))
R^2 0.3009; y=-0.0966x + -0.0300
Out[73]:
<ggplot: (-9223372036539981813)>
In [74]:
(plotnine.ggplot(all_voters_d, plotnine.aes(x="Turnout_change_presidential", y="Turnout_change_midterm")) 
 + plotnine.geom_point() + plotnine.theme_light()
 #+ plotnine.geom_label(plotnine.aes(label="County"))
 
)
Out[74]:
<ggplot: (317651229)>

It looks like there's not really a relationship between turnout before and after — in other words, the counties with particularly low turnout in 2016 didn't necessarily have less turnout in 2018 (or really, a smaller increase). E.g., turnout dropped significantly in Milwaukee in 2016 (compared to 2012), but it increased by a typical amount in 2018.

Eligible voters vs. Registered voters

How closely do eligible voters track registered voters?

In [75]:
all_voters_d_with_demo["registered_voters_2016"] = all_voters_d_with_demo["registered_voters_2016"].astype('int')
all_voters_d_with_demo['HC01_VC113'] = all_voters_d_with_demo['HC01_VC113'].astype('int')
In [76]:
plot, summary = plot_scatter(all_voters_d_with_demo, "registered_voters_2016", 'HC01_VC113' )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 8
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 8.svg
  warn('Filename: {}'.format(filename))
R^2 0.9929; y=1.1511x + 3219.0593
Out[76]:
<ggplot: (-9223372036537063010)>
In [77]:
plot, summary = plot_scatter(all_voters_d_with_demo, "registered_voters_2018", 'HC01_VC113' )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 9
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 9.svg
  warn('Filename: {}'.format(filename))
R^2 0.9925; y=1.2787x + 2357.3767
Out[77]:
<ggplot: (-9223372036536213290)>
In [78]:
all_voters_d_with_demo.head()
Out[78]:
County nan_2016 total_votes_2016 REP (Donald J. Trump Michael R. Pence) DEM (Hillary Clinton Tim Kaine) registered_voters_2016 election_2016 election_2012 registered_voters_2012 presidential_registered_change ... HC04_VC114 HC01_VC115 HC02_VC115 HC03_VC115 HC04_VC115 percent_poc percent_eligible_vote median_age voting_population Turnout_2018_bin
0 ADAMS NaN 10130 5966 3745 11892 11/8/2016 11/05/2012 12055 -163 ... 0.5 7789 70 46.3 0.5 9.3 0.837204 52.6 16837 (0.81, 0.832]
1 ASHLAND NaN 8032 3303 4226 9844 11/8/2016 11/05/2012 10276 -432 ... 0.3 6163 37 50.6 0.3 17.3 0.771215 41.7 12169 (0.767, 0.789]
2 BARRON NaN 22671 13614 7889 26333 11/8/2016 11/05/2012 26802 -469 ... 0.1 17671 66 50.4 0.1 6.2 0.773094 44.2 35066 (0.746, 0.767]
3 BAYFIELD NaN 9612 4124 4953 11299 11/8/2016 11/05/2012 11092 207 ... 0.3 6021 44 49.2 0.3 15.0 0.816182 51.6 12246 (0.81, 0.832]
4 BROWN NaN 129011 67210 53382 150152 11/8/2016 11/05/2012 146310 3842 ... 0.2 95283 441 51.0 0.2 18.4 0.723779 37.0 186738 (0.81, 0.832]

5 rows × 407 columns

Turnout Change

In [79]:
plot, summary = plot_scatter(all_voters_d_with_demo, "percent_poc",
                             "Turnout_change_midterm",
                            title="Turnout compared to percent people of color by county",
                            x_title="Percent people of color",
                            y_title="Turnout change between 2014 and 2018")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 10
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 10.svg
  warn('Filename: {}'.format(filename))
R^2 0.0684; y=0.0012x + 0.1381
Out[79]:
<ggplot: (-9223372036536402565)>
In [103]:
all_voters_d_with_demo["Turnout_change_presidential_scaled"] = all_voters_d_with_demo["Turnout_change_presidential"]*100
all_voters_d_with_demo["Turnout_change_midterm_scaled"] = all_voters_d_with_demo["Turnout_change_midterm"]*100

plot, summary = plot_scatter(all_voters_d_with_demo,"Turnout_change_presidential_scaled", "Turnout_change_midterm_scaled",
                            title="Turnout change since last presidential versus last midterm")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 26
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 26.svg
  warn('Filename: {}'.format(filename))
R^2 0.0955; y=-0.5038x + 14.2963
Out[103]:
<ggplot: (-9223372036537044164)>
In [81]:
all_voters_d_demo_no_outliers = all_voters_d_with_demo[all_voters_d_with_demo["percent_poc"] < 30]

plot, summary = plot_scatter(all_voters_d_demo_no_outliers, "percent_poc",
                             "Turnout_change_midterm_scaled",
                            title="Turnout change compared to percent people of color by county",
                            x_title="Percent people of color",
                            y_title="Turnout change, 2014 to 2018\n(percentage points)")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 12
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 12.svg
  warn('Filename: {}'.format(filename))
R^2 0.0911; y=-0.1770x + 0.2144
Out[81]:
<ggplot: (318385356)>
In [110]:
#all_voters_d_demo_no_outliers = all_voters_d_with_demo[all_voters_d_with_demo["HC03_VC55"] < 30]

plot, summary = plot_scatter(all_voters_d_with_demo, "HC03_VC55",
                             "Turnout_change_midterm_scaled",
                            title="Turnout change compared to Black resident proportion",
                            x_title="Percent of residents who are Black",
                            y_title="Turnout change, 2014 to 2018\n(percentage points)")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 33
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 33.svg
  warn('Filename: {}'.format(filename))
R^2 0.0166; y=0.2087x + 14.7630
Out[110]:
<ggplot: (-9223372036537092934)>
In [109]:
all_voters_d_demo_no_outliers = all_voters_d_with_demo[all_voters_d_with_demo["HC03_VC55"] < 10]

plot, summary = plot_scatter(all_voters_d_demo_no_outliers, "HC03_VC55",
                             "Turnout_change_midterm_scaled",
                            title="Turnout change compared to Black resident proportion",
                            x_title="Percent of residents who are Black",
                            y_title="Turnout change, 2014 to 2018\n(percentage points)")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 32
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 32.svg
  warn('Filename: {}'.format(filename))
R^2 0.1009; y=1.5292x + 13.3043
Out[109]:
<ggplot: (-9223372036536272538)>

Turnout

In [82]:
plot, summary = plot_scatter(all_voters_d_with_demo, "percent_poc",
                             "Turnout_2018_scaled",
                            title="Turnout compared to percent people of color by county",
                            x_title="Percent people of color",
                            y_title="Turnout 2018")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 13
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 13.svg
  warn('Filename: {}'.format(filename))
R^2 0.0881; y=-0.0925x + 81.3545
Out[82]:
<ggplot: (-9223372036536409954)>
In [83]:
[col for col in all_voters_d_with_demo.columns if "turnout" in col.lower()]
Out[83]:
['Turnout_2016',
 'Turnout_2016_scaled',
 'Turnout_2012',
 'Turnout_2012_scaled',
 'Turnout_ratio_presidential',
 'Turnout_change_presidential',
 'Turnout_2018',
 'Turnout_2018_scaled',
 'Turnout_2014',
 'Turnout_2014_scaled',
 'Turnout_ratio_midterm',
 'Turnout_change_midterm',
 'Turnout_2018_bin',
 'Turnout_change_presidential_scaled',
 'Turnout_change_midterm_scaled']
In [84]:
plot, summary = plot_scatter(all_voters_d_with_demo, "percent_poc",
                             "Turnout_2016_scaled",
                            title="Turnout compared to percent people of color by county",
                            x_title="Percent people of color",
                            y_title="Turnout 2016")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 14
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 14.svg
  warn('Filename: {}'.format(filename))
R^2 0.6464; y=-0.1857x + 86.5239
Out[84]:
<ggplot: (314457983)>
In [85]:
plot, summary = plot_scatter(all_voters_d_with_demo, "Democratic_margin_2016",
                             "Turnout_2016",
                            title="Turnout compared to Democratic margin by county",
                            x_title="Democratic margin",
                            y_title="Turnout 2016")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 15
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 15.svg
  warn('Filename: {}'.format(filename))
R^2 0.4391; y=-0.0954x + 0.8316
Out[85]:
<ggplot: (317632036)>
In [86]:
plot, summary = plot_scatter(all_voters_d_with_demo, "percent_poc",
                             "Turnout_2012_scaled",
                            title="Turnout compared to percent people of color by county",
                            x_title="Percent people of color",
                            y_title="Turnout 2012")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 16
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 16.svg
  warn('Filename: {}'.format(filename))
R^2 0.1094; y=-0.0880x + 86.9150
Out[86]:
<ggplot: (317687286)>
In [87]:
turnout_long = all_voters_d_with_demo.melt(id_vars=["County", "percent_poc"], value_vars=["Turnout_2018", "Turnout_2016", "Turnout_2014", "Turnout_2012"], var_name="Year", value_name="Turnout")

turnout_long["Turnout"] = turnout_long["Turnout"].astype("float64") * 100

turnout_long['Year'] = turnout_long["Year"].str.replace("Turnout_","")

font_main = plotnine.themes.element_text(family="Marcellus", size=16)
font_sans = plotnine.themes.element_text(family="DejaVu Sans", size=9)
font_sans_small = plotnine.themes.element_text(family="DejaVu Sans", size=7)


plot = (plotnine.ggplot(turnout_long, plotnine.aes(x="percent_poc", y="Turnout")) 
 + plotnine.geom_point(shape=".") + plotnine.facet_wrap(facets="Year") 
 + plotnine.geom_smooth(method="lm") + plotnine.theme_light()
+ plotnine.theme(text=font_main, axis_text=font_sans, strip_text=font_sans))

plot += plotnine.labels.ggtitle(title="Turnout by percentage people of color")
plot += plotnine.scale_x_continuous(name="Percent people of color")
plot += plotnine.scale_y_continuous(name="Turnout")

plot.save(f"turnout {ts} {count}", width=7.50, height=6, dpi=150)
plot.save(f"turnout {ts} {count}.svg")

count += 1

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 17
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 17.svg
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
Out[87]:
<ggplot: (-9223372036539979846)>
In [88]:
#HC03_VC54 White ; HC03_VC55 Black; HC03_VC56 Native American; HC03_VC61 Asian American; HC03_VC75 Multiracial; HC03_VC93 Hispanic/Latinx
turnout_long = all_voters_d_with_demo.melt(id_vars=["County", "percent_poc", "HC03_VC54", "HC03_VC55", "HC03_VC56", "HC03_VC61", "HC03_VC75", "HC03_VC93"], 
                                           value_vars=["Turnout_2018", "Turnout_2016", "Turnout_2014", "Turnout_2012"], 
                                           var_name="Year", value_name="Turnout")

turnout_long["White"] = turnout_long["HC03_VC54"]
del turnout_long["HC03_VC54"]
turnout_long["Black"] = turnout_long["HC03_VC55"]
del turnout_long["HC03_VC55"]
turnout_long["Native American"] = turnout_long["HC03_VC56"]
del turnout_long["HC03_VC56"]
turnout_long["Asian American"] = turnout_long["HC03_VC61"]
del turnout_long["HC03_VC61"]
turnout_long["Multiracial"] = turnout_long["HC03_VC75"]
del turnout_long["HC03_VC75"]
turnout_long["Hispanic/Latinx"] = turnout_long["HC03_VC93"]
del turnout_long["HC03_VC93"]


turnout_long = turnout_long.melt(id_vars=["County", "Year", "percent_poc", "Turnout"], var_name="Race", value_name="Percent")

turnout_long.head()

turnout_long["Turnout"] = turnout_long["Turnout"].astype("float64") * 100
turnout_long["Percent"] = turnout_long["Percent"].astype("float64")
turnout_long['Year'] = turnout_long["Year"].str.replace("Turnout_","")


plot = (plotnine.ggplot(turnout_long, plotnine.aes(x="Percent", y="Turnout")) 
 + plotnine.geom_bin2d() + plotnine.facet_grid(facets=["Year","Race"]) 
 + plotnine.geom_smooth(method="lm") + plotnine.theme_light()
+ plotnine.theme(text=font_main, axis_text=font_sans, strip_text=font_sans_small, strip_text_y=font_sans))

plot += plotnine.labels.ggtitle(title="Turnout by race and year")
plot += plotnine.scale_x_continuous(name="Race's proportion of residents")
plot += plotnine.scale_y_continuous(name="Turnout rate")

#plot.save(f"turnout {ts} {count}", width=7.50, height=6, dpi=150)
#plot.save(f"turnout {ts} {count}.svg")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
Out[88]:
<ggplot: (318562701)>

This is a bit hard to read because for several racial groups, the counties all fall with a ten-percent range. Let's allow the x axes to vary by race.

In [89]:
plot = plot + plotnine.geom_point(shape=".") + plotnine.facet_grid(facets=["Year","Race"], scales="free_x") + plotnine.theme(legend_position = "none") 

plot.save(f"turnout {ts} {count}", width=7.50, height=6, dpi=150)
plot.save(f"turnout {ts} {count}.svg")
count +=1

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 18
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 18.svg
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
Out[89]:
<ggplot: (-9223372036536215566)>
In [90]:
all_voters_d_with_demo["HC03_VC55"] = all_voters_d_with_demo["HC03_VC55"].astype('float64')

plot, summary = plot_scatter(all_voters_d_with_demo, "HC03_VC55",
                             "Turnout_2018_scaled",
                            title="Turnout compared to county's proportion of residents who are Black",
                            x_title="Proportion of Black residents",
                            y_title="2018 Turnout")

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 19
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 19.svg
  warn('Filename: {}'.format(filename))
R^2 0.0036; y=0.0659x + 80.2546
Out[90]:
<ggplot: (-9223372036534871089)>

Demographics plus partisanship Modeling

In [91]:
x = all_voters_d_with_demo[["percent_poc", "Democratic_margin_2018"]] 
y = all_voters_d_with_demo["Turnout_change_midterm"] 
x = statsmodels.api.add_constant(x)

model = statsmodels.api.OLS(y, x)
results = model.fit()

results.summary()
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
Out[91]:
OLS Regression Results
Dep. Variable: Turnout_change_midterm R-squared: 0.205
Model: OLS Adj. R-squared: 0.182
Method: Least Squares F-statistic: 8.875
Date: Tue, 11 Jun 2019 Prob (F-statistic): 0.000372
Time: 14:06:12 Log-Likelihood: 115.83
No. Observations: 72 AIC: -225.7
Df Residuals: 69 BIC: -218.8
Df Model: 2
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
const 0.1616 0.010 15.508 0.000 0.141 0.182
percent_poc 0.0001 0.001 0.234 0.815 -0.001 0.001
Democratic_margin_2018 0.1187 0.035 3.437 0.001 0.050 0.188
Omnibus: 1.271 Durbin-Watson: 2.188
Prob(Omnibus): 0.530 Jarque-Bera (JB): 0.689
Skew: 0.183 Prob(JB): 0.709
Kurtosis: 3.310 Cond. No. 96.4


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [92]:
x = all_voters_d_with_demo[["percent_poc", "Democratic_margin_2018", "Turnout_change_presidential"]] 
y = all_voters_d_with_demo["Turnout_change_midterm"] 
x = statsmodels.api.add_constant(x)

model = statsmodels.api.OLS(y, x)
results = model.fit()

results.summary()
Out[92]:
OLS Regression Results
Dep. Variable: Turnout_change_midterm R-squared: 0.217
Model: OLS Adj. R-squared: 0.183
Method: Least Squares F-statistic: 6.299
Date: Tue, 11 Jun 2019 Prob (F-statistic): 0.000779
Time: 14:06:12 Log-Likelihood: 116.41
No. Observations: 72 AIC: -224.8
Df Residuals: 68 BIC: -215.7
Df Model: 3
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
const 0.1587 0.011 14.748 0.000 0.137 0.180
percent_poc -2.186e-05 0.001 -0.036 0.971 -0.001 0.001
Democratic_margin_2018 0.1074 0.036 2.973 0.004 0.035 0.179
Turnout_change_presidential -0.2144 0.203 -1.057 0.294 -0.619 0.190
Omnibus: 1.262 Durbin-Watson: 2.194
Prob(Omnibus): 0.532 Jarque-Bera (JB): 0.757
Skew: 0.229 Prob(JB): 0.685
Kurtosis: 3.205 Cond. No. 557.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [93]:
x = all_voters_d_with_demo[["percent_poc","Democratic_margin_2016" ]] 
y = all_voters_d_with_demo["Democratic_margin_2018"] 
x = statsmodels.api.add_constant(x)

model = statsmodels.api.OLS(y, x)
results = model.fit()

results.summary()
Out[93]:
OLS Regression Results
Dep. Variable: Democratic_margin_2018 R-squared: 0.960
Model: OLS Adj. R-squared: 0.959
Method: Least Squares F-statistic: 832.1
Date: Tue, 11 Jun 2019 Prob (F-statistic): 5.01e-49
Time: 14:06:12 Log-Likelihood: 129.96
No. Observations: 72 AIC: -253.9
Df Residuals: 69 BIC: -247.1
Df Model: 2
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
const 0.0801 0.010 7.725 0.000 0.059 0.101
percent_poc -0.0020 0.001 -3.875 0.000 -0.003 -0.001
Democratic_margin_2016 1.1066 0.032 34.261 0.000 1.042 1.171
Omnibus: 5.372 Durbin-Watson: 1.758
Prob(Omnibus): 0.068 Jarque-Bera (JB): 4.776
Skew: -0.621 Prob(JB): 0.0918
Kurtosis: 3.217 Cond. No. 111.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [94]:
model = smf.ols(formula="Turnout_change_presidential ~ Democratic_margin_2016 + percent_poc", data=all_voters_d_with_demo)

results = model.fit()

results.summary()
Out[94]:
OLS Regression Results
Dep. Variable: Turnout_change_presidential R-squared: 0.314
Model: OLS Adj. R-squared: 0.294
Method: Least Squares F-statistic: 15.80
Date: Tue, 11 Jun 2019 Prob (F-statistic): 2.24e-06
Time: 14:06:12 Log-Likelihood: 156.37
No. Observations: 72 AIC: -306.7
Df Residuals: 69 BIC: -299.9
Df Model: 2
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -0.0232 0.007 -3.233 0.002 -0.038 -0.009
Democratic_margin_2016 -0.0806 0.022 -3.603 0.001 -0.125 -0.036
percent_poc -0.0004 0.000 -1.151 0.254 -0.001 0.000
Omnibus: 0.756 Durbin-Watson: 2.275
Prob(Omnibus): 0.685 Jarque-Bera (JB): 0.786
Skew: -0.233 Prob(JB): 0.675
Kurtosis: 2.786 Cond. No. 111.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [95]:
plot, summary = plot_scatter(all_voters_d_with_demo, 
                              'Democratic_margin_2016', "Democratic_margin_2018",
                             title="Democratic margin in 2018 vs. 2016"
                            )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 20
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 20.svg
  warn('Filename: {}'.format(filename))
R^2 0.9515; y=1.0290x + 0.0474
Out[95]:
<ggplot: (-9223372036536220328)>
In [96]:
plot, summary = plot_scatter(all_voters_d_with_demo, 
                              'Democratic_margin_2014', "Democratic_margin_2018",
                             title="Democratic margin in 2018 vs. 2014"
                            )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 21
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 21.svg
  warn('Filename: {}'.format(filename))
R^2 0.9327; y=1.0012x + 0.0200
Out[96]:
<ggplot: (-9223372036536305120)>
In [97]:
plot, summary = plot_scatter(all_voters_d_with_demo, 
                              'Turnout_2018', "Turnout_2014",
                             title="Turnout in 2018 vs. 2016"
                            )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 22
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 22.svg
  warn('Filename: {}'.format(filename))
R^2 0.4505; y=0.7934x + 0.0620
Out[97]:
<ggplot: (-9223372036540417455)>
In [98]:
plot, summary = plot_scatter(all_voters_d_with_demo, 
                              'Turnout_2012', "Turnout_2016",
                             title="Turnout in 2016 vs. 2012"
                            )

plot
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/numpy/core/fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
  return ptp(axis=axis, out=out, **kwargs)
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 23
  warn('Filename: {}'.format(filename))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:706: UserWarning: Saving 7.5 x 6 in image.
  from_inches(height, units), units))
/Users/alys/.virtualenvs/notebooks/lib/python3.7/site-packages/plotnine/ggplot.py:707: UserWarning: Filename: turnout 19_06_11_14_05 23.svg
  warn('Filename: {}'.format(filename))
R^2 0.2736; y=0.4539x + 0.4551
Out[98]:
<ggplot: (-9223372036535810125)>
In [99]:
model = smf.ols(formula="Turnout_change_presidential ~ Democratic_margin_2016 + median_age + percent_poc ", data=all_voters_d_with_demo)

results = model.fit()

results.summary()
Out[99]:
OLS Regression Results
Dep. Variable: Turnout_change_presidential R-squared: 0.357
Model: OLS Adj. R-squared: 0.329
Method: Least Squares F-statistic: 12.59
Date: Tue, 11 Jun 2019 Prob (F-statistic): 1.22e-06
Time: 14:06:17 Log-Likelihood: 158.70
No. Observations: 72 AIC: -309.4
Df Residuals: 68 BIC: -300.3
Df Model: 3
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -0.0886 0.031 -2.818 0.006 -0.151 -0.026
Democratic_margin_2016 -0.0594 0.024 -2.474 0.016 -0.107 -0.011
median_age 0.0016 0.001 2.133 0.037 0.000 0.003
percent_poc -0.0004 0.000 -1.008 0.317 -0.001 0.000
Omnibus: 1.158 Durbin-Watson: 2.187
Prob(Omnibus): 0.561 Jarque-Bera (JB): 1.044
Skew: -0.100 Prob(JB): 0.593
Kurtosis: 2.445 Cond. No. 451.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [100]:
all_voters_d_with_demo["proportion_poc"] = all_voters_d_with_demo["percent_poc"]/100

model = smf.ols(formula="Turnout_change_presidential ~ (Democratic_margin_2016) * median_age * proportion_poc ", data=all_voters_d_with_demo)

results = model.fit()

results.summary()
Out[100]:
OLS Regression Results
Dep. Variable: Turnout_change_presidential R-squared: 0.403
Model: OLS Adj. R-squared: 0.337
Method: Least Squares F-statistic: 6.167
Date: Tue, 11 Jun 2019 Prob (F-statistic): 1.57e-05
Time: 14:06:17 Log-Likelihood: 161.36
No. Observations: 72 AIC: -306.7
Df Residuals: 64 BIC: -288.5
Df Model: 7
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept 0.0221 0.073 0.303 0.763 -0.124 0.168
Democratic_margin_2016 -0.0025 0.186 -0.013 0.989 -0.374 0.369
median_age -0.0009 0.002 -0.533 0.596 -0.004 0.003
Democratic_margin_2016:median_age -0.0021 0.004 -0.475 0.636 -0.011 0.007
proportion_poc -1.0085 0.645 -1.564 0.123 -2.296 0.279
Democratic_margin_2016:proportion_poc 0.9971 0.940 1.061 0.293 -0.881 2.875
median_age:proportion_poc 0.0215 0.016 1.364 0.177 -0.010 0.053
Democratic_margin_2016:median_age:proportion_poc -0.0153 0.028 -0.540 0.591 -0.072 0.041
Omnibus: 0.429 Durbin-Watson: 2.260
Prob(Omnibus): 0.807 Jarque-Bera (JB): 0.416
Skew: -0.173 Prob(JB): 0.812
Kurtosis: 2.865 Cond. No. 1.31e+04


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.31e+04. This might indicate that there are
strong multicollinearity or other numerical problems.