In [20]:
%matplotlib inline

In [21]:
import statsmodels.api as sm
import statsmodels.formula.api as smf
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt

In [22]:
bodyfat = pd.read_fwf('bodyfat.txt', names=['dietfat', 'bodyfat'])

fatmodel = smf.ols('bodyfat ~ dietfat', bodyfat)

fatresults = fatmodel.fit()

In [34]:
fatresults.summary()


Out[34]:
OLS Regression Results
Dep. Variable: bodyfat R-squared: 0.376
Model: OLS Adj. R-squared: 0.337
Method: Least Squares F-statistic: 9.659
Date: Tue, 06 Jan 2015 Prob (F-statistic): 0.00677
Time: 18:01:28 Log-Likelihood: -27.595
No. Observations: 18 AIC: 59.19
Df Residuals: 16 BIC: 60.97
Df Model: 1
coef std err t P>|t| [95.0% Conf. Int.]
Intercept 7.3141 1.006 7.271 0.000 5.182 9.447
dietfat 0.1162 0.037 3.108 0.007 0.037 0.196
Omnibus: 0.259 Durbin-Watson: 2.156
Prob(Omnibus): 0.879 Jarque-Bera (JB): 0.008
Skew: -0.002 Prob(JB): 0.996
Kurtosis: 2.894 Cond. No. 96.7

In [23]:
sm.qqplot(fatresults.resid, line='s')


Out[23]:

In [42]:
plt.plot(fatresults.resid, 'o')


Out[42]:
[<matplotlib.lines.Line2D at 0x10e44b198>]

In [25]:
sns.lmplot('dietfat', 'bodyfat', bodyfat)


Out[25]:
<seaborn.axisgrid.FacetGrid at 0x10d6c7898>

In [29]:
# 2nd order polynomial
sns.lmplot('dietfat', 'bodyfat', bodyfat, order=2)


Out[29]:
<seaborn.axisgrid.FacetGrid at 0x10da82d68>

In [31]:
# Residual plot
sns.residplot('dietfat', 'bodyfat', bodyfat)


Out[31]:
<matplotlib.axes._subplots.AxesSubplot at 0x10dbab208>

In [37]:
electric = pd.read_fwf('electric.txt', names=['bill', 'income', 'people', 'sqft'])

emodel = smf.ols('bill ~ income + people + sqft', electric)

eresults = emodel.fit()

In [38]:
eresults.summary()


Out[38]:
OLS Regression Results
Dep. Variable: bill R-squared: 0.840
Model: OLS Adj. R-squared: 0.824
Method: Least Squares F-statistic: 52.36
Date: Tue, 06 Jan 2015 Prob (F-statistic): 4.92e-12
Time: 18:21:59 Log-Likelihood: -214.29
No. Observations: 34 AIC: 436.6
Df Residuals: 30 BIC: 442.7
Df Model: 3
coef std err t P>|t| [95.0% Conf. Int.]
Intercept -554.9445 98.575 -5.630 0.000 -756.261 -353.628
income 0.2476 0.024 10.531 0.000 0.200 0.296
people 82.1051 16.283 5.042 0.000 48.851 115.359
sqft -0.0144 0.014 -1.027 0.313 -0.043 0.014
Omnibus: 1.293 Durbin-Watson: 2.134
Prob(Omnibus): 0.524 Jarque-Bera (JB): 1.173
Skew: 0.423 Prob(JB): 0.556
Kurtosis: 2.665 Cond. No. 1.97e+04

In [39]:
sns.corrplot(electric)


Out[39]:
<matplotlib.axes._subplots.AxesSubplot at 0x10d877da0>

In [ ]: