{"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-12T20:11:27.910547Z","iopub.execute_input":"2022-07-12T20:11:27.911513Z","iopub.status.idle":"2022-07-12T20:11:36.184355Z","shell.execute_reply.started":"2022-07-12T20:11:27.911305Z","shell.execute_reply":"2022-07-12T20:11:36.183204Z"},"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-12T20:12:14.539476Z","iopub.execute_input":"2022-07-12T20:12:14.539872Z","iopub.status.idle":"2022-07-12T20:12:14.853880Z","shell.execute_reply.started":"2022-07-12T20:12:14.539843Z","shell.execute_reply":"2022-07-12T20:12:14.852144Z"},"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":"print(3.33*6)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:13:29.131034Z","iopub.execute_input":"2022-07-12T20:13:29.131430Z","iopub.status.idle":"2022-07-12T20:13:29.136970Z","shell.execute_reply.started":"2022-07-12T20:13:29.131400Z","shell.execute_reply":"2022-07-12T20:13:29.136107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"book_sales.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:14:50.462508Z","iopub.execute_input":"2022-07-12T20:14:50.463896Z","iopub.status.idle":"2022-07-12T20:14:50.485283Z","shell.execute_reply.started":"2022-07-12T20:14:50.463812Z","shell.execute_reply":"2022-07-12T20:14:50.482752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ar.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:15:54.019035Z","iopub.execute_input":"2022-07-12T20:15:54.019414Z","iopub.status.idle":"2022-07-12T20:15:54.029839Z","shell.execute_reply.started":"2022-07-12T20:15:54.019371Z","shell.execute_reply":"2022-07-12T20:15:54.028679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View the solution (Run this line to receive credit!)\nq_1.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:13:34.684131Z","iopub.execute_input":"2022-07-12T20:13:34.684647Z","iopub.status.idle":"2022-07-12T20:13:34.695723Z","shell.execute_reply.started":"2022-07-12T20:13:34.684602Z","shell.execute_reply":"2022-07-12T20:13:34.694635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uncomment the next line for a hint\n#q_1.hint()","metadata":{},"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-12T20:13:47.763240Z","iopub.execute_input":"2022-07-12T20:13:47.763714Z","iopub.status.idle":"2022-07-12T20:13:48.086160Z","shell.execute_reply.started":"2022-07-12T20:13:47.763676Z","shell.execute_reply":"2022-07-12T20:13:48.085282Z"},"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":"markdown","source":"Series_1 0.95 and series_2 -0.95.","metadata":{}},{"cell_type":"code","source":"# View the solution (Run this cell to receive credit!)\nq_2.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:17:03.973319Z","iopub.execute_input":"2022-07-12T20:17:03.973748Z","iopub.status.idle":"2022-07-12T20:17:03.982001Z","shell.execute_reply.started":"2022-07-12T20:17:03.973719Z","shell.execute_reply":"2022-07-12T20:17:03.981046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uncomment the next line for a hint\n#q_2.hint()","metadata":{},"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":"average_sales\n","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:29:47.916630Z","iopub.execute_input":"2022-07-12T20:29:47.917977Z","iopub.status.idle":"2022-07-12T20:29:47.931096Z","shell.execute_reply.started":"2022-07-12T20:29:47.917907Z","shell.execute_reply":"2022-07-12T20:29:47.930035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\n\ndf = average_sales.to_frame()\n\n# YOUR CODE HERE: Create a time dummy\ntime = np.arange(len(df))\n\ndf['time'] = time \n\n# YOUR CODE HERE: Create training data\nX = df.loc[:,['time']]  # features\ny = df.loc[:,'sales']  # target\n\n# Train the model\nmodel = LinearRegression()\nmodel.fit(X, y)\n\n# Store the fitted values as a time series with the same time index as\n# the training data\ny_pred = pd.Series(model.predict(X), index=X.index)\n\n\n# Check your answer\nq_3.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:33:18.978150Z","iopub.execute_input":"2022-07-12T20:33:18.978592Z","iopub.status.idle":"2022-07-12T20:33:19.008457Z","shell.execute_reply.started":"2022-07-12T20:33:18.978554Z","shell.execute_reply":"2022-07-12T20:33:19.007158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(df.loc[:,['time']])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:35:27.176231Z","iopub.execute_input":"2022-07-12T20:35:27.177189Z","iopub.status.idle":"2022-07-12T20:35:27.184509Z","shell.execute_reply.started":"2022-07-12T20:35:27.177152Z","shell.execute_reply":"2022-07-12T20:35:27.183209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(df.loc[:,'time'])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:37:08.442213Z","iopub.execute_input":"2022-07-12T20:37:08.442606Z","iopub.status.idle":"2022-07-12T20:37:08.449371Z","shell.execute_reply.started":"2022-07-12T20:37:08.442571Z","shell.execute_reply":"2022-07-12T20:37:08.448529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(df['time'])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:36:23.112194Z","iopub.execute_input":"2022-07-12T20:36:23.112597Z","iopub.status.idle":"2022-07-12T20:36:23.120424Z","shell.execute_reply.started":"2022-07-12T20:36:23.112562Z","shell.execute_reply":"2022-07-12T20:36:23.119057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['time']==df.loc[:,['time']]","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:38:17.424993Z","iopub.execute_input":"2022-07-12T20:38:17.425397Z","iopub.status.idle":"2022-07-12T20:38:17.568099Z","shell.execute_reply.started":"2022-07-12T20:38:17.425350Z","shell.execute_reply":"2022-07-12T20:38:17.566990Z"},"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_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-12T20:41:32.435422Z","iopub.execute_input":"2022-07-12T20:41:32.435867Z","iopub.status.idle":"2022-07-12T20:41:32.727859Z","shell.execute_reply.started":"2022-07-12T20:41:32.435824Z","shell.execute_reply":"2022-07-12T20:41:32.726773Z"},"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\n# YOUR CODE HERE: Create a lag feature from the target 'sales'\nlag_1 = df.loc[:,'sales'].shift(1)\n\ndf['lag_1'] = lag_1  # add to dataframe\n\nX = df.loc[:, ['lag_1']].dropna()  # features\ny = df.loc[:, 'sales']  # 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.\nmodel = LinearRegression()\nmodel.fit(X,y)\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\n# Check your answer\nq_4.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T20:44:21.204593Z","iopub.execute_input":"2022-07-12T20:44:21.204970Z","iopub.status.idle":"2022-07-12T20:44:21.226289Z","shell.execute_reply.started":"2022-07-12T20:44:21.204941Z","shell.execute_reply":"2022-07-12T20:44:21.225464Z"},"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_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-12T20:44:39.169060Z","iopub.execute_input":"2022-07-12T20:44:39.169442Z","iopub.status.idle":"2022-07-12T20:44:39.402421Z","shell.execute_reply.started":"2022-07-12T20:44:39.169409Z","shell.execute_reply":"2022-07-12T20:44:39.401528Z"},"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":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [course discussion forum](https://www.kaggle.com/learn/time-series/discussion) to chat with other learners.*","metadata":{}}]}