HW4: lm

Download and Instructions

Here’s your fourth set of koans. This set teaches lm(), the function that fits a line of best fit and reports its equation. You’ve been drawing that line since HW3 with geom_smooth(method = "lm"). Now you get the two numbers behind it, where the line starts and how steep it is, and learn to read them.

There are four koans. The first two are review, built around one question: what predicts how much a student in this class sleeps? You answer it with dplyr tables in the first koan and with ggplot2 pictures in the second. The third koan asks the same question with lm(), so you can put the equation of each line next to its picture. The fourth koan shrinks the data down to five rows that you type in yourself, so that every number lm() reports is one you can check by hand: the intercept, the slope, a prediction for each row, and the error (residual) for each row.

Getting the data

As in HW2 and HW3, the class survey is not inside the zip file. Koans 1 to 3 read it from the web, so you need an internet connection while you work. The URL is posted on Canvas, in the Homework 4: lm assignment (it’s the same URL as before). The first koan tells you exactly where to paste it. Please don’t share the URL, and don’t post the data anywhere. Koan 4 doesn’t use the survey at all.

Setting up

First, download koans4.zip by clicking the link in the first paragraph. Unzip it, and then rename the unzipped folder so that it has your name in it, for example koans4_lastname. Do this before you open anything. Your Downloads folder already holds the earlier koans folders and their zip files, and they all look alike. Renaming right away means you always know which folder you are editing and, when you finish, which folder to zip and upload. The original koans4.zip stays in Downloads with the unedited koans in it, so if you skip this step it is easy to upload the wrong one.

Then open the project by double-clicking koans4.Rproj inside your renamed folder (not the individual .R files). If nothing happens when you double-click koans4.Rproj, open RStudio and navigate to File > Open Project, and then find koans4.Rproj inside koans4_lastname in your Downloads folder.

You set up the keyboard shortcut for running tests in HW1. If you’re on a new computer, redo that step:

Macs: Tools > Modify keyboard shortcuts > Run a test file > Cmd Shift T > Apply

Windows: Tools > Modify keyboard shortcuts > Run a test file > Ctrl Shift T > Apply

Now open koan_01_predicting_sleep.R in the R/ folder and paste the URL from Canvas into read_csv(), exactly as you did in HW2 and HW3. Run the file top to bottom before you run any tests. Koans 2 and 3 each begin with the line source("R/koan_01_predicting_sleep.R"), which re-runs the first koan, so whenever you restart R, that one line brings the data back.

Then work through the remaining koans in order: koan_02_sleep_with_plots.R, koan_03_lines_of_best_fit.R, and koan_04_by_hand.R. They work the same way as HW2 and HW3: each file teaches with live examples that you should run as you read, and under each question header you’ll find a copy of the most recent example, which you edit into your answer. Run your code in the console (Ctrl/Cmd + Enter) and look at the result, save your edits (Ctrl/Cmd + S), and press Ctrl+Shift+T (Cmd+Shift+T on Mac) to run the tests.

One thing to watch for in koans 3 and 4. Every dplyr verb takes the data as its first argument, which is why the pipe works. lm() takes the formula first and the data second, so piping into it needs the placeholder: class_data |> lm(sleep_hours ~ hours_working, data = _). If you leave out data = _, R complains that it “cannot coerce class ‘formula’ to a data.frame”, and the tests report the question as a failure with that message.

The tests for an lm() answer check the model, not its printout: which variable is the outcome, which is the predictor, and whether the intercept and slope match the line fit to the data you loaded. The tests for the plots work as in HW3, by looking inside the plot object. The same two cautions carry over: a question you haven’t edited yet is reported as skipped, and a syntax error in one file stops the tests for all four, with a message that points at the line.

Turning it in

Before you zip anything, run the tests one last time and read the output. Every koan must be at 100% passing, with no errors. Do not upload a submission that stops with an error, and do not upload one with failing or skipped tests. My grader runs the same tests you do, so if the tests stop on your computer they stop on mine too, and questions the tests cannot reach get no credit even if the code underneath them is right. If the tests will not run, that is the thing to fix, not something to upload and hope about. Also check that your edits are saved: the tests read the files on disk, so if the test output does not match what you see in the editor, you have unsaved changes.

When every test passes, go to your Downloads folder and find the folder you renamed, koans4_lastname. Compress that folder into a zip file (not the original koans4.zip you downloaded, and not one of the earlier homework folders). Finally, upload that zip file to Canvas for me to grade. The koans are due before class on Monday 10/26. If you get stuck, come to office hours or email to make an appointment.

Check Your Understanding

The twenty multiple choice questions below cover the two new koans in this set: writing a formula and piping into lm(), reading the intercept and slope it reports, and getting a prediction and a residual for every row. They don’t use the survey. Instead, each quiz works from a tiny tibble called quiz (five rows, two columns: hours studied and score) whose lm() output is printed at the top of every section, so you can answer every question from what’s on the screen.


Formulas and lm()


Predictions and Residuals