In [41]:
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
plt.style.use('fivethirtyeight') # obvs I'm using matplotlib's fivethirtyeight preset style
In [21]:
df=pd.read_csv("Obama_approval.csv")
In [35]:
#df.index=df['Sub_group']
lm = smf.ols(formula="Favor_Iran_deal~Obama_Approval",data=df).fit() #notice the formula regresses Y on X (Y~X)
intercept,slope=lm.params
lm.params
Out[35]:
In [44]:
df.plot(kind='scatter', x='Obama_Approval', y ='Favor_Iran_deal')
plt.plot(df["Obama_Approval"],slope*df["Obama_Approval"]+intercept,"-",color="red")
Out[44]:
In [ ]:
In [ ]: