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. The lab specifications can be found here.

To clarify which is which: R is the name of the programming language itself and RStudio is a convenient interface for working with R . Think of it like this:

  • R is like a car’s engine,
  • RStudio is like a car’s dashboard
The difference between R and Rstudio
The difference between R and Rstudio

Exercise 1

Knit your RMarkdown file and observe the result in the form of a knitted report. Now change the following in your RMarkdown (a) In the YAML, identify the author’s name and replace it with your name. (b) Replace the date to show today’s date. (c) Identify the sentence “The lab specifications can be found here,” and turn the word “here” into a link, (d) Add an image of the car dashboard (a metaphor for RStudio) and the car engine (a metaphor for R).

The arbuthnot dataset

The Arbuthnot data set refers to the work of 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.

Exercise 2

Load the tidyverse library into your environment, and then load the arbuthnot dataset. Now use the glimpse function to display its content, and the dim function to display its dimensions. Write down the variables associated with the dataset and the number of observations in your dataset.

# Load the tidyverse library below
library(tidyverse)

# load the data into the environment
arbuthnot <- read_csv("data/arbuthnot.csv")

# Use `glimpse` function to inspect your dataset below
glimpse (arbuthnot)
## Rows: 82
## Columns: 3
## $ year  <dbl> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639…
## $ boys  <dbl> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5366…
## $ girls <dbl> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4784…
# Use the `dim` function to see the dimensions of your dataset 

dim(arbuthnot)
## [1] 82  3

Answer: There are 3 variables in the arbuthnot dataset. The names of the variables are: year, boys and girls. The dataset consists of 82 observations (rows).

Exercise 3

What command would you use to extract just the counts of girls baptized? Try it out in the console!

Answer: To extract the counts of baptized girls, we need to use the following command: “arbuthnot$girls”.

Exercise 4

Create the plot and answer the following: is there an apparent trend in the number of girls baptized over the years? How would you describe it?

# Insert code below
ggplot(data = arbuthnot, aes(x = year, y = girls)) + 

  geom_line()

Answer: There is an overall increase in baptisms for girls from around 4500 to almost 8000 between 1620 and 1700, but between 1640 and 1660 there is a clear reduction to around 3000.

Exercise 5

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

# Insert code below
arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)

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

ggplot(data = arbuthnot, aes(x = year, y = boy_to_girl_ratio)) + 
  geom_line()

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

Answer: While there is a lot of variation year by year, there seems to be an overall trend towards a lower boy to girl ratio in this timeframe. As the starting rate is around 1.08, this means the number of girls and boys being born are becoming more similar.

Thepresent dataset

Answer the following questions with the present dataset:

Exercise 6

What years are included in this data set? What are the dimensions of the data frame? What are the variable names? How many observations are in your data?

# We already loaded the tidyverse library
# so we do not need to do this again. 
# However, we do need to load the new data into the environment
# Insert code below

present <- read_csv("data/present.csv")
## Rows: 63 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): year, boys, girls
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# What years are included in the dataset?
present |> 
  summarize(
    min = min(year),
    max = max(year)
    )
# What are the dimensions of the dataset?
dim(present)
## [1] 63  3
# What are the variable names? 

names(present)
## [1] "year"  "boys"  "girls"

Answer: 1940-2002 are included; the variables are called boys,girls,year; there are 63 observations

Exercise 7

How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?

# Insert code below
ggplot(
  data = present
  ) + 
  geom_point(aes(x = year, y = girls), color = "black") + 
  geom_point(aes(x = year, y = boys), color = "green") + 
  labs(y = "count")

ggplot(
  data = arbuthnot
  ) + 
  geom_point(aes(x = year, y = girls), color = "black") + 
  geom_point(aes(x = year, y = boys), color = "green") + 
  labs(y = "count")

Answer: There is a drastic increase in the number of children baptized. in the 17th century the number ranged between 3000 and 9000. In the 20th century it ranged between 1.25 and 2.25 million.

Exercise 8

Make a plot that displays the proportion of boys born over time. What do you see? Does Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S.? What explains the differences you observe? Include a plot in your response.

# Insert code below
present <- present %>%
  mutate(total = boys + girls)

present <- present %>%
  mutate(boy_to_total_ratio = boys / total)

ggplot(data = present, aes(x = year, y = boy_to_total_ratio)) + 
  geom_line()

Answer: There is a decrease of the proportion of boys, but overall there remains more boys than girls being baptized.

Exercise 9

In which year did we see the largest total number of births in the U.S.?

# Insert code below
present |> 
   arrange (desc(total))

Answer: In the year 1961 4.269 million children were born in the US