| coefplot | R Documentation |
This function plots the results of estimations (coefficients and confidence intervals).
The function iplot restricts the output to variables created with i, either
interactions with factors or raw factors.
coefplot(
...,
objects = NULL,
style = NULL,
se,
ci_low,
ci_high,
df.t = NULL,
vcov = NULL,
cluster = NULL,
x,
x.shift = 0,
horiz = FALSE,
dict = NULL,
keep,
drop,
order,
ci.width = "1%",
ci_level = 0.95,
add = FALSE,
plot_prms = list(),
pch = c(20, 17, 15, 21, 24, 22),
col = 1:8,
cex = 1,
lty = 1,
lwd = 1,
ylim = NULL,
xlim = NULL,
pt.pch = pch,
pt.bg = NULL,
pt.cex = cex,
pt.col = col,
ci.col = col,
pt.lwd = lwd,
ci.lwd = lwd,
ci.lty = lty,
grid = TRUE,
grid.par = list(lty = 3, col = "gray"),
zero = TRUE,
zero.par = list(col = "black", lwd = 1),
pt.join = FALSE,
pt.join.par = list(col = pt.col, lwd = lwd),
ci.join = FALSE,
ci.join.par = list(lwd = lwd, col = col, lty = 2),
ci.fill = FALSE,
ci.fill.par = list(col = "lightgray", alpha = 0.5),
ref = "auto",
ref.line = "auto",
ref.line.par = list(col = "black", lty = 2),
lab.cex,
lab.min.cex = 0.85,
lab.max.mar = 0.25,
lab.fit = "auto",
xlim.add,
ylim.add,
only.params = FALSE,
sep,
as.multiple = FALSE,
bg,
group = "auto",
group.par = list(lwd = 2, line = 3, tcl = 0.75),
main = "Effect on __depvar__",
value.lab = "Estimate and __ci__ Conf. Int.",
ylab = NULL,
xlab = NULL,
sub = NULL,
i.select = NULL,
do_iplot = NULL
)
iplot(
...,
i.select = 1,
objects = NULL,
style = NULL,
se,
ci_low,
ci_high,
df.t = NULL,
vcov = NULL,
cluster = NULL,
x,
x.shift = 0,
horiz = FALSE,
dict = NULL,
keep,
drop,
order,
ci.width = "1%",
ci_level = 0.95,
add = FALSE,
plot_prms = list(),
pch = c(20, 17, 15, 21, 24, 22),
col = 1:8,
cex = 1,
lty = 1,
lwd = 1,
ylim = NULL,
xlim = NULL,
pt.pch = pch,
pt.bg = NULL,
pt.cex = cex,
pt.col = col,
ci.col = col,
pt.lwd = lwd,
ci.lwd = lwd,
ci.lty = lty,
grid = TRUE,
grid.par = list(lty = 3, col = "gray"),
zero = TRUE,
zero.par = list(col = "black", lwd = 1),
pt.join = FALSE,
pt.join.par = list(col = pt.col, lwd = lwd),
ci.join = FALSE,
ci.join.par = list(lwd = lwd, col = col, lty = 2),
ci.fill = FALSE,
ci.fill.par = list(col = "lightgray", alpha = 0.5),
ref = "auto",
ref.line = "auto",
ref.line.par = list(col = "black", lty = 2),
lab.cex,
lab.min.cex = 0.85,
lab.max.mar = 0.25,
lab.fit = "auto",
xlim.add,
ylim.add,
only.params = FALSE,
sep,
as.multiple = FALSE,
bg,
group = "auto",
group.par = list(lwd = 2, line = 3, tcl = 0.75),
main = "Effect on __depvar__",
value.lab = "Estimate and __ci__ Conf. Int.",
ylab = NULL,
xlab = NULL,
sub = NULL
)
... |
Other arguments to be passed to |
objects |
A list of |
style |
A character scalar giving the style of the plot to be used. You
can set styles with the function |
se |
The standard errors of the estimates. It may be missing. |
ci_low |
If |
ci_high |
If |
df.t |
Integer scalar or |
vcov |
Versatile argument to specify the VCOV. In general, it is either a character scalar equal to a VCOV type, either a formula of the form: vcov_type ~ variables. The VCOV types implemented are: "iid", "hetero" (or "HC1"), "cluster", "twoway", "NW" (or "newey_west"), "DK" (or "driscoll_kraay"), and "conley". It also accepts object from vcov_cluster, vcov_NW, NW, vcov_DK, DK, vcov_conley and conley. It also accepts covariance matrices computed externally. Finally it accepts functions to compute the covariances. See the vcov documentation in the vignette. You can pass several VCOVs (as above) if you nest them into a list.
If the number of VCOVs equals the number of models, eahc VCOV is mapped to the appropriate model.
If there is one model and several VCOVs, or if the first element of the list is equal to
|
cluster |
Tells how to cluster the standard-errors (if clustering is requested).
Can be either a list of vectors, a character vector of variable names, a formula or
an integer vector. Assume we want to perform 2-way clustering over |
x |
The value of the x-axis. If missing, the names of the argument |
x.shift |
Shifts the confidence intervals bars to the left or right, depending
on the value of |
horiz |
A logical scalar, default is |
dict |
A named character vector or a logical scalar. It changes the original variable names
to the ones contained in the |
keep |
Character vector. This element is used to display only a subset of variables. This
should be a vector of regular expressions (see Example: you have the variable |
drop |
Character vector. This element is used if some variables are not to be displayed.
This should be a vector of regular expressions (see Example: you have the variable |
order |
Character vector. This element is used if the user wants the variables to be
ordered in a certain way. This should be a vector of regular expressions (see Example: you have the following variables: |
ci.width |
The width of the extremities of the confidence intervals. Default is |
ci_level |
Scalar between 0 and 1: the level of the CI. By default it is equal to 0.95. |
add |
Default is |
plot_prms |
A named list. It may contain additionnal parameters to be passed to the plot. |
pch |
The patch of the coefficient estimates. Default is 1 (circle).
This is an alias to tha argument |
col |
The color of the points and the confidence intervals. Default is 1
("black"). Note that you can set the colors separately for each of them
with |
cex |
Numeric, default is 1. Expansion factor for the points |
lty |
The line type of the confidence intervals. Default is 1.
This is an alias to the argument |
lwd |
General line with. Default is 1. |
ylim |
Numeric vector of length 2 which gives the limits of the plotting region for
the y-axis. The default is |
xlim |
Numeric vector of length 2 which gives the limits of the plotting region for
the x-axis. The default is |
pt.pch |
The patch of the coefficient estimates. Default is 1 (circle). |
pt.bg |
The background color of the point estimate (when the |
pt.cex |
The size of the coefficient estimates. Default is the other argument |
pt.col |
The color of the coefficient estimates. Default is equal to the argument |
ci.col |
The color of the confidence intervals. Default is equal to the argument |
pt.lwd |
The line width of the coefficient estimates. Default is equal to
the other argument |
ci.lwd |
The line width of the confidence intervals. Default is equal to
the other argument |
ci.lty |
The line type of the confidence intervals. Default is 1. |
grid |
Logical, default is |
grid.par |
List. Parameters of the grid. The default values are: |
zero |
Logical scalar, default is |
zero.par |
A named list of graphical parameters or a logical scalar.
This argument is a list containing the graphical parameters used to draw the zero-line.
The default value is |
pt.join |
Logical, default is |
pt.join.par |
List. Parameters of the line joining the coefficients. The
default values are: |
ci.join |
Logical default to |
ci.join.par |
A list of parameters to be passed to |
ci.fill |
Logical default to |
ci.fill.par |
A list of parameters to be passed to |
ref |
Used to add points at |
ref.line |
Logical or numeric, default is "auto", whose behavior depends
on the situation. It is |
ref.line.par |
List. Parameters of the vertical line on the reference. The
default values are: |
lab.cex |
The size of the labels of the coefficients. Default is missing.
It is automatically set by an internal algorithm which can go as low as |
lab.min.cex |
The minimum size of the coefficients labels, as set by the internal algorithm. Default is 0.85. |
lab.max.mar |
The maximum size the left margin can take when trying to fit
the coefficient labels into it (only when |
lab.fit |
The method to fit the coefficient labels into the plotting region
(only when |
xlim.add |
A numeric vector of length 1 or 2. It represents an extension
factor of xlim, in percentage. Eg: |
ylim.add |
A numeric vector of length 1 or 2. It represents an extension
factor of ylim, in percentage. Eg: |
only.params |
Logical, default is |
sep |
The distance between two estimates – only when argument |
as.multiple |
Logical: default is |
bg |
Background color for the plot. By default it is white. |
group |
A list, default is missing. Each element of the list reports the
coefficients to be grouped while the name of the element is the group name. Each
element of the list can be either: i) a character vector of length 1, ii) of
length 2, or ii) a numeric vector. If equal to: i) then it is interpreted as
a pattern: all element fitting the regular expression will be grouped (note that
you can use the special character "^^" to clean the beginning of the names, see
example), if ii) it corresponds to the first and last elements to be grouped,
if iii) it corresponds to the coefficients numbers to be grouped. If equal to
a character vector, you can use a percentage to tell the algorithm to look at
the coefficients before aliasing (e.g. |
group.par |
A list of parameters controlling the display of the group. The
parameters controlling the line are: |
main |
The title of the plot. Default is |
value.lab |
The label to appear on the side of the coefficient values. If
|
ylab |
The label of the y-axis, default is |
xlab |
The label of the x-axis, default is |
sub |
A subtitle, default is |
i.select |
Integer scalar, default is 1. In |
do_iplot |
Logical, default is |
iplot(): Plots the coefficients generated with i()
The function coefplot dispose of many arguments to parametrize the plots. Most
of these arguments can be set once an for all using the function setFixest_coefplot.
See Example 3 below for a demonstration.
The function iplot restricts coefplot to interactions or factors created
with the function i. Only one of the i-variables will be plotted at a time.
If you have several i-variables, you can navigate through them with the i.select argument.
The argument i.select is an index that will go through all the i-variables.
It will work well if the variables are pure, meaning not interacted with other
variables. If the i-variables are interacted, the index may have an odd behavior
but will (in most cases) work all the same, just try some numbers up until you
(hopefully) obtain the graph you want.
Note, importantly, that interactions of two factor variables are (in general) disregarded since they would require a 3-D plot to be properly represented.
You can add plotmath mathematical expressions in the arguments
main, sub, xlab, or ylab. To do so, start the character string with an ampersand.
For example main = "&lambda^2".
The arguments keep, drop and order use regular expressions. If you are not aware
of regular expressions, I urge you to learn it, since it is an extremely powerful way
to manipulate character strings (and it exists across most programming languages).
For example drop = "Wind" would drop any variable whose name contains "Wind". Note that
variables such as "Temp:Wind" or "StrongWind" do contain "Wind", so would be dropped.
To drop only the variable named "Wind", you need to use
drop = "^Wind$" (with "^" meaning beginning, resp. "$" meaning end,
of the string => this is the language of regular expressions).
Although you can combine several regular expressions in a single character
string using pipes, drop also accepts a vector of regular expressions.
You can use the special character "!" (exclamation mark) to reverse the effect
of the regular expression (this feature is specific to this function).
For example drop = "!Wind" would drop any variable that does not contain "Wind".
By default, the regular expressions are checked against the variables after
they have been renamed with the dictionary (argument dict).
You can use the *_raw versions of drop/keep/order to apply the regular
expressions on the original variable names.
Note that alternatively you can use the special character "%" (percentage) at the
beginning of drop/keep/order's regular expressions to refer to the original variable name.
For example, you have a variable named "Month6",
and use a dictionary dict = c(Month6="June").
Thus the variable will be displayed as "June".
If you want to delete that variable, you can use either drop="June", drop_raw="Month6",
or drop="%Month6".
The argument order takes in a vector of regular expressions, the order will follow the
elements of this vector. The vector gives a list of priorities,
on the left the elements with highest priority.
For example, order = c("Wind", "!Inter", "!Temp") would give highest priorities to
the variables containing "Wind" (which would then appear first),
second highest priority is the variables not containing "Inter", last,
with lowest priority, the variables not containing "Temp".
If you had the following variables: (Intercept), Temp:Wind, Wind, Temp you
would end up with the following order: Wind, Temp:Wind, Temp, (Intercept).
Laurent Berge
See setFixest_coefplot to set the default values of coefplot, and the estimation
functions: e.g. feols, fepois, feglm, fenegbin.
#
# Example 1: Stacking two sets of results on the same graph
#
# Estimation on Iris data with one fixed-effect (Species)
# + we cluster the standard-errors
est = feols(Petal.Length ~ Petal.Width + Sepal.Width | Species,
iris, vcov = "cluster")
# Now with "regular" standard-errors
est_std = summary(est, vcov = "iid")
# You can plot the two results at once
coefplot(est, est_std)
# You could also use the argument vcov
coefplot(est, vcov = list("cluster", "iid"))
# Alternatively, you can use the argument x.shift
# to do it sequentially:
# First graph with clustered standard-errors
coefplot(est, x.shift = -.2)
# 'x.shift' was used to shift the coefficients to the left.
# Second set of results: this time with
# standard-errors that are not clustered.
coefplot(est, vcov = "iid", x.shift = .2,
add = TRUE, col = 2, ci.lty = 2, pch = 15)
legend("topright", col = 1:2, pch = 20, lwd = 1, lty = 1:2,
legend = c("Clustered", "IID"), title = "Standard-Errors")
#
# Example 2: Interactions
#
# Now we estimate and plot the "yearly" treatment effects
data(base_did)
base_inter = base_did
# We interact the variable 'period' with the variable 'treat'
est_did = feols(y ~ x1 + i(period, treat, 5) | id + period, base_inter)
# In the estimation, the variable treat is interacted
# with each value of period but 5, set as a reference
# coefplot will show all the coefficients:
coefplot(est_did)
# Note that the grouping of the coefficients is due to 'group = "auto"'
# If you want to keep only the coefficients
# created with i() (ie the interactions), use iplot
iplot(est_did)
# We can see that the graph is different from before:
# - only interactions are shown,
# - the reference is present,
# => this is fully flexible
iplot(est_did, ref.line = FALSE, pt.join = TRUE)
#
# What if the interacted variable is not numeric?
# Let's create a "month" variable
all_months = c("aug", "sept", "oct", "nov", "dec", "jan",
"feb", "mar", "apr", "may", "jun", "jul")
base_inter$period_month = all_months[base_inter$period]
# The new estimation
est = feols(y ~ x1 + i(period_month, treat, "oct") | id+period, base_inter)
# Since 'period_month' of type character, coefplot sorts it
iplot(est)
# To respect a plotting order, use a factor
base_inter$month_factor = factor(base_inter$period_month, levels = all_months)
est = feols(y ~ x1 + i(month_factor, treat, "oct") | id + period, base_inter)
iplot(est)
#
# Example 3: Setting defaults
#
# coefplot has many arguments, which makes it highly flexible.
# If you don't like the default style of coefplot. No worries,
# you can set *your* default by using the function
# setFixest_coefplot()
dict = c("Petal.Length"="Length (Petal)", "Petal.Width"="Width (Petal)",
"Sepal.Length"="Length (Sepal)", "Sepal.Width"="Width (Sepal)")
setFixest_coefplot(ci.col = 2, pt.col = "darkblue", ci.lwd = 3,
pt.cex = 2, pt.pch = 15, ci.width = 0, dict = dict)
est = feols(Petal.Length ~ Petal.Width + Sepal.Length +
Sepal.Width + i(Species), iris)
# And that's it
coefplot(est)
# You can set separate default values for iplot
setFixest_coefplot("iplot", pt.join = TRUE, pt.join.par = list(lwd = 2, lty = 2))
iplot(est)
# To reset to the default settings:
setFixest_coefplot("all", reset = TRUE)
coefplot(est)
#
# Example 4: group + cleaning
#
# You can use the argument group to group variables
# You can further use the special character "^^" to clean
# the beginning of the coef. name: particularly useful for factors
est = feols(Petal.Length ~ Petal.Width + Sepal.Length +
Sepal.Width + Species, iris)
# No grouping:
coefplot(est)
# now we group by Sepal and Species
coefplot(est, group = list(Sepal = "Sepal", Species = "Species"))
# now we group + clean the beginning of the names using the special character ^^
coefplot(est, group = list(Sepal = "^^Sepal.", Species = "^^Species"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.