{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "ee35d39f2b1e9d0464c5b90033efc7e2", "grade": false, "grade_id": "cell-6cf23f5486d00725", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Worksheet A-4: Tidy Data & The Model-Fitting Paradigm in R\n", "\n", "By the end of this worksheet, you will be able to:\n", "\n", "(From tidyr)\n", "\n", "- convert a dataset between the 'long' and 'wide' format, using `tidyr::pivot_longer()` and `tidyr::pivot_wider()`\n", "- assess which format is best suited for each type of analysis\n", "- deal with missing data in a tibble\n", "\n", "(From modelling)\n", "\n", "- make a model object in R, using `lm()` as an example.\n", "- write a formula in R.\n", "- predict on a model object with the `broom::augment()` and `predict()` functions.\n", "- extract information from a model object using `broom::tidy()`, `broom::glance()`, and traditional means.\n", "\n", "To get full marks for this worksheet, you must successfully answer at least 10 of the autograded questions. There are 15 questions in total.\n", "\n", "## Getting Started\n", "\n", "Load the requirements for this worksheet:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "9dfeb6bf4af3e22e4340768b14700684", "grade": false, "grade_id": "cell-0e57a40968a03e65", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "library(testthat)\n", "library(digest)\n", "suppressPackageStartupMessages(library(tidyverse))\n", "suppressPackageStartupMessages(library(broom))\n", "suppressPackageStartupMessages(library(gapminder))\n", "lotr <- suppressMessages(read_csv(\"https://raw.githubusercontent.com/jennybc/lotr-tidy/master/data/lotr_tidy.csv\"))\n", "guest <- suppressMessages(read_csv(\"https://raw.githubusercontent.com/STAT545-UBC/Classroom/master/data/wedding/attend.csv\"))" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "3169cb72d2c60bf507a5877969e0bf56", "grade": false, "grade_id": "cell-8c33d8917362545a", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "The following code chunk has been unlocked, to give you the flexibility to start this document with some of your own code. Remember, it's bad manners to keep a call to `install.packages()` in your source code, so don't forget to delete these lines if you ever need to run them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# An unlocked code cell." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "ee049ccb004a3a4316287385a1f4ef63", "grade": false, "grade_id": "cell-67a56affda78d4f8", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Part 1: Tidy Data with Univariate Pivoting\n", "\n", "Consider the Lord of the Rings data. Run the code cell below to see the first few lines of the tibble." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "fc425e5b52ea398a18b8c4b41a977d5f", "grade": false, "grade_id": "cell-0700841dc6bbc1ab", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "print(lotr, n = 5)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "970c6985ca4739678bf69aefe8bf0b57", "grade": false, "grade_id": "cell-31b880187338213a", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 1.1\n", "Widen the data so that we see the words spoken by each race, by puttting race as its own column. Store the answer in `answer1.1`.\n", "\n", "```\n", "(answer1.1 <- lotr %>%\n", " FILL_THIS_IN(names_from = FILL_THIS_IN,\n", " values_from = FILL_THIS_IN))\n", "```\n", "\n", "Your `answer1.1` should look something like this (full tibble not always shown):\n", "\n", "![answer1.1](https://github.com/UBC-STAT/stat545.stat.ubc.ca/raw/master/content/data/worksheet_04a/answer1.1.png)\n", "\n", "_Sidenote:_ Putting a variable assignment in parenthesis will not only assign the value to the variable, but also print to console. Normally when you assign a variable, you do not get to see the value of the variable. This is a helpful tip so you can see what you are storing!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1ab0480bddce3c237ca8ceabda584592", "grade": false, "grade_id": "cell-69d0326c263cea22", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer1.1)\n", "arrange_all(answer1.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a6c2a143efafaf1b021ad60c192912bf", "grade": true, "grade_id": "cell-cdf83fd8542d1a5f", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 1.1\", {\n", " expect_true(all(c(\"Film\", \"Gender\", \"Elf\", \"Hobbit\", \"Man\") %in% names(answer1.1)))\n", " expect_equal(nrow(answer1.1), 6L)\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "61fe3546bb35cd6ef6a982ec56ccab61", "grade": false, "grade_id": "cell-e605b348e5f2d218", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 1.2\n", "Re-lengthen the wide `lotr` data (i.e. `answer1.1`) from Question 1.1 above. Store your answer in `answer1.2`.\n", "\n", "**Hint:** the resulting data frame should appear to be the _almost the same_ as the original! (No need to reorder the columns)\n", "\n", "```\n", "(answer1.2 <- answer1.1 %>% \n", " FILL_THIS_IN(cols = c(-FILL_THIS_IN, -FILL_THIS_IN), \n", " names_to = FILL_THIS_IN, \n", " values_to = FILL_THIS_IN))\n", "```\n", "\n", "Your `answer1.2` should look something like this (full tibble not always shown):\n", "\n", "![answer1.2](https://github.com/UBC-STAT/stat545.stat.ubc.ca/raw/master/content/data/worksheet_04a/answer1.2.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "2e1f1fc9f62b4b402e47efd3f111f415", "grade": false, "grade_id": "cell-dd9ee0af42a31d96", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer1.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "ba65f1619691dd0883207930d7529723", "grade": true, "grade_id": "cell-9ca00a4e9e835c5b", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 1.2\", {\n", " expect_true(all(c(\"Film\", \"Gender\", \"Race\", \"Words\") %in% names(answer1.2)))\n", " expect_equal(nrow(answer1.2), 18L)\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "1d235deabbbe07b887cc0fffba83e4b4", "grade": false, "grade_id": "cell-8ca39a3585dfa195", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## QUESTION 1.3\n", "\n", "Using the `gapminder` dataset: what's the relationship between Canada's GDP per capita and the United Kingdom's? First, produce a tidy tibble from the `gapminder` tibble to address this question. Store your tibble in a variable named `question1.3`. Do not rename any columns.\n", "\n", "_Food for thought_: After tidying the data for this problem, we should be able to make a scatterplot of Canada's GDP per capita against the UK's. But, doing so for the original `gapminder` dataset would be difficult. \n", "\n", "```\n", "answer1.3 <- gapminder %>% \n", " filter(FILL_TIHS_IN) %>% \n", " pivot_FILL_THIS_IN(FILL_THIS_IN)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "30c01b29f1bfb6dc14d77ce71ca81f0f", "grade": false, "grade_id": "cell-b6f4518297129ee5", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer1.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "9bd351322766a0399c4f7f6787ffbc43", "grade": true, "grade_id": "cell-02bda51b10d57f91", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 1.3\", {\n", " expect_true(\"Canada\" %in% names(answer1.3))\n", " expect_true(\"United Kingdom\" %in% names(answer1.3))\n", " expect_equal(nrow(answer1.3), 12L)\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "00150ba251618ea412837c501720bc09", "grade": false, "grade_id": "cell-74b6ea9dd9cd3970", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Part 2: Tidy Data with Multivariate Pivoting\n", "\n", "Congratulations, you’re getting married! In addition to the wedding, you’ve decided to hold two other events: a day-of brunch and a day-before round of golf. You’ve made a guestlist of attendance so far, along with food preference for the food events (wedding and brunch).\n", "\n", "Run the code cell below to see the first few rows of the `guest` data frame." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "6c0ceeb14101994f6cb6b2f6917c8e33", "grade": false, "grade_id": "cell-f7d65824be40afab", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "head(guest)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "903f6bae5debe519f1d6f34b3907fc8b", "grade": false, "grade_id": "cell-a5e314661b36e3ae", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 2.1\n", "Put `meal` and `attendance` as their own columns, with the events living in a new column. Store your answer in `answer2.1`.\n", "\n", "```\n", "(answer2.1 <- guest %>% \n", " FILL_THIS_IN(cols = c(-FILL_THIS_IN, -FILL_THIS_IN), \n", " names_to = c(FILL_THIS_IN, FILL_THIS_IN),\n", " names_sep = FILL_THIS_IN))\n", "``` \n", "\n", "**Hint**: Read the possible values for `names_to` in the corresponding documentation of the function you choose!\n", "\n", "Your `answer2.1` should look something like this (full tibble not always shown):\n", "\n", "![answer2.1](https://github.com/UBC-STAT/stat545.stat.ubc.ca/raw/master/content/data/worksheet_04a/answer2.1.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "576225da011ab4f9b458ba61188e43b9", "grade": false, "grade_id": "cell-aa203ff65e28a500", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer2.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "51c71ea8965025a90cc9c1721cd56f07", "grade": true, "grade_id": "cell-2f380c98eb6f4b92", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 2.1\", {\n", " expect_true(all(c(\"party\", \"name\", \"event\", \"meal\", \"attendance\") %in% names(answer2.1)))\n", " expect_equal(nrow(answer2.1), 90L)\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "adf4e3105c833d3d03df080dca520ef6", "grade": false, "grade_id": "cell-4bf2cab5040a45a4", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 2.2\n", "Use `tidyr::separate()` to split the `name` in `answer2.1` into two columns: `first_name` and `last_name`. Store your answer in `answer2.2`.\n", "\n", "```\n", "(answer2.2 <- answer2.1 %>% \n", " FILL_THIS_IN(FILL_THIS_IN, into = c(FILL_THIS_IN, FILL_THIS_IN), sep=FILL_THIS_IN))\n", "``` \n", "\n", "Your `answer2.2` should look something like this (full tibble not always shown):\n", "\n", "![answer2.2](https://github.com/UBC-STAT/stat545.stat.ubc.ca/raw/master/content/data/worksheet_04a/answer2.2.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "a141fa3dd02872dad3abb48996c6488b", "grade": false, "grade_id": "cell-fb46ea7012910edc", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer2.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "6d525ffe304f04e87c526536481d882b", "grade": true, "grade_id": "cell-0cba9b35e5add034", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 2.2\", {\n", " expect_true(all(c(\"party\", \"first_name\", \"last_name\", \"event\", \"meal\", \"attendance\") %in% names(answer2.2)))\n", " expect_equal(nrow(answer2.2), 90L)\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "34737497de0642add774fec990ace9a1", "grade": false, "grade_id": "cell-4c777f9f3d24eca8", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "### Question 2.3\n", "Re-unite `first_name` and `last_name` in `answer2.2` back into `name` using `tidyr::unite()`. Store your answer in `answer2.3`.\n", "\n", "```\n", "(answer2.3 <- answer2.2 %>%\n", " FILL_THIS_IN(col = FILL_THIS_IN, c(FILL_THIS_IN, FILL_THIS_IN), sep = FILL_THIS_IN))\n", "``` \n", "\n", "Your `answer2.3` should look something like this (full tibble not always shown):\n", "\n", "![answer2.3](https://github.com/UBC-STAT/stat545.stat.ubc.ca/raw/master/content/data/worksheet_04a/answer2.3.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "14c67d43a1fbf9224f7475204e3e3a31", "grade": false, "grade_id": "cell-955ac07a83ed5e20", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer2.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "0580ffc204b8a2f0ed1536fc28be89f8", "grade": true, "grade_id": "cell-ee5e83f00c565943", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 2.3\", {\n", " expect_true(all(c(\"party\", \"name\", \"event\", \"meal\", \"attendance\") %in% names(answer2.3)))\n", " expect_equal(nrow(answer2.3), 90L)\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "224de48f5e65871ef86d851fc62e8cff", "grade": false, "grade_id": "cell-defaffa374701c1e", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 2.4\n", "\n", "Which parties still have a \"PENDING\" attendance status for all of its members and all of the events? Your answer should be a vector of party ID's (not a tibble). Store your answer in `answer2.4`.\n", "\n", "**Hint**: use `answer2.1` as a starting point. Use `pull()` to access a column as a vector.\n", "\n", "```\n", "answer2.4 <- answer2.1 %>% \n", " group_by(FILL_THIS_IN) %>% \n", " summarize(all_pending = all(FILL_THIS_IN == \"PENDING\")) %>%\n", " filter(all_pending) %>%\n", " FILL_THIS_IN(party)\n", "``` " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "85aa81d0f3d83dd9e8cee385fdad9e4e", "grade": false, "grade_id": "cell-caf4c538c01ad19c", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer2.4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "5f7a7c23acc4392e0b7be37930e4e404", "grade": true, "grade_id": "cell-7bd8a19402a8d8f2", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 2.4\", {\n", " expect_equal(digest(unclass(answer2.4)),\"f13a65bc5c8793a2cad1415aad7dff93\")\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "9db5418934d896cd26b5ad0a19336fea", "grade": false, "grade_id": "cell-7ce9d28a450b8dae", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 2.5\n", "Which parties still have a \"PENDING\" attendance status for all of its members, for the wedding event only? Your answer should be a vector of party ID's (not a tibble). Store your answer in `answer2.5`.\n", "\n", "**Hint**: Use `pull()` to access a column as a vector.\n", "\n", "```\n", "answer2.5 <- guest %>% \n", " group_by(FILL_THIS_IN) %>% \n", " summarize(pending_wedding = all(FILL_THIS_IN == \"PENDING\")) %>%\n", " filter(FILL_THIS_IN) %>%\n", " FILL_THIS_IN(party)\n", "``` " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "6580817ac5be3a4339f884e8eda9beae", "grade": false, "grade_id": "cell-69089bca0ebe5314", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer2.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "9587f1290ceaed34a63737d4f94ed87c", "grade": true, "grade_id": "cell-cbecf06071548ada", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 2.5\", {expect_equal(digest(unclass(answer2.5)), \"f13a65bc5c8793a2cad1415aad7dff93\")})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "5c138d79a4449ec5e49c3a361a7a84d3", "grade": false, "grade_id": "cell-51cc3f9c26a52de9", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Part 3: The Model-Fitting Paradigm in R\n", "\n", "**Overview**\n", "\n", "1. Fit a linear regression model to life expectancy (\"Y\") from year (\"X\") by filling in the formula. Notice what appears as the output.\n", "2. Use the `unclass` function to uncover the object's true nature: a list." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "295aa1acad253535e684ae1adc47a992", "grade": false, "grade_id": "cell-c9bb114babd6a9b0", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 3.1\n", "First, create a subset of the `gapminder` dataset containing only the country of `France`. Store your answer in `answer3.1`.\n", "\n", "```\n", "(answer3.1 <- gapminder %>%\n", " FILL_THIS_IN(FILL_THIS_IN == FILL_THIS_IN))\n", "``` " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "024305056f38d3c8a22c3ccddbb6fb3a", "grade": false, "grade_id": "cell-eac384fd230e3178", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer3.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "470e7f27ccbb2ec3ace2385337a4609f", "grade": true, "grade_id": "cell-5d65551ed7f404dd", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 3.1\", {expect_equal(digest(unclass(answer3.1)), \"a6125bcbb25047b7a8c932acbb1f2250\")})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "d5d926752629890d00af8375f7545f54", "grade": false, "grade_id": "cell-9d3e1e0a4ba92267", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "### Question 3.2\n", "\n", "> Fit a linear regression model to life expectancy (\"Y\") from year (\"X\") by filling in the formula\n", "\n", "Now, using the `lm()` function we will create the linear model. Store your answer in `answer3.2`.\n", "\n", "```\n", "(answer3.2 <- FILL_THIS_IN(FILL_THIS_IN ~ FILL_THIS_IN, answer3.1)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "d7d88f3472df2967ef641d309d7f1d60", "grade": false, "grade_id": "cell-bd086872f7c88b6f", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer3.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a4f38b8888914854770c240ee9cfc8b8", "grade": true, "grade_id": "cell-c32548a2940be4dd", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 3.2\", {\n", " expect_known_hash(round(coef(answer3.2), 4), \"e7375b4c2683882feb9d7215f6929f69\")\n", " expect_known_hash(answer3.2$terms, \"9c71cfd9974bfbc8e160b1a31936d137\")\n", "})\n", "print(\"Success!\")" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "e9bc0726c2d192ce8fe869704596cdd6", "grade": false, "grade_id": "cell-0d8ce437120f0108", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 3.3\n", "\n", "We are interested in the modeling results around the modeling period which starts at year 1952. To get a meaniningful \"interpretable\" intercept we can use the `I()` function.\n", "\n", "Use `I()` to make the intercept so that the \"beginning\" of our dataset (1952) corresponds to '0' in the model. This makes all the years in the data set relative to the first year, 1952.\n", "\n", "Store your answer in `answer3.3`.\n", "\n", "```\n", "answer3.3 <- FILL_THIS_IN(FILL_THIS_IN ~ I(FILL_THIS_IN-1952), answer3.1)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "00e146e90647bde49727c7e4bd0cb2ca", "grade": false, "grade_id": "cell-de478c362e72ff34", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer3.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a223e427e3e1b0a392fae644b992053c", "grade": true, "grade_id": "cell-f63ce0b4b32b1c75", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 3.3\", {\n", " expect_known_hash(round(coef(answer3.3), 4), \"6a83f591b39b440f2a699dbee2c23468\")\n", " expect_known_hash(answer3.3$terms, \"f7d8f19ef010f5932ba9b321f3f88282\")\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "92a2a472efe2710d3ef8c31aeba6737b", "grade": false, "grade_id": "cell-e7c56c14d223147e", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 3.4\n", "Use the `unclass()` function to take a look at how the `lm()` object actually looks like. Store your answer in `answer3.4`.\n", "\n", "```\n", "answer3.4 <- FILL_THIS_IN(answer3.3)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "2b5c4fdd351a5538db72befde4e6922e", "grade": false, "grade_id": "cell-314348ceb2597207", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer3.4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "99ea6d9b8e892a171fd12d4acc246826", "grade": true, "grade_id": "cell-bb41acc822c171ff", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 3.4\", {\n", " expect_known_hash(round(answer3.4$coefficients, 4), \"6a83f591b39b440f2a699dbee2c23468\")\n", " expect_known_hash(class(answer3.4), \"086ebc4c59c08c43e75bae74f1e16897\")\n", " expect_known_hash(answer3.4$terms, \"f7d8f19ef010f5932ba9b321f3f88282\")\n", " \n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "877d8e1ea3781fafe097497db1d5d7ce", "grade": false, "grade_id": "cell-4952813e2dd68ae0", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Part 4: Producing Tidy Tibbles with broom\n", "\n", "## Question 4.1\n", "\n", "Apply `broom::tidy()` to `answer3.3`. Store your answer in `answer4.1`.\n", "\n", "```\n", "answer4.1 <- FILL_THIS_IN(answer3.3)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "2f527796ad9d3684c086f91009e5bb27", "grade": false, "grade_id": "cell-ac2a0fd0bdb6d050", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer4.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "f6847b669d83ff1c50c0013471229d65", "grade": true, "grade_id": "cell-bcc656e0f1096684", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 4.1\", {\n", " expect_known_hash(dimnames(answer4.1), \"b7e5db66048ee1c33cb090078dc59103\")\n", " expect_known_hash(answer4.1[[1]], \"6736fedebcaa557ef7a78f8db206000f\")\n", " expect_known_hash(round(answer4.1[[3]], 4), \"004727aa166a650b3f55c2c11f6be257\")\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "f29fbfd40877c43a1ac25ccd6e579688", "grade": false, "grade_id": "cell-9b91f64b029dc78f", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 4.2\n", "Apply `broom::augment()` to `answer3.3`. Store your answer in `answer4.2`.\n", "\n", "```\n", "answer4.2 <- FILL_THIS_IN(answer3.3)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "ae09ff412dabf99718319b599d19adc6", "grade": false, "grade_id": "cell-11acddecce607fde", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer4.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "9f4b7426811ba605bcc3d8617b64d06e", "grade": true, "grade_id": "cell-6f28d2ff5f8a394c", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 4.2\", {\n", " expect_known_hash(round(answer4.2$.fitted, 4), \"3a2e6323312173544d30c4dc75d5b604\")\n", " expect_known_hash(round(answer4.2$.std.resid, 4), \"b2796c0f920703cb5066fcda716be2e7\")\n", "})" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "930307c8120dd6a552f486130ae0ee8a", "grade": false, "grade_id": "cell-10c482e46bfb6925", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Question 4.3\n", "Apply `broom::glance()` to `answer3.3`. Store your answer in `answer4.3`.\n", "\n", "```\n", "answer4.3 <- FILL_THIS_IN(answer3.3)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "565342293534ce3f622e45ede3d9d8db", "grade": false, "grade_id": "cell-6fc7f2ff75650015", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# your code here\n", "fail() # No Answer - remove if you provide an answer\n", "print(answer4.3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "88422bae6debf6dbd3c1595e90eec4dd", "grade": true, "grade_id": "cell-0c6c4d5f47779e91", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "test_that(\"Question 4.3\", {\n", " expect_known_hash(dimnames(answer4.3), \"78044903eb403fb9220d796ac127297c\")\n", " expect_known_hash(round(answer4.3[[2]], 4), \"a3ec6ee89f16b0783571e8f9e26c9ef5\")\n", " expect_known_hash(round(answer4.3[[4]], 4), \"f95d29661fc511c6a038ae4e06b9ea02\")\n", "})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Attribution\n", "\n", "Thanks to Diana Lin, Icíar Fernández Boyano, David Kepplinger, and Vincenzo Coia for putting this worksheet together." ] } ], "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": "4.1.2" } }, "nbformat": 4, "nbformat_minor": 4 }