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]:
In [42]:
lm = smf.ols(formula="favor_deal~approve_obama",data=df).fit()
lm.params
Out[42]:
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]:
In [46]:
lm2 = smf.ols(formula="confident_admin_total~approve_obama",data=df).fit()
lm2.params
Out[46]:
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]:
In [ ]: