{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nlist.files(path = \"../input\")\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-07-12T18:06:38.272949Z","iopub.execute_input":"2022-07-12T18:06:38.274474Z","iopub.status.idle":"2022-07-12T18:06:38.299574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Loading and review all datasets**","metadata":{}},{"cell_type":"code","source":"data_holiday = read.csv ('../input/store-sales-time-series-forecasting/holidays_events.csv') # the symbol <- inconvenient to use\nstr (data_holiday)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.301684Z","iopub.execute_input":"2022-07-12T18:06:38.302891Z","iopub.status.idle":"2022-07-12T18:06:38.329947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_oil = read.csv ('../input/store-sales-time-series-forecasting/oil.csv')\nstr (data_oil)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.332089Z","iopub.execute_input":"2022-07-12T18:06:38.333342Z","iopub.status.idle":"2022-07-12T18:06:38.353284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_stores = read.csv ('../input/store-sales-time-series-forecasting/stores.csv')\nstr (data_stores)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.355509Z","iopub.execute_input":"2022-07-12T18:06:38.356753Z","iopub.status.idle":"2022-07-12T18:06:38.377336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_transactions = read.csv ('../input/store-sales-time-series-forecasting/transactions.csv')\nstr (data_transactions)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.379579Z","iopub.execute_input":"2022-07-12T18:06:38.380853Z","iopub.status.idle":"2022-07-12T18:06:38.475423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Clean the data**","metadata":{}},{"cell_type":"code","source":"# import required libraries\nlibrary (janitor)\n\nclean = clean_names (data_holiday)\ncolnames (clean)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.477533Z","iopub.execute_input":"2022-07-12T18:06:38.478792Z","iopub.status.idle":"2022-07-12T18:06:38.506487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tabyl (clean, type, locale)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.508558Z","iopub.execute_input":"2022-07-12T18:06:38.509820Z","iopub.status.idle":"2022-07-12T18:06:38.614250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_pct_formatting (digits = 2, affix_sign = TRUE)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.616338Z","iopub.execute_input":"2022-07-12T18:06:38.617651Z","iopub.status.idle":"2022-07-12T18:06:38.667333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_totals ()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.669348Z","iopub.execute_input":"2022-07-12T18:06:38.670621Z","iopub.status.idle":"2022-07-12T18:06:38.717714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_totals (where = \"col\")","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.719860Z","iopub.execute_input":"2022-07-12T18:06:38.721106Z","iopub.status.idle":"2022-07-12T18:06:38.776056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_totals (where = c (\"row\", \"col\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.778130Z","iopub.execute_input":"2022-07-12T18:06:38.779374Z","iopub.status.idle":"2022-07-12T18:06:38.834395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_totals(\"row\") %>% adorn_percentages (\"row\") %>%\nadorn_pct_formatting () %>% adorn_ns ()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.842084Z","iopub.execute_input":"2022-07-12T18:06:38.843375Z","iopub.status.idle":"2022-07-12T18:06:38.961454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_totals ('row') %>% adorn_percentages ('row') %>% adorn_pct_formatting () %>% adorn_ns ('front')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:38.963654Z","iopub.execute_input":"2022-07-12T18:06:38.964928Z","iopub.status.idle":"2022-07-12T18:06:39.078527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale, locale_name)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:39.081843Z","iopub.execute_input":"2022-07-12T18:06:39.083449Z","iopub.status.idle":"2022-07-12T18:06:39.797538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove duplicate records\nclean %>% get_dupes (type, locale, locale_name, description, transferred)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:39.799731Z","iopub.execute_input":"2022-07-12T18:06:39.801037Z","iopub.status.idle":"2022-07-12T18:06:39.931561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale) %>% adorn_totals ('row') %>% adorn_percentages ('row') %>% adorn_pct_formatting () %>% adorn_ns ('front')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:39.933701Z","iopub.execute_input":"2022-07-12T18:06:39.935005Z","iopub.status.idle":"2022-07-12T18:06:40.040711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean %>% tabyl (type, locale, locale_name)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:40.042831Z","iopub.execute_input":"2022-07-12T18:06:40.044129Z","iopub.status.idle":"2022-07-12T18:06:40.764030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove duplicate records\nclean %>% get_dupes (type, locale, locale_name, description, transferred)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:40.766165Z","iopub.execute_input":"2022-07-12T18:06:40.767472Z","iopub.status.idle":"2022-07-12T18:06:40.879959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the class dataset\nclass (data_holiday)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:40.882061Z","iopub.execute_input":"2022-07-12T18:06:40.883317Z","iopub.status.idle":"2022-07-12T18:06:40.897964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the data_holiday in time series object\n# import required libraries\nlibrary (tsbox)\n\ndata_holiday$date =  as.Date (data_holiday$date)\n\nholiday_ts = ts (data = data_holiday, start = c (2012, 3, 2), end = c (2017, 12, 26), frequency = 365)\nhead (holiday_ts)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:40.900096Z","iopub.execute_input":"2022-07-12T18:06:40.901471Z","iopub.status.idle":"2022-07-12T18:06:41.030647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the convert operation\nis.ts (holiday_ts)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:41.032831Z","iopub.execute_input":"2022-07-12T18:06:41.034138Z","iopub.status.idle":"2022-07-12T18:06:41.049254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Exploratory data analysis**","metadata":{}},{"cell_type":"code","source":"plot (holiday_ts)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:41.051392Z","iopub.execute_input":"2022-07-12T18:06:41.052712Z","iopub.status.idle":"2022-07-12T18:06:41.514770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist (holiday_ts [, \"date\"], 30)\nhist (diff (holiday_ts [, \"date\"], 30))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:41.518221Z","iopub.execute_input":"2022-07-12T18:06:41.520333Z","iopub.status.idle":"2022-07-12T18:06:41.665369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot (holiday_ts [, 'date'], holiday_ts [, \"description\"])\nplot (diff (holiday_ts [, 'date']), diff (holiday_ts [, 'description']))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:41.667516Z","iopub.execute_input":"2022-07-12T18:06:41.668765Z","iopub.status.idle":"2022-07-12T18:06:41.967672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot (stats::lag (diff (holiday_ts [, 'date']), 1), diff (holiday_ts [, 'description']))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:41.969871Z","iopub.execute_input":"2022-07-12T18:06:41.971163Z","iopub.status.idle":"2022-07-12T18:06:42.121598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2d visualizations\nt (matrix (sample(holiday_ts, 1200, replace = FALSE, prob = NULL), nrow = 12, ncol = 12))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:42.123676Z","iopub.execute_input":"2022-07-12T18:06:42.124918Z","iopub.status.idle":"2022-07-12T18:06:42.148798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot each year on a set of axes that reflect the progression of the months across the year\ncolors = c (\"red\", \"green\", \"pink\", \"blue\", \"yellow\", \"lightsalmon\", \"black\", \"gray\", \"cyan\",\n           \"lightblue\", \"maroon\", \"purple\")\nholiday_s = sample(holiday_ts, 1200, replace = FALSE, prob = NULL)\nmatplot (matrix (holiday_s, nrow = 12, ncol = 12), type = \"l\", col = colors, lty = 1, lwd = 2.5,\n        xaxt = \"n\", ylab = \"locale\")\nlegend (\"topleft\", legend = 2012:2017, lty = 1, lwd = 2.5, col = colors)\naxis (1, at = 1:12, labels = c (\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\",\n                               \"Sep\", \"Oct\", \"Nov\", \"Dec\"))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:42.150882Z","iopub.execute_input":"2022-07-12T18:06:42.152133Z","iopub.status.idle":"2022-07-12T18:06:42.275559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot of per-month curves agaisnt years\nmonths =c(\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\")\nmatplot (t (matrix (holiday_s, nrow = 12, ncol = 12)), \n         type = 'l', col = colors, lty = 1, lwd = 2.5)\nlegend ('left', legend = months, col = colors, lty = 1, lwd = 2.5)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:42.277679Z","iopub.execute_input":"2022-07-12T18:06:42.278959Z","iopub.status.idle":"2022-07-12T18:06:42.399957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthplot (holiday_s)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:42.401947Z","iopub.execute_input":"2022-07-12T18:06:42.403151Z","iopub.status.idle":"2022-07-12T18:06:42.543840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Naive forecast**","metadata":{}},{"cell_type":"code","source":"library (forecast)\nplot (naive (holiday_s, h = 5))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:07:53.716321Z","iopub.execute_input":"2022-07-12T18:07:53.717832Z","iopub.status.idle":"2022-07-12T18:07:53.830611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Seasonal naive forecast**","metadata":{}},{"cell_type":"code","source":"plot (snaive (holiday_s, h = 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:43.131247Z","iopub.execute_input":"2022-07-12T18:06:43.132657Z","iopub.status.idle":"2022-07-12T18:06:43.281104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Mean forecasting**","metadata":{}},{"cell_type":"code","source":"# plot mean forecasts\nplot (meanf (holiday_s, h = 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:43.283687Z","iopub.execute_input":"2022-07-12T18:06:43.285003Z","iopub.status.idle":"2022-07-12T18:06:43.421054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = residuals (naive (holiday_s))\nplot (res)\nggtitle ('Residuals from naive method')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:06:43.423469Z","iopub.execute_input":"2022-07-12T18:06:43.424913Z","iopub.status.idle":"2022-07-12T18:06:43.619994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pre Processing\n# So creating 12 dummy variables \nX = data.frame(outer(rep(month.abb,length = 1200), month.abb,\"==\") + 0 ) # Creating dummies for 12 months\nView(X)\ncolnames(X) = month.abb # Assigning month names \nView(X)\nholiday_s = cbind(holiday_s, X)\nView (holiday_s)\ncolnames(holiday_s)\n\n# partitioning\ntrain = holiday_s\ntest = holiday_s\ntest\n\n# linear model\n\nlinear_model = lm(data = train)\nsummary(linear_model)\nlinear_pred = data.frame(predict(linear_model, interval='predict', newdata = test))\n?predict\nlinear_pred\nrmse_linear = sqrt(mean((holiday_s-linear_pred$fit)^2, na.rm = T))\nrmse_linear","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:21:01.526002Z","iopub.execute_input":"2022-07-12T18:21:01.527680Z","iopub.status.idle":"2022-07-12T18:21:02.414929Z"},"trusted":true},"execution_count":null,"outputs":[]}]}