{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**This notebook is an exercise in the [Time Series](https://www.kaggle.com/learn/time-series) course.  You can reference the tutorial at [this link](https://www.kaggle.com/ryanholbrook/linear-regression-with-time-series).**\n\n---\n","metadata":{}},{"cell_type":"markdown","source":"# Introduction #\n\nRun this cell to set everything up!","metadata":{}},{"cell_type":"code","source":"# Setup feedback system\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.time_series.ex1 import *\n\n# Setup notebook\nfrom pathlib import Path\nfrom learntools.time_series.style import *  # plot style settings\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom sklearn.linear_model import LinearRegression\n\n\ndata_dir = Path('../input/ts-course-data/')\ncomp_dir = Path('../input/store-sales-time-series-forecasting')\n\nbook_sales = pd.read_csv(\n    data_dir / 'book_sales.csv',\n    index_col='Date',\n    parse_dates=['Date'],\n).drop('Paperback', axis=1)\nbook_sales['Time'] = np.arange(len(book_sales.index))\nbook_sales['Lag_1'] = book_sales['Hardcover'].shift(1)\nbook_sales = book_sales.reindex(columns=['Hardcover', 'Time', 'Lag_1'])\n\nar = pd.read_csv(data_dir / 'ar.csv')\n\ndtype = {\n    'store_nbr': 'category',\n    'family': 'category',\n    'sales': 'float32',\n    'onpromotion': 'uint64',\n}\nstore_sales = pd.read_csv(\n    comp_dir / 'train.csv',\n    dtype=dtype,\n    parse_dates=['date'],\n    infer_datetime_format=True,\n)\nstore_sales = store_sales.set_index('date').to_period('D')\nstore_sales = store_sales.set_index(['store_nbr', 'family'], append=True)\naverage_sales = store_sales.groupby('date').mean()['sales']","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:35:43.552424Z","iopub.execute_input":"2022-07-28T01:35:43.553307Z","iopub.status.idle":"2022-07-28T01:35:53.029602Z","shell.execute_reply.started":"2022-07-28T01:35:43.553205Z","shell.execute_reply":"2022-07-28T01:35:53.028392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--------------------------------------------------------------------------------\n\nOne advantage linear regression has over more complicated algorithms is that the models it creates are *explainable* -- it's easy to interpret what contribution each feature makes to the predictions. In the model `target = weight * feature + bias`, the `weight` tells you by how much the `target` changes on average for each unit of change in the `feature`.\n\nRun the next cell to see a linear regression on *Hardcover Sales*.","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.plot('Time', 'Hardcover', data=book_sales, color='0.75')\nax = sns.regplot(x='Time', y='Hardcover', data=book_sales, ci=None, scatter_kws=dict(color='0.25'))\nax.set_title('Time Plot of Hardcover Sales');","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:35:53.032021Z","iopub.execute_input":"2022-07-28T01:35:53.032978Z","iopub.status.idle":"2022-07-28T01:35:53.352506Z","shell.execute_reply.started":"2022-07-28T01:35:53.032933Z","shell.execute_reply":"2022-07-28T01:35:53.351369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1) Interpret linear regression with the time dummy\n\nThe linear regression line has an equation of (approximately) `Hardcover = 3.33 * Time + 150.5`. Over 6 days how much on average would you expect hardcover sales to change? After you've thought about it, run the next cell.","metadata":{}},{"cell_type":"code","source":"# View the solution (Run this line to receive credit!)\nq_1.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:35:53.353975Z","iopub.execute_input":"2022-07-28T01:35:53.354388Z","iopub.status.idle":"2022-07-28T01:35:53.364238Z","shell.execute_reply.started":"2022-07-28T01:35:53.354347Z","shell.execute_reply":"2022-07-28T01:35:53.363458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uncomment the next line for a hint\nq_1.hint()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:37:17.003036Z","iopub.execute_input":"2022-07-28T01:37:17.003971Z","iopub.status.idle":"2022-07-28T01:37:17.012411Z","shell.execute_reply.started":"2022-07-28T01:37:17.003933Z","shell.execute_reply":"2022-07-28T01:37:17.011418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\nInterpreting the regression coefficients can help us recognize serial dependence in a time plot. Consider the model `target = weight * lag_1 + error`, where `error` is random noise and `weight` is a number between -1 and 1. The `weight` in this case tells you how likely the next time step will have the same sign as the previous time step: a `weight` close to 1 means `target` will likely have the same sign as the previous step, while a `weight` close to -1 means `target` will likely have the opposite sign.\n\n# 2) Interpret linear regression with a lag feature\n\nRun the following cell to see two series generated according to the model just described.","metadata":{}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(11, 5.5), sharex=True)\nax1.plot(ar['ar1'])\nax1.set_title('Series 1')\nax2.plot(ar['ar2'])\nax2.set_title('Series 2');","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:35:53.374166Z","iopub.execute_input":"2022-07-28T01:35:53.374579Z","iopub.status.idle":"2022-07-28T01:35:53.704019Z","shell.execute_reply.started":"2022-07-28T01:35:53.374446Z","shell.execute_reply":"2022-07-28T01:35:53.702884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One of these series has the equation `target = 0.95 * lag_1 + error` and the other has the equation `target = -0.95 * lag_1 + error`, differing only by the sign on the lag feature. Can you tell which equation goes with each series?","metadata":{}},{"cell_type":"code","source":"# View the solution (Run this cell to receive credit!)\nq_2.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:37:03.891234Z","iopub.execute_input":"2022-07-28T01:37:03.891637Z","iopub.status.idle":"2022-07-28T01:37:03.899761Z","shell.execute_reply.started":"2022-07-28T01:37:03.891603Z","shell.execute_reply":"2022-07-28T01:37:03.898931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uncomment the next line for a hint\nq_2.hint()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:37:06.320022Z","iopub.execute_input":"2022-07-28T01:37:06.321039Z","iopub.status.idle":"2022-07-28T01:37:06.329766Z","shell.execute_reply.started":"2022-07-28T01:37:06.320999Z","shell.execute_reply":"2022-07-28T01:37:06.328528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\nNow we'll get started with the *Store Sales - Time Series Forecasting* competition data. The entire dataset comprises almost 1800 series recording store sales across a variety of product families from 2013 into 2017. For this lesson, we'll just work with a single series (`average_sales`) of the average sales each day.\n\n# 3) Fit a time-step feature\n\nComplete the code below to create a linear regression model with a time-step feature on the series of average product sales. The target is in a column called `'sales'`.","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\ndf = average_sales.to_frame()\n\ntime = np.arange(len(df.index))  # time dummy\n\ndf['time'] = time\n\nX = df.loc[:, ['time']]  # features\ny = df.loc[:, 'sales']  # target\n\nmodel = LinearRegression()\nmodel.fit(X, y)\n\ny_pred = pd.Series(model.predict(X), index=X.index)\n# Check your answer\nq_3.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:39:33.811001Z","iopub.execute_input":"2022-07-28T01:39:33.811533Z","iopub.status.idle":"2022-07-28T01:39:33.831086Z","shell.execute_reply.started":"2022-07-28T01:39:33.811487Z","shell.execute_reply":"2022-07-28T01:39:33.830020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lines below will give you a hint or solution code\n# q_3.hint()\n# q_3.solution()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:38:07.782569Z","iopub.execute_input":"2022-07-28T01:38:07.783049Z","iopub.status.idle":"2022-07-28T01:38:07.787921Z","shell.execute_reply.started":"2022-07-28T01:38:07.783007Z","shell.execute_reply":"2022-07-28T01:38:07.786643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Run this cell if you'd like to see a plot of the result.","metadata":{}},{"cell_type":"code","source":"ax = y.plot(**plot_params, alpha=0.5)\nax = y_pred.plot(ax=ax, linewidth=3)\nax.set_title('Time Plot of Total Store Sales');","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:38:09.924109Z","iopub.execute_input":"2022-07-28T01:38:09.924461Z","iopub.status.idle":"2022-07-28T01:38:10.230817Z","shell.execute_reply.started":"2022-07-28T01:38:09.924432Z","shell.execute_reply":"2022-07-28T01:38:10.229529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-------------------------------------------------------------------------------\n\n# 4) Fit a lag feature to Store Sales\n\nComplete the code below to create a linear regression model with a lag feature on the series of average product sales. The target is in a column of `df` called `'sales'`.","metadata":{}},{"cell_type":"code","source":"df = average_sales.to_frame()\n\nlag_1 = df['sales'].shift(1)\n\ndf['lag_1'] = lag_1\n\nX = df.loc[:, ['lag_1']]\nX.dropna(inplace=True)  # drop missing values in the feature set\ny = df.loc[:, 'sales']  # create the target\ny, X = y.align(X, join='inner')  # drop corresponding values in target\n\n# YOUR CODE HERE: Create a LinearRegression instance and fit it to X and y.\n\nmodel = LinearRegression()\nmodel.fit(X, y)\n\n\n# YOUR CODE HERE: Create Store the fitted values as a time series with\n# the same time index as the training data\ny_pred = pd.Series(model.predict(X), index=X.index)\n\n# Check your answer\nq_4.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:39:43.647025Z","iopub.execute_input":"2022-07-28T01:39:43.647566Z","iopub.status.idle":"2022-07-28T01:39:43.672921Z","shell.execute_reply.started":"2022-07-28T01:39:43.647517Z","shell.execute_reply":"2022-07-28T01:39:43.671768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lines below will give you a hint or solution code\n# q_4.hint()\n# q_4.solution()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:39:24.126251Z","iopub.execute_input":"2022-07-28T01:39:24.126671Z","iopub.status.idle":"2022-07-28T01:39:24.131267Z","shell.execute_reply.started":"2022-07-28T01:39:24.126637Z","shell.execute_reply":"2022-07-28T01:39:24.130297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Run the next cell if you'd like to see the result.","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.plot(X['lag_1'], y, '.', color='0.25')\nax.plot(X['lag_1'], y_pred)\nax.set(aspect='equal', ylabel='sales', xlabel='lag_1', title='Lag Plot of Average Sales');","metadata":{"execution":{"iopub.status.busy":"2022-07-28T01:39:45.367734Z","iopub.execute_input":"2022-07-28T01:39:45.368415Z","iopub.status.idle":"2022-07-28T01:39:45.584346Z","shell.execute_reply.started":"2022-07-28T01:39:45.368380Z","shell.execute_reply":"2022-07-28T01:39:45.583123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Keep Going #\n\n[**Model trend**](https://www.kaggle.com/ryanholbrook/trend) in time series with moving average plots and the time dummy.","metadata":{}}]}