The online versions of the worked examples are different from those in the book:

We will update these online examples periodically to maintain compatibility with R upgrades, improve the code, and possibly add more material. It’s also worth checking our personal website and Github for minor updates, suggestions, discussion, etc.

The scripts should be fine for R version 4.3.1.

A note on the R code

Each example is based on an Rmarkdown notebook, which has two important features:

The scripts rely on two external R scripts:

The data files

We’ve created our own working files from the raw data from each example. When that’s the case, you’ll also see a link to our file, and generally in the scripts.

We’ll load those files, rather than importing directly from a journal repository, Dryad, etc. That’s largely for convenience, and because most files require a little housekeeping before loading into a dataframe. Often that’s just some tidying of the first few rows and renaming of variables, but it can be more extensive. Excel files are quite common, and they often have a few header rows. We could deal with that as part of the script for each example, but for teaching purposes, it’s cleaner to just be able to load a file where the variable names match the rest of the script exactly.

In a few cases, the analyses in the published papers used a more complex subset of a larger dataset that was provided online. There, such as for Box 13.9, we’ve included an R code chunk replicating the data subset in the paper. In Box 13.9, that code chunk was kindly provided by the authors.

In future, we may provide the example data files as a complete set, either as a zip or an R package.

Directory structures

The pages for each of the worked examples are in one folder, and the data, R scripts, and documents/images are in separate folders. These folders are shared with other material, so the directory structure is slightly unusual. The Rmarkdown files, if you download them, link to three “parent” folders, R, data, and media. If you download the examples as a zipped document, that structure will be created, and you’ll just need to use the examples folder as the session directory.

If you download individual files, make sure you recreate that structure: main directory has example.html and four folders:

Chapter 2

Chapter 3

This chapter has one example box; it uses summary data from Chapter 2, which you can get from Box 2.2.

Chapter 4

No worked examples.

Chapter 5

No worked examples. The code to replicate the exploratory procedures is available on request

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14

Chapter 15

Chapter 16

Chapter 17