Advice for Math 150:

Below is a lab that walks through using R within RStudio. If you are unfamiliar with R/RStudio, I highly recommend going through the lab so that you can complete the assignments. If you are familiar with R/RStudio, feel free to skip to the bottom of this page to compete the assignment due on Monday.

One other way to learn more R (either in lieu of or in addition to this activity) is to work through some swirl. For example, try this:

library(swirl)
options(swirl_courses_dir = "Swirl", swirl_data_dir = "Swirl")
install_course("R Programming")
swirl( )

Then select R Programming and, for example, “12: Looking at Data” and run through the commands.

The RStudio Interface

The goal of this lab is to introduce you to R and RStudio, which you’ll be using throughout the course both to learn the statistical concepts discussed in the course and to analyze real data and come to informed conclusions. To clarify which is which: R is the name of the programming language itself and RStudio is a convenient interface.

As the labs progress, you are encouraged to explore beyond what the labs dictate; a willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.

Go ahead and launch RStudio at https://rstudio.campus.pomona.edu/. You should see a window that looks like the image shown below.


The panel on the lower left is where the action happens. It’s called the console. Every time you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running. Below that information is the prompt. As its name suggests, this prompt is really a request: a request for a command. Initially, interacting with R is all about typing commands and interpreting the output. These commands and their syntax have evolved over decades (literally) and now provide what many users feel is a fairly natural way to access data and organize, describe, and invoke statistical computations.

The panel in the upper right contains your workspace as well as a history of the commands that you’ve previously entered.

Any plots that you generate will show up in the panel in the lower right corner. This is also where you can browse your files, access help, manage packages, etc.

R Packages

R is an open-source programming language, meaning that users can contribute packages that make our lives easier, and we can use them for free. For this lab, and many others in the future, we will use the following R packages:

  • dplyr: for data wrangling
  • ggplot2: for data visualization
  • oilabs: for data and custom functions with the OpenIntro labs

If these packages are not already available in your R environment (they will be on the server, so you won’t need to do this if you are using the server!), install them by typing the following three lines of code into the console of your RStudio session, pressing the enter/return key after each one. Note that you can check to see which packages (and which versions) are installed by inspecting the Packages tab in the lower right panel of RStudio.

install.packages("dplyr")
install.packages("ggplot2")

You may need to select a server from which to download; any of them will work. Next, you need to load these packages in your working environment. We do this with the library function. Run the following three lines in your console. Remember to run the library command each time you run a markdown file.

library(dplyr)
library(ggplot2)

Note that you only need to install packages once, but you need to load (i.e., library command) them each time you relaunch RStudio.

Creating a reproducible lab report

We will be using R Markdown to create reproducible lab reports. See the following videos describing why and how:

Why use R Markdown for Lab Reports?

Using R Markdown for Lab Reports in RStudio

Going forward you should refrain from typing your code directly in the console, and instead type any code (final correct answer, or anything you’re just trying out) in the R Markdown file and run the chunk using either the Run button on the chunk (green sideways triangle) or by highlighting the code and clicking Run on the top right corner of the R Markdown editor. If at any point you need to start over, you can Run All Chunks above the chunk you’re working in by clicking on the down arrow in the code chunk.

Dr. Arbuthnot’s Baptism Records

To get you started, run the following command to load the data.

arbuthnot <- readr::read_csv("http://pages.pomona.edu/~jsh04747/courses/math150/arbuthnot.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   year = col_double(),
##   boys = col_double(),
##   girls = col_double()
## )

You can do this by

  • clicking on the green arrow at the top right of the code chunk in the R Markdown (Rmd) file, or
  • putting your cursor on this line, and hit the Run button on the upper right corner of the pane, or
  • hitting Ctrl-Shift-Enter, or
  • typing the code in the console.

This command instructs R to load some data: the Arbuthnot baptism counts for boys and girls. You should see that the workspace area in the upper right hand corner of the RStudio window now lists a data set called arbuthnot that has 82 observations on 3 variables. As you interact with R, you will create a series of objects. Sometimes you load them as we have done here, and sometimes you create them yourself as the byproduct of a computation or some analysis you have performed.

The Arbuthnot data set refers to Dr. John Arbuthnot, an 18th century physician, writer, and mathematician. He was interested in the ratio of newborn boys to newborn girls, so he gathered the baptism records for children born in London for every year from 1629 to 1710. We can view the data by typing its name into the console.

arbuthnot

However printing the whole dataset in the console is not that useful. One advantage of RStudio is that it comes with a built-in data viewer. Click on the name arbuthnot in the Environment pane (upper right window) that lists the objects in your workspace. This will bring up an alternative display of the data set in the Data Viewer (upper left window). You can close the data viewer by clicking on the x in the upper left hand corner.

What you should see are four columns of numbers, each row representing a different year: the first entry in each row is simply the row number (an index we can use to access the data from individual years if we want), the second is the year, and the third and fourth are the numbers of boys and girls baptized that year, respectively. Use the scrollbar on the right side of the console window to examine the complete data set.

Note that the row numbers in the first column are not part of Arbuthnot’s data. R adds them as part of its printout to help you make visual comparisons. You can think of them as the index that you see on the left side of a spreadsheet. In fact, the comparison to a spreadsheet will generally be helpful. R has stored Arbuthnot’s data in a kind of spreadsheet or table called a data frame.

You can see the dimensions of this data frame as well as the names of the variables and the first few observations by typing:

glimpse(arbuthnot)

This command should output the following

## Observations: 82
## Variables: 4
## $ X1    <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ year  <dbl> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638…
## $ boys  <dbl> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359…
## $ girls <dbl> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952…

We can see that there are 82 observations and 3 variables in this dataset. The variable names are year, boys, and girls. At this point, you might notice that many of the commands in R look a lot like functions from math class; that is, invoking R commands means supplying a function with some number of arguments. The glimpse command, for example, took a single argument, the name of a data frame.

Some Exploration

Let’s start to examine the data a little more closely. We can access the data in a single column of a data frame separately using a command like

arbuthnot$boys

This command will only show the number of boys baptized each year. The dollar sign basically says “go to the data frame that comes before me, and find the variable that comes after me”.

  1. What command would you use to extract just the counts of girls baptized? Try it!

Notice that the way R has printed these data is different. When we looked at the complete data frame, we saw 82 rows, one on each line of the display. These data are no longer structured in a table with other variables, so they are displayed one right after another. Objects that print out in this way are called vectors; they represent a set of numbers. R has added numbers in [brackets] along the left side of the printout to indicate locations within the vector. For example, 5218 follows [1], indicating that 5218 is the first entry in the vector. And if [43] starts a line, then that would mean the first number on that line would represent the 43rd entry in the vector.

Data visualization

R has some powerful functions for making graphics. We can create a simple plot of the number of girls baptized per year with the command

qplot(x = year, y = girls, data = arbuthnot)

The qplot() function (meaning “quick plot”) considers the type of data you have provided it and makes the decision to visualize it with a scatterplot. The plot should appear under the Plots tab of the lower right panel of RStudio. Notice that the command above again looks like a function, this time with three arguments separated by commas. The first two arguments in the qplot() function specify the variables for the x-axis and the y-axis and the third provides the name of the data set where they can be found. If we wanted to connect the data points with lines, we could add a fourth argument to specify the geometry that we’d like.

qplot(x = year, y = girls, data = arbuthnot, geom = "line")

You might wonder how you are supposed to know that it was possible to add that fourth argument. Thankfully, R documents all of its functions extensively. To read what a function does and learn the arguments that are available to you, just type in a question mark followed by the name of the function that you’re interested in. Try the following.

?qplot

Notice that the help file replaces the plot in the lower right panel. You can toggle between plots and help files using the tabs at the top of that panel.

  1. Is there an apparent trend in the number of girls baptized over the years? How would you describe it? (To ensure that your lab report is comprehensive, be sure to include the code needed to make the plot as well as your written interpretation.)

R as a big calculator

Now, suppose we want to plot the total number of baptisms. To compute this, we could use the fact that R is really just a big calculator. We can type in mathematical expressions like

5218 + 4683

to see the total number of baptisms in 1629. We could repeat this once for each year, but there is a faster way. If we add the vector for baptisms for boys to that of girls, R will compute all sums simultaneously.

arbuthnot$boys + arbuthnot$girls

What you will see are 82 numbers (in that packed display, because we aren’t looking at a data frame here), each one representing the sum we’re after. Take a look at a few of them and verify that they are right.

Adding a new variable to the data frame

We’ll be using this new vector to generate some plots, so we’ll want to save it as a permanent column in our data frame.

arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)

The %>% operator is called the piping operator. It takes the output of the previous expression and pipes it into the first argument of the function in the following one. To continue our analogy with mathematical functions, x %>% f(y) is equivalent to f(x, y).

A note on piping: Note that we can read these three lines of code as the following:

“Take the arbuthnot dataset and pipe it into the mutate function. Mutate the arbuthnot data set by creating a new variable called total that is the sum of the variables called boys and girls. Then assign the resulting dataset to the object called arbuthnot, i.e. overwrite the old arbuthnot dataset with the new one containing the new variable.”

This is equivalent to going through each row and adding up the boys and girls counts for that year and recording that value in a new column called total.

Where is the new variable? When you make changes to variables in your dataset, click on the name of the dataset again to update it in the data viewer.

You’ll see that there is now a new column called total that has been tacked on to the data frame. The special symbol <- performs an assignment, taking the output of one line of code and saving it into an object in your workspace. In this case, you already have an object called arbuthnot, so this command updates that data set with the new mutated column.

We can make a plot of the total number of baptisms per year with the command

qplot(x = year, y = total, data = arbuthnot, geom = "line")

Similarly to how we computed the total number of births, we can compute the ratio of the number of boys to the number of girls baptized in 1629 with

5218 / 4683

or we can act on the complete columns with the expression

arbuthnot <- arbuthnot %>%
  mutate(boy_to_girl_ratio = boys / girls)

We can also compute the proportion of newborns that are boys in 1629

5218 / (5218 + 4683)

or this may also be computed for all years simultaneously and append it to the dataset:

arbuthnot <- arbuthnot %>%
  mutate(boy_ratio = boys / total)

Note that we are using the new total variable we created earlier in our calculations.

  1. Now, generate a plot of the proportion of boys born over time. What do you see?

Tip: If you use the up and down arrow keys, you can scroll through your previous commands, your so-called command history. You can also access it by clicking on the history tab in the upper right panel. This will save you a lot of typing in the future.

Finally, in addition to simple mathematical operators like subtraction and division, you can ask R to make comparisons like greater than, >, less than, <, and equality, ==. For example, we can ask if boys outnumber girls in each year with the expression

arbuthnot <- arbuthnot %>%
  mutate(more_boys = boys > girls)

This command add a new variable to the arbuthnot dataframe containing the values of either TRUE if that year had more boys than girls, or FALSE if that year did not (the answer may surprise you). This variable contains a different kind of data than we have encountered so far. All other columns in the arbuthnot data frame have values that are numerical (the year, the number of boys and girls). Here, we’ve asked R to create logical data, data where the values are either TRUE or FALSE. In general, data analysis will involve many different kinds of data types, and one reason for using R is that it is able to represent and compute with many of them.

With all of the new variables you’ve created, you might want to look at your dataframe one more time.

glimpse(arbuthnot)

To turn in:

Creating a reproducible lab report

We will be using R Markdown to create reproducible lab reports. See the following video describing how to get started with creating reports:

Basic R Markdown with an OpenIntro Lab

You are welcome to use the oilabs structure or to just use standard RMarkdown files.


In the previous few pages, you practiced using R for Arbuthnot’s baptism data. Your current assignment involves using the games data from your text to create your first R Markdown file. Remember, save as either html or pdf, print, and turn in on Monday.

# you can figure out the correct location of the file by using the "Import Dataset" icon at the top.
# Then navigate to the right place, import the data, then copy the code into your markdown file.
games1 <- read_csv("~/Dropbox/teaching/math150/PracStatCD/Data Sets/Chapter 02/CSV Files/C2 Games1.csv")

The data are stored in a data frame called games1.

  1. Chapter 2, A1: For this study, identify the units, the population for which conclusions can be drawn, the explanatory variable, and the response variable.

  2. Chapter 2, A2: Is this study an experiment or an observational study? Explain.

  3. Chapter 2, A3: The researchers hoped to determine if distracting colors influence college students’ response times when playing a computerized game. Write out in words and symbols appropriate null and alternative hypotheses. Let \(\mu_1\) represent the true mean response time of the color group and let \(\mu_2\) represent the true mean response time of the standard group. Use a two-sided alternative hypothesis for this question.

  4. Chapter 2, A4: Create an individual value plot or a boxplot of the Games1 data from this study. Describe the graph. For example, does it look as if the groups have equal means or equal standard deviations? Are there any unusual observations in the data set? Calculate the mean and standard deviation of the color distracter responses, \(\overline{y}_1\) and \(s_1\), as well as the mean and standard deviation of the standard game responses, \(\overline{y}_2\) and \(s_2\). You can use any plots with which you are familiar. I’m going to probably give you examples in the ggplot2 / qplot family of plotting functions. Either of the plots below are fine for you to use (you don’t need both).

qplot(x = Type, y=Time, data=games1, geom="boxplot")
ggplot(games1, aes(x=Type, y=Time)) + geom_boxplot()
  1. Chapter 2, A5: Assume we have two very small populations that can be written as: \(y_{1,1}=15, y_{1,2} = 17, y_{1,3} = 16, y_{2,1} = 11, y_{2,2}=9, y_{2,3}=10\). Find \(\mu_1, \mu_2, \epsilon_{1,1}, \epsilon_{1,3}, \mbox{ and } \epsilon_{2,1}\).
# use R as a calculator
  1. Chapter 2, A6: Use the game study and the data in the file Games1 to identify \(y_{1,12}, y_{2,12}, \hat{\epsilon}_{1,12}, \hat{\epsilon}_{2,12}, n_1, n_2\). (Note that we are now asking about a sample and not a population.)
str(games1)

games1 %>%
  group_by(Type) %>%
  summarize(mean(Time), sd(Time), n())

This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel from a lab written by Mark Hansen of UCLA Statistics.


Resources for learning R and working in RStudio

That was a short introduction to R and RStudio, but we will provide you with more functions and a more complete sense of the language as the course progresses.

In this course we will be using R packages called dplyr for data wrangling and ggplot2 for data visualization. If you are googling for R code, make sure to also include these package names in your search query. For example, instead of googling “scatterplot in R”, google “scatterplot in R with ggplot2”.

These cheatsheets may come in handy throughout the semester:

Chester Ismay has put together a resource for new users of R, RStudio, and R Markdown here. It includes examples showing working with R Markdown files in RStudio recorded as GIFs.

Note that some of the code on these cheatsheets may be too advanced for this course, however the majority of it will become useful throughout the semester.