The MBR manuscript demonstrates WATS plots with data prepared for Rodgers, St. John, & Coleman (2005). In that paper and the MBR manuscript, the denominator of the GFR (General Fertility Rate) is the initial 1990 county population of females ages 15-44.

This vignette uses slightly different Census estimates. The intercensal population estimates (for females ages 15-44) are used for Jan 1990, Jan 1991, Jan 1992, ..., Jan 2000. Linear interpolation is then used to complete the remaining 11 months of each year. These monthly estimates become the denominator of each county's monthly GFR.

#It works better if the root directory is set in its own chunk.
library(knitr)
knitr::opts_knit$set(root.dir = "../")
library(grDevices)
library(grid)
library(scales)
library(ggplot2)
library(boot)
library(Wats)

knitr::opts_chunk$set(
    comment=NA,
    tidy=FALSE,
    fig.width=6.5,
    fig.height=1.6,
    out.width="600px", #This affects only the markdown, not the png.  Height will scale appropriately.
    fig.path='figure-okc-fertility-intercensal-rmd/',
    dpi=200
)

if (base::Sys.info()["sysname"] == "Windows")
  grDevices::windows.options(antialias = "cleartype")
change_month <- base::as.Date("1996-02-15") #as.Date("1995-04-19") + lubridate::weeks(39) = "1996-01-17"

vp_layout <- function(x, y) { grid::viewport(layout.pos.row=x, layout.pos.col=y) }

full_spread <- function(scores) {
  return(base::range(scores)) #A new function isn't necessary.  It's defined in order to be consistent.
}
h_spread <- function(scores) {
  return(stats::quantile(x=scores, probs=c(.25, .75)))
}
se_spread <- function(scores) {
  return(::mean(scores) + base::c(-1, 1) * stats::sd(scores) / base::sqrt(base::sum(!base::is.na(scores))))
}
boot_spread <- function(scores, conf=.68) {
  plugin <- function(d, i) { base::mean(d[i]) }

  distribution <- boot::boot(data=scores, plugin, R=99) #999 for the publication
  ci <- boot::boot.ci(distribution, type = c("bca"), conf=conf)
  return(ci$bca[4:5]) #The fourth & fifth elements correspond to the lower & upper bound.
}

dark_theme <- ggplot2::theme(
  axis.title          = ggplot2::element_text(color="gray30", size=9),
  axis.text.x         = ggplot2::element_text(color="gray30", hjust=0),
  axis.text.y         = ggplot2::element_text(color="gray30"),
  axis.ticks          = ggplot2::element_blank(),
#   panel.grid.minor.y  = element_line(color="gray95", size=.1),
#   panel.grid.major    = element_line(color="gray90", size=.1),
  panel.spacing       = grid::unit(c(0, 0, 0, 0), "cm"),
  plot.margin         = grid::unit(c(0, 0, 0, 0), "cm")
)
light_theme <- dark_theme + ggplot2::theme(
  axis.title          = ggplot2::element_text(color="gray80", size=9),
  axis.text.x         = ggplot2::element_text(color="gray80", hjust=0),
  axis.text.y         = ggplot2::element_text(color="gray80"),
  panel.grid.minor.y  = ggplot2::element_line(color="gray99", size=.1),
  panel.grid.major    = ggplot2::element_line(color="gray95", size=.1)
)
date_sequence <- base::seq.Date(from=base::as.Date("1990-01-01"), to=base::as.Date("1999-01-01"), by="years")
x_scale       <- ggplot2::scale_x_date(breaks=date_sequence, labels=scales::date_format("%Y"))
x_scale_blank  <- ggplot2::scale_x_date(breaks=date_sequence, labels=NULL) #This keeps things proportional down the three frames.

Section 1: Cartesian Rolling Plot

This section corresponds to the MBR manuscript, but with the modified dataset.

Smoothed monthly birth rates (General Fertility Rates; GFR) for Oklahoma County, 1990-1999, plotted in a linear plot. The top plot shows the connected raw data with a February smoother; the middle plot shows smoothing with a 12-month moving average, blue/green line, superimposed on a February smoother, red tan line); the bottom plot shows the smoothers and confidence bands, which are H-spreads (i.e., the distance between the .25 and .75 quantiles) defined using the distribution of GFRs for the given month and 11 previous months.

# ds_linear <- utils::read.csv("./Datasets/county_month_birth_rate_2014_version.csv", stringsAsFactors=FALSE)
# ds_linear$date <- base::as.Date(ds_linear$date)
# ds_linear <- ds_linear[ds_linear$county_name=="oklahoma", ]

# Uncomment this line to use the version built into the package.  By default, it uses the
# CSV to promote reproducible research, since the CSV format is more open and accessible to more software.
ds_linear_all <- county_month_birth_rate_2014_version
ds_linear <- ds_linear_all[ds_linear_all$county_name=="oklahoma", ]

ds_linear <- Wats::augment_year_data_with_month_resolution(ds_linear=ds_linear, date_name="date")

portfolio_cartesian <- Wats::annotate_data(ds_linear, dv_name="birth_rate", center_function=stats::median, spread_function=h_spread)

top_panel <- Wats::cartesian_rolling(
  ds_linear = portfolio_cartesian$ds_linear,
  x_name = "date",
  y_name = "birth_rate",
  stage_id_name = "stage_id",
  change_points = change_month,
  y_title = "General Fertility Rate",
  change_point_labels = "Bombing Effect",
  draw_rolling_band = FALSE,
  draw_rolling_line = FALSE
)

middle_panel <- cartesian_rolling(
  ds_linear = portfolio_cartesian$ds_linear,
  x_name = "date",
  y_name = "birth_rate",
  stage_id_name = "stage_id",
  change_points = change_month,
  y_title = "General Fertility Rate",
  change_point_labels = "",
  draw_rolling_band = FALSE,
  draw_jagged_line = FALSE
)

bottom_panel <- Wats::cartesian_rolling(
  ds_linear = portfolio_cartesian$ds_linear,
  x_name = "date",
  y_name = "birth_rate",
  stage_id_name = "stage_id",
  change_points = change_month,
  y_title = "General Fertility Rate",
  change_point_labels = "",
  draw_jagged_line = FALSE
)

top_panel <- top_panel + x_scale + dark_theme
middle_panel <- middle_panel + x_scale + dark_theme
bottom_panel <- bottom_panel + x_scale_blank + dark_theme

grid::grid.newpage()
grid::pushViewport(grid::viewport(layout=grid::grid.layout(3,1)))
print(top_panel, vp=vp_layout(1, 1))
print(middle_panel, vp=vp_layout(2, 1))
print(bottom_panel, vp=vp_layout(3, 1))
grid::popViewport()

Section 2: Cartesian Periodic

This section corresponds to the MBR manuscript, but with the modified dataset.

Cartesian plot of the GFR time series data in Oklahoma County, with H-spread Bands superimposed.

cartesian_periodic <- Wats::cartesian_periodic(
  portfolio_cartesian$ds_linear,
  portfolio_cartesian$ds_periodic,
  x_name = "date",
  y_name = "birth_rate",
  stage_id_name = "stage_id",
  change_points = change_month,
  change_point_labels = "Bombing Effect",
  y_title = "General Fertility Rate",
  draw_periodic_band = TRUE #The only difference from the simple linear graph above
)
cartesian_periodic <- cartesian_periodic + x_scale + dark_theme
print(cartesian_periodic)

Section 3: Polar Periodic

This section corresponds to the MBR manuscript, but with the modified dataset.

Wrap Around Time Series (WATS Plot) of the Oklahoma City GFR data, 1990-1999

portfolio_polar <- Wats::polarize_cartesian(
  ds_linear = portfolio_cartesian$ds_linear,
  ds_stage_cycle = portfolio_cartesian$ds_stage_cycle,
  y_name = "birth_rate",
  stage_id_name = "stage_id",
  plotted_point_count_per_cycle = 7200
)

grid::grid.newpage()
grid::pushViewport(grid::viewport(
  layout=grid::grid.layout(
    nrow = 2, ncol = 2, respect = TRUE,
    widths = grid::unit(c(1,1), c("null", "null")),
    heights = grid::unit(c(1,.5), c("null", "null"))
  ),
  gp = grid::gpar(cex=1, fill=NA)
))

## Create top left panel
grid::pushViewport(grid::viewport(layout.pos.col=1, layout.pos.row=1))
top_left_panel <- Wats::polar_periodic(
  ds_linear = portfolio_polar$ds_observed_polar,
  ds_stage_cycle_polar = portfolio_polar$ds_stage_cycle_polar,
  y_name = "radius",
  stage_id_name = "stage_id",
  cardinal_labels = c("Jan1", "Apr1", "July1", "Oct1")
)
grid::upViewport()

## Create top right panel
grid::pushViewport(grid::viewport(layout.pos.col=2, layout.pos.row=1))
topRighttPanel <- Wats::polar_periodic(
  ds_linear = portfolio_polar$ds_observed_polar,
  ds_stage_cycle_polar = portfolio_polar$ds_stage_cycle_polar,
  y_name = "radius",
  stage_id_name = "stage_id",
  draw_observed_line = FALSE,
  cardinal_labels = c("Jan1", "Apr1", "July1", "Oct1"),
  origin_label = NULL
)
grid::upViewport()

## Create bottom panel
grid::pushViewport(grid::viewport(layout.pos.col=1:2, layout.pos.row=2, gp=grid::gpar(cex=1)))
print(cartesian_periodic, vp=vp_layout(x=1:2, y=2)) #Print across both columns of the bottom row.
upViewport()

Section 4: Confirmatory Analysis of Interrupted Time Series

The remaining two sections depart from the MBR manuscript analyses. Its goal is to determine if the significant findings of Rodgers, St. John, & Coleman still appear with the modified Census estimates. As shown below, the the post-bombing fertility is still significantly higher than the pre-bombing fertility.

This section uses an approach advocated by McLeod, Yu, & Mahdi (2011), which is consistent other articles, including Rodgers et al. (2005). There are two trends that are de-seasonalized. The first is the 'classic' approach which uses the observed trend line (see decompose() on CRAN). The second is a smoothed version, where a loess is passed through the observed data; this smoothed line is then de-seasonalized (see stl() on CRAN). Both approaches lead to comparable conclusions. The post-bombing fertility is significantly higher than the pre-bombing fertility (i.e., the step coefficient is significantly more positive).

ds_linear <- Wats::augment_year_data_with_month_resolution(ds_linear=ds_linear, date_name="date")

tsData <- stats::ts(
  data = ds_linear$birth_rate,
  start = as.integer(ds_linear[1, c("year", "month")]),
  end = as.integer(ds_linear[nrow(ds_linear), c("year", "month")]),
  frequency = 12L
)

#Create unsmoothed and smoothed version
seasonalClassic <- stats::decompose(tsData)
plot(seasonalClassic)

#Watch out, the 2nd & 3rd columns have swapped positions, compared to `decompose()`
seasonalLoess <- stats::stl(x = tsData, s.window = "periodic")
plot(seasonalLoess)

# Seasonality is accounted for without a smoother
lag1 <- 1L #Significant for many different values of lag, including 1
y <- seasonalClassic$trend[(lag1+1):length(seasonalClassic$trend)]
y1 <- seasonalClassic$trend[1:(length(seasonalClassic$trend)-lag1)]
# step <- c(rep(0L, times=sum(ds_linear$stage_id==1L)-lag1), rep(1L, times=sum(ds_linear$stage_id==2L)))
step <- ds_linear$stage_id[(lag1+1):length(seasonalClassic$trend)] - 1L
dsClassic <- tibble::tibble(y=y, y1=y1, step=step)
rm(lag1, y, y1, step)
fitClassic <-  glm(y ~ 1 + step + y1, data=dsClassic)
summary(fitClassic)

#Seasonality is accounted for after a loess is fit through it.
lag1 <- 1L #Significant for many different values of lag, including 1
trendLineLoess <- as.numeric(seasonalLoess$time.series[[2]])
y <- trendLineLoess[(lag1+1):length(trendLineLoess)]
y1 <- trendLineLoess[1:(length(trendLineLoess) - lag1)]
# step <- c(rep(0L, times=sum(ds_linear$stage_id==1L)-lag1), rep(1L, times=sum(ds_linear$stage_id==2L)))
step <- ds_linear$stage_id[(lag1+1):length(trendLineLoess)] - 1L
dsLoess <- tibble::tibble(y=y, y1=y1, step=step)
rm(lag1, y, y1, step)
fitLoess <-  glm(y ~ 1 + step + y1, data=dsLoess)
summary(fitLoess)

Section 5: Confirmatory Analysis of Interrupted Time Series with Bayes Factors

A similar set of Bayesian analyses support the claim that post-bombing fertility is higher. The BayesSingleSub package by De Vries and Morey. The third number in each row describes the test for a mean different. The package also has tests that indicate the post-bombing slope may be more positive than the pre-bombing slope. Significance is suggested with a Bayes Factors is less than one (and therefore its log is negative).

# Seasonality is accounted for without a smoother
beforeClassic <- seasonalClassic$trend[ds_linear$stage_id==1L]
afterClassic <- seasonalClassic$trend[ds_linear$stage_id==2L]

#Set the number of MCMC iterations
mcmcRepCount <- 1000#000
#Determine if the intermediate progress should be displayed
showMcmcProgress <- FALSE

(gClassic <- BayesSingleSub::trendtest.Gibbs.AR(beforeClassic[!is.na(beforeClassic)], afterClassic[!is.na(afterClassic)], return.chains=F, iterations=mcmcRepCount, progress=showMcmcProgress))
(mcClassic <- BayesSingleSub::trendtest.MC.AR(beforeClassic[!is.na(beforeClassic)], afterClassic[!is.na(afterClassic)], iterations=mcmcRepCount, progress=showMcmcProgress))
# summary(mcClassic)
# coda::gelman.diag(g$chains) #it needs multiple chains to asses.

#Seasonality is accounted for after a loess is fit through it.
beforeLoess <- seasonalLoess$time.series[ds_linear$stage_id==1L, 2]
afterLoess <- seasonalLoess$time.series[ds_linear$stage_id==2L, 2]
BayesSingleSub::trendtest.Gibbs.AR(beforeLoess, afterLoess, iterations=mcmcRepCount, progress=showMcmcProgress)
BayesSingleSub::trendtest.MC.AR(beforeLoess, afterLoess, iterations=mcmcRepCount, progress=showMcmcProgress)

Session Info

The current vignette was build on a system using the following software.

base::cat("Report created by", base::Sys.info()["user"], "at", base::strftime(base::Sys.time(), "%c, %z"))
utils::sessionInfo()

References



OuhscBbmc/Wats documentation built on April 22, 2024, 10:32 p.m.