text_plain_output_1.png
import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import statsmodels.formula.api as smf

import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0)

data.shape

#Check assumption 2
#Testing the assumptions of a Linear Relationship between the independent and dependent varaible (s)
    #If the relationship between the Independent Variable (IV) and Dependent Varaible (DV) is not linear, the results of regression will under-estimate the true relationship
    #This under-estimation can present 2 major problems
        #1.) an increased chance of a Type II error for that IV
        #2.) and with multiple regression an increases risk of Type I errors (over-estimation) for other IVs that share variance with that IV
    #How to test for linearity:
        #1.)Scatterplots
fig, axs=plt.subplots(1,3,sharey=True)
data.plot(kind='scatter', x='TV', y='Sales', ax=axs[0], figsize=(12,4))
data.plot(kind='scatter', x='Radio', y='Sales', ax=axs[1])
data.plot(kind='scatter', x='Newspaper', y='Sales', ax=axs[2])

import statsmodels.formula.api as smf
lm = smf.ols(formula='Sales~TV', data=data).fit()
lm.params

frame = pd.DataFrame({'TV': [50]})
lm.predict(frame)

frame = pd.DataFrame(data.TV)
preds = lm.predict(frame)

lm.conf_int()

lm.pvalues

lm.rsquared