{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, you will learn how to use Python to perform Multiple Linear Regression. Run each cell in this notebook in order.  \n",
    "\n",
    "When you are asked to insert your own code, write your code in the cell provided and run the cell.  Correct any errors and rerun. When you have the result you want, move on and run the next cell.  Keep running each cell until you are again asked to add code yourself.\n",
    "\n",
    "To get started, run the cell below to load the code and the table you will need to use in the rest of the Notebook.\n",
    "\n",
    "Ignore the \"read_table is deprecateed\" warning message."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_data = 'http://www.millerjw.com/dom/mgmt462/pfiles/'\n",
    "from datascience import *\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plots\n",
    "plots.style.use('fivethirtyeight')\n",
    "import statsmodels.formula.api as smf\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import train_test_split\n",
    "# Read in the Elantra data into a datascience table for use in this assignment\n",
    "elantra = Table.read_table(path_data + 'elantra.csv')\n",
    "# Copy the copy of the data in pandas dataframe format \n",
    "elantrap = elantra.to_df()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Display the elantra table by running the cell below. Seeing the table data with column headings will help you in the rest of this exercise. Remember that you can display a table by using print(table) or by coding a single line with the table name at the end of any code cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write code below to divide table elantra into the training set (elantra_train) and the test set (elantra_test).\n",
    "\n",
    "You have functions are.above and are.below available to you.  Your training set should select all rows with years leas than 2013.  This code will do the job (you can copy it)\n",
    "elantra_train = elantra.where('Year', are.below(2013))\n",
    "\n",
    "Your test set, elantra_test should include all years greater than 2012. Use the are.above function to code the elantra_test = statement.\n",
    "\n",
    "As the last statements in the coding cell below, paste these print statements\n",
    "\n",
    "print('elantra_train is ')\n",
    "\n",
    "print(elantra_train)\n",
    "\n",
    "print('elantra_test is ')\n",
    "\n",
    "print(elantra_test)\n",
    "\n",
    "\n",
    "When you are done your code should look like\n",
    "\n",
    "elantra_train = you code or paste in this statement\n",
    "\n",
    "elantra_test =  you code or paste in this statement\n",
    "\n",
    "print statements\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the last assignment, you got the number of rows in a table by coding \n",
    "\n",
    "tablename.num_rows\n",
    "\n",
    "Code the statement to get the number of rows in the training set.  You need this number for your homework assignment. If necessary, refer to the prior coding cell to get the name of the table containing the training set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now is a good time to save and checkpoint your work.  Go to the homework and answer the question about number of rows in the training set.  Then, continue with this notebook\n",
    "\n",
    "There are several packages that can be included in our Python Program to perform linear regressions.  We will include..    Run the cell below to load the package so we can use it in the rest of this Notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will perform the regression on the training set so convert it to a Python dataframe by pasting this line of code into the code cell below.\n",
    "\n",
    "elantra_trainp = elantra_train.to_df()\n",
    " \n",
    "Then run the code "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "elantra_trainp = elantra_train.to_df()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now have the elantra training set in a Pandas dataframe.  This is very similar to the datascience tables we have been using.  The reason we use both is that they have different capabilities (methods).  For example, datascience tables have very simple plot methods (tablename.plot(), tablename.scatter()).  Pandas dataframes have easy-to-use methods to quickly produce descriptive statistics.  Run the describe method on the elantra training dataframe (elantra_trainp)\n",
    "\n",
    "The general format for the describe method is dataframname.describe().\n",
    "\n",
    "Write the one statement to run the describe method on the elantra training table.\n",
    "\n",
    "Remember that the name of the Elantra Training dataframe is elantra_trainp."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Go to your homework and answer the questions that ask you for statistics from the above table.  Then, continue with this notebook.\n",
    "\n",
    "We would like to use Linear Regression to predict sales of Hyundai Elantra cars.  First lets do some scatter plots using the elantra training table in datascience format (elantra_train). The scatter plot for Queries and sales is coded below. Run the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "elantra_train.scatter('Queries','ElantraSales')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, you write the code to draw the scatter plots for Unemployment,CPI_energy,CPI_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "Save and Checkpoint your work and then go to your homework and answer the homework question that asks you to analyze the scatter plots. \n",
    "\n",
    "Then, continue with this notebook\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "FIRST REGRESSION: The code in the cell below will run a regression using dataframe elantrap_train.  It uses one features (x variable)   Run the code below to verify that it works. Then, go to your homework and answer the question about the First Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### STATSMODELS ###\n",
    "print(elantra.labels)\n",
    "# create a fitted model\n",
    "lm1 = smf.ols(formula='ElantraSales ~ Unemployment', data=elantra_trainp).fit()\n",
    "\n",
    "# print the coefficients\n",
    "lm1.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SECOND REGRESSION: Build a linear regression model to predict monthly Elantra sales using Unemployment, CPI_all, CPI_energy and Queries as the independent variables. Use the training set data to do this.  Change the equation below to use the 4 independent variables specified above.  Keep ElantraSales as the dependent variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(elantra.labels)\n",
    "# create a fitted model\n",
    "lm1 = smf.ols(formula='ElantraSales ~ Queries + Unemployment', data=elantra_trainp).fit()\n",
    "\n",
    "# print the coefficients\n",
    "lm1.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
