Skip to content

Commit

Permalink
Rather limit to just vcov and alternatives for safety.
Browse files Browse the repository at this point in the history
  • Loading branch information
grantmcdermott committed Dec 12, 2023
1 parent f837d3c commit 48f9e37
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
7 changes: 4 additions & 3 deletions R/ggcoefplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@
#' channel. For example, we can make the CI band lighter with
#' `ci.fill.par = list(alpha = 0.2)` (the default alpha is 0.3).
#' * `dict` a dictionary for overriding coefficient names.
#' * All other `...` arguments are passed to `summary.fixest` before plotting
#' (e.g., for on-the-fly VCOV adjustment) and will be silently ignored if
#' not relevant.
#' * `vcov`, `cluster` or `se` as alternative options for adjusting the
#' standard errors of the model object(s) on the fly. See `summary.fixest` for
#' details. Written here in superseding order; `cluster` will only be
#' considered if `vcov` is not null, etc.
#' @details These functions generally try to mimic the functionality and (where
#' appropriate) arguments of `fixest::coefplot` and `fixest::iplot` as
#' closely as possible. However, by leveraging the ggplot2 API and
Expand Down
32 changes: 26 additions & 6 deletions R/ggiplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ ggiplot = function(
ref.line.par = list(col = "black", lty = 2, lwd = 0.3)
if (!is.null(dots[["ref.line.par"]])) ref.line.par = utils::modifyList(ref.line.par, dots[["ref.line.par"]])

# VCOV adjustments (if any)
vcov = if (!is.null(dots[['vcov']])) dots[['vcov']] else NULL
cluster = if (!is.null(dots[['cluster']])) dots[['cluster']] else NULL
se = if (!is.null(dots[['se']])) dots[['se']] else NULL

# The next few blocks grab the underlying iplot/coefplot data, contingent on the
# object that was passed into the function (i.e. fixest, fixest_multi, or
# list)
Expand All @@ -57,11 +62,21 @@ ggiplot = function(
if (inherits(object, c("fixest", "fixest_multi"))) {

if (length(ci_level) == 1) {
data = iplot_data_func(object, .ci_level = ci_level, .dict = dict, .aggr_es = aggr_eff, .keep = keep, .drop = drop, .group = group, .i.select = i.select, ...)
data = iplot_data_func(
object,
.ci_level = ci_level, .dict = dict, .aggr_es = aggr_eff,
.keep = keep, .drop = drop, .group = group, .i.select = i.select,
.vcov = vcov, .cluster = cluster, .se = se
)
} else {
data = lapply(
ci_level,
function(ci_l) iplot_data_func(object, .ci_level = ci_l, .dict = dict, .aggr_es = aggr_eff, .keep = keep, .drop = drop, .group = group, .i.select = i.select, ...)
function(ci_l) iplot_data_func(
object,
.ci_level = ci_l, .dict = dict, .aggr_es = aggr_eff,
.keep = keep, .drop = drop, .group = group, .i.select = i.select,
.vcov = vcov, .cluster = cluster, .se = se
)
)
data = do.call("rbind", data)
}
Expand All @@ -81,13 +96,18 @@ ggiplot = function(
if (length(ci_level) == 1) {
data = lapply(
object, iplot_data_func,
.ci_level = ci_level, .dict = dict, .aggr_es = aggr_eff, .group = group, .i.select = i.select, ...
.ci_level = ci_level, .dict = dict, .aggr_es = aggr_eff,
.group = group, .i.select = i.select,
.vcov = vcov, .cluster = cluster, .se = se
)
} else {
data = lapply(ci_level, function(ci_l) {
lapply(object, iplot_data_func,
.ci_level = ci_l,
.dict = dict, .aggr_es = aggr_eff, .group = group, .i.select = i.select, ...
lapply(
object, iplot_data_func,
.ci_level = ci_l,
.dict = dict, .aggr_es = aggr_eff,
.group = group, .i.select = i.select,
.vcov = vcov, .cluster = cluster, .se = se
)
})
data = do.call(function(...) Map("rbind", ...), data)
Expand Down
34 changes: 26 additions & 8 deletions R/iplot_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
#' aggregated mean treatment effects for some subset of the model should be
#' added as a column to the returned data frame. Passed to
#' `aggr_es(..., aggregation = "mean")`.
#' @param ... Other arguments passed on to `summary.fixest`, e.g. for
#' post-estimation VCOV adjustment. Irrelevant arguments will be silently
#' ignored.
#' @param .vcov,.cluster,.se Alternative options for adjusting the standard
#' errors of the model object on the fly. See `summary.fixest` for details
#' (although note that the "." period prefix should be ignored in the latter's
#' argument documentation). Written here in superseding order; `.cluster` will
#' only be considered if `.vcov` is not null, etc.
#' @details This function is a wrapper around
#' `fixest::iplot(..., only.params = TRUE)`, but with various checks and tweaks
#' to better facilitate plotting with `ggplot2` and handling of complex object
Expand Down Expand Up @@ -82,7 +84,9 @@ iplot_data = function(
# .aggr_es = c("none", "post", "pre", "both"),
.aggr_es = NULL,
.group = "auto",
...
.vcov = NULL,
.cluster = NULL,
.se = NULL
) {

# .aggr_es = match.arg(.aggr_es)
Expand All @@ -99,8 +103,14 @@ iplot_data = function(
.group = NULL
}

# Catch any args pass through ... to summary.fixest (e.g., vcov adjustments)
object = summary(object, ...)
# Catch VCOV adjustments (if any)
if (!is.null(.vcov)) {
object = summary(object, vcov = .vcov)
} else if (!is.null(.cluster)) {
object = summary(object, cluster = .cluster)
} else if (!is.null(.se)) {
object = summary(object, se = .se)
}

p = coefplot(object, only.params = TRUE, ci_level = .ci_level, dict = .dict, keep = .keep, drop = .drop, internal.only.i = .internal.only.i, i.select = .i.select)
d = p$prms
Expand Down Expand Up @@ -432,9 +442,17 @@ coefplot_data = function(
.internal.only.i = FALSE,
.i.select = 1,
.aggr_es = "none",
...
.vcov = NULL,
.cluster = NULL,
.se = NULL
) {

iplot_data(object, .ci_level = .ci_level, .dict = .dict, .keep = .keep, .drop = .drop, .internal.only.i = .internal.only.i, .group = .group, ...)
iplot_data(
object,
.ci_level = .ci_level, .dict = .dict,
.keep = .keep, .drop = .drop,
.internal.only.i = .internal.only.i, .group = .group,
.vcov = .vcov, .cluster = .cluster, .se = .se
)

}
7 changes: 4 additions & 3 deletions man/ggcoefplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions man/iplot_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48f9e37

Please sign in to comment.