In [1]:
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', 20)
pd.set_option('display.max_rows', 25)

mpl.rc('figure', figsize=(10, 8))

In [2]:
data = pd.read_csv('./data/Advertising.csv', index_col=[0])
data.head()


Out[2]:
      TV  Radio  Newspaper  Sales
1  230.1   37.8       69.2   22.1
2   44.5   39.3       45.1   10.4
3   17.2   45.9       69.3    9.3
4  151.5   41.3       58.5   18.5
5  180.8   10.8       58.4   12.9

[5 rows x 4 columns]

In [3]:
fig, axes=plt.subplots(figsize=(15, 5), nrows=1, ncols=3)
axes[0].scatter(data['TV'], data['Sales'], c='r')
axes[0].set_title("TV")

axes[1].scatter(data['Radio'], data['Sales'], c='r')
axes[1].set_title("Radio")

axes[2].scatter(data['Newspaper'], data['Sales'], c='r')
axes[2].set_title("Newspaper")
fig.tight_layout()



In [4]:
import statsmodels.formula.api as sm

Simple linear regression

$ Y \approx \beta_0 + \beta_1X $

$ sales \approx \beta_0 + \beta_1 \times TV $


In [5]:
tv_model = sm.ols(formula='Sales ~ TV', data=data)
tv_fitted = tv_model.fit()

radio_model = sm.ols(formula='Sales ~ Radio', data=data)
radio_fitted = radio_model.fit()

np_model = sm.ols(formula='Sales ~ Newspaper', data=data)
np_fitted = np_model.fit()

In [6]:
fig, axes=plt.subplots(figsize=(15, 5), nrows=1, ncols=3)
axes[0].plot(data['TV'], data['Sales'], 'ro')
axes[0].plot(data['TV'], tv_fitted.fittedvalues, 'b')
axes[0].set_title('TV')

axes[1].plot(data['Radio'], data['Sales'], 'ro')
axes[1].plot(data['Radio'], radio_fitted.fittedvalues, 'b')
axes[1].set_title('Radio')

axes[2].plot(data['Newspaper'], data['Sales'], 'ro')
axes[2].plot(data['Newspaper'], np_fitted.fittedvalues, 'b')
axes[2].set_title('Newspaper')

fig.tight_layout()



In [7]:
tv_fitted.summary()


Out[7]:
OLS Regression Results
Dep. Variable: Sales R-squared: 0.612
Model: OLS Adj. R-squared: 0.610
Method: Least Squares F-statistic: 312.1
Date: Sat, 15 Feb 2014 Prob (F-statistic): 1.47e-42
Time: 12:23:10 Log-Likelihood: -519.05
No. Observations: 200 AIC: 1042.
Df Residuals: 198 BIC: 1049.
Df Model: 1
coef std err t P>|t| [95.0% Conf. Int.]
Intercept 7.0326 0.458 15.360 0.000 6.130 7.935
TV 0.0475 0.003 17.668 0.000 0.042 0.053
Omnibus: 0.531 Durbin-Watson: 1.935
Prob(Omnibus): 0.767 Jarque-Bera (JB): 0.669
Skew: -0.089 Prob(JB): 0.716
Kurtosis: 2.779 Cond. No. 338.

In [8]:
radio_fitted.summary()


Out[8]:
OLS Regression Results
Dep. Variable: Sales R-squared: 0.332
Model: OLS Adj. R-squared: 0.329
Method: Least Squares F-statistic: 98.42
Date: Sat, 15 Feb 2014 Prob (F-statistic): 4.35e-19
Time: 12:23:10 Log-Likelihood: -573.34
No. Observations: 200 AIC: 1151.
Df Residuals: 198 BIC: 1157.
Df Model: 1
coef std err t P>|t| [95.0% Conf. Int.]
Intercept 9.3116 0.563 16.542 0.000 8.202 10.422
Radio 0.2025 0.020 9.921 0.000 0.162 0.243
Omnibus: 19.358 Durbin-Watson: 1.946
Prob(Omnibus): 0.000 Jarque-Bera (JB): 21.910
Skew: -0.764 Prob(JB): 1.75e-05
Kurtosis: 3.544 Cond. No. 51.4

In [9]:
np_fitted.summary()


Out[9]:
OLS Regression Results
Dep. Variable: Sales R-squared: 0.052
Model: OLS Adj. R-squared: 0.047
Method: Least Squares F-statistic: 10.89
Date: Sat, 15 Feb 2014 Prob (F-statistic): 0.00115
Time: 12:23:10 Log-Likelihood: -608.34
No. Observations: 200 AIC: 1221.
Df Residuals: 198 BIC: 1227.
Df Model: 1
coef std err t P>|t| [95.0% Conf. Int.]
Intercept 12.3514 0.621 19.876 0.000 11.126 13.577
Newspaper 0.0547 0.017 3.300 0.001 0.022 0.087
Omnibus: 6.231 Durbin-Watson: 1.983
Prob(Omnibus): 0.044 Jarque-Bera (JB): 5.483
Skew: 0.330 Prob(JB): 0.0645
Kurtosis: 2.527 Cond. No. 64.7

In [10]:
all_model = sm.ols(formula='Sales ~ TV + Radio + Newspaper', data=data)
all_fitted = all_model.fit()
all_fitted.summary()


Out[10]:
OLS Regression Results
Dep. Variable: Sales R-squared: 0.897
Model: OLS Adj. R-squared: 0.896
Method: Least Squares F-statistic: 570.3
Date: Sat, 15 Feb 2014 Prob (F-statistic): 1.58e-96
Time: 12:23:13 Log-Likelihood: -386.18
No. Observations: 200 AIC: 780.4
Df Residuals: 196 BIC: 793.6
Df Model: 3
coef std err t P>|t| [95.0% Conf. Int.]
Intercept 2.9389 0.312 9.422 0.000 2.324 3.554
TV 0.0458 0.001 32.809 0.000 0.043 0.049
Radio 0.1885 0.009 21.893 0.000 0.172 0.206
Newspaper -0.0010 0.006 -0.177 0.860 -0.013 0.011
Omnibus: 60.414 Durbin-Watson: 2.084
Prob(Omnibus): 0.000 Jarque-Bera (JB): 151.241
Skew: -1.327 Prob(JB): 1.44e-33
Kurtosis: 6.332 Cond. No. 454.

In [11]:
data.corr()


Out[11]:
                 TV     Radio  Newspaper     Sales
TV         1.000000  0.054809   0.056648  0.782224
Radio      0.054809  1.000000   0.354104  0.576223
Newspaper  0.056648  0.354104   1.000000  0.228299
Sales      0.782224  0.576223   0.228299  1.000000

[4 rows x 4 columns]