Skip to contents

The package has four plotting functions and one theme. Each function returns a ggplot object, so labels, scales, and themes can be changed by adding ggplot2 layers.

Function Input Shows
plot_design() the survey data The design for all ages pooled: survey means, the fit through the pre-shock surveys, the extrapolated counterfactual, and the gap
plot_profile() output of cdd() The age profile: effect by age in the exposed survey with its intervals
plot_dynamics() output of cdd_groups() Trajectories: effect by years since onset for cohort groups
plot_bandwidth() output of cdd_bandwidth() Estimated mean squared error by number of pre-shock surveys
theme_cdd() none The plain theme used by all four
library(cohortDD)
library(ggplot2)
data(gss_culture)

ctl <- c("factor(race)", "factor(sex)", "educ")

All examples use the 1992-93 shock and the outcome homosex, level 1 (“always wrong”), with onset 1992 and first exposed survey 1993. The George Floyd example of the replication article has one pre-shock survey, which leaves no cohort trend to fit; this article therefore uses a General Social Survey example with five pre-shock surveys.

plot_design(): the design in one picture

plot_design() works on the raw data and needs no fitted object. It pools respondents aged lo to hi, computes the weighted mean of the outcome in each survey, fits the extrapolant through the H pre-shock surveys, and extends it to the exposed survey.

plot_design(gss_culture, "homosex", onset = 1992, wave = 1993, H = 5,
            lo = 20, hi = 70, from = 1985, level = 1, scale = 100) +
  labs(y = "Share answering \"always wrong\" (percent)")

Survey means of the share answering always wrong, with the local-linear fit through the pre-shock surveys and its extrapolation to 1993.

How to read the figure:

  • Filled black points are survey means before the shock and open points are survey means after it.
  • The shaded region covers the H pre-shock surveys used in the fit.
  • The vertical dashed line marks the onset.
  • The solid line is the kernel-weighted local-linear fit and the dotted segment is its extrapolation to the exposed survey.
  • The grey point is the extrapolated counterfactual, the estimate of what the exposed cohort would have reported without the shock.
  • The arrow is the gap between the observed mean and the counterfactual, labelled with its value. This is the estimate for the pooled age range.

Useful arguments are from (the first survey shown), level (the level of a categorical outcome to plot as a share, or NULL for a numeric outcome), scale (use 100 for percent), digits (digits in the gap label), and the colours col_fit, col_window, and col_cf. The figure is descriptive: it uses no covariates and pools all ages, so its gap need not equal an estimate from cdd() with controls.

Pointing wave at a later survey shows the extrapolation over a longer distance.

plot_design(gss_culture, "homosex", onset = 1992, wave = 1996, H = 5,
            lo = 20, hi = 70, from = 1985, level = 1, scale = 100,
            col_fit = "#1B2A41", col_window = "#CBDCEB") +
  labs(y = "Share answering \"always wrong\" (percent)")

The same design figure with the extrapolation carried to the 1996 survey.

plot_profile(): the age profile

plot_profile() draws the estimates from cdd() against age in the exposed survey.

fit <- cdd(gss_culture, "homosex", onset = 1992, wave = 1993, H = 5, b = 5,
           order = 1, ages = 20:70, levels = 1, controls = ctl,
           reweight = c("factor(race)", "factor(sex)"))
plot_profile(fit, level = "1", scale = 100, ylab = "Effect (percentage points)")

Age profile of the effect in 1993 with confidence and bias-aware bands.

How to read the figure:

  • The black line is the estimated effect for the cohort that has the age on the horizontal axis in the exposed survey. Younger cohorts are on the left.
  • The dark band is the 95 percent confidence interval (lower, higher).
  • The light band is the bias-aware interval (honest_lower, honest_higher), which also allows for curvature in the cohort trend.
  • The dotted horizontal line is zero.

Estimates at neighbouring ages use overlapping age bands and are strongly correlated, so short wiggles in the line are not evidence of differences between adjacent cohorts.

Options

honest = FALSE drops the bias-aware band.

plot_profile(fit, level = "1", scale = 100, honest = FALSE,
             ylab = "Effect (percentage points)")

Age profile with the confidence band only.

groups overlays estimates for age groups from cdd_groups() as open circles with confidence bars. Each circle is placed at the midpoint age of its group. The group estimates are less noisy than the single-year profile because each pools a wider age range.

grp <- cdd_groups(gss_culture, "homosex", onset = 1992, wave = 1993, H = 5,
                  order = 1, levels = 1, align = "interview", controls = ctl)
plot_profile(fit, level = "1", groups = grp, scale = 100,
             ylab = "Effect (percentage points)")

Age profile with age-group estimates overlaid as open circles.

facet splits the plot by a column of the input, which is how to compare profiles across exposed surveys, outcomes, or specifications. Stack the fits in one data frame with a column that identifies them.

fit96 <- cdd(gss_culture, "homosex", onset = 1992, wave = 1996, H = 5, b = 5,
             order = 1, ages = 20:70, levels = 1, controls = ctl,
             reweight = c("factor(race)", "factor(sex)"))
two <- rbind(cbind(as.data.frame(fit), wave = 1993),
             cbind(as.data.frame(fit96), wave = 1996))
plot_profile(two, level = "1", scale = 100, facet = "wave",
             ylab = "Effect (percentage points)")

Age profiles in the exposed surveys of 1993 and 1996, side by side.

In the right panel the horizontal axis is age in 1996, so a given cohort appears three years further to the right than in the left panel. The bands are wider in 1996 because the extrapolation covers a longer distance, which raises both the variance and the bias factor.

level selects the outcome level when the fit contains several. The effects on the levels of a categorical outcome sum to zero at each age, so a fall in one answer shows up as a rise in others.

fit_all <- cdd(gss_culture, "homosex", onset = 1992, wave = 1993, H = 5, b = 5,
               order = 1, ages = 20:70, levels = c(1, 4), controls = ctl)
lev <- as.data.frame(fit_all)
lev$answer <- ifelse(lev$level == "1", "Always wrong", "Not wrong at all")
lev$level <- "x"
plot_profile(lev, level = "x", scale = 100, facet = "answer",
             ylab = "Effect (percentage points)")

Age profiles for the answers always wrong and not wrong at all.

plot_dynamics(): trajectories of cohort groups

plot_dynamics() shows how the effect on the same cohorts evolves with time since onset. Its input is the output of cdd_groups() with align = "onset" over several exposed surveys. With this alignment each group is defined by age at onset, and the target age advances with the survey so that the same birth cohorts are followed.

traj <- cdd_groups(gss_culture, "homosex", onset = 1992,
                   waves = c(1993, 1994, 1996, 1998), H = 5, order = 1,
                   levels = 1, align = "onset", controls = ctl)
plot_dynamics(traj, level = "1", focal = "20-30", scale = 100,
              ylab = "Effect (percentage points)")

Effect by years since onset for four cohort groups, with the group aged 20 to 30 at onset highlighted.

How to read the figure:

  • The horizontal axis is k, the years since onset. The exposed surveys of 1993, 1994, 1996, and 1998 are at k of 1, 2, 4, and 6.
  • Each line is one cohort group, labelled by its age range at onset.
  • The group named in focal is drawn in black with 95 percent confidence bars. The other groups are drawn in grey without bars.
  • A line that stays level indicates an effect that persists. A line that moves away from zero indicates an effect that keeps growing, and a line that returns to zero indicates one that fades.

The confidence bars lengthen with k because the counterfactual is extrapolated further from the same pre-shock surveys. Estimates at large k therefore say less, and the bias factor bb in the cdd_groups() output shows how quickly the possible curvature bias grows.

Change focal to highlight another group.

plot_dynamics(traj, level = "1", focal = "45-59", scale = 100,
              ylab = "Effect (percentage points)")

The same trajectories with the group aged 45 to 59 at onset highlighted.

plot_estimates(): estimates with both intervals

plot_estimates() draws a set of estimates that differ by group, date, or specification. Each estimate is a point with two vertical bars, a thick one for the 95 percent sampling interval and a thin one for the bias-aware interval. Its input is any stack of rows returned by cdd_groups(), cdd_popavg(), or cdd(), and by names the column on the horizontal axis.

plot_estimates(subset(traj, wave %in% c(1993, 1994)), by = "group", series = "wave",
               scale = 100, xlab = "Cohort group (age at onset)",
               ylab = "Effect (percentage points)")

Cohort-group estimates in 1993 and 1994 with sampling and bias-aware intervals.

The argument series splits the estimates into series drawn side by side in shades of one colour, open marks estimates to be drawn with open grey markers, such as placebo dates, and ref adds a reference estimate as a dashed line with a band. The Get started article uses all three.

plot_bandwidth(): estimated mean squared error by window

plot_bandwidth() displays the output of cdd_bandwidth().

bw <- cdd_bandwidth(gss_culture, "homosex", onset = 1992, wave = 1993, grid = 2:6,
                    ages = seq(25, 65, by = 5), b = 5, levels = 1, controls = ctl)
plot_bandwidth(bw, level = "1") +
  labs(x = "Number of pre-shock surveys H", y = "Root estimated MSE")

Root estimated mean squared error against the number of pre-shock surveys for both extrapolants.

How to read the figure:

  • The horizontal axis is H, the number of pre-shock surveys.
  • The vertical axis is the square root of the estimated mean squared error, averaged over the target ages. It is in outcome units, so 0.08 is eight percentage points.
  • There is one line per extrapolant, the kernel mean (p = 0 in the legend) and the local-linear fit (p = 1).
  • The open circle on each line marks the H with the smallest estimated mean squared error for that extrapolant.

The article Choosing the extrapolant and the window explains how the criterion is built and how to use it.

theme_cdd() and customization

All four functions end with theme_cdd(), a plain theme that can be used for other figures built from the package output. Because the functions return ggplot objects, the usual ggplot2 tools apply.

plot_profile(fit, level = "1", scale = 100, ylab = "Effect (percentage points)") +
  scale_x_continuous(breaks = seq(20, 70, by = 10)) +
  labs(title = "Effect of the 1992-93 shock by age in 1993",
       subtitle = "Share answering that same-sex relations are always wrong") +
  theme_cdd(base_size = 12)

Age profile with a title, custom axis breaks, and a larger base font size.

For a figure that the package does not provide, plot the output directly. The output of cdd() and of the group functions is a data frame with the columns ate, lower, and higher.

ggplot(grp, aes(group, 100 * ate)) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  geom_pointrange(aes(ymin = 100 * lower, ymax = 100 * higher)) +
  labs(x = "Age in 1993", y = "Effect (percentage points)") +
  theme_cdd()

Age-group estimates drawn directly with ggplot2 as points with confidence bars.

To save a figure, use ggplot2::ggsave(), for example ggsave("profile.pdf", width = 6, height = 3.5).