{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Section 5.1: Using the model" ] }, { "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(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": "markdown", "metadata": {}, "source": [ "## Modelling abrasion loss\n", "\n", "This example concerns the dataset `rubber`, which you first met in Exercise 3.1. The experiment introduced in that exercise concerned an investigation of the resistance of rubber to abrasion (the abrasion loss) and how that resistance depended on various attributes of the rubber. In Exercise 3.1, the only explanatory variable we analysed was hardness; but in Exercise 3.19, a second explanatory variable, tensile strength, was taken into account too. There are 30 datapoints." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [], "source": [ "rubber <- read.csv('rubber.csv')\n", "rubber" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The figures below show scatterplots of abrasion loss against hardness and of abrasion loss against strength. (A scatterplot of abrasion loss against hardness also appeared in Solution 3.1.) Figure (a) suggests a strong decreasing linear relationship of abrasion loss with hardness. Figure (b) is much less indicative of any strong dependence of abrasion loss on tensile strength." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": false }, "outputs": [], "source": [ "hardloss <- ggplot(rubber, aes(x=hardness, y=loss)) + geom_point()\n", "strloss <- ggplot(rubber, aes(x=strength, y=loss)) + geom_point()\n", "\n", "multiplot(hardloss, strloss, cols=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In exercise 3.5(a) you obtained the following output for the regression of abrasion loss on hardness." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "fit <- lm(loss ~ hardness, data = rubber)\n", "summary(fit)\n", "anova(fit)\n", "# af <- anova(fit)\n", "# afss <- af$\"Sum Sq\"\n", "# print(cbind(af,PctExp=afss/sum(afss)*100))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# ggplot(rubber, aes(x=hardness, y=loss)) + \n", "# geom_point() +\n", "# stat_smooth(method = \"lm\", col = \"red\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "ggplotRegression(fit)" ] }, { "cell_type": "markdown", "metadata": { "solution": "shown", "solution2": "hidden", "solution2_first": true, "solution_first": true }, "source": [ "### Exercise 5.1\n", "Now repeat the for the regression of abrasion loss on tensile strength.\n", "\n", "Enter your solution in the cell below." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Your solution here" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\n", "Call:\n", "lm(formula = loss ~ strength, data = rubber)\n", "\n", "Residuals:\n", " Min 1Q Median 3Q Max \n", "-155.640 -59.919 2.795 61.221 183.285 \n", "\n", "Coefficients:\n", " Estimate Std. Error t value Pr(>|t|) \n", "(Intercept) 305.2248 79.9962 3.815 0.000688 ***\n", "strength -0.7192 0.4347 -1.654 0.109232 \n", "---\n", "Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1\n", "\n", "Residual standard error: 85.56 on 28 degrees of freedom\n", "Multiple R-squared: 0.08904,\tAdjusted R-squared: 0.0565 \n", "F-statistic: 2.737 on 1 and 28 DF, p-value: 0.1092\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Df | Sum Sq | Mean Sq | F value | Pr(>F) | |
---|---|---|---|---|---|
strength | 1 | 20034.77 | 20034.772 | 2.736769 | 0.1092317 |
Residuals | 28 | 204976.59 | 7320.593 | NA | NA |