{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3d plots of regression surfaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### Initialisation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true,
    "init_cell": true
   },
   "outputs": [],
   "source": [
    "library(tidyverse)\n",
    "# library(cowplot)\n",
    "library(repr)\n",
    "library(ggfortify)\n",
    "\n",
    "# Change plot size to 4 x 3\n",
    "options(repr.plot.width=6, repr.plot.height=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true,
    "init_cell": true
   },
   "outputs": [],
   "source": [
    "source('plot_extensions.R')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true,
    "init_cell": true
   },
   "outputs": [],
   "source": [
    "library(scatterplot3d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Show plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rubber <- read.csv('rubber.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fit.hs <- lm(loss ~ hardness + strength, data = rubber)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s3d <- scatterplot3d(x = rubber$hardness, y = rubber$strength, z = rubber$loss,\n",
    "             xlab = 'Hardness', ylab = \"Strength\", zlab = \"Loss\",\n",
    "             angle = 55, \n",
    "             type='h')\n",
    "s3d$plane3d(fit.hs, draw_polygon = TRUE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "anaerobic <- read.csv('anaerob.csv')\n",
    "anaerobic$oxy2 <- anaerobic$oxygen^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "anaerobic.n <- data.frame(scale(anaerobic))\n",
    "head(anaerobic.n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fit.o2 <- lm(ventil ~ oxygen + oxy2, data = anaerobic.n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s3d <- with(anaerobic.n, scatterplot3d(x = oxygen, y = oxy2, z = ventil,\n",
    "             xlab = 'Oxygen', ylab = \"Oxygen2\", zlab = \"Ventil\",\n",
    "             angle = 205, \n",
    "             type='h'))\n",
    "s3d$plane3d(fit.o2, draw_polygon = TRUE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}