| plot_data | R Documentation |
Returns the tidy data.frame that plot() draws for a given type, so the
underlying series, weights, or placebo paths can be inspected, joined into a
table, or re-plotted directly. plot() stays the quick path; plot_data()
is the handle for anyone who wants to relabel simplified series names, feed
the numbers into their own figure, or postprocess them further.
plot_data(x, ...)
## Default S3 method:
plot_data(x, ...)
## S3 method for class 'coresynth'
plot_data(
x,
type = c("trend", "gap", "weights", "pred_weights"),
align = FALSE,
top_n = Inf,
show_donors = 0,
...
)
## S3 method for class 'scm_placebo'
plot_data(x, type = c("gaps", "ratios"), mspe_prune = Inf, ...)
x |
A |
... |
Passed to methods (unused by the current methods). |
type |
For a |
align |
For |
top_n |
For |
show_donors |
For |
mspe_prune |
For a |
The frame mirrors what the matching plot(x, type = ...) call shows, with
two deliberate departures that make it a better data source:
Plain column names (time, value, series, weight, ...) are used
instead of the dotted convention of augment(), since this is data to
manipulate rather than model-augmented observations.
The cosmetic "drop donors with weight below 1e-4" filter that
plot(type = "weights") applies is not used here: every donor is
returned (use top_n to subset), so the frame is the complete set of
weights.
Only the arguments that change which rows or values appear are accepted
(align, top_n, show_donors, mspe_prune); purely cosmetic arguments
of plot() (colors, labels, linetypes, vline, fill, ...) have no
data counterpart and are not part of this interface.
A tidy data.frame. Columns by type:
"trend"time, value, series ("Treated" /
"Synthetic Control"); with show_donors > 0, also "Donors" rows
and a unit column.
"gap"time, gap (treated minus synthetic control).
"weights"unit, weight; SDID fits add a panel column
("omega" unit weights, "lambda" time weights), with unit holding
the pre-period label for "lambda" rows.
"pred_weights"predictor, weight (sharp SCM only).
"gaps"time, gap, unit (NA for the treated series),
series ("Treated" / "Placebo (donor pool)").
"ratios"unit, ratio, series.
plot.coresynth(), plot.scm_placebo()
set.seed(1)
panel <- expand.grid(unit = 1:10, year = 1:20)
panel$treated <- as.integer(panel$unit == 5 & panel$year > 15)
panel$gdp <- panel$unit + 0.5 * panel$year +
rnorm(nrow(panel)) + 3 * panel$treated
fit <- scm_fit(gdp ~ treated | unit + year, data = panel, method = "scm")
head(plot_data(fit, type = "trend"))
plot_data(fit, type = "gap")
plot_data(fit, type = "weights")
# Relabel the simplified series names, then plot it yourself
df <- plot_data(fit, type = "trend")
df$series <- sub("Synthetic Control", "Synthetic Unit 5", df$series)
ggplot2::ggplot(df, ggplot2::aes(time, value, color = series)) +
ggplot2::geom_line()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.