This function estimates maximum likelihood models with any number of fixed-effects.
femlm(fml, data, family = c("poisson", "negbin", "logit", "gaussian"),
start = 0, fixef, offset, panel.id,
na_inf.rm = getFixest_na_inf.rm(), fixef.tol = 1e-05,
fixef.iter = 1000, nthreads = getFixest_nthreads(), verbose = 0,
warn = TRUE, notes = getFixest_notes(), theta.init, combine.quick,
...)fenegbin(fml, data, theta.init, start = 0, fixef, offset, panel.id,
na_inf.rm = getFixest_na_inf.rm(), fixef.tol = 1e-05,
fixef.iter = 1000, nthreads = getFixest_nthreads(), verbose = 0,
warn = TRUE, notes = getFixest_notes(), combine.quick, ...)
A formula representing the relation to be estimated. For example: fml = z~x+y
. To include fixed-effects, you can 1) either insert them in this formula using a pipe (e.g. fml = z~x+y|fixef_1+fixef_2
), or 2) either use the argument fixef
.
A data.frame containing the necessary variables to run the model. The variables of the non-linear right hand side of the formula are identified with this data.frame
names. Can also be a matrix.
Character scalar. It should provide the family. The possible values are "poisson" (Poisson model with log-link, the default), "negbin" (Negative Binomial model with log-link), "logit" (LOGIT model with log-link), "gaussian" (Gaussian model).
Starting values for the coefficients. Can be: i) a numeric of length 1 (e.g. start = 0
, the default), ii) a numeric vector of the exact same length as the number of variables, or iii) a named vector of any length (the names will be used to initialize the appropriate coefficients).
Character vector. The name/s of a/some variable/s within the dataset to be used as fixed-effects. These variables should contain the identifier of each observation (e.g., think of it as a panel identifier).
A formula or a numeric vector. An offset can be added to the estimation. If equal to a formula, it should be of the form (for example) ~0.5*x**2
. This offset is linearly added to the elements of the main formula 'fml'.
The panel identifiers. Can either be: i) a one sided formula (e.g. panel.id~id+time
), ii) a character vector of length 2 (e.g. panel.id=c('id', 'time')
, or iii) a character scalar of two variables separated by a comma (e.g. panel.id='id,time'
). Note that you can combine variables with ^
only inside formulas (see the dedicated section in feols
).
Logical, default is TRUE
. If the variables necessary for the estimation contain NA/Infs and na_inf.rm = TRUE
, then all observations containing NA are removed prior to estimation and a note is displayed detailing the number of observations removed. Otherwise, an error is raised.
Precision used to obtain the fixed-effects. Defaults to 1e-5
. It corresponds to the maximum absolute difference allowed between two coefficients of successive iterations. Argument fixef.tol
cannot be lower than 10000*.Machine$double.eps
. Note that this parameter is dynamically controlled by the algorithm.
Maximum number of iterations in the step obtaining the fixed-effects (only in use for 2+ fixed-effects). Default is 1000.
Integer: Number of nthreads to be used (accelerates the algorithm via the use of openMP routines). The default is to use the total number of nthreads available minus two. You can set permanently the number of nthreads used within this package using the function setFixest_nthreads
.
Integer, default is 0. It represents the level of information that should be reported during the optimisation process. If verbose=0
: nothing is reported. If verbose=1
: the value of the coefficients and the likelihood are reported. If verbose=2
: 1
+ information on the computing time of the null model, the fixed-effects coefficients and the hessian are reported.
Logical, default is TRUE
. Whether warnings should be displayed (concerns warnings relating to: convergence state, collinearity issues and observation removal due to only 0/1 outcomes or presence of NA values).
Logical. By default, two notes are displayed: when NAs are removed (to show additional information) and when some observations are removed because of only 0 (or 0/1) outcomes in a fixed-effect (in Poisson/Neg. Bin./Logit models). To avoid displaying these messages, you can set notes = FALSE
. You can remove these messages permanently by using setFixest_notes(FALSE)
.
Positive numeric scalar. The starting value of the dispersion parameter if family="negbin"
. By default, the algorithm uses as a starting value the theta obtained from the model with only the intercept.
Logical. When you combine different variables to transform them into a single fixed-effects you can do e.g. y ~ x | paste(var1, var2)
. The algorithm provides a shorthand to do the same operation: y ~ x | var1^var2
. Because pasting variables is a costly operation, the internal algorithm may use a numerical trick to hasten the process. The cost of doing so is that you lose the labels. If you are interested in getting the value of the fixed-effects coefficients after the estimation, you should use combine.quick = FALSE
. By default it is equal to FALSE
if the number of observations is lower than 50,000, and to TRUE
otherwise.
Not currently used.
A fixest
object.
The number of observations.
The linear formula of the call.
The call of the function.
The method used to estimate the model.
The family used to estimate the model.
[where relevant] The "full" formula containing the linear part and the fixed-effects.
The number of parameters of the model.
The names of each fixed-effect dimension.
The list (of length the number of fixed-effects) of the fixed-effects identifiers for each observation.
The size of each fixed-effect (i.e. the number of unique identifierfor each fixed-effect dimension).
Logical, convergence status.
The convergence message from the optimization procedures.
[where relevant] In the case there were fixed-effects and some observations were removed because of only 0/1 outcome within a fixed-effect, it gives the row numbers of the observations that were removed. Also reports the NA observations that were removed.
[where relevant] In the case there were fixed-effects and some observations were removed because of only 0/1 outcome within a fixed-effect, it gives the list (for each fixed-effect dimension) of the fixed-effect identifiers that were removed.
The named vector of estimated coefficients.
The table of the coefficients with their standard errors, z-values and p-values.
The log-likelihood.
Number of iterations of the algorithm.
Log-likelihood of the null model (i.e. with the intercept only).
Log-likelihood of the model with only the fixed-effects.
Sum of the squared residuals of the null model (containing only with the intercept).
The adjusted pseudo R2.
The fitted values are the expected value of the dependent variable for the fitted model: that is \(E(Y|X)\).
The residuals (y minus the fitted values).
Squared correlation between the dependent variable and the expected predictor (i.e. fitted.values) obtained by the estimation.
The Hessian of the parameters.
The variance-covariance matrix of the parameters.
The standard-error of the parameters.
The matrix of the scores (first derivative for each observation).
The difference between the dependent variable and the expected predictor.
The sum of the fixed-effects coefficients for each observation.
[where relevant] The offset formula.
[where relevant] The weights formula.
fenegbin
: Fixed-effects negative binomial estimation
You can combine two variables to make it a new fixed-effect using ^
. The syntax is as follows: fe_1^fe_2
. Here you created a new variable which is the combination of the two variables fe_1 and fe_2. This is identical to doing paste0(fe_1, "_", fe_2)
but more convenient.
Note that pasting is a costly operation, especially for large data sets. Thus, the internal algorithm uses a numerical trick which is fast, but the drawback is that the identity of each observation is lost (i.e. they are now equal to a meaningless number instead of being equal to paste0(fe_1, "_", fe_2)
). These “identities” are useful only if you're interested in the value of the fixed-effects (that you can extract with fixef.fixest
). If you're only interested in coefficients of the variables, it doesn't matter. Anyway, you can use combine.quick = FALSE
to tell the internal algorithm to use paste
instead of the numerical trick. By default, the numerical trick is performed only for large data sets.
To use leads/lags of variables in the estimation, you can: i) either provide the argument panel.id
, ii) either set you data set as a panel with the function panel
. Doing either of the two will give you acceess to the lagging functions l
and f
.
You can provide several leads/lags at once: e.g. if your formula is equal to f(y) ~ l(x, -1:1)
, it means that the dependent variable is equal to the lead of y
, and you will have as explanatory variables the lead of x1
, x1
and the lag of x1
. See the examples in function l
for more details.
You can interact a variable with a "factor-like" variable by using the syntax var::fe(ref)
, where fe
is the variable to be interacted with and the argument ref
is a value of fe
taken as a reference.
The full syntax is: var::fe(ref, confirm)
. You have two arguments, ref
and confirm
, the two are optional. The argument confirm
is there to avoid mistakenly estimating a model with (too) many variables. If the variable fe
takes over 100 different values, then you have to add the argument confirm = TRUE
. It is important to note that *if you do not care about the standard-errors of the interactions*, then you can add interactions in the fixed-effects part of the formula (using the syntax fe[[var]], as explained in the section “Varying slopes”).
Introducing interactions with this syntax leads to a different display of the interacted values in etable
and offers a special representation of the interacted coefficients in the function coefplot
. See examples.
The syntax var::fe(ref)
is in fact a shorthand for interact(var, fe, ref)
, you have more information in interact
help pages.
This function estimates maximum likelihood models where the conditional expectations are as follows:
Gaussian likelihood: $$E(Y|X)=X\beta$$ Poisson and Negative Binomial likelihoods: $$E(Y|X)=\exp(X\beta)$$ where in the Negative Binomial there is the parameter \(\theta\) used to model the variance as \(\mu+\mu^2/\theta\), with \(\mu\) the conditional expectation. Logit likelihood: $$E(Y|X)=\frac{\exp(X\beta)}{1+\exp(X\beta)}$$
When there are one or more fixed-effects, the conditional expectation can be written as: $$E(Y|X) = h(X\beta+\sum_{k}\sum_{m}\gamma_{m}^{k}\times C_{im}^{k}),$$ where \(h(.)\) is the function corresponding to the likelihood function as shown before. \(C^k\) is the matrix associated to fixed-effect dimension \(k\) such that \(C^k_{im}\) is equal to 1 if observation \(i\) is of category \(m\) in the fixed-effect dimension \(k\) and 0 otherwise.
When there are non linear in parameters functions, we can schematically split the set of regressors in two:
$$f(X,\beta)=X^1\beta^1 + g(X^2,\beta^2)$$
with first a linear term and then a non linear part expressed by the function g. That is, we add a non-linear term to the linear terms (which are \(X*beta\) and the fixed-effects coefficients). It is always better (more efficient) to put into the argument NL.fml
only the non-linear in parameter terms, and add all linear terms in the fml
argument.
To estimate only a non-linear formula without even the intercept, you must exclude the intercept from the linear formula by using, e.g., fml = z~0
.
The over-dispersion parameter of the Negative Binomial family, theta, is capped at 10,000. If theta reaches this high value, it means that there is no overdispersion.
Berge, Laurent, 2018, "Efficient estimation of maximum likelihood models with multiple fixed-effects: the R package FENmlm." CREA Discussion Papers, 13 (https://wwwen.uni.lu/content/download/110162/1299525/file/2018_13).
For models with multiple fixed-effects:
Gaure, Simen, 2013, "OLS with multiple high dimensional category variables", Computational Statistics & Data Analysis 66 pp. 8--18
On the unconditionnal Negative Binomial model:
Allison, Paul D and Waterman, Richard P, 2002, "Fixed-Effects Negative Binomial Regression Models", Sociological Methodology 32(1) pp. 247--265
See also summary.fixest
to see the results with the appropriate standard-errors, fixef.fixest
to extract the fixed-effects coefficients, and the function etable
to visualize the results of multiple estimations.
And other estimation methods: feols
, feglm
, fepois
, feNmlm
.
# NOT RUN {
#
# Linear examples
#
# Load trade data
data(trade)
# We estimate the effect of distance on trade => we account for 3 fixed-effects
# 1) Poisson estimation
est_pois = femlm(Euros ~ log(dist_km)|Origin+Destination+Product, trade)
# 2) Log-Log Gaussian estimation (with same FEs)
est_gaus = update(est_pois, log(Euros+1) ~ ., family="gaussian")
# Comparison of the results using the function esttable
esttable(est_pois, est_gaus)
# Now using two way clustered standard-errors
esttable(est_pois, est_gaus, se = "twoway")
# Comparing different types of standard errors
sum_white = summary(est_pois, se = "white")
sum_oneway = summary(est_pois, se = "cluster")
sum_twoway = summary(est_pois, se = "twoway")
sum_threeway = summary(est_pois, se = "threeway")
esttable(sum_white, sum_oneway, sum_twoway, sum_threeway)
# }
Run the code above in your browser using DataLab