{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "library(ggplot2)\nlibrary(data.table)\nlibrary(scales)\nlibrary(lubridate)\nlibrary(ggrepel)\nlibrary(magrittr)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "Questions:\n\n2. Do several clicks in a hotel cluster imply bookings in the same cluster?\n3. Is there any hotel cluster booked without previous clicks?"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "1. How do users explore and book hotels?\n\nLet's try to answer this question with a plot. \nThe plot shows the clicks and bookings of a random user in the training set."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "a <- fread(\"../input/train.csv\", select = \"user_id\")\nstr(a)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "dt_users <- fread(\"../input/train.csv\", \n                  select = c(\"user_id\", \"is_booking\", \"hotel_cluster\", \"date_time\"),\n                 nrows = 1000000)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Take a random user in the training set and all its events\nset.seed(22)\nrandom_user <- sample(dt_users[, user_id], 1)\nbehavior <- dt_users[user_id == random_user]\ninvisible( behavior[, date_time := ymd_hms(date_time)] )\ninvisible( behavior[, user_id := as.factor(user_id)] )\ninvisible( behavior[, is_booking := as.factor(is_booking)] )\ninvisible( behavior[, hotel_cluster := as.factor(hotel_cluster)] )\n\n# Plot!\nggplot(behavior) +\ngeom_text_repel(mapping = aes(x = date_time, y = user_id, label = hotel_cluster, color = is_booking)) +\nscale_x_datetime(labels = date_format(\"%d/%m/%y\")) +\nxlab(\"Date\") + ylab(\"User\") + ggtitle(\"Hotel clicks and bookings\") +\ntheme_bw() +\ntheme(axis.text.x = element_text(angle = 90, hjust = 1))"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "It looks like sometimes clicks can predict bookings, and sometimes the can't. \nIt seems like users usually explore lots of hotels before booking.\nRe-run the previous chunk several times with different seeds to get a sense of user behavior.\n\nThis helps a bit, but one user is not enough to understand the whole data set.\n"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "2. To answer this question, for each booking in the training set, \nlet's take the previous clicks (if they exist), and see if the\nbooked hotel cluster is contained in those previous clicks:"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "bookings <- dt_users[is_booking == 1]\nbookings[, lag_time := c(NA, date_time[-.N]), by = .(user_id)]\nsetkey(bookings, user_id, date_time)\n\nclicks <- dt_users[is_booking == 0]\nsetnames(clicks, date_time, aux_date_time)\nsetkey(clicks, user_id, aux_date_time)\n\nTODO"
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}