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.
To load the data enter the following command at the R prompt (i.e. right after >
on the console).
source("http://www.openintro.org/stat/data/arbuthnot.R")
This command instructs R to access the OpenIntro website and fetch some data: the Arbuthnot baptism counts for boys and girls. You should see that the workspace area in the upper righthand 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. Note that because you are accessing data from the web, this command (and the entire assignment) will work in a computer lab, in the library, or in your dorm room; anywhere you have access to the Internet.
Click on the arbuthnot
data set in the Environment tab to view the data. 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 by typing:
dim(arbuthnot)
## [1] 82 3
This command should output [1] 82 3
, indicating that there are 82 rows and 3 columns (we’ll get to what the [1]
means in a bit), just as it says next to the object in your workspace. You can see the names of these columns (or variables) by typing:
names(arbuthnot)
## [1] "year" "boys" "girls"
You should see that the data frame contains the columns 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 dim
and names
commands, for example, each took a single argument, the name of a data frame.
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.
For the following exercises insert your answers (code + narrative) in your markdown document.
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.
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
plot(arbuthnot$girls ~ arbuthnot$year)
By default, R creates a scatterplot with each x,y pair indicated by an open circle. The plot itself should appear under the Plots tab of the lower right panel of RStudio. In the command above the tilde (~) stands for “versus”, and we always plot y
versus x
.
If we wanted to connect the data points with lines, we could add a third argument, the letter l
for line.
plot(arbuthnot$girls ~ arbuthnot$year, type = "l")
And a fourth argument for the color of the line.
plot(arbuthnot$girls ~ arbuthnot$year, type = "l", col = "blue")
You might wonder how you are supposed to know that it was possible to add that third 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.
?plot
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.
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 and 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.
We can also store these values in the data frame:
arbuthnot$total = arbuthnot$boys + arbuthnot$girls
Therefore, we can make a plot of the total number of baptisms per year with the command
plot(arbuthnot$total ~ arbuthnot$year, type = "l")
This time, note that we left out the names of the first two arguments. We can do this because the help file shows that the default for plot
is for the first argument to be the x-variable and the second argument to be the y-variable.
First, let’s find which record is an exact duplicate of another one:
duplicated(arbuthnot$total)
This command returns 82 values of either TRUE
if that year has a duplicated number of total births as a past year, or FALSE
if not. This output shows a different kind of data than we have considered so far. In the arbuthnot
data frame our values 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.
Parsing through the many TRUE
s and FALSE
s is cumbersome, and would be even more difficult if the dataset were larger. Instead we can find out the index (row) number of that record:
which(duplicated(arbuthnot$total))
We can also view the value of total births in the year with that index number:
arbuthnot$total[which(duplicated(arbuthnot$total))]
Let’s store this value so that we can refer to it later:
dup_val = arbuthnot$total[which(duplicated(arbuthnot$total))]
We can then find out which element in the arbuthnot$total
vector equals this value:
arbuthnot$total == dup_val
The ==
is a logical operator, reads as “if equal to”. Basically we’re asking R to report as TRUE
/FALSE
whether each element in the vector arbuthnot$total
is equal to the dup_val
.
We can nest the above line of code inside the which
function once again and find out which rows have this value:
which(arbuthnot$total == dup_val)
Let’s assume that the the first entry was correct and the later was a data entry error. The most naive way to impute the value for this year would be to plug in the average value of the years before and after it.
First, let’s make a note of the index number of the later year with the duplicate value:
dup_index = which(arbuthnot$total == dup_val)
dup_index
to refer to the elements in arbuthnot$total
with indices one before and one after. Average the values of these elements, and replace the duplicated value in arbuthnot$total[dup_index]
with this average.Similarly to how we computed the proportion of boys, 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 vectors with the expression
arbuthnot$boys / arbuthnot$girls
The proportion of newborns that are boys
5218 / (5218 + 4683)
or this may also be computed for all years simultaneously:
arbuthnot$boys / (arbuthnot$boys + arbuthnot$girls)
Note that with R as with your calculator, you need to be conscious of the order of operations. Here, we want to divide the number of boys by the total number of newborns, so we have to use parentheses. Without them, R will first do the division, then the addition, giving you something that is not a proportion.
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$boys > arbuthnot$girls
Earlier you recreated some of the displays and preliminary analysis of Arbuthnot’s baptism data. Your assignment involves repeating these steps, but for present day birth records in the United States. Load up the present day data with the following command.
source("http://www.openintro.org/stat/data/present.R")
The data are stored in a data frame called present
.
What years are included in this data set? What are the dimensions of the data frame and what are the variable or column names?
How do these counts compare to Arbuthnot’s? Are they on a similar scale?
Make a plot that displays the boy-to-girl ratio for every year in the data set. What do you see? Does Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S.? Include the plot in your response.
In what year did we see the most total number of births in the U.S.? You can refer to the help files or the R reference card http://cran.r-project.org/doc/contrib/Short-refcard.pdf to find helpful commands.
Plot the number of girls born vs. year. Next (in the same code chunk) use the lines
function, which works just like the plot
function but allows you to overlay a line on the existing plot, to plot the number of boys born vs. year. Use a different color than those used in the lab (other than black and blue). This means you will need to find out what color names are acceptable parameters in the col
argument.
These data come from a report by the Centers for Disease Control http://www.cdc.gov/nchs/data/nvsr/nvsr53/nvsr53_20.pdf. Check it out if you would like to read more about an analysis of sex ratios at birth in the United States.
When you are done, export the HTML file you created, and upload it to Sakai under the appropriate assignment. To export, check the box next to the HTML file, click on More, and then Export. This will download the file (likely to your Desktop or your Downloads folder, depending on your operating system and configurations). You can then upload this file to Sakai.