Chapter 1 Preliminaries

1.1 Download and Install R

This book shows examples done in R, so you need to begin by installing R if you do not already have it. The home for R is http://r-project.org. There is a ton of stuff here, but I call your attention to the links on the left side. Toward the bottom there is a link for Manuals. If you click there you have a choice of several formats for the R manuals. An Introduction to R can be helpful if you are new to R. You probably don’t want any of the others unless you are a fairly advanced user of R. Some of the FAQs could be helpful, but you need to wade through a lot of material to find what you’re looking for.

CRAN is the “Comprehensive R Archive Network.” Basically it is a bunch of sites that mirror the main R program files for downloading. Back at the http://r-project.org home page, click on CRAN link on the left (it’s right at the top).

Now you get presented with a long list of places that mirror the R download material. I generally scroll down to the USA sites and pick (click on) Iowa State. I’m sure any of the others would work just as well, but ISU is first in the list USA site list. It might be fun to download R from Pamukkale University in Denizli, Turkey, but I just stay local. (Of course, if you are in Turkey …)

Once you get to the mirror site it will say “The Comprehensive R Archive Network” at the top. The very first thing is a set of links for downloading R for Linux, MacOS, or Windows. For Mac, you just want to get the “latest release” (which as of July 26, 2021 is R-4.1.0.pkg). For Windows, first click on “base”; then you will have the opportunity to download the latest release of R.

In either case, get the installer for your system from CRAN, download it, and run it.

1.2 RStudio

Note: RStudio is apparently changing its name to Posit sometime in October 2022. The new website will be http://posit.co

RStudio is a wrapper for base R that adds lots of useful functionality. I use it, and I recommend that you use it too.

Go to http://rstudio.com/products/rstudio/download/ and click on the DOWNLOAD button for the free version of RStudio Desktop. This will scroll you down to another download button that is the version recommended for your system. (Further down the page it lets you can download the MacOS version to Windows and vice versa, but, really?)

So get your version of RStudio and install it.

1.3 Packages

Base R can be extended with packages (libraries) that contain additional functions and data. There are literally tens of thousands of R packages available; fortunately, we won’t need all of them.

  1. Start up RStudio.

  2. I believe the default will bring up a window with four panes. (You can change these around, so I don’t want to sound too definite.) My window looks like this: RStudio Screen Shot Within each pane are usually a handful of tabs.

    • The lower left pane has a “Console” tab. This is where you type R commands, so you will do a lot of your work here.
    • The upper right tab has a “History” pane that lets you quickly see your previous commands. You can also move commands from there to the Console window.
    • The lower right pane has three tabs you will use a lot: “Plots,” “Packages,” and “Help.”
  3. Click on the Packages tab in the lower right pane.

  4. You should see an “Install” button in the Packages tab. Click on it.

  5. There should be a popup dialog box. You may be asked to choose a CRAN mirror/repository. If so, choose one.

  6. There should be a box where you can type in the name of a package, and there should be a check box for “Install dependencies.” Make sure that the check box is checked. Many packages will only work if you have other packages that they depend on, so clicking this will save you a lot of trouble.

  7. Type mvtnorm in the package name box; it should look something like this: Package Install Screen Shot Click the “Install” button, and watch the magic happen (we hope).

  8. You need to install the cfcdae package (Companion to a First Course in Design and Analysis of Experiments). It includes a few additional functions (many of which are just slight variations on functionality available in base R or other packages), but what is really useful is that it contains all the data sets from the text A First Course in Design and Analysis of Experiments, second edition. Be sure to install dependencies.
    There is also a second package bcfcdae (Bayesian Companion to A First Course …). You will only need it if you plan to do Bayesian analysis. bcfcdae has quite a few dependencies, and some of them (rstan in particular) are quite large and will take a while. Installing bcfcdae is easy, but some users have had difficulty installing the dependencies.

  9. You will need a few additional packages as we go through the examples. You can install them later, but you might as well install them now while you are installing everything else:

    • nlme
    • lme4
    • car
    • perm
    • effects
    • tseries
    • FrF2
    • RLRsim
    • rsm
    • conf.design
    • emmeans
    • multcomp
  10. Advisory: if you update R to a new version, you may need to reinstall your packages.

1.4 Loading a package

Installing a package is like buying a book and putting on your bookshelf. You know it’s there, but you can’t read it when it’s on the shelf. Anytime you want to read the book you must pick it up and open it. For a package, we do something similar. If you want to use the cfcdae package, give the command in R
library(cfcdae)
Of course, this only works if you have previously installed the package (just like you can’t pick up and read a book that you don’t have on your bookshelf).

You only need to buy the book once; then you can pick it up to read it multiple times. You only need to install a package once; but you need to load it into every R session where you want to use it. You may assume that the first command in virtually every one of the R examples given here will be library(cfcdae), although that might not always be explicit.

There is also a GUI way to load packages in RStudio. Go the the Packages tab in the lower right pane. This will show a list of all installed packages with a little check box to the left. Checking the box loads the package. In the image above, you can see that I have the base package loaded and a lot of unloaded packages.

1.5 Keeping Results

You often don’t want your R work to disappear when you exit R. You might want to turn in homework or report the results of an experiment, so you’ll need to keep some of the output. The simplest, easiest(?), old-fashioned way to do this is via copy and paste.

  • Highlight and copy the text output from the R command window that you want to use and paste it into a MSWord or other editing document. Easy as pie. Remember to use a monospaced font such as Courier to show R output. That is so that columns line up.
  • In the Plots tab, there is an Export drop down menu. One of the options is to copy the plot to the clipboard. From there you can paste it into a document (MSWord or whatever).
  • Alternatively, the Export drop down also allows you to save a plot to a file (either pdf or png/jpg/tiff/etc image format). These can be viewed, embedded, or printed separately.

1.6 Keeping results the modern way

The modern way to save your results is using RMarkdown. Markdown is a system for formatting documents (e.g., commands to start a section, change font, set up a table, etc.). RMarkdown also allows you to embed R commands into the document. When you process the RMarkdown file, it typesets the text, but it also runs the R commands and saves the output and plots into the finished document. Thus you have:

  • No more cutting and pasting.
  • No more accidentally copying the wrong analysis.
  • No more worrying about changing the document if the data get updated.
  • A good step toward reproducible research.

RMarkdown can output to several formats including html, MSWord, and LaTeX. This book was produced in a version of Rmarkdown called bookdown.

To learn RMarkdown, start at http://rmarkdown.rstudio.com/lesson-1.html. There are also many helpful web pages out there that you can find with a simple search.

There is a new software application called Quarto that seems very similar to Rmarkdown. It’s main claim to fame is that it works with other things beyond R (like Python). You might want to look into it, but I will continue with Rmarkdown for now.