{"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":"3.6.3"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"library(fpp)\nlibrary(TSA)\nlibrary(tseries)\nlibrary(ggplot2)\nlibrary(forecast)\nlibrary(tidyverse) # metapackage of all tidyverse packages\nlist.files(path = \"../input\")","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2021-06-11T08:52:22.754512Z","iopub.execute_input":"2021-06-11T08:52:22.756891Z","iopub.status.idle":"2021-06-11T08:52:26.033339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(data.table)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:26.035484Z","iopub.execute_input":"2021-06-11T08:52:26.064083Z","iopub.status.idle":"2021-06-11T08:52:26.185913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = read.csv(\"../input/mlb-player-digital-engagement-forecasting/example_sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:26.187978Z","iopub.execute_input":"2021-06-11T08:52:26.189216Z","iopub.status.idle":"2021-06-11T08:52:26.394769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = fread(\"../input/mlb-train-processed-data/nextDayPlayerEngagement.csv\")\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:26.397025Z","iopub.execute_input":"2021-06-11T08:52:26.398325Z","iopub.status.idle":"2021-06-11T08:52:30.083602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colnames(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.086738Z","iopub.execute_input":"2021-06-11T08:52:30.088295Z","iopub.status.idle":"2021-06-11T08:52:30.101495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# pick playerID == 628317","metadata":{}},{"cell_type":"code","source":"train = train[playerId == 628317]","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.103502Z","iopub.execute_input":"2021-06-11T08:52:30.104676Z","iopub.status.idle":"2021-06-11T08:52:30.176139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"head(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.179419Z","iopub.execute_input":"2021-06-11T08:52:30.180992Z","iopub.status.idle":"2021-06-11T08:52:30.204826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pick target 1","metadata":{}},{"cell_type":"code","source":"train = train[, c(2,4)]\nhead(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.207876Z","iopub.execute_input":"2021-06-11T08:52:30.209418Z","iopub.status.idle":"2021-06-11T08:52:30.231485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tail(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.233695Z","iopub.execute_input":"2021-06-11T08:52:30.234996Z","iopub.status.idle":"2021-06-11T08:52:30.262553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = ts(train$target1, s = c(2018, 1, 2), e = c(2021, 5, 1), f = 365)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.264703Z","iopub.execute_input":"2021-06-11T08:52:30.265941Z","iopub.status.idle":"2021-06-11T08:52:30.277083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.279224Z","iopub.execute_input":"2021-06-11T08:52:30.280454Z","iopub.status.idle":"2021-06-11T08:52:30.613922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kpss.test(train)\n# p-value = 0.1 > 0.05 ==>  The process is stationary. ","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.616096Z","iopub.execute_input":"2021-06-11T08:52:30.617387Z","iopub.status.idle":"2021-06-11T08:52:30.642853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adf.test(train)\n# p-value = 0.01 < 0.05 ==> The process is stationary","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.645026Z","iopub.execute_input":"2021-06-11T08:52:30.646294Z","iopub.status.idle":"2021-06-11T08:52:30.675236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decomposed = stl(train, s.window=\"periodic\")\nseasonal <- decomposed$time.series[,1]\ntrend   <- decomposed$time.series[,2]\nremainder <- decomposed$time.series[,3]","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.677362Z","iopub.execute_input":"2021-06-11T08:52:30.678624Z","iopub.status.idle":"2021-06-11T08:52:30.70253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(trend)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.704869Z","iopub.execute_input":"2021-06-11T08:52:30.706249Z","iopub.status.idle":"2021-06-11T08:52:30.785195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(seasonal)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.787336Z","iopub.execute_input":"2021-06-11T08:52:30.788611Z","iopub.status.idle":"2021-06-11T08:52:30.899346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsdisplay(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:30.902839Z","iopub.execute_input":"2021-06-11T08:52:30.905025Z","iopub.status.idle":"2021-06-11T08:52:31.047826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* exponential decay in ACF\n* The ACF looks like the damped wave\n* It suggests an auto regression model","metadata":{}},{"cell_type":"code","source":"?auto.arima","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:31.050092Z","iopub.execute_input":"2021-06-11T08:52:31.051542Z","iopub.status.idle":"2021-06-11T08:52:31.431536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = auto.arima(train,seasonal = TRUE, trace = TRUE, lambda = BoxCox.lambda(train))","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:31.435016Z","iopub.execute_input":"2021-06-11T08:52:31.436813Z","iopub.status.idle":"2021-06-11T08:52:37.581742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model$var.coef","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:37.585347Z","iopub.execute_input":"2021-06-11T08:52:37.587512Z","iopub.status.idle":"2021-06-11T08:52:37.616427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(model)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:37.620041Z","iopub.execute_input":"2021-06-11T08:52:37.622305Z","iopub.status.idle":"2021-06-11T08:52:37.647979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkresiduals(model)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:37.651696Z","iopub.execute_input":"2021-06-11T08:52:37.65391Z","iopub.status.idle":"2021-06-11T08:52:38.673813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Box.test(resid(model), lag = 12, type=\"Ljung-Box\")\n# p-value = 0.02 < 0.05 ==> reject Null hypothesis \n# ==>  The TS is not independently distributed ","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:38.676038Z","iopub.execute_input":"2021-06-11T08:52:38.677368Z","iopub.status.idle":"2021-06-11T08:52:38.693076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(forecast(model, h = 365))","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:38.695287Z","iopub.execute_input":"2021-06-11T08:52:38.696555Z","iopub.status.idle":"2021-06-11T08:52:38.803043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = arfima(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:38.806504Z","iopub.execute_input":"2021-06-11T08:52:38.808546Z","iopub.status.idle":"2021-06-11T08:52:39.277595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(model2)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:39.281658Z","iopub.execute_input":"2021-06-11T08:52:39.284105Z","iopub.status.idle":"2021-06-11T08:52:39.310544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(forecast(model2, h = 365))","metadata":{"execution":{"iopub.status.busy":"2021-06-11T08:52:39.314355Z","iopub.execute_input":"2021-06-11T08:52:39.316725Z","iopub.status.idle":"2021-06-11T08:52:39.465697Z"},"trusted":true},"execution_count":null,"outputs":[]}]}