Martin_Nguyen_33169772

Question 1: Read the data into R. Are the data in a tidy format? Why/why not?

library(readr)
Warning: package 'readr' was built under R version 4.5.1
children_per_woman_total_fertility <- read_csv("children_per_woman_total_fertility.csv")
Rows: 197 Columns: 302
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr   (1): country
dbl (301): 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810,...

ℹ 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.
View(children_per_woman_total_fertility)

For data to be considered tidy it must follow these three rules

  1. Each variable must have its own column.

  2. Each observation must have its own row.

  3. Each value must have its own cell.

As we can observe from the data there are two variables, country and years. Each country has its own row however each year is a column when year is actually a variable. For this reason, the data is currently not in a tidy format.

  1. Extract only the data for Japan and Australia for the period 1900–2020, and store in a tibble in tidy format. (Hint: you will need to make sure the years are stored as numbers so that it will be easy to draw the plots in the tasks below.)


library(dplyr)
Warning: package 'dplyr' was built under R version 4.5.1

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyr)
Warning: package 'tidyr' was built under R version 4.5.1
jp_au_1900_2020 <- children_per_woman_total_fertility %>%
  filter(country %in% c("Japan", "Australia")) %>% 
  select(country, `1900`:`2020`) %>%
  pivot_longer(-country, names_to = "year", values_to = "fertility_rate") %>%
  mutate(year = as.integer(year)) %>%
  arrange(country, year)

head(jp_au_1900_2020)
# A tibble: 6 × 3
  country    year fertility_rate
  <chr>     <int>          <dbl>
1 Australia  1900           3.66
2 Australia  1901           3.64
3 Australia  1902           3.58
4 Australia  1903           3.39
5 Australia  1904           3.54
6 Australia  1905           3.51
  1. Plot the fertility rate for Japan and Australia for the period 1900–2020, using lines. Use a different coloured line for each country. Briefly describe the main features visible in the plot (2–3 sentences per country).


library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.5.1
ggplot(jp_au_1900_2020, aes(x = year, y = fertility_rate, color = country)) +
  geom_line() +
  labs(x = "Year", y = "Children per woman", color = NULL)

  1. The replacement rate is usually defined as the TFR that would lead to a stable population level, assuming that mortality rates remain constant and net migration is zero. For developed countries, the replacement rate is typically 2.1 babies per woman. Repeat your plot and add a reference line to represent this replacement rate. What do you notice about the fertility rates for Japan and Australia?
ggplot(jp_au_1900_2020, aes(x = year, y = fertility_rate, color = country)) +
  geom_line(size = 1) +
  geom_hline(yintercept = 2.1, linetype = "dashed", color = "red") +
  labs(x = "Year",
       y = "Children per woman (TFR)",
       color = NULL,
       title = "Fertility Rates in Japan and Australia (1900–2020)",
       subtitle = "Dashed red line = Replacement rate (2.1)") +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

  1. Sample a single country at random out of all countries in the dataset, using some appropriate R code. Set the random seed to your student ID just before you do this. For example, set.seed(1234567) if your student ID is 1234567. If you happen to sample Australia or Japan, sample another country until you get one that differs from these.
set.seed(33169772)
random_country <- children_per_woman_total_fertility %>%
  distinct(country) %>%         
  pull(country) %>%                 
  sample(size = 1)
hungary_1900_2020 <- children_per_woman_total_fertility %>%
  filter(country == "Hungary") %>%
  select(country, `1900`:`2020`) %>%
  pivot_longer(-country, names_to = "year", values_to = "fertility_rate") %>%
  mutate(year = as.integer(year)) %>%
  arrange(country, year)
  

ggplot(hungary_1900_2020, aes(x = year, y = fertility_rate)) +
  geom_line(size = 1) +
  labs(title = "Fertility Rate of Hungary from 1900–2020",
       x = "Year", y = "Children per woman", color = NULL)