library(tinytex)
library(tidyverse)
library(openintro)
###Arbuthnot
data('arbuthnot', package='openintro')
arbuthnot
## # A tibble: 82 × 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
## 7 1635 5106 4928
## 8 1636 4917 4605
## 9 1637 4703 4457
## 10 1638 5359 4952
## # … with 72 more rows
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…
$boys arbuthnot
## [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
$girls arbuthnot
## [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()
There is an apparent flucturating trend in the number of girls baptized over the years. Between 1650 and 1658 the number of girls baptised dropped tremendously below 3000. Around 1659 to after 1700 the number of girls baptised began to increase again reaching close to 8000. You can see a sudden dip in 1670 and 1740 which picked up quickly.
# Insert code for Exercise 2 here
ggplot(data= arbuthnot, aes(x = year, y = girls)) +
geom_line()
5218 + 4683
## [1] 9901
$boys + arbuthnot$girls arbuthnot
## [1] 9901 9315 8524 9584 9997 9855 10034 9522 9160 10311 10150 10850
## [13] 10670 10370 9410 8104 7966 7163 7332 6544 5825 5612 6071 6128
## [25] 6155 6620 7004 7050 6685 6170 5990 6971 8855 10019 10292 11722
## [37] 9972 8997 10938 11633 12335 11997 12510 12563 11895 11851 11775 12399
## [49] 12626 12601 12288 12847 13355 13653 14735 14702 14730 14694 14951 14588
## [61] 14771 15211 15054 14918 15159 13632 13976 14861 15829 16052 15363 14639
## [73] 15616 15687 15448 11851 16145 15369 16066 15862 15220 14928
<- arbuthnot %>%
arbuthnot mutate(total = boys + girls)
arbuthnot
## # A tibble: 82 × 4
## year boys girls total
## <int> <int> <int> <int>
## 1 1629 5218 4683 9901
## 2 1630 4858 4457 9315
## 3 1631 4422 4102 8524
## 4 1632 4994 4590 9584
## 5 1633 5158 4839 9997
## 6 1634 5035 4820 9855
## 7 1635 5106 4928 10034
## 8 1636 4917 4605 9522
## 9 1637 4703 4457 9160
## 10 1638 5359 4952 10311
## # … with 72 more rows
ggplot(data = arbuthnot, aes(x = year, y = total)) +
geom_line()
5218 / 4683
## [1] 1.114243
<- arbuthnot %>%
arbuthnot mutate(boy_to_girl_ratio = boys / girls)
arbuthnot
## # A tibble: 82 × 5
## year boys girls total boy_to_girl_ratio
## <int> <int> <int> <int> <dbl>
## 1 1629 5218 4683 9901 1.11
## 2 1630 4858 4457 9315 1.09
## 3 1631 4422 4102 8524 1.08
## 4 1632 4994 4590 9584 1.09
## 5 1633 5158 4839 9997 1.07
## 6 1634 5035 4820 9855 1.04
## 7 1635 5106 4928 10034 1.04
## 8 1636 4917 4605 9522 1.07
## 9 1637 4703 4457 9160 1.06
## 10 1638 5359 4952 10311 1.08
## # … with 72 more rows
5218 / (5218 + 4683)
## [1] 0.5270175
<- arbuthnot %>%
arbuthnot mutate(boy_ratio = boys / total)
arbuthnot
## # A tibble: 82 × 6
## year boys girls total boy_to_girl_ratio boy_ratio
## <int> <int> <int> <int> <dbl> <dbl>
## 1 1629 5218 4683 9901 1.11 0.527
## 2 1630 4858 4457 9315 1.09 0.522
## 3 1631 4422 4102 8524 1.08 0.519
## 4 1632 4994 4590 9584 1.09 0.521
## 5 1633 5158 4839 9997 1.07 0.516
## 6 1634 5035 4820 9855 1.04 0.511
## 7 1635 5106 4928 10034 1.04 0.509
## 8 1636 4917 4605 9522 1.07 0.516
## 9 1637 4703 4457 9160 1.06 0.513
## 10 1638 5359 4952 10311 1.08 0.520
## # … with 72 more rows
I see extreme flucturation over the years of boys born over time. Each year there is a sudden decrease and a sudden increase over the years.
# Insert code for Exercise 3 here
ggplot(data = arbuthnot, aes(x = year, y = boy_ratio)) +
geom_line()
<- 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
%>%
arbuthnot summarize(min = min(boys), max = max(boys))
## # A tibble: 1 × 2
## min max
## <int> <int>
## 1 2890 8426
###Present
data('present', package='openintro')
present
## # A tibble: 63 × 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
## 7 1946 1691220 1597452
## 8 1947 1899876 1800064
## 9 1948 1813852 1721216
## 10 1949 1826352 1733177
## # … with 53 more rows
The years that are included is 1940 to 2002. The dimensions of the data frame is 63 rows and 3 columns. The variable (column) names are year, boys, and girls.
# Insert code for Exercise 4 here
range(present$year)
## [1] 1940 2002
Continue for Exercise 4
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…
No, the the counts compare to Arbuthnot’s count are not of a similar magnitute. The total counts of boys and girls for the present dataset is over 4,000,000 compare to the total counts of boys and girls for the Arbuthnot’s dataset.
# Insert code for Exercise 5 here
$boys + present$girls present
## [1] 2360399 2513427 2808996 2936860 2794800 2735456 3288672 3699940 3535068
## [10] 3559529 3554149 3750850 3846986 3902120 4017362 4047295 4163090 4254784
## [19] 4203812 4244796 4257850 4268326 4167362 4098020 4027490 3760358 3606274
## [28] 3520959 3501564 3600206 3731386 3555970 3258411 3136965 3159958 3144198
## [37] 3167788 3326632 3333279 3494398 3612258 3629238 3680537 3638933 3669141
## [46] 3760561 3756547 3809394 3909510 4040958 4158212 4110907 4065014 4000240
## [55] 3952767 3899589 3891494 3880894 3941553 3959417 4058814 4025933 4021726
Continue for Exercise 5
<- present %>%
present mutate(total = boys + girls)
present
## # A tibble: 63 × 4
## year boys girls total
## <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399
## 2 1941 1289734 1223693 2513427
## 3 1942 1444365 1364631 2808996
## 4 1943 1508959 1427901 2936860
## 5 1944 1435301 1359499 2794800
## 6 1945 1404587 1330869 2735456
## 7 1946 1691220 1597452 3288672
## 8 1947 1899876 1800064 3699940
## 9 1948 1813852 1721216 3535068
## 10 1949 1826352 1733177 3559529
## # … with 53 more rows
I see a decrease in ratio of boys to girls born over time. Even though there is a decrease in ratio of boy to girl born over time, Arbuthnot’s observation about boys being born in greater poportion than girls hold up in the U.S.
<- present %>%
present mutate(boy_to_girl_ratio = boys / girls)
present
## # A tibble: 63 × 5
## year boys girls total boy_to_girl_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399 1.05
## 2 1941 1289734 1223693 2513427 1.05
## 3 1942 1444365 1364631 2808996 1.06
## 4 1943 1508959 1427901 2936860 1.06
## 5 1944 1435301 1359499 2794800 1.06
## 6 1945 1404587 1330869 2735456 1.06
## 7 1946 1691220 1597452 3288672 1.06
## 8 1947 1899876 1800064 3699940 1.06
## 9 1948 1813852 1721216 3535068 1.05
## 10 1949 1826352 1733177 3559529 1.05
## # … with 53 more rows
Continue for Exercise 6
<- present %>%
present mutate(boy_ratio = boys / total)
present
## # A tibble: 63 × 6
## year boys girls total boy_to_girl_ratio boy_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399 1.05 0.513
## 2 1941 1289734 1223693 2513427 1.05 0.513
## 3 1942 1444365 1364631 2808996 1.06 0.514
## 4 1943 1508959 1427901 2936860 1.06 0.514
## 5 1944 1435301 1359499 2794800 1.06 0.514
## 6 1945 1404587 1330869 2735456 1.06 0.513
## 7 1946 1691220 1597452 3288672 1.06 0.514
## 8 1947 1899876 1800064 3699940 1.06 0.513
## 9 1948 1813852 1721216 3535068 1.05 0.513
## 10 1949 1826352 1733177 3559529 1.05 0.513
## # … with 53 more rows
Continue for Exercise 6
<- present %>%
present mutate(more_boys = boys > girls)
present
## # A tibble: 63 × 7
## year boys girls total boy_to_girl_ratio boy_ratio more_boys
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl>
## 1 1940 1211684 1148715 2360399 1.05 0.513 TRUE
## 2 1941 1289734 1223693 2513427 1.05 0.513 TRUE
## 3 1942 1444365 1364631 2808996 1.06 0.514 TRUE
## 4 1943 1508959 1427901 2936860 1.06 0.514 TRUE
## 5 1944 1435301 1359499 2794800 1.06 0.514 TRUE
## 6 1945 1404587 1330869 2735456 1.06 0.513 TRUE
## 7 1946 1691220 1597452 3288672 1.06 0.514 TRUE
## 8 1947 1899876 1800064 3699940 1.06 0.513 TRUE
## 9 1948 1813852 1721216 3535068 1.05 0.513 TRUE
## 10 1949 1826352 1733177 3559529 1.05 0.513 TRUE
## # … with 53 more rows
Continue for Exercise 6
ggplot(data = present, aes(x = year, y = boy_ratio)) +
geom_line()
The year that we see with the most total number of births in the U.S. is 1961.
# Insert code for Exercise 7 here
<- present %>%
present mutate(present$boys + present$girls)
<- present %>%
present arrange(desc(total))
head(present)
## # A tibble: 6 × 8
## year boys girls total boy_to_girl_ratio boy_ratio more_boys present$…¹
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <dbl>
## 1 1961 2186274 2082052 4268326 1.05 0.512 TRUE 4268326
## 2 1960 2179708 2078142 4257850 1.05 0.512 TRUE 4257850
## 3 1957 2179960 2074824 4254784 1.05 0.512 TRUE 4254784
## 4 1959 2173638 2071158 4244796 1.05 0.512 TRUE 4244796
## 5 1958 2152546 2051266 4203812 1.05 0.512 TRUE 4203812
## 6 1962 2132466 2034896 4167362 1.05 0.512 TRUE 4167362
## # … with abbreviated variable name ¹`present$boys + present$girls`