I'm using fixest::coefplot()
to plot the coefficients of a regression (with fixed effects) , but I keep having the same problem when trying to use both "keep" and "x".
The error I get:
"Argument 'x' must have the same length as the number of coefficients"
Reproducing the problem with the iris dataset:
m1 <- lm(Sepal.Width ~ Petal.Length +
factor(Species), data = iris)
fixest::coefplot(m1_robust_clustered,
main = "", horiz = T,
keep = c("Petal.Length"),
x = "Petal Length")
I'm trying to use "keep" to select only one coefficient (i.e., "Petal.Length"). But when I try to rename it using "x" (say it, to "Petal Length"), I get the following error:
Error in coefplot_prms(object = object, ..., sd = sd, ci_low = ci_low, : Argument 'x' must have the same length as the number of coefficients (currently 1 vs 4).
(1) Now, I get what the error is. Still, I wanted to know if there is a way to fix it without having to, for instance, rename every single variable before and then using "keep" (on my data, I have fixed effects for roughly 50 countries, so renaming before would be very troublesome).
(2) I also know that probably using fixest::feols
or any other "native" fixest function would probably solve this issue. I was just wondering if there's a way to make it work :)
(3) I could also use different coefplot()
functions from other packages, but fixest
allows me to also use robust clustered std errors, which is a "must" for me in this case (even if not present in the example above).
Thanks in advance!
This should do it. Think about specifying x
before keep
. x
should match up with the coefficients in the model, potentially applying new names. The names of variables you don't want to keep can be anything, including NA
as below. The values in keep
are used in a regular expression to find suitable values in x
to keep.
m1 <- lm(Sepal.Width ~ Petal.Length +
Species, data = iris)
fixest::coefplot(m1,
main = "", horiz = F,
x = c(NA, "Petal Length", "Versicolor", NA),
keep = c("Petal Length", "Vers")
)
Created on 2024-04-05 with reprex v2.0.2