| fitstat | R Documentation |
Computes various fit statistics or hypothesis tests for fixest estimations.
These statistics can also be used within etable or set to be displayed when printing the
model with setFixest_print.
fitstat(
x,
type,
vcov = NULL,
cluster = NULL,
ssc = NULL,
simplify = FALSE,
verbose = TRUE,
show_types = FALSE,
htest = FALSE,
frame = parent.frame(),
...
)
x |
A |
type |
Character vector or one sided formula. No default.
The type of fit statistic or tests to be computed.
The classic ones are: |
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: |
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 |
ssc |
An object of class |
simplify |
Logical, default is |
verbose |
Logical, default is |
show_types |
Logical, default is |
htest |
Logical scalar, default is |
frame |
An environment in which to evaluate variables, default is |
... |
For internal use. |
Any statistic available in fitstat can also be used directly in etable via its
argument fitstat. For example etable(est, fitstat = c("r2", "rmse")) will report
the R2 and the RMSE in the fit statistics section of the table.
If one wants to change the default set of statistics reported when printing the model,
this is possible with the function setFixest_print which accepts the argument fitstat.
For example setFixest_print(fitstat = ~r2 + f) will report the R2 and the F-test
for each estimation.
By default an object of class fixest_fitstat is returned.
This object is a simple list containing the statistics/tests requested
by the user.
For example fitstat(x, c("r2", "f")) returns a list with two elements named r2 and f.
Each statistic from the fixest_fitstat object can be of two types:
a list of numeric scalars whose elements depend on the type of fit statistic/test.
For example the F-test, accessed with f, contains the elements
stat, p, df1, and df2. The wald test contains the elements
stat, p, df1, df2 and vcov. The likelihood ratio, lr, contains the elements
stat, p, df. Etc.
a numeric scalar, for either: i) scalar fit statistics, like r2 or rmse,
ii) when a specific element of a fit statistic is accessed, like e.g. f.stat
which reports the stat element of the fit statistic f.
The types of fit statistics and their structure are detailed
in the section Available types of this documentation.
The class fixest_fitstat has only a dedicated print method.
If the argument htest=TRUE, the object returned is a plain list containing the requested
statistics and each test is formatted according to the htest class from the stats package.
htest objects are lists containing the following elements:
A numeric scalar equal to test statistic.
A numeric scalar equal to the p-value of the test.
A list containing various parameters used to calculate the
p-value and the statistic. It can be df, df1, df2, or vcov.
The alternative hypothesis.
The name of the test.
The estimation call on which the test is applied.
Using verbose = FALSE removes the fixest_fitstat class from the returned object,
turning it into a plain list (this is ignored when htest=TRUE).
If only one type is selected, simplify = TRUE leads to the selected statistic to
be directly returned (and hence is not nested inside a list).
For example fitstat(x, "r2", simplify = TRUE) returns a simple scalar.
If show_types=TRUE, this function returns instead a character vector with
all the available types.
You can register custom fit statistics with the function fitstat_register.
These statistics can be anything. Please see its documentation.
The types are case sensitive, please use lower case only. The types available are:
n, ll, aic, bic, rmse: The number of observations, the log-likelihood, the AIC, the BIC and the root mean squared error, respectively.
my: Mean of the dependent variable.
g: The degrees of freedom used to compute the t-test (it influences the p-values of the coefficients). When the VCOV is clustered, this value is equal to the minimum cluster size, otherwise, it is equal to the sample size minus the number of variables.
r2, ar2, wr2, awr2, pr2, apr2, wpr2, awpr2: All r2 that can be
obtained with the function r2. The a stands for 'adjusted', the w for 'within' and
the p for 'pseudo'. Note that the order of the letters a, w and p does not matter.
The pseudo R2s are McFadden's R2s (ratios of log-likelihoods).
theta: The over-dispersion parameter in Negative Binomial models. Low values mean high overdispersion.
f, wf: The F-tests of nullity of the coefficients. The w stands for
'within'. These types return the following values: stat, p, df1 and df2.
If you want to display only one of these, use their name after a dot: e.g. f.stat
will give the statistic of the F-test, or wf.p will give the p-values of the F-test
on the projected model (i.e. projected onto the fixed-effects).
wald: Wald test of joint nullity of the coefficients. This test always excludes
the intercept and the fixed-effects. This type returns the following values:
stat, p, df1, df2 and vcov. The element vcov reports the way the VCOV
matrix was computed since it directly influences this statistic.
ivf, ivf1, ivf2, ivfall: These statistics are specific to IV estimations.
They report either the IV F-test (namely the Cragg-Donald F statistic in the presence
of only one endogenous regressor) of the first stage (ivf or ivf1), of the
second stage (ivf2) or of both (ivfall). The F-test of the first stage is
commonly named weak instrument test. The value of ivfall is only useful in etable
when both the 1st and 2nd stages are displayed (it leads to the 1st stage F-test(s)
to be displayed on the 1st stage estimation(s), and the 2nd stage one on the
2nd stage estimation – otherwise, ivf1 would also be displayed on the 2nd stage
estimation). These types return the following values: stat, p, df1 and df2.
ivwald, ivwald1, ivwald2, ivwaldall: These statistics are specific to IV
estimations. They report either the IV Wald-test of the first stage (ivwald or ivwald1),
of the second stage (ivwald2) or of both (ivwaldall). The Wald-test of the first stage
is commonly named weak instrument test. Note that if the estimation was done with a robust
VCOV and there is only one endogenous regressor, this is equivalent to the
Kleibergen-Paap statistic. The value of ivwaldall is only useful in etable when both
the 1st and 2nd stages are displayed (it leads to the 1st stage Wald-test(s) to be displayed
on the 1st stage estimation(s), and the 2nd stage one on the 2nd stage estimation –
otherwise, ivwald1 would also be displayed on the 2nd stage estimation). These types
return the following values: stat, p, df1, df2, and vcov.
cd: The Cragg-Donald test for weak instruments.
kpr: The Kleibergen-Paap test for weak instruments.
wh: This statistic is specific to IV estimations. Wu-Hausman endogeneity test.
H0 is the absence of endogeneity of the instrumented variables. It returns the following
values: stat, p, df1, df2.
sargan: Sargan test of overidentifying restrictions. H0: the instruments are
not correlated with the second stage residuals. It returns the
following values: stat, p, df.
lr, wlr: Likelihood ratio and within likelihood ratio tests. It returns
the following elements: stat, p, df. Concerning the within-LR test, note that,
contrary to estimations with femlm or feNmlm, estimations with feglm/fepois
need to estimate the model with fixed-effects only which may prove time-consuming
(depending on your model). Bottom line, if you really need the within-LR and estimate a
Poisson model, use femlm instead of fepois (the former uses direct ML maximization for
which the only FEs model is a by product).
data(trade)
gravity = feols(log(Euros) ~ log(dist_km) | Destination + Origin, trade)
# Extracting the 'working' number of observations used to compute the pvalues
fitstat(gravity, "g", simplify = TRUE)
# Some fit statistics
fitstat(gravity, ~ rmse + r2 + wald + wf)
# This is a simple list:
names(fitstat(gravity, ~ rmse + r2 + wald + wf))
# You can use them in etable
etable(gravity, fitstat = ~ rmse + r2 + wald + wf)
# For wald and wf, you could show the pvalue instead:
etable(gravity, fitstat = ~ rmse + r2 + wald.p + wf.p)
# You can display the tests with the htest format with htest=TRUE
fitstat(gravity, ~ rmse + r2 + wald + wf, htest = TRUE)
# Now let's display some statistics that are not built-in
# => we use fitstat_register to create them
# We need: a) type name, b) the function to be applied
# c) (optional) an alias
fitstat_register("tstand", function(x) tstat(x, se = "stand")[1], "t-stat (regular)")
fitstat_register("thc", function(x) tstat(x, se = "heter")[1], "t-stat (HC1)")
fitstat_register("t1w", function(x) tstat(x, se = "clus")[1], "t-stat (clustered)")
fitstat_register("t2w", function(x) tstat(x, se = "twow")[1], "t-stat (2-way)")
# Now we can use these keywords in fitstat:
etable(gravity, fitstat = ~ . + tstand + thc + t1w + t2w)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.