Descriptives for fertility-despair project

Author

Luttinen & Schaefer

1. Load Libraries

2. Import Data (NMFR& Deaths of Despair)

*note we are using mortality data from 2005-2016 and non-marital fertility data from 2010-2021 because we are using a 5-year lag between mortality and fertility

nmfr <- read_csv("C:/Users/rlutt/OneDrive/Fall 2023/Advanced Demography/nmfr.csv") 
New names:
Rows: 687 Columns: 6
── Column specification
──────────────────────────────────────────────────────── Delimiter: "," dbl
(6): ...1, STATEFIP, Pop, YEAR, Births, nmfr
ℹ 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.
• `` -> `...1`
nmfr25under<-read_csv("C:/Users/rlutt/OneDrive/Fall 2023/Advanced Demography/popwbirthsunder25.csv")
Rows: 687 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): STATEFIP, Pop, YEAR, Births, nmfr

ℹ 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.
#DOD Data

DOD05 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD05.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD06 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD06.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD07 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD07.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD08 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD08.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 163 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD09 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD09.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 167 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD10 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD10.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD11 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD11.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD12 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD12.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD13 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD13.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD14 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD14.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 162 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD15 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD15.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
DOD16 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata/DOD16.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 160 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.

3. Clean DOD Data & non-married population data

4. Import Midlife Mortality Data

#mid-life mortality data
MLM05 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM05.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM06 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM06.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM07 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM07.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM08 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM08.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM09 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM09.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM10 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM10.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM11 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM11.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM12 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM12.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM13 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM13.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM14 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM14.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 88 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM15 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM15.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.
MLM16 <- read_delim("C:/Users/rlutt/Downloads/mortalitydata2/MLM16.txt", 
    delim = "\t", escape_double = FALSE, 
    trim_ws = TRUE)
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 86 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: "\t"
chr (3): Notes, State, State Code
dbl (4): Deaths, Population, Crude Rate, Age Adjusted Rate

ℹ 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.

5. Clean Midlife Mortality Data

6. Join mortality data sets together

7. Mortality graphs (DoD & Midlife Mortality)

National DoD from 2005-2016

longDODdata <- DODtestdf %>% pivot_longer(cols = c(Y05, Y06, Y07, Y08, Y09, Y10, Y11, Y12, Y13, Y14, Y15, Y16), names_to = 'year', values_to = 'DDR')

#calculate averages per year

longDODdata2<-longDODdata%>%
group_by(year)%>%
   summarise(mean_DoD = mean(DDR))


#graphic showing variation DoD over the years

library(ggplot2)
ggplot(longDODdata2, aes(x = year, y = mean_DoD))+
  geom_point() + 
  labs(title = "State-level Deaths of Despair Rates", subtitle= "2006 to 2016",
       x = "Year",
       y = "DDR (per 100,000; age-adjusted)",
       caption = "Source: CDC wonder",) +geom_line()
`geom_line()`: Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?

National Mid-life all-cause mortality from 2005-2016

longMLMdata <- MLMtestdf %>% pivot_longer(cols=c(Y05, Y06, Y07, Y08, Y09, Y10, Y11, Y12, Y13, Y14, Y15, Y16),names_to = 'year', values_to = 'MLDR')

#calculate averages per year

longMLMdata2<-longMLMdata%>%
group_by(year)%>%
   summarise(mean_MR = mean(MLDR))


#graphic showing variation DoD over the years

library(ggplot2)
ggplot(longMLMdata2, aes(x = year, y = mean_MR))+
  geom_point() + 
  labs(title = "State-level All Cause Mortality Rates", subtitle= "2006 to 2016",
       x = "Year",
       y = "DDR (per 100,000; age-adjusted)",
       caption = "Source: CDC wonder",) + geom_line() 
`geom_line()`: Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?

State examples of DoD rates from 2005-2016

longDODdata %>% ggplot(aes(x=year, y=DDR, group=State))+geom_line(aes(color=State))+geom_point(aes(color=State))

exmort<-dplyr::filter(longDODdata, State=="Washington" | State== "West Virginia" | State== "Connecticut" | State== "Alabama"| State== "New Mexico" | State=="Texas")

exmort %>% ggplot(aes(x=year, y=DDR, group=State))+geom_line()+geom_point()+facet_wrap(~ State)

State examples of Mid-life all-cause mortality rates from 2005-2016

longMLMdata <- MLMtestdf %>% pivot_longer(cols=c(Y05, Y06, Y07, Y08, Y09, Y10, Y11, Y12, Y13, Y14, Y15, Y16),names_to = 'year', values_to = 'MLDR')

longMLMdata %>% ggplot(aes(x=year, y=MLDR, group=State))+geom_line(aes(color=State))+geom_point(aes(color=State))

exmortmid<-dplyr::filter(longMLMdata, State=="Washington" | State== "West Virginia" | State== "Connecticut" | State== "Alabama"| State== "New Mexico" | State=="Texas")

exmortmid %>% ggplot(aes(x=year, y=MLDR, group=State))+geom_line()+geom_point()+facet_wrap(~State)

8. Non-marital fertility graphs (all non-marital fertility and 25 and under non-marital fertility)

National non-marital fertility from 2010-2021

#calculate average per year

nmfr2<-nmfr%>%
group_by(YEAR)%>%
   summarise(mean_nmfr = mean(nmfr))

#graphic showing variation in nmfr over the years

library(ggplot2)
ggplot(nmfr2, aes(x = YEAR, y = mean_nmfr)) + scale_x_continuous(breaks=c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021))+
  geom_point() + 
  labs(title = "State-level Non-marital fertility", subtitle= "2010 to 2021",
       x = "Year",
       y = "NMFR (per 1,000)",
       caption = "Sources: CDC wonder & American Community Survey",)+geom_line()
Warning: Removed 2 rows containing missing values (`geom_point()`).
Warning: Removed 2 rows containing missing values (`geom_line()`).

National non-marital fertility for women aged 25 and under from 2010-2021

#calculate average per year

popwbirthsunder252<-nmfr25under%>%
group_by(YEAR)%>%
   summarise(mean_nmfr = mean(nmfr))

#graphic showing variation in nmfr over the years

library(ggplot2)
ggplot(popwbirthsunder252, aes(x = YEAR, y = mean_nmfr)) + scale_x_continuous(breaks=c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021))+
  geom_point() + 
  labs(title = "State-level Non-marital fertility", subtitle= "2010 to 2021",
       x = "Year",
       y = "NMFR (per 1,000)",
       caption = "Sources: CDC wonder & American Community Survey") +geom_line()
Warning: Removed 2 rows containing missing values (`geom_point()`).
Warning: Removed 2 rows containing missing values (`geom_line()`).

State examples of non-marital fertility from 2010-2021

#add a zero to state fips to match state code
nmfr$'State Code' <- nmfr$STATEFIP %>% str_pad(2,pad = "0")

#merge with mortality data to get state names 
joined<-merge(longDODdata,nmfr, by='State Code')

exfert<-dplyr::filter(joined, State=="Washington" | State== "West Virginia" | State== "Connecticut" | State== "Alabama"| State== "New Mexico" | State=="Texas")

exfert %>% ggplot(aes(x=YEAR, y=nmfr, group=State))+geom_line()+geom_point()+facet_wrap(~State)
Warning: Removed 72 rows containing missing values (`geom_line()`).
Warning: Removed 72 rows containing missing values (`geom_point()`).

State examples of non-marital fertility for women aged 25 and under from 2010-2021

#add a zero to state fips to match state code
nmfr25under$'State Code' <- nmfr25under$STATEFIP %>% str_pad(2,pad = "0")

#merge with mortality data to get state names 
joinedunder25<-merge(longDODdata,nmfr25under, by='State Code')

exfertunder25<-dplyr::filter(joinedunder25, State=="Washington" | State== "West Virginia" | State== "Connecticut" | State== "Alabama"| State== "New Mexico" | State=="Texas")

exfertunder25 %>% ggplot(aes(x=YEAR, y=nmfr, group=State))+geom_line()+geom_point()+facet_wrap(~State)
Warning: Removed 72 rows containing missing values (`geom_line()`).
Warning: Removed 72 rows containing missing values (`geom_point()`).