{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e97f0dd4-4901-ff62-91e7-177d70b1a96f"
      },
      "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": "7312993c-d358-fad5-1cae-cb1fc356c829"
      },
      "outputs": [],
      "source": [
        "train <- read.csv(\"../input/train.csv\")\n",
        "test <- read.csv(\"../input/test.csv\")\n",
        "head(train)\n",
        "str(train)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "040571cd-39b5-372d-55be-e72e2947ed7a"
      },
      "outputs": [],
      "source": [
        "library(rpart)\n",
        "library(rpart.plot)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b278bd52-2ae3-afd8-c6a7-34c61401e5f1"
      },
      "outputs": [],
      "source": [
        "cart <- rpart(SalePrice ~ PoolArea + YearBuilt + LotArea + Neighborhood + SaleCondition + Exterior1st + GarageCars + RoofStyle + HouseStyle + Heating + KitchenQual, data = train, method = \"anova\")\n",
        "summary(cart)\n",
        "names(cart)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c7982375-f450-e6fc-0c93-1bee0241f7e9"
      },
      "outputs": [],
      "source": [
        "test_1 <- test[, all.vars(as.formula(cart))[-1]]\n",
        "# str(test_1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ea559e81-1cca-2e0f-e62a-33a21e2be2fb"
      },
      "outputs": [],
      "source": [
        "yhat <- predict(cart, newdata = test_1)\n",
        "head(yhat)\n",
        "str(yhat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "00caed4e-6a0b-f380-8a46-b1a08609695f"
      },
      "outputs": [],
      "source": [
        "submission <- data.frame(Id = test[ , \"Id\"], SalePrice = yhat)\n",
        "#submission[ , 2] <- yhat\n",
        "write.csv(submission, file=\"2017-03-21_v1.csv\", row.names = F)"
      ]
    }
  ],
  "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
}