library(tidyverse)
library(openintro)data('arbuthnot', package='openintro')glimpse(arbuthnot)## Rows: 82
## Columns: 3
## $ year <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639…
## $ boys <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5366…
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4784…
arbuthnot$girls## [1] 4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 4784 5332 5200 4910 4617
## [16] 3997 3919 3395 3536 3181 2746 2722 2840 2908 2959 3179 3349 3382 3289 3013
## [31] 2781 3247 4107 4803 4881 5681 4858 4319 5322 5560 5829 5719 6061 6120 5822
## [46] 5738 5717 5847 6203 6033 6041 6299 6533 6744 7158 7127 7246 7119 7214 7101
## [61] 7167 7302 7392 7316 7483 6647 6713 7229 7767 7626 7452 7061 7514 7656 7683
## [76] 5738 7779 7417 7687 7623 7380 7288
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()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.)]
Using geom_smooth, we are able to creating a linear model and see that throughout the years (even though there is a dip around the 1650’s and 1700’s) there is an upwards trend towards the baptism of girls throughout the years.
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()+geom_smooth()## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Now, generate a plot of the proportion of boys born over time. What do you see?
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)arbuthnot <- arbuthnot %>%
mutate(boy_to_girl_ratio = boys / girls)arbuthnot <- arbuthnot %>%
mutate(boy_ratio = boys / total)arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)arbuthnot## # A tibble: 82 × 7
## year boys girls total boy_to_girl_ratio boy_ratio more_boys
## <int> <int> <int> <int> <dbl> <dbl> <lgl>
## 1 1629 5218 4683 9901 1.11 0.527 TRUE
## 2 1630 4858 4457 9315 1.09 0.522 TRUE
## 3 1631 4422 4102 8524 1.08 0.519 TRUE
## 4 1632 4994 4590 9584 1.09 0.521 TRUE
## 5 1633 5158 4839 9997 1.07 0.516 TRUE
## 6 1634 5035 4820 9855 1.04 0.511 TRUE
## 7 1635 5106 4928 10034 1.04 0.509 TRUE
## 8 1636 4917 4605 9522 1.07 0.516 TRUE
## 9 1637 4703 4457 9160 1.06 0.513 TRUE
## 10 1638 5359 4952 10311 1.08 0.520 TRUE
## # … with 72 more rows
ggplot(data = arbuthnot, aes(x = year, y =boy_ratio )) +
geom_line()+geom_smooth()## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?
data('present', package='openintro')
head(present)## # A tibble: 6 × 3
## year boys girls
## <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715
## 2 1941 1289734 1223693
## 3 1942 1444365 1364631
## 4 1943 1508959 1427901
## 5 1944 1435301 1359499
## 6 1945 1404587 1330869
tail(present)## # A tibble: 6 × 3
## year boys girls
## <dbl> <dbl> <dbl>
## 1 1997 1985596 1895298
## 2 1998 2016205 1925348
## 3 1999 2026854 1932563
## 4 2000 2076969 1981845
## 5 2001 2057922 1968011
## 6 2002 2057979 1963747
Assuming the years are ordered, we can see it begins at 1940 and ends at 2002. We can confirm with the range function.
range(present$year)## [1] 1940 2002
dim(df) tells us the dimensions of the dataframe. It is a 63x3 dataframe (63 rows, 3 columns). The column names are year, boys, girls.
dim(present)## [1] 63 3
How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?
mean(arbuthnot$total)## [1] 11441.74
present <- present %>%
mutate(total = boys + girls)mean(present$total)## [1] 3679515
Numerically, we see there is an increased magnitude between the two datasets. The mean of baptisms throughout the ~80 year range of Arbuthnot’s data was about 11,000.
The mean of baptisms throughout the ~60 year range of present data is about 370,000.
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.? Include the plot in your response. Hint: You should be able to reuse your code from Exercise 3 above, just replace the dataframe name.
present <- present %>%
mutate(boy_ratio = boys / total)
ggplot(data = present, aes(x = year, y = boy_ratio )) +
geom_line()+geom_smooth()## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
In Arbuthnot’s data, we saw an upward trend in baptisms, but here in present data, we see a downward trend. It is important to take note that the magnitude (sheer number of male baptisms) is different between the two datasets, so the results might not be conclusive. Both ratios are still about 50%.
In what year did we see the most total number of births in the U.S.? Hint: First calculate the totals and save it as a new variable. Then, sort your dataset in descending order based on the total column. You can do this interactively in the data viewer by clicking on the arrows next to the variable names. To include the sorted result in your report you will need to use two new functions: arrange (for sorting). We can arrange the data in a descending order with another function: desc (for descending order). The sample code is provided below.
present %>%
arrange(desc(total))## # A tibble: 63 × 5
## year boys girls total boy_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1961 2186274 2082052 4268326 0.512
## 2 1960 2179708 2078142 4257850 0.512
## 3 1957 2179960 2074824 4254784 0.512
## 4 1959 2173638 2071158 4244796 0.512
## 5 1958 2152546 2051266 4203812 0.512
## 6 1962 2132466 2034896 4167362 0.512
## 7 1956 2133588 2029502 4163090 0.513
## 8 1990 2129495 2028717 4158212 0.512
## 9 1991 2101518 2009389 4110907 0.511
## 10 1963 2101632 1996388 4098020 0.513
## # … with 53 more rows
We saw the most number of births in 1961.