# Visualization Options {#sec-visualization}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, message = FALSE, warning = FALSE)
```
In this chapter, we explore various visualization options available in the `fdid` package.
```{r}
library(fdid)
```
```{r readcsv, include=FALSE}
data(fdid)
```
```{r gen, include=FALSE}
mortality$uniqueid <- paste(as.character(mortality$provid), as.character(mortality$countyid), sep = "-")
mortality$highzupu <- ifelse(mortality$pczupu >= median(mortality$pczupu, na.rm = TRUE), 1, 0)
```
```{r parameters, include=FALSE}
tr_period <- 1958:1961
ref_period <- 1957
treat <- "highzupu"
Y <- "mortality"
time <- "year"
unit <- "uniqueid"
covar <- c("avggrain", "nograin", "urban", "dis_bj", "dis_pc",
"rice", "minority", "edu", "lnpop")
```
```{r prepare, include=FALSE}
prepared_d <- fdid_prepare(
data = mortality,
Y_label = Y,
X_labels = covar,
G_label = treat,
unit_label = unit,
time_label = time
)
```
```{r ols1, include=FALSE}
fdid_results_ols1 <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
vartype = "robust",
)
```
```{r ols2, include=FALSE}
fdid_results_ols2 <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
method = "ols2",
vartype = "robust",
)
```
```{r did, include=FALSE}
fdid_results_did <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
method = "did",
vartype = "robust",
)
```
```{r ebal, cache=TRUE, include=FALSE}
fdid_results_ebal <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
method = "ebal",
vartype = "robust",
target.pop = "1"
)
```
```{r ipw, cache=TRUE, include=FALSE}
fdid_results_ipw <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
method = "ipw",
vartype = "robust",
)
```
```{r aipw, cache=TRUE, include=FALSE}
fdid_results_aipw <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
method = "aipw",
vartype = "robust",
)
```
```{r boot, cache=TRUE, include=FALSE}
fdid_results_ols1_boot <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
vartype = "bootstrap",
parallel = TRUE,
cores = 6,
nsims = 100
)
```
```{r jackknife, cache=TRUE, include=FALSE}
fdid_results_ols1_jack <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
vartype = "jackknife",
parallel = TRUE,
cores = 6,
)
```
```{r ols2_target1, cache=TRUE, include=FALSE}
fdid_results_ols2_target_1 <- fdid(
s = prepared_d,
tr_period = tr_period,
ref_period = ref_period,
method = "ols2",
vartype = "robust",
target.pop = "1"
)
```
## Raw means
- `ci = FALSE` removes the 95% confidence intervals.
```{r}
plot(fdid_results_ols1, type = "raw", ci = FALSE)
```
- `connected = TRUE` connects points with lines (default: `FALSE`).
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE)
```
- `group_labels` renames the two groups.
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE,
group_labels = c("Low Social Capital", "High Social Capital"))
```
- `palette` chooses an RColorBrewer palette (default: "Set2").
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE,
group_labels = c("Low Social Capital", "High Social Capital"), palette = "Set1")
```
- `shade_periods` controls the position of shaded region. Default is `x$tr_period`, i.e. the event window.
- By setting `shade_periods = c(1954, 1957)`, the pre-event periods can be set shaded.
- By setting `shade_periods = NULL`, the user can also remove the shaded region.
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE,
group_labels = c("Low Social Capital", "High Social Capital"),
shade_periods = c(1954, 1957))
```
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE,
group_labels = c("Low Social Capital", "High Social Capital"),
shade_periods = NULL)
```
- `alpha_shade` controls the transparency of the shaded region.
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE,
group_labels = c("Low Social Capital", "High Social Capital"),
alpha_shade = 0.6)
```
- The user can also set `ylim`, `xlab`, `ylab`, and `main` (currently, xlim is not supported).
```{r}
plot(fdid_results_ols1, type = "raw", connected = TRUE,
group_labels = c("Low Social Capital", "High Social Capital"),
ylim = c(0, 80), xlab = "Year", ylab = "Mortality", main = "Raw Mortality")
```
## Dynamic estimates
- `ci = FALSE` removes the 95% confidence intervals.
```{r}
plot(fdid_results_ols1, type = "dynamic", ci = FALSE)
```
- `connected = TRUE` connects points with lines (default: `FALSE`).
```{r}
plot(fdid_results_ols1, type = "dynamic", connected = TRUE)
```
- `shade_periods`, `alpha_shade`, `ylim`, `xlab`, `ylab`, and `main` customize the plot as above.
```{r}
plot(fdid_results_ols1, type = "dynamic", connected = TRUE,
shade_periods = c(1954, 1957),
alpha_shade = 0.8, ylim = c(-10, 4),
ylab = "Estimates", xlab = "Year",
main = "Effect Modification of High Social Capital"
)
```
- `xlim` zooms into a subset of years.
```{r}
plot(fdid_results_ols1, type = "dynamic",
alpha_shade = 0.8, ylim = c(-10, 4), xlim = c(1954, 1961),
ylab = "Estimates", xlab = "Year",
main = "Hiding Post-Event Periods"
)
```
## Overlap test
The user can adjust `palette`, `ylim`, `xlab`, and `main`:
```{r}
plot(fdid_results_aipw, type = "overlap",
palette = "Set1", ylim = c(-0.1, 0.1),
xlab = 'Score', main = 'Diagnostic Test for Overlap Assumption'
)
```
## Comparison of methods
By default, the comparison plot uses a vertical layout (estimates stacked top to bottom). Set `vertical = FALSE` for a horizontal layout.
```{r}
plot(fdid_list(fdid_results_did, fdid_results_ols1, fdid_results_ols2,
fdid_results_ebal, fdid_results_aipw), vertical = FALSE
)
```
Common options:
- `xlab` and `main` change labels/titles.
- `ylim` sets the range of the estimates axis.
```{r}
plot(fdid_list(fdid_results_did, fdid_results_ols1, fdid_results_ols2,
fdid_results_ebal, fdid_results_aipw),
ylim = c(-5,5)
)
```
```{r}
plot(fdid_list(fdid_results_did, fdid_results_ols1,
fdid_results_ols2, fdid_results_ebal, fdid_results_aipw),
xlab = 'Static Estimates',
main = 'Comparing Different Methods', vertical = FALSE,
ylim = c(-10,10)
)
```
The labels reflect `method`, and add `vartype` when it is not the default `"robust"`. Other settings (e.g., `target.pop`) are not currently shown in the labels.
```{r}
plot(fdid_list(fdid_results_ols1, fdid_results_ols1_boot,
fdid_results_ols1_jack, fdid_results_ols2, fdid_results_ols2_target_1,
fdid_results_aipw),
ylim = c(-5,1), vertical = TRUE
)
```