{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Linear Regression - Analysis\n",
"============\n",
"***\n",
"\n",
"We're going to pick up where we left off at the end of the exploration and define a linear model with two independent variables determining the dependent variable, Interest Rate.\n",
"\n",
"Our investigation is now defined as:\n",
"\n",
"_Investigate FICO Score and Loan Amount as predictors of Interest Rate for the Lending Club sample of 2,500 loans._\n",
"\n",
"We use Multivariate Linear Regression to model Interest Rate variance with FICO Score and Loan Amount using:\n",
"\n",
"$$InterestRate = a_0 + a_1 * FICOScore + a_2 * LoanAmount$$\n",
"\n",
"We're going to use modeling software to generate the model coefficients $a_0$, $a_1$ and $a_2$ and then some error estimates that we'll only touch upon lightly at this point. \n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n",
"Coefficients: [ 72.88279832 -0.08844242]\n",
"Intercept: 0.000210747768548\n",
"P-Values: [ 0.00000000e+000 0.00000000e+000 5.96972978e-203]\n",
"R-Squared: 0.656632624649\n"
]
}
],
"source": [
"%pylab inline\n",
"import pylab as pl\n",
"import numpy as np\n",
"#from sklearn import datasets, linear_model\n",
"import pandas as pd\n",
"import statsmodels.api as sm\n",
"\n",
"# import the cleaned up dataset\n",
"df = pd.read_csv('../datasets/loanf.csv')\n",
"\n",
"intrate = df['Interest.Rate']\n",
"loanamt = df['Loan.Amount']\n",
"fico = df['FICO.Score']\n",
"\n",
"# reshape the data from a pandas Series to columns \n",
"# the dependent variable\n",
"y = np.matrix(intrate).transpose()\n",
"# the independent variables shaped as columns\n",
"x1 = np.matrix(fico).transpose()\n",
"x2 = np.matrix(loanamt).transpose()\n",
"\n",
"# put the two columns together to create an input matrix \n",
"# if we had n independent variables we would have n columns here\n",
"x = np.column_stack([x1,x2])\n",
"\n",
"# create a linear model and fit it to the data\n",
"X = sm.add_constant(x)\n",
"model = sm.OLS(y,X)\n",
"f = model.fit()\n",
"\n",
"print 'Coefficients: ', f.params[0:2]\n",
"print 'Intercept: ', f.params[2]\n",
"print 'P-Values: ', f.pvalues\n",
"print 'R-Squared: ', f.rsquared\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we have a lot of numbers here and we're going to understand some of them.\n",
"\n",
"Coefficients: contains $a_1$ and $a_2$ respectively. \n",
"Intercept: is the $a_0$.\n",
"\n",
"How good are these numbers, how reliable? We need to have some idea. After all we are estimating. We're going to learn a very simple pragmatic way to use a couple of these. \n",
"\n",
"Let's look at the second two numbers. \n",
"We are going to talk loosely here so as to give some flavor of why these are important.\n",
"But this is by no means a formal explanation.\n",
"\n",
"P-Values are probabilities. Informally, each number represents a probability that the respective coefficient we have is a really bad one. To be fairly confident we want this probability to be close to zero. The convention is it needs to be 0.05 or less.\n",
"For now suffice it to say that if we have this true for each of our coefficients then we have good confidence in the model. If one or other of the coefficients is equal to or greater than 0.05 then we have less confidence in that particular dimension being useful in modeling and predicting.\n",
"\n",
"$R$-$squared$ or $R^2$ is a measure of how much of the variance in the data is captured by the model. What does this mean? For now let's understand this as a measure of how well the model captures the **spread** of the observed values not just the average trend. \n",
"\n",
"R is a coefficient of correlation between the independent variables and the dependent variable - i.e. how much the Y depends on the separate X's. R lies between -1 and 1, so $R^2$ lies between 0 and 1. \n",
"\n",
"A high $R^2$ would be close to 1.0 a low one close to 0. The value we have, 0.65, is a reasonably good one. It suggests an R with absolute value in the neighborhood of 0.8.\n",
"The details of these error estimates deserve a separate discussion which we defer until another time.\n",
"\n",
"In summary we have a linear multivariate regression model for Interest Rate based on FICO score and Loan Amount which is well described by the parameters above.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.display import HTML\n",
"def css_styling():\n",
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
" return HTML(styles)\n",
"css_styling()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}