{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "d56c0645-5eb5-34a1-427f-39884ad91df2"
      },
      "source": [
        "Load Libraries and other things"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "251eb436-9c04-fcee-475e-dc8fa1b85556"
      },
      "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",
        "suppressMessages(library(ggplot2)) \n",
        "suppressMessages(library(readr)) \n",
        "suppressMessages(library(data.table))\n",
        "suppressMessages(library(jsonlite))\n",
        "suppressMessages(library(lubridate))\n",
        "suppressMessages(library(dplyr))\n",
        "suppressMessages(library(ggplot2))\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": "markdown",
      "metadata": {
        "_cell_guid": "4bd0b3e7-d8d7-fd23-3483-b9dbef6885e3"
      },
      "source": [
        "Load Data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4e2eb82d-f74b-cf2b-e72d-67110a89ed0c"
      },
      "outputs": [],
      "source": [
        "t1 <- fromJSON(\"../input/train.json\")\n",
        "\n",
        "t2 <- data.table(bathrooms=unlist(t1$bathrooms)\n",
        "                 ,bedrooms=unlist(t1$bedrooms)\n",
        "                 ,building_id=as.factor(unlist(t1$building_id))\n",
        "                 ,created=as.POSIXct(unlist(t1$created))\n",
        "                 # ,description=unlist(t1$description) # parse errors\n",
        "                 # ,display_address=unlist(t1$display_address) # parse errors\n",
        "                 ,latitude=unlist(t1$latitude)\n",
        "                 ,longitude=unlist(t1$longitude)\n",
        "                 ,listing_id=unlist(t1$listing_id)\n",
        "                 ,manager_id=as.factor(unlist(t1$manager_id))\n",
        "                 ,price=unlist(t1$price)\n",
        "                 ,interest_level=as.factor(unlist(t1$interest_level))\n",
        "                 # ,street_adress=unlist(t1$street_address) # parse errors\n",
        ")\n",
        "t2[,\":=\"(yday=yday(created)\n",
        "         ,month=month(created)\n",
        "         ,mday=mday(created)\n",
        "         ,wday=wday(created)\n",
        "         ,hour=hour(created))]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "92b84c43-609e-062d-017a-7aaccad4a717"
      },
      "outputs": [],
      "source": [
        "t2_price3 <- filter(t2, price < (mean(t2$price) + 3 *sd(t2$price)))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e763921a-64c0-002d-2cf2-956f260204db"
      },
      "source": [
        "#Longitude Latitude and Interest Level"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c5617388-3627-cb8e-b3c4-8f4e2ed3de08"
      },
      "outputs": [],
      "source": [
        "ggplot(t2) + geom_point(aes(latitude, longitude , color = interest_level)) + \n",
        "  scale_x_continuous(limits = c(40.5, 41.0)) +\n",
        "  scale_y_continuous(limits = c(-74.1, -73.75))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "740fbfdc-d3cc-5895-7627-44b61bfc3fde"
      },
      "outputs": [],
      "source": [
        "ggplot(t2) + geom_point(aes(longitude,latitude  , color = interest_level)) + \n",
        "  scale_y_continuous(limits = c(40.5, 41.0)) +\n",
        "  scale_x_continuous(limits = c(-74.1, -73.75))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1fc3efc4-9e16-6b7d-7cd7-09707b9f8dd0"
      },
      "outputs": [],
      "source": [
        "ggplot(t2_price3) + geom_point(aes(longitude , latitude  , color = (price))) + \n",
        "  scale_y_continuous(limits = c(40.5, 41.0)) +\n",
        "  scale_x_continuous(limits = c(-74.1, -73.75)) + \n",
        "  facet_grid(.~bedrooms)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d03e34dc-984c-49e7-3642-2b38b01c33b7"
      },
      "outputs": [],
      "source": [
        "ggplot(t2_price3) + geom_point(aes(longitude , latitude  , color = factor(interest_level))) + \n",
        "  scale_y_continuous(limits = c(40.5, 41.0)) +\n",
        "  scale_x_continuous(limits = c(-74.1, -73.75)) + \n",
        "  facet_grid(.~bedrooms)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b0b4a6c3-81d9-80f5-5950-8ea813855e87"
      },
      "source": [
        "#Day of week vs Interest level"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "aa8466a0-0d9d-38b5-39f0-8328c4139ca1"
      },
      "outputs": [],
      "source": [
        "ggplot(t2, aes(interest_level)) +\n",
        "  geom_freqpoly(aes(group = wday, colour = factor(wday)),stat=\"count\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "651d6793-ce9c-9ac7-5de3-0ce85b248fa0"
      },
      "source": [
        "#Price and Interest level"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b16e2dcf-a086-063c-eb6c-00400cfce091"
      },
      "outputs": [],
      "source": [
        "ggplot(t2) +\n",
        "  stat_boxplot(aes(interest_level, price))  "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5a876a2f-a7a3-81f2-4524-362904808cf1"
      },
      "outputs": [],
      "source": [
        "t21 <- filter(t2, price <10**6)\n",
        "ggplot(t21) +\n",
        "  geom_point(aes(price, interest_level))  "
      ]
    }
  ],
  "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.3.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}