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(
object,
...,
style = NULL,
sd,
ci_low,
ci_high,
df.t = NULL,
x,
x.shift = 0,
horiz = FALSE,
dict = getFixest_dict(),
keep,
drop,
order,
ci.width = "1%",
ci_level = 0.95,
add = FALSE,
pt.pch = c(20, 17, 15, 21, 24, 22),
pt.bg = NULL,
cex = 1,
pt.cex = cex,
col = 1:8,
pt.col = col,
ci.col = col,
lwd = 1,
pt.lwd = lwd,
ci.lwd = lwd,
ci.lty = 1,
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
)
iplot(
object,
...,
i.select = 1,
style = NULL,
sd,
ci_low,
ci_high,
df.t = NULL,
x,
x.shift = 0,
horiz = FALSE,
dict = getFixest_dict(),
keep,
drop,
order,
ci.width = "1%",
ci_level = 0.95,
add = FALSE,
pt.pch = c(20, 17, 15, 21, 24, 22),
pt.bg = NULL,
cex = 1,
pt.cex = cex,
col = 1:8,
pt.col = col,
ci.col = col,
lwd = 1,
pt.lwd = lwd,
ci.lwd = lwd,
ci.lty = 1,
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
)
object |
Can be either: i) an estimation object (obtained for example from
|
... |
Other arguments to be passed to |
style |
A character scalar giving the style of the plot to be used. You
can set styles with the function |
sd |
The standard errors of the estimates. It may be missing. |
ci_low |
If |
ci_high |
If |
df.t |
Integer scalar or |
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 |
drop |
Character vector. This element is used if some variables are not to be displayed.
This should be a vector of regular expressions (see |
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 |
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 |
pt.pch |
The patch of the coefficient estimates. Default is 1 (circle). |
pt.bg |
The background color of the point estimate (when the |
cex |
Numeric, default is 1. Expansion factor for the points |
pt.cex |
The size of the coefficient estimates. Default is the other 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 |
pt.col |
The color of the coefficient estimates. Default is equal to the other argument |
ci.col |
The color of the confidence intervals. Default is equal to the other argument |
lwd |
General line with. Default is 1. |
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, default is |
zero.par |
List. Parameters of the zero-line. The default values are
|
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 equal to 0 (typically to visualize reference points).
Either: i) "auto" (default), ii) a character vector of length 1, iii) a list
of length 1, iv) a named integer vector of length 1, or v) a numeric vector.
By default, in |
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 |
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.
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".
You can use the special character "%" (percentage) to make reference to the original variable
name instead of the aliased 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"
, or drop="%Month6"
(which makes
reference to its original name).
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)
est = feols(Petal.Length ~ Petal.Width + Sepal.Length +
Sepal.Width | Species, iris)
# Estimation results with clustered standard-errors
# (the default when fixed-effects are present)
est_clu = summary(est)
# Now with "regular" standard-errors
est_std = summary(est, se = "iid")
# You can plot the two results at once
coefplot(list(est_clu, est_std))
# 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 on the left.
# Second set of results: this time with
# standard-errors that are not clustered.
coefplot(est, se = "iid", x.shift = .2,
add = TRUE, col = 2, ci.lty = 2, pch=15)
# Note that we used 'se', an argument that will
# be passed to summary.fixest
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)
# When estimations contain interactions, as before,
# the default behavior of coefplot changes,
# it now only plots interactions:
coefplot(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.