{"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)\nlibrary(data.table)\nlibrary(vars)","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2021-06-11T16:21:05.508359Z","iopub.execute_input":"2021-06-11T16:21:05.51023Z","iopub.status.idle":"2021-06-11T16:21:05.795977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"engage = fread(\"../input/mlb-train-processed-data/nextDayPlayerEngagement.csv\")\nengage","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:16.085203Z","iopub.execute_input":"2021-06-11T18:26:16.087319Z","iopub.status.idle":"2021-06-11T18:26:16.553478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# pick playerID == 628317","metadata":{}},{"cell_type":"code","source":"engage = engage[playerId == 628317]","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:18.098355Z","iopub.execute_input":"2021-06-11T18:26:18.099861Z","iopub.status.idle":"2021-06-11T18:26:18.212319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pick target 1","metadata":{}},{"cell_type":"code","source":"engage = engage[1:1186, c(2,4)]\nengage","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:19.228048Z","iopub.execute_input":"2021-06-11T18:26:19.229675Z","iopub.status.idle":"2021-06-11T18:26:19.27863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = ts(engage$target1, s = c(2018, 1, 2), e = c(2021, 4, 1), f = 12)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:22.534919Z","iopub.execute_input":"2021-06-11T18:26:22.536427Z","iopub.status.idle":"2021-06-11T18:26:22.548365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(train)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:06:35.336709Z","iopub.execute_input":"2021-06-11T18:06:35.338204Z","iopub.status.idle":"2021-06-11T18:06:35.407829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"award = read.csv('../input/mlb-train-processed-data/awards.csv')\naward[award$playerId == 628317,]","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:24.882696Z","iopub.execute_input":"2021-06-11T18:26:24.884294Z","iopub.status.idle":"2021-06-11T18:26:24.942578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# This player belongs to team 119","metadata":{}},{"cell_type":"code","source":"score = read.csv(\"../input/mlb-train-processed-data/playerBoxScores.csv\")\nscore = score[score$playerId == 628317,]\nscore","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:26.70316Z","iopub.execute_input":"2021-06-11T18:26:26.704772Z","iopub.status.idle":"2021-06-11T18:26:30.77476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"as.data.frame(cbind(lapply(lapply(score, is.na), sum)))[,1]","metadata":{"execution":{"iopub.status.busy":"2021-06-11T17:26:18.926062Z","iopub.execute_input":"2021-06-11T17:26:18.927658Z","iopub.status.idle":"2021-06-11T17:26:18.967801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fans = read.csv(\"../input/mlb-train-processed-data/teamTwitterFollowers.csv\")\nfans","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:32.83203Z","iopub.execute_input":"2021-06-11T18:26:32.833628Z","iopub.status.idle":"2021-06-11T18:26:32.924133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fans = fans[fans$teamId == 119, ]\nfans","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:36.106909Z","iopub.execute_input":"2021-06-11T18:26:36.108512Z","iopub.status.idle":"2021-06-11T18:26:36.168732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"follower =  ts(fans$numberOfFollowers, s = c(2018, 1, 2), e = c(2021, 4, 1), f = 12)\nplot(follower)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:39.090905Z","iopub.execute_input":"2021-06-11T18:26:39.092485Z","iopub.status.idle":"2021-06-11T18:26:39.164511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:01:34.304229Z","iopub.execute_input":"2021-06-11T18:01:34.305818Z","iopub.status.idle":"2021-06-11T18:01:34.333575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"follower","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:01:31.528538Z","iopub.execute_input":"2021-06-11T18:01:31.530221Z","iopub.status.idle":"2021-06-11T18:01:31.554303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1 = tslm(train ~ follower)\n       ","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:02:34.855298Z","iopub.execute_input":"2021-06-11T18:02:34.856827Z","iopub.status.idle":"2021-06-11T18:02:34.874215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:03:03.809781Z","iopub.execute_input":"2021-06-11T18:03:03.81127Z","iopub.status.idle":"2021-06-11T18:03:03.82631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"follower","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:04:14.833484Z","iopub.execute_input":"2021-06-11T18:04:14.835199Z","iopub.status.idle":"2021-06-11T18:04:14.857815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(engage$target1)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:07:28.122227Z","iopub.execute_input":"2021-06-11T18:07:28.124035Z","iopub.status.idle":"2021-06-11T18:07:28.142215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(m1$fitted.values, fans$numberOfFollowers)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:08:11.140732Z","iopub.execute_input":"2021-06-11T18:08:11.142367Z","iopub.status.idle":"2021-06-11T18:08:11.220063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(m1)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:08:25.839831Z","iopub.execute_input":"2021-06-11T18:08:25.841315Z","iopub.status.idle":"2021-06-11T18:08:25.87563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkresiduals(m1)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:08:50.358379Z","iopub.execute_input":"2021-06-11T18:08:50.360048Z","iopub.status.idle":"2021-06-11T18:08:51.264487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ACF has a cutoff","metadata":{}},{"cell_type":"markdown","source":"# VAR","metadata":{}},{"cell_type":"code","source":"?VAR()","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:11:38.845708Z","iopub.execute_input":"2021-06-11T18:11:38.847482Z","iopub.status.idle":"2021-06-11T18:11:39.250669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train\nx = follower","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:26:55.402068Z","iopub.execute_input":"2021-06-11T18:26:55.403673Z","iopub.status.idle":"2021-06-11T18:26:55.418438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"engage","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:19:08.449766Z","iopub.execute_input":"2021-06-11T18:19:08.451296Z","iopub.status.idle":"2021-06-11T18:19:08.497228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = as.data.frame(train)\nfollower = as.data.frame(follower)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:27:03.73415Z","iopub.execute_input":"2021-06-11T18:27:03.735737Z","iopub.status.idle":"2021-06-11T18:27:03.749623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train$x = train$x\ntrain$y = follower$x","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:27:48.503571Z","iopub.execute_input":"2021-06-11T18:27:48.505277Z","iopub.status.idle":"2021-06-11T18:27:48.519864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(vars)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:27:05.756703Z","iopub.execute_input":"2021-06-11T18:27:05.75822Z","iopub.status.idle":"2021-06-11T18:27:05.771538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m2 = VAR(train, p = 10)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:28:18.390162Z","iopub.execute_input":"2021-06-11T18:28:18.39178Z","iopub.status.idle":"2021-06-11T18:28:18.411582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m2","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:28:19.683123Z","iopub.execute_input":"2021-06-11T18:28:19.684799Z","iopub.status.idle":"2021-06-11T18:28:19.703611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(pred = predict(m2, n.ahead = 18)$fcst)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:29:13.087662Z","iopub.execute_input":"2021-06-11T18:29:13.089295Z","iopub.status.idle":"2021-06-11T18:29:13.152952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(pred$x[,1])","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:29:16.833675Z","iopub.execute_input":"2021-06-11T18:29:16.835251Z","iopub.status.idle":"2021-06-11T18:29:16.901557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(pred$y[,1])","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:29:26.384921Z","iopub.execute_input":"2021-06-11T18:29:26.386459Z","iopub.status.idle":"2021-06-11T18:29:26.450811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(res = residuals(m2))","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:29:45.458816Z","iopub.execute_input":"2021-06-11T18:29:45.460327Z","iopub.status.idle":"2021-06-11T18:29:45.491242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acf(res[,1]) # engagement\nacf(res[,2]) # followers\nacf(res)","metadata":{"execution":{"iopub.status.busy":"2021-06-11T18:29:58.080843Z","iopub.execute_input":"2021-06-11T18:29:58.082544Z","iopub.status.idle":"2021-06-11T18:29:58.280621Z"},"trusted":true},"execution_count":null,"outputs":[]}]}