This assignment builds on what you did in the previous assignment. It uses the dataframe you saved at the end.
It also requires you to submit your work by posting a document on RPubs. This will allow you to create interactive graphs.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.0
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
load("by_state_0323.Rdata")
Make a graph showing the total number of births per year. Use plotly.
g = ggplot(new_df, aes(x=Year, y=Births)) +
geom_col()
ggplotly(g)
It is somewhat surprising that the total number of births in the US has declined. Has the number of women been declining in this time period?
Redo the previous dataframe and include the total number of women. Also compute the ratio of births to women. Print the head and tail of this dataframe so we can see all three variables.
ratio_df<- new_df %>%
select(Births, Fpop, Rate)
head(ratio_df)
## # A tibble: 6 × 3
## Births Fpop Rate
## <dbl> <dbl> <dbl>
## 1 8095 157477 0.0514
## 2 8126 159259 0.0510
## 3 7771 161701 0.0481
## 4 8537 164708 0.0518
## 5 18633 163919 0.114
## 6 18581 163736 0.113
tail(ratio_df)
## # A tibble: 6 × 3
## Births Fpop Rate
## <dbl> <dbl> <dbl>
## 1 127 16075 0.0079
## 2 135 15785 0.00855
## 3 116 15958 0.00727
## 4 147 16085 0.00914
## 5 134 16592 0.00808
## 6 135 17327 0.00779
The total fertility rate (TFR) is the number of births for a woman during her lifetime. The rate data we have is the number of births per woman per year while she is in one of the 5-year age groups.
How do we use the rate information to construct the TFR? We add up all of the rates for the individual age groups and multiply the sum by 5. Do this for the State of Washington. Plot the time series using geom_point() and plotly.
wa_TFR <- new_df %>%
filter(State=="Washington") %>%
group_by(Year) %>%
mutate(TFR=5*sum(Rate))
g = ggplot(wa_TFR,aes(x=Year, y=TFR))+
geom_point()
ggplotly(g)
Repeat the exercise for all states. Again, use plotly so we will be able to identify states. In the aes(), add “group = State”. Also draw a red horizontal line at 2.1. Use the plotly framework I have set up.
Using the interactive graph, which states have the highest and lowest TFR values in 2003 and 2023?
Unsure if I have a mistake that’s carried over from A3, or if the datasets provided from wonder.cdc.gov/natality.html failed to return the full set of data, but I have no female population from which to calculate rates and TFR for 2021-2023. Not to mention select rows from other years.
Highest TFR 2003: Utah Lowest TFR 2003: Vermont Highest TFR 2020: South Dakota Lowest TFR 2020: Vermont
all_TFR <- new_df %>%
group_by(Year, State) %>%
mutate(TFR=5*sum(Rate))
g = ggplot(all_TFR,aes(x=Year, y=TFR, group=State))+
geom_point()
ggplotly(g)
Create a graph showing the TFR for the US as a whole. We can’t use the rate data directly. We need to compute the total numbers of births and total numbers of women for each age group and year. Then compute the TFR by adding the calculated rates and multiplying by 5. Plot this using plotly.
I have calculated total births/population/ratio for each age group for each year, but cannot get a TFR that matches anything reasonable.
US_TFR <- new_df %>%
group_by(Year, Age) %>%
mutate(total_births=sum(Births), total_pop=sum(Fpop), total_ratio=sum(Births)/sum(Fpop)) %>%
mutate(total_TFR=5*sum(total_ratio))
g = ggplot(US_TFR,aes(x=Year, y=total_TFR))+
geom_point()
ggplotly(g)