{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "a41438f2-d831-da94-18e6-6bde8480227a"
      },
      "source": [
        "Alex"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "45a592aa-8fd5-4ada-7e72-f7ec295a2d16"
      },
      "outputs": [],
      "source": [
        "# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n",
        "# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "library(ggplot2) # Data visualization\n",
        "library(readr) # CSV file I/O, e.g. the read_csv function\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "system(\"ls ../input\")\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4b22b084-fcef-cd59-ed96-efca781e8755"
      },
      "outputs": [],
      "source": [
        "library(data.table)\n",
        "library(dplyr)\n",
        "library(ggplot2)\n",
        "library(ranger)\n",
        "library(plotly)\n",
        "library(tidyr)\n",
        "library(FNN)\n",
        "library(xgboost)\n",
        "\n",
        "fb <- fread(\"../input/train.csv\", integer64 = \"character\", showProgress = FALSE)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c9cc0f79-fb9d-2920-0de3-bb4e7bf823ec"
      },
      "outputs": [],
      "source": [
        "head(fb)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6176c61f-675d-8a87-9b20-1aa8d718aba6"
      },
      "outputs": [],
      "source": [
        "fb <- fb %>% filter(x > 1, x < 1.25, y > 2.5, y < 2.75) \n",
        "head(fb)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a4f0fba8-a59d-71eb-4776-0819155d1a88"
      },
      "outputs": [],
      "source": [
        "fb$hour <- (fb$time/60) %% 24\n",
        "fb$weekday <- (fb$time/(60*24)) %% 7\n",
        "fb$month <- (fb$time/(60*24*30)) %% 12\n",
        "fb$year <- (fb$time/(60*24*365))\n",
        "fb$day <- (fb$time/(60*24)) %% 365\n",
        "head(fb)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "648a1574-fbe3-af2b-f708-6f250a1ae005"
      },
      "outputs": [],
      "source": [
        "small_train <- fb[fb$time < 7.3e5, ]\n",
        "small_val <- fb[fb$time >= 7.3e5, ]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ea519b3a-4a3e-7522-3202-ba0e883d32ec"
      },
      "outputs": [],
      "source": [
        "ggplot(small_train, aes(x,y)) +\n",
        "    geom_point(aes(color = place_id)) +\n",
        "    theme_minimal() +\n",
        "    theme(legend.position = \"none\") +\n",
        "    ggtitle(\"Check-ins colored by place_id\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "94de921b-4594-62bf-cdfb-714121551bde"
      },
      "outputs": [],
      "source": [
        "ids <- small_train %>% \n",
        "    count(place_id) %>%\n",
        "    filter(n > 500)\n",
        "\n",
        "small_trainz <- small_train[small_train$place_id %in% ids$place_id, ]\n",
        "head(small_trainz)\n",
        "plot_ly(data = small_trainz, x = x, y = y, z = hour, color = place_id, type = \"scatter3d\", \n",
        "       mode = \"markers\", marker = list(size=5)) %>% \n",
        "    layout(title = \"Place_id's by position and Time of Day\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0c042109-38e1-5667-fb74-658779e38d9e"
      },
      "outputs": [],
      "source": [
        "plot_ly(data = small_trainz, x=x, y=y, z=weekday, color=place_id, type=\"scatter3d\", \n",
        "       mode=\"markers\", marker=list(size=5)) %>% \n",
        "    layout(title=\"Place_id's by position and Day of Week\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "380d5157-f9d8-96cb-ee0e-abe45a5a1ee6"
      },
      "outputs": [],
      "source": [
        "length(unique(small_train$place_id))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a81a6c7a-db96-9310-e5b3-248d766dee5d"
      },
      "outputs": [],
      "source": [
        "ids <- small_train %>% count(place_id) %>% filter(n > 3) \n",
        "small_train <- small_train[small_train$place_id %in% ids$place_id, ]\n",
        "length(unique(small_train$place_id))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c205a6f1-c786-0a25-99ca-3f0f7cf6a431"
      },
      "outputs": [],
      "source": [
        "s = 2\n",
        "l = 125\n",
        "w = 500\n",
        "\n",
        "create_matrix = function(train) {\n",
        "    cbind(s*train$y, \n",
        "          train$x,\n",
        "          train$hour/l,\n",
        "          train$weekday/w,\n",
        "          train$year/w,\n",
        "          train$time/(w*60*24*7))\n",
        "}\n",
        "\n",
        "X = create_matrix(small_train)\n",
        "X_val = create_matrix(small_val)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c0dd8b23-f026-d873-e0e2-eb8a2539729e"
      },
      "outputs": [],
      "source": [
        "model_knn <- FNN::knn(train = X, test = X_val, cl = small_train$place_id, k = 20)\n",
        "\n",
        "preds <- as.character(model_knn)\n",
        "truth <- as.character(small_val$place_id)\n",
        "mean(truth == preds)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ab1d295f-601b-3f84-f174-08fbd63d7384"
      },
      "outputs": [],
      "source": [
        "set.seed(131L)\n",
        "small_train$place_id <- as.factor(small_train$place_id)\n",
        "\n",
        "model_rf <- ranger(place_id ~ x + y + accuracy + hour + weekday + month + year, \n",
        "                  small_train,\n",
        "                  num.trees = 20,\n",
        "                  write.forest = TRUE,\n",
        "                  importance = \"impurity\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5667214b-90f9-58ba-5433-893a962b80fd"
      },
      "outputs": [],
      "source": [
        "pred <- predict(model_rf, small_val)\n",
        "pred <- pred$predictions\n",
        "accuracy <- mean(pred == small_val$place_id)\n",
        "accuracy"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4cab5138-b88c-3aaf-8d67-d95d173c6129"
      },
      "outputs": [],
      "source": [
        "small_val$Correct <- (pred == small_val$place_id)\n",
        "\n",
        "ggplot(small_val, aes(x,y)) +\n",
        "    geom_point(aes(color = Correct)) +\n",
        "    theme_minimal() +\n",
        "    scale_color_brewer(palette = \"Set1\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c2bb1de0-6ffb-8699-c8f7-5f1b5a61ab5e"
      },
      "outputs": [],
      "source": [
        "small_val$place_id <- factor(small_val$place_id,\n",
        "                            levels = names(sort(table(small_val$place_id), decreasing = TRUE)))\n",
        "small_val %>%\n",
        "    ggplot(aes(x = place_id)) + geom_bar(aes(fill = Correct)) + \n",
        "    theme_minimal() +\n",
        "    theme(axis.text.x = element_blank()) +\n",
        "    ggtitle(\"Prediction Accuracy by ID and Popularity\") +\n",
        "    scale_fill_brewer(palette = \"Set1\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b29b9e36-c7cf-855c-b5d4-ea002b71a24b"
      },
      "outputs": [],
      "source": [
        "data.frame(as.list(model_rf$variable.importance)) %>% gather() %>%\n",
        "    ggplot(aes(x = reorder(key, value), y = value)) +\n",
        "    geom_bar(stat = \"identity\", width = 0.6, fill = \"grey\") +\n",
        "    coord_flip() +\n",
        "    theme_minimal() +\n",
        "    ggtitle(\"Variable Importance (Gini Index)\") +\n",
        "    theme(axis.title.y = element_blank())"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.4.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}