text_plain_output_1.png
from sklearn.linear_model import ElasticNetCV

from statsmodels.graphics.gofplots import ProbPlot

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

import statsmodels.api as sm

df = pd.read_csv('../input/2016-FCC-New-Coders-Survey-Data.csv', usecols=['Gender', 'Age', 'CommuteTime', 'HasChildren', 'AttendedBootcamp', 'HasDebt', 'HoursLearning', 'MonthsProgramming', 'Income'])

df.rename(columns={'Gender': 'IsWoman'}, inplace=True)
df['IsWoman'] = df['IsWoman'] == 'female'
df.dropna(inplace=True)

x = df[['Age', 'CommuteTime', 'HasChildren', 'AttendedBootcamp', 'IsWoman', 'HasDebt', 'HoursLearning', 'MonthsProgramming']]
y = df['Income']

elnet = ElasticNetCV(cv=10)
elnet.fit(x, y)

coefs = list(zip(x.columns, elnet.coef_))
coefs

nonzero_coefs = [i[0] for i in coefs if i[1] != 0]
nonzero_coefs

x = df[nonzero_coefs]
x = sm.add_constant(x)
model = sm.OLS(y, x).fit()

# statsmodels Q-Q plot on model residuals
QQ = ProbPlot(model.resid)
fig = QQ.qqplot(alpha=0.5, markersize=5, line='s')
plt.title('QQ plot');

model_norm_resid = model.get_influence().resid_studentized_internal
model_norm_resid_abs_sqrt = np.sqrt(np.abs(model_norm_resid))

model_leverage = model.get_influence().hat_matrix_diag
sns.regplot(model_leverage, model.resid_pearson, fit_reg=False)
plt.xlim(xmin=0, xmax=0.037)
plt.xlabel('Leverage')
plt.ylabel('Pearson residuals')
plt.title('Residuals vs leverage')