{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Section 5.1 solutions" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "### Imports and defintions" ] }, { "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": { "init_cell": true }, "outputs": [], "source": [ "source('plot_extensions.R')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 5.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the `cemheat` dataset." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cemheat <- read.csv('cemheat.csv')\n", "head(cemheat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make scatterplots of heat against each of TA and TS in turn, and comment on what you see." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "taheat <- ggplot(cemheat, aes(x=TA, y=heat)) + geom_point()\n", "tsheat <- ggplot(cemheat, aes(x=TS, y=heat)) + geom_point()\n", "\n", "multiplot(taheat, tsheat, cols=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Blah, blah, comment, blah." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use GenStat to fit each individual regression equation (of heat on TA and of heat on TS) in turn, and then to fit the regression equation with two explanatory variables. Does the latter regression equation give you a better model than either of the individual ones?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "fit.ta <- lm(heat ~ TA, data = cemheat)\n", "summary(fit.ta)\n", "anova(fit.ta)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "ggplotRegression(fit.ta)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "fit.ts <- lm(heat ~ TS, data = cemheat)\n", "summary(fit.ts)\n", "anova(fit.ts)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "ggplotRegression(fit.ts)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "fit.tats <- lm(heat ~ TA + TS, data = cemheat)\n", "summary(fit.tats)\n", "anova(fit.tats)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now combine the results into one dataframe for easy comparison." ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Vars | Adj.R.2 |
---|---|
TA | 0.4915797 |
TS | 0.6359290 |
TA, TS | 0.9744140 |