Journalistically, it is pretty irresponsible to plot these data in the way 538 did. For the sake of this exercise pretending to be unaware of it


In [3]:
import pandas as pd
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
%matplotlib inline

In [41]:
df = pd.read_csv('fox_news_data.csv')
df


Out[41]:
Unnamed: 0 approve_obama favor_deal confident_in_admin_somewhat confident_in_admin_very confident_admin_total
0 Democrats 78 60 49 29 78
1 Republicans 10 34 13 4 17
2 Independent 37 44 29 15 44
3 Men 41 46 28 17 45
4 Women 47 47 35 17 52
5 White 35 45 29 11 40
6 Black 85 54 46 37 83
7 college_degree 47 50 35 20 55
8 no_degree 43 45 29 14 43
9 u_35 56 51 39 18 57
10 34_54 35 46 29 13 42
11 a55 55 45 29 19 48
12 a65 43 41 26 18 44
13 u55k_income 48 47 34 18 52
14 a55k_income 42 48 31 16 47
15 Lib 69 61 48 24 72
16 Con 24 35 19 10 29
17 Tea 15 35 11 6 17

In [42]:
lm = smf.ols(formula="favor_deal~approve_obama",data=df).fit()
lm.params


Out[42]:
Intercept        30.690072
approve_obama     0.347628
dtype: float64

In [43]:
intercept, slope = lm.params

In [44]:
fig, ax = plt.subplots(figsize=(7,7))
plt.style.use('fivethirtyeight')

ax = df.plot(ax = ax, kind='scatter', x="approve_obama", y="favor_deal")
plt.plot(df['approve_obama'],slope*df['approve_obama']+intercept, color="red", linewidth=2)

ax.set_xlim(0,85)
ax.set_ylim(0,85)

ax.set_ylabel("Favor Iran deal")
ax.set_xlabel("Approve of Obama")


Out[44]:
<matplotlib.text.Text at 0x110d2eeb8>

In [46]:
lm2 = smf.ols(formula="confident_admin_total~approve_obama",data=df).fit()
lm2.params


Out[46]:
Intercept        7.636140
approve_obama    0.898209
dtype: float64

In [47]:
intercept2, slope2 = lm2.params

In [48]:
fig, ax = plt.subplots(figsize=(7,7))
plt.style.use('fivethirtyeight')

ax = df.plot(ax = ax, kind='scatter', x="approve_obama", y="confident_admin_total")
plt.plot(df['approve_obama'],slope2*df['approve_obama']+intercept2, color="red", linewidth=2)

ax.set_xlim(0,85)
ax.set_ylim(0,85)

ax.set_ylabel("Confident administration will do a good job in negotiations")
ax.set_xlabel("Approve of Obama")


Out[48]:
<matplotlib.text.Text at 0x110f07860>

In [ ]: