library(tidyverse)
library(openintro)
library(tidyverse)
library(openintro)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
Exploring Dr. Arbuthnot’s Baptism Records.
data('arbuthnot', package = 'openintro')Viewing the entire data frame:
Viewing only the dimensions of the data frame and the variables contained the data frame:
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…
head(arbuthnot)## # A tibble: 6 × 3
## year boys girls
## <int> <int> <int>
## 1 1629 5218 4683
## 2 1630 4858 4457
## 3 1631 4422 4102
## 4 1632 4994 4590
## 5 1633 5158 4839
## 6 1634 5035 4820
Examining exclusively the boys column:
arbuthnot$boys## [1] 5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 5366 5518 5470 5460 4793
## [16] 4107 4047 3768 3796 3363 3079 2890 3231 3220 3196 3441 3655 3668 3396 3157
## [31] 3209 3724 4748 5216 5411 6041 5114 4678 5616 6073 6506 6278 6449 6443 6073
## [46] 6113 6058 6552 6423 6568 6247 6548 6822 6909 7577 7575 7484 7575 7737 7487
## [61] 7604 7909 7662 7602 7676 6985 7263 7632 8062 8426 7911 7578 8102 8031 7765
## [76] 6113 8366 7952 8379 8239 7840 7640
Exploring only the girls:
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
Descriptive statistics:
summary(arbuthnot)## year boys girls
## Min. :1629 Min. :2890 Min. :2722
## 1st Qu.:1649 1st Qu.:4759 1st Qu.:4457
## Median :1670 Median :6073 Median :5718
## Mean :1670 Mean :5907 Mean :5535
## 3rd Qu.:1690 3rd Qu.:7576 3rd Qu.:7150
## Max. :1710 Max. :8426 Max. :7779
Data Visualization.
?ggplotHistorical trend in boys baptism
ggplot(data = arbuthnot, aes(x=year, y=boys)) +
geom_point()ggplot(data = arbuthnot, aes(x = year, y = boys))+
geom_line()ggplot(data = arbuthnot, aes(x=year, y= girls)) +
geom_line()ggplot(data = arbuthnot, aes(x =year, y =girls))+
geom_point()Interpreting the findings: Both graphics reveal a continuous increase in the numbers of children who receive baptism over the years. The observed trends and patterns are similar for both boys and girls for the period covered by the data frame.
Calculating the ratio of newborn boys over newborn girls in 1629:
5218/4683## [1] 1.114243
Finding the ratio of newborns that are boys for each year of the data set:
arbuthnot <-arbuthnot %>%
mutate (boy_to_girl_ratio = boys/girls)
print(arbuthnot)## # A tibble: 82 × 4
## year boys girls boy_to_girl_ratio
## <int> <int> <int> <dbl>
## 1 1629 5218 4683 1.11
## 2 1630 4858 4457 1.09
## 3 1631 4422 4102 1.08
## 4 1632 4994 4590 1.09
## 5 1633 5158 4839 1.07
## 6 1634 5035 4820 1.04
## 7 1635 5106 4928 1.04
## 8 1636 4917 4605 1.07
## 9 1637 4703 4457 1.06
## 10 1638 5359 4952 1.08
## # ℹ 72 more rows
Finding the proportion of the newborns that are boys for all years simultaneously
arbuthnot <-arbuthnot %>%
mutate ( boy_ratio = boys/arbuthnot$boys +arbuthnot$girls)
print(arbuthnot)## # A tibble: 82 × 5
## year boys girls boy_to_girl_ratio boy_ratio
## <int> <int> <int> <dbl> <dbl>
## 1 1629 5218 4683 1.11 4684
## 2 1630 4858 4457 1.09 4458
## 3 1631 4422 4102 1.08 4103
## 4 1632 4994 4590 1.09 4591
## 5 1633 5158 4839 1.07 4840
## 6 1634 5035 4820 1.04 4821
## 7 1635 5106 4928 1.04 4929
## 8 1636 4917 4605 1.07 4606
## 9 1637 4703 4457 1.06 4458
## 10 1638 5359 4952 1.08 4953
## # ℹ 72 more rows
Plot of the proportion of boys born over time:
library(ggplot2)
ggplot(data = arbuthnot, aes(x = year , y =boy_to_girl_ratio ))+
geom_point()ggplot(data = arbuthnot, aes(x = year , y =boy_to_girl_ratio ))+
geom_line()
Interpreting the findings: One notices the absence of
the linear, upward, and mostly one directional patterns observed while
examining the trends in baptism over years. Linearity has been replaced
by a zigzag line when when exploring the trends via
ratio/proportion.
Finding whether the number of births of boys outnumber that of girls in each year:
arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
glimpse(arbuthnot)## Rows: 82
## Columns: 6
## $ year <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637…
## $ boys <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703…
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457…
## $ boy_to_girl_ratio <dbl> 1.114243, 1.089971, 1.078011, 1.088017, 1.065923, 1.…
## $ boy_ratio <dbl> 4684, 4458, 4103, 4591, 4840, 4821, 4929, 4606, 4458…
## $ more_boys <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE…
Finding the max and the minimum amount of boys born in a year within the arbuthnot data frame:
arbuthnot %>%
summarise(min = min(boys), max = max(boys))## # A tibble: 1 × 2
## min max
## <int> <int>
## 1 2890 8426
Loading and exploring the data frame: Finding the dimension and variables using the glimpse function:
data('present', package = "openintro")
glimpse(present)## Rows: 63
## Columns: 3
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950…
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 1404587, 1691220, 1…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 1330869, 1597452, 1…
Finding the variables name using the head() function
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
Descriptive statistics:
summary(present)## year boys girls
## Min. :1940 Min. :1211684 Min. :1148715
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711404
## Median :1971 Median :1924868 Median :1831679
## Mean :1971 Mean :1885600 Mean :1793915
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538
## Max. :2002 Max. :2186274 Max. :2082052
Finding the max and the minimum amount of boys born in a year with present data frame:
present %>%
summarise(min = min(boys), max = max(boys))## # A tibble: 1 × 2
## min max
## <dbl> <dbl>
## 1 1211684 2186274
The counts in present (above) data frame are of much bigger magnitude in scope when compared with the nab data set (below)
## # A tibble: 1 × 2
## min max
## <int> <int>
## 1 2890 8426
Proportion of newborns that are boys:
present <- present %>%
mutate(boy_ratio = boys/present$boys|present$girls)
glimpse(present)## Rows: 63
## Columns: 4
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, …
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 1404587, 169122…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 1330869, 159745…
## $ boy_ratio <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, …
present <-present %>%
mutate (boy_to_girl_ratio = boys/girls)
print(present)## # A tibble: 63 × 5
## year boys girls boy_ratio boy_to_girl_ratio
## <dbl> <dbl> <dbl> <lgl> <dbl>
## 1 1940 1211684 1148715 TRUE 1.05
## 2 1941 1289734 1223693 TRUE 1.05
## 3 1942 1444365 1364631 TRUE 1.06
## 4 1943 1508959 1427901 TRUE 1.06
## 5 1944 1435301 1359499 TRUE 1.06
## 6 1945 1404587 1330869 TRUE 1.06
## 7 1946 1691220 1597452 TRUE 1.06
## 8 1947 1899876 1800064 TRUE 1.06
## 9 1948 1813852 1721216 TRUE 1.05
## 10 1949 1826352 1733177 TRUE 1.05
## # ℹ 53 more rows
Graphics/data visualization.
ggplot(data = present, aes(x =year, y =boys))+
geom_point()ggplot(data = present, aes(x =year, y =boys))+
geom_line()ggplot(data = present, aes(x =year, y =girls))+
geom_line()ggplot(data = present, aes(x = year , y =boy_to_girl_ratio ))+
geom_point()ggplot(data = present, aes(x = year , y =boy_to_girl_ratio ))+
geom_line()Plots showing the proportion of boys born over time in the US from 1940 to 2002:
ggplot(data = present,aes(x= year, y= boy_to_girl_ratio )) +
geom_point()ggplot(data = present,aes(x= year, y= boy_to_girl_ratio )) +
geom_line()Interpreting the graphics:
The present data frame reveals a downside trend in newborns in the U.S. during the second half of the 20th century (1940 -2002). The descending curve of the plot is also in sharp opposition with the ascending curve observed in London during the 18th century as illustrated by the arbuthnot data frame.
Do newborn boys outnumbers girls per year in present?
summary(present)## year boys girls boy_ratio
## Min. :1940 Min. :1211684 Min. :1148715 Mode:logical
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711404 TRUE:63
## Median :1971 Median :1924868 Median :1831679
## Mean :1971 Mean :1885600 Mean :1793915
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538
## Max. :2002 Max. :2186274 Max. :2082052
## boy_to_girl_ratio
## Min. :1.046
## 1st Qu.:1.050
## Median :1.051
## Mean :1.051
## 3rd Qu.:1.053
## Max. :1.059
Year with the highest total number of births: was 2002
present %>%
arrange(desc(total))## # A tibble: 63 × 5
## year boys girls boy_ratio boy_to_girl_ratio
## <dbl> <dbl> <dbl> <lgl> <dbl>
## 1 2002 2057979 1963747 TRUE 1.05
## 2 2001 2057922 1968011 TRUE 1.05
## 3 2000 2076969 1981845 TRUE 1.05
## 4 1999 2026854 1932563 TRUE 1.05
## 5 1998 2016205 1925348 TRUE 1.05
## 6 1997 1985596 1895298 TRUE 1.05
## 7 1996 1990480 1901014 TRUE 1.05
## 8 1995 1996355 1903234 TRUE 1.05
## 9 1994 2022589 1930178 TRUE 1.05
## 10 1993 2048861 1951379 TRUE 1.05
## # ℹ 53 more rows