Using data from this FiveThirtyEight post, write code to calculate the correlation of the responses from the poll. Respond to the story in your PR. Is this a good example of data journalism? Why or why not?
http://fivethirtyeight.com/datalab/opinions-about-the-iran-deal-are-more-about-obama-than-iran/
In [1]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt # package for doing plotting (necessary for adding the line)
import statsmodels.formula.api as smf # package we'll be using for linear regression
import matplotlib
import pandas as pd
import numpy as np
plt.style.use('ggplot')
import dateutil.parser
import math
import random
import matplotlib.ticker as plticker
matplotlib.rcParams['ps.fonttype'] = 42
In [2]:
df = pd.read_excel("iran_obama.xlsx")
df.head()
Out[2]:
In [10]:
lm = smf.ols(formula="Iran_Deal~Approval_for_Obama",data=df).fit() #notice the formula regresses Y on X (Y~X)
lm.params
Out[10]:
In [11]:
intercept, slope = lm.params
In [12]:
fig, ax = plt.subplots(figsize=(10,5))
df.plot(kind="scatter",x="Approval_for_Obama",y="Iran_Deal", ax=ax)
plt.plot(df["Approval_for_Obama"],slope*df["Approval_for_Obama"]+intercept,"-",color="red")
ax.set_ylim([0, 100])
ax.set_title("Predict Feelings on Iran Deal", color='brown')
ax.set_xlabel('Approval for Obama', color='brown')
ax.set_ylabel('Favour Iran Deal', color='brown')
Out[12]:
In [6]:
lm.summary()
Out[6]:
In [7]:
neg_df = pd.read_excel("negiran_obama.xlsx")
neg_df.head()
Out[7]:
In [13]:
neg_lm = smf.ols(formula="Iran_neg~Approval_for_Obama",data=neg_df).fit() #notice the formula regresses Y on X (Y~X)
neg_lm.params
Out[13]:
In [14]:
intercept, slope = neg_lm.params
In [15]:
fig, ax = plt.subplots(figsize=(10,5))
neg_df.plot(kind="scatter",x="Approval_for_Obama",y="Iran_neg", ax=ax)
plt.plot(df["Approval_for_Obama"],slope*df["Approval_for_Obama"]+intercept,"-",color="red")
ax.set_ylim([0, 100])
ax.set_title("Opinion on Iran Deal", color='brown')
ax.set_xlabel('Approval for Obama', color='brown')
ax.set_ylabel('Confident in Negotiations with Iran', color='brown')
Out[15]:
In [16]:
neg_lm.summary()
Out[16]:
In [ ]: