| plot.fect | R Documentation |
Visualizes results from a fect estimation.
## S3 method for class 'fect'
plot(
x,
type = NULL,
restrict = "rm",
loo = FALSE,
highlight = NULL,
highlight.fill = FALSE,
plot.ci = NULL,
show.points = TRUE,
loess.fit = TRUE,
show.group = NULL,
bound = NULL,
show.count = TRUE,
proportion = 0.3,
pre.periods = NULL,
f.threshold = NULL,
tost.threshold = NULL,
effect.bound.ratio = FALSE,
stats = NULL,
stats.labs = NULL,
raw = "none",
vis = NULL,
main = NULL,
xlim = NULL,
ylim = NULL,
xlab = NULL,
ylab = NULL,
xangle = NULL,
yangle = NULL,
xbreaks = NULL,
ybreaks = NULL,
xticklabels = NULL,
yticklabels = NULL,
gridOff = NULL,
legendOff = FALSE,
legend.pos = NULL,
legend.nrow = NULL,
legend.labs = NULL,
stats.pos = NULL,
show.stats = TRUE,
theme.bw = TRUE,
nfactors = NULL,
include.FE = TRUE,
id = NULL,
cex.main = NULL,
cex.main.sub = NULL,
cex.axis = NULL,
cex.lab = NULL,
cex.legend = NULL,
cex.text = NULL,
axis.adjust = FALSE,
axis.lab = "both",
axis.lab.gap = c(0, 0),
shade.post = FALSE,
start0 = FALSE,
return.test = FALSE,
return.data = FALSE,
balance = NULL,
weight = NULL,
lcolor = NULL,
lwidth = NULL,
ltype = NULL,
line.color = NULL,
line.width = NULL,
count = NULL,
preset = NULL,
connected = NULL,
ci.outline = FALSE,
color = NULL,
pre.color = NULL,
post.color = NULL,
est.lwidth = NULL,
est.pointsize = NULL,
count.color = NULL,
count.alpha = NULL,
count.outline.color = NULL,
placebo.color = NULL,
carryover.color = NULL,
carryover.rm.color = NULL,
sens.original.color = NULL,
sens.colors = NULL,
counterfactual.color = NULL,
counterfactual.raw.controls.color = NULL,
counterfactual.raw.treated.color = NULL,
counterfactual.linetype = NULL,
box.control = NULL,
box.treat = NULL,
calendar.color = NULL,
calendar.lcolor = NULL,
calendar.cicolor = NULL,
heterogeneous.color = NULL,
heterogeneous.lcolor = NULL,
heterogeneous.cicolor = NULL,
equiv.color = NULL,
status.treat.color = NULL,
status.control.color = NULL,
status.missing.color = NULL,
status.removed.color = NULL,
status.placebo.color = NULL,
status.carryover.color = NULL,
status.carryover.rm.color = NULL,
status.balanced.post.color = NULL,
status.balanced.pre.color = NULL,
status.background.color = NULL,
covariate = NULL,
covariate.labels = NULL,
covariate.value = NULL,
covariate.value.range = FALSE,
relative.time = FALSE,
pretreatment = FALSE,
num.pretreatment = 3,
cm = FALSE,
legacy.style = FALSE,
...
)
x |
A fitted |
type |
Plot type. Options include: |
restrict |
For sensitivity plots ( |
loo |
Logical; if |
highlight |
Controls which test-period types receive the colored-glyph treatment in placebo / carryover plots. Accepts:
|
highlight.fill |
Logical. If |
plot.ci |
Specifies the confidence interval to be plotted. Options include:
|
show.points |
Logical; if |
show.group |
Optional character string or |
bound |
Which bounds to display in bounding/equivalence tests:
|
show.count |
Logical; if |
proportion |
Numeric (0 to 1); for event study plots, restricts plotted time points to those where the number of treated units is at least this fraction of the maximum number of treated units observed across all periods. Default is |
pre.periods |
Optional numeric vector; specifies pre-treatment periods to be used for bounding and equivalence tests. Default is |
f.threshold |
Numeric or |
tost.threshold |
Numeric or |
effect.bound.ratio |
Logical; if |
stats |
Character vector or |
stats.labs |
Character vector or |
raw |
For |
vis |
(Deprecated) Formerly controlled line display for placebo/carryover tests. This is now handled by |
main |
Character string or |
xlim |
Numeric vector of length 2 or |
ylim |
Numeric vector of length 2 or |
xlab |
Character string or |
ylab |
Character string or |
xangle |
Numeric or |
yangle |
Numeric or |
xbreaks |
Numeric vector or |
ybreaks |
Numeric vector or |
xticklabels |
Character vector or |
yticklabels |
Character vector or |
gridOff |
Logical or |
legendOff |
Logical; if |
legend.pos |
Character string or |
legend.nrow |
Integer or |
legend.labs |
Character vector or |
stats.pos |
Numeric vector of length 2 or |
show.stats |
Logical; if |
theme.bw |
Logical; if |
nfactors |
Integer or |
include.FE |
Logical; if |
id |
Character vector or |
cex.main |
Numeric or |
cex.main.sub |
Numeric or |
cex.axis |
Numeric or |
cex.lab |
Numeric or |
cex.legend |
Numeric or |
cex.text |
Numeric or |
axis.adjust |
Logical; if |
axis.lab |
Controls axis labeling in |
axis.lab.gap |
Numeric vector of length 1 or 2; gap spacing for axis labels in |
shade.post |
Logical; if |
start0 |
Logical; if |
return.test |
Logical; if |
return.data |
Logical; if |
balance |
Logical or |
weight |
Logical or |
lcolor |
Character vector of length 1 or 2, or |
lwidth |
Numeric vector of length 1 or 2, or |
ltype |
Character vector of length 1 or 2, or |
line.color |
(Deprecated) Use |
line.width |
(Deprecated) Use |
count |
(Deprecated) Use |
preset |
A character string specifying a color and style preset: |
connected |
Logical or |
ci.outline |
Logical; if |
color |
Character string or |
pre.color |
Character string or |
post.color |
Character string or |
est.lwidth |
Numeric or |
est.pointsize |
Numeric or |
count.color |
Character string or |
count.alpha |
Numeric (0-1) or |
count.outline.color |
Character string or |
placebo.color |
Character string or |
carryover.color |
Character string or |
carryover.rm.color |
Character string or |
sens.original.color |
Character string or |
sens.colors |
Vector of colors or |
counterfactual.color |
Character string or |
counterfactual.raw.controls.color |
Character string or |
counterfactual.raw.treated.color |
Character string or |
counterfactual.linetype |
Character string or |
box.control |
Character string or |
box.treat |
Character string or |
calendar.color |
Character string or |
calendar.lcolor |
Character string or |
calendar.cicolor |
Character string or |
heterogeneous.color |
Character string or |
heterogeneous.lcolor |
Character string or |
heterogeneous.cicolor |
Character string or |
equiv.color |
Character string or |
status.treat.color |
Character string or |
status.control.color |
Character string or |
status.missing.color |
Character string or |
status.removed.color |
Character string or |
status.placebo.color |
Character string or |
status.carryover.color |
Character string or |
status.carryover.rm.color |
Character string or |
status.balanced.post.color |
Character string or |
status.balanced.pre.color |
Character string or |
status.background.color |
Character string or |
covariate |
Character string or |
covariate.labels |
Character vector or |
covariate.value |
Numeric or |
covariate.value.range |
Logical; if |
relative.time |
Logical; if |
pretreatment |
Logical; if |
num.pretreatment |
Integer; number of pre-treatment periods to include when |
cm |
Logical; if |
legacy.style |
Logical; if |
loess.fit |
Logical; if |
... |
Additional graphical parameters passed to internal plotting routines, primarily those accepted by |
plot.fect generates various visualizations for objects estimated via fect. Depending on
the selected type, it can show treatment-effect dynamics ("gap", "exit", "cumul"),
equivalence test outcomes ("equiv"), factor/loadings for factor models ("factors", "loadings"),
raw vs. counterfactual trajectories ("counterfactual"), treatment status ("status"),
effects by calendar time ("calendar"), distribution of individual effects ("box"),
or sensitivity analysis results ("sens", "sens_es").
The preset argument allows for quick customization of plot aesthetics using predefined color schemes.
Many individual color and style parameters can be set to further customize the appearance, and these will override any settings from a preset or the base style.
The function heavily relies on ggplot2 for plotting and esplot for event-study style visualizations.
By default, a ggplot2 object representing the plot.
If return.data=TRUE, returns a list containing:
p |
The |
data |
A named list of data frames containing the data used to construct each layer of the plot (no extra all-NA columns are added). |
If return.test=TRUE as well, the list also contains:
test.out |
A list or data frame containing relevant test statistics (such as F-tests, equivalence p-values, placebo test p-values, etc.), if applicable to the plot type and options chosen. |
If return.test=TRUE and return.data=FALSE, returns a list containing p and test.out.
Licheng Liu, Ye Wang, Yiqing Xu, Ziyi Liu, Rivka Lipkovitz
Xu, Y. (2017). Generalized Synthetic Control Method: Causal Inference with Interactive Fixed Effects Models. Political Analysis, 25(1), 57–76.
Liu, L., Wang, Y., & Xu, Y. (2022). A Practical Guide to Counterfactual Estimators for Causal Inference with Time-Series Cross-Sectional Data.
American Journal of Political Science, 66(1), 220-237. (Provides context for esplot which is used internally for many plot types)
Rambachan, A., & Roth, J. (2023). A More Credible Approach to Parallel Trends.
Review of Economic Studies, 90(5), 2555-2591. (Provides context for sensitivity analysis plots like type="sens" and type="sens_es")
library(fect)
# For CRAN checks, use a small number of bootstraps
# In practice, use a larger number (e.g., nboots = 200 or more)
if(requireNamespace("ggplot2") && requireNamespace("ggrepel")) {
data(simdata)
# Estimate with fixed effects method
out.fect <- fect(
Y ~ D + X1 + X2,
data = simdata,
index = c("id","time"),
method = "fe",
force = "two-way",
se = TRUE,
parallel = FALSE,
nboots = 5 # nboots low for example
)
# Default gap plot
plot(out.fect, main = "Estimated ATT (FEct)", ylab = "Effect of D on Y")
# Gap plot with vibrant preset and custom line color
# plot(out.fect, preset = "vibrant", color = "darkgreen",
# main = "Estimated ATT (Vibrant Preset, Custom Line)")
# Counterfactual plot for the first treated unit
# Need to know the ID of a treated unit. Let's find one.
treated_ids <- unique(simdata$id[simdata$D == 1])
if (length(treated_ids) > 0) {
plot(out.fect, type = "counterfactual", id = treated_ids[1],
main = paste("Counterfactual for Unit", treated_ids[1]))
}
# Status plot
plot(out.fect, type = "status")
# Cumulative effect plot (if est.eff is available from fect call)
# This example might not have it by default, but showing how to call
# out.fect.cumul <- fect(Y ~ D, data = simdata, index = c("id","time"), method = "fe",
# cumulative = TRUE, se = TRUE, parallel = FALSE, nboots = 5)
# if (exists("out.fect.cumul")) {
# plot(out.fect.cumul, type = "cumul", main = "Cumulative ATT")
# }
# Example for sensitivity plot (requires IFE/GSYNTH method and sensitivity analysis)
# \donttest{
# out.ife <- fect(Y ~ D, data = simdata, index = c("id","time"),
# method = "ife", se = TRUE, r = 2,
# sensitivity.analysis = TRUE, sensitivity.plot = FALSE, # run analysis
# parallel = FALSE, nboots = 5) # nboots low for example
# if (!is.null(out.ife$sensitivity.rm)) {
# plot(out.ife, type = "sens", restrict = "rm",
# main = "Sensitivity Analysis (Relative Magnitude)")
# plot(out.ife, type = "sens_es", restrict = "rm",
# main = "Event-Study Sensitivity (Relative Magnitude)")
# }
# }
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.