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?
In [10]:
import pandas as pd
%matplotlib inline
import statsmodels.formula.api as smf
import matplotlib.pyplot as plt
In [13]:
df = pd.read_csv("Iran_data_3.csv")
In [14]:
df
Out[14]:
In [15]:
df.head()
Out[15]:
In [21]:
obama_admin_supporters_df = df[df['Sentiment'] == 'Approve']
In [22]:
obama_admin_supporters_df
Out[22]:
In [25]:
del obama_admin_supporters_df ['Sentiment']
In [26]:
iran_deal_supporters_df = df[df['Sentiment'] == 'Favor']
In [27]:
iran_deal_supporters_df
Out[27]:
In [28]:
del iran_deal_supporters_df['Sentiment']
In [29]:
iran_deal_supporters_df
Out[29]:
In [30]:
deal_and_obama_approval_df = obama_admin_supporters_df.append(iran_deal_supporters_df)
In [31]:
deal_and_obama_approval_df
Out[31]:
In [32]:
del deal_and_obama_approval_df['Subject']
In [33]:
del deal_and_obama_approval_df['Total']
In [34]:
deal_and_obama_approval_transpose_df = deal_and_obama_approval_df.transpose()
In [42]:
deal_and_obama_approval_transpose_df.columns = ["Obama Admin Approval", "Iran Deal Support"]
deal_and_obama_approval_transpose_df
Out[42]:
In [43]:
plt.style.use('fivethirtyeight')
In [44]:
deal_and_obama_approval_transpose_df.plot(kind='scatter', x= 'Obama Admin Approval', y='Iran Deal Support')
In [45]:
deal_and_obama_approval_transpose_df.corr()
Out[45]:
In [46]:
lm = smf.ols(formula='Favor_Deal~Approve_Obama',deal_and_obama_approval_transpose_df).fit()
lm.params
In [47]:
intercept, slope = lm.params
ax = deal_and_obama_approval_transpose_df.plot(kind='scatter', x= 'Obama Admin Approval', y='Iran Deal Support')
plt.plot(deal_and_obama_approval_transpose_df["Approve_Obama"],slope*deal_and_obama_approval_transpose_df["bama Admin Approval"]+intercept,"-",color="blue")
ax.set_title("Obama Admin Approval Prediction Towards Iran Deal Support")
ax.set_ylabel('Iran Deal Support')
ax.set_xlabel("Approve of Obama")
In [ ]:
In [ ]:
In [ ]: