{"cells":[
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "### 1. Load packages and set random seed so results are reproducable"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "library(ggplot2) # Data visualization\nlibrary(readr) # CSV file I/O, e.g. the read_csv function\nlibrary(caret) # createDataPartition function\nlibrary(dplyr) # select, filter, group_by, arrange, summarize functions\nlibrary(RColorBrewer)\nlibrary(repr)\nlibrary(IRdisplay)\n\nset.seed(42)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "### 2. Read in training data from file"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Read data from file (Runtime: About 40 seconds)\ntrain <- read_csv(\"../input/train.csv\")\n\nhead(train)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Convert place_id column to characer\ntrain$place_id <- as.character(train$place_id)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## 3. Group data by place_id and only keep the top 100 places by event count"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Create a list of the top 100 place_ids by number of events (Runtime: About 15 seconds)\nplaces_top_100 <- train %>%\n    group_by(place_id) %>%\n    summarise(\n        cnt_events = n(),\n        x_avg = mean(x, na.rm = T),\n        y_avg = mean(y, na.rm = T),      \n        acc_avg = mean(accuracy, na.rm = T),\n        time_avg = mean(time, na.rm = T)\n    ) %>%\n    arrange(desc(cnt_events)) %>%\n    mutate(row_id = 1:n()) %>%\n    filter(row_id <= 100)\n\n# Show the first few rows in the data frame\nhead(places_top_100)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## 4. Subsample the 'train' dataframe, only keep rows from the top 100 place_ids"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Join the original train dataframe to the list of top 100 place_ids\ntrain_top_100_places <- inner_join(train, places_top_100, by=\"place_id\")\n\n# How many rows to we have now?\npaste(\"Number of rows: \",nrow(train_top_100_places))"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## 5. Generate X vs. Y visualization"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Create X vs. Y plot (Runtime: About ## seconds)\noptions(repr.plot.width=8, repr.plot.height=8)\nggplot(data=train_top_100_places, aes(x=y, y=x)) +\ngeom_point(aes(color=place_id, size=accuracy)) +\ncoord_cartesian(xlim = c(2,5), ylim = c(0,10)) +\nggtitle(\"X versus Y\") +\ntheme(legend.position=\"none\")\n\n# Note: This graph shows a different color for each place_id.  There aren't\n# many colors in the pallet, and we have 100 places.  Several places will have\n# the same color.  Also the size of the bubbles are proportional to the accuracy."
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## 6. Generate 'time' versus 'y' visualization"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Create 'time' vs. 'y' plot (Runtime: About ## seconds)\noptions(repr.plot.width=8, repr.plot.height=8)\nggplot(data=train_top_100_places, aes(x=y, y=time)) +\ngeom_point(aes(color=place_id, size=accuracy)) +\ncoord_cartesian(xlim = c(2,5)) +\nggtitle(\"Time versus Y\") +\ntheme(legend.position=\"none\")\n\n# Note:  It looks like some events are continuous over time, and \n# others occur during specific time intervals."
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "## 7. Generate 'time' versus 'x' visualization"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Create 'time' vs. 'x' plot\noptions(repr.plot.width=8, repr.plot.height=8)\nggplot(data=train_top_100_places, aes(x=x, y=time)) +\ngeom_point(aes(color=place_id, size=accuracy, alpha=0.2)) +\ncoord_cartesian(xlim = c(3,4)) +\nggtitle(\"Time versus X\") +\ntheme(legend.position=\"none\")\n\n# Note: This graph is interesting.  I'm not exactly sure how to interpret it yet."
 }
],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"}}, "nbformat": 4, "nbformat_minor": 0}