femlm {fixest} | R Documentation |
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, 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, 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, ...)
fml |
A formula representing the relation to be estimated. For example: |
data |
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 |
family |
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). |
start |
Starting values for the coefficients. Can be: i) a numeric of length 1 (e.g. |
fixef |
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). |
offset |
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) |
na_inf.rm |
Logical, default is |
fixef.tol |
Precision used to obtain the fixed-effects (ie cluster coefficients). Defaults to |
fixef.iter |
Maximum number of iterations in the step obtaining the fixed-effects (only in use for 2+ clusters). Default is 10000. |
nthreads |
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 |
verbose |
Integer, default is 0. It represents the level of information that should be reported during the optimisation process. If |
warn |
Logical, default is |
notes |
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 |
theta.init |
Positive numeric scalar. The starting value of the dispersion parameter if |
combine.quick |
Logical. When you combine different variables to transform them into a single fixed-effects you can do e.g. |
... |
Not currently used. |
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) = exp(X*beta) / (1 + exp(X*beta))
When there are one or more clusters, the conditional expectation can be written as:
E(Y|X) = h(Xβ+∑_{k}∑_{m}γ_{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 cluster k such that C^k_{im} is equal to 1 if observation i is of category m in cluster k and 0 otherwise.
When there are non linear in parameters functions, we can schematically split the set of regressors in two:
f(X,β)=X^1β^1 + g(X^2,β^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 cluster 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.
An femlm
object.
coefficients |
The named vector of coefficients. |
coeftable |
The table of the coefficients with their standard errors, z-values and p-values. |
loglik |
The loglikelihood. |
iterations |
Number of iterations of the algorithm. |
n |
The number of observations. |
nparams |
The number of parameters of the model. |
call |
The call. |
fml |
The linear formula of the call. |
ll_null |
Log-likelihood of the null model (i.e. with the intercept only). |
pseudo_r2 |
The adjusted pseudo R2. |
message |
The convergence message from the optimization procedures. |
sq.cor |
Squared correlation between the dependent variable and the expected predictor (i.e. fitted.values) obtained by the estimation. |
hessian |
The Hessian of the parameters. |
fitted.values |
The fitted values are the expected value of the dependent variable for the fitted model: that is E(Y|X). |
cov.unscaled |
The variance-covariance matrix of the parameters. |
se |
The standard-error of the parameters. |
scores |
The matrix of the scores (first derivative for each observation). |
family |
The ML family that was used for the estimation. |
residuals |
The difference between the dependent variable and the expected predictor. |
sumFE |
The sum of the fixed-effects for each observation. |
offset |
The offset formula. |
NL.fml |
The nonlinear formula of the call. |
bounds |
Whether the coefficients were upper or lower bounded. – This can only be the case when a non-linear formula is included and the arguments 'lower' or 'upper' are provided. |
isBounded |
The logical vector that gives for each coefficient whether it was bounded or not. This can only be the case when a non-linear formula is included and the arguments 'lower' or 'upper' are provided. |
fixef_vars |
The names of each cluster. |
fixef_id |
The list (of length the number of clusters) of the cluster identifiers for each observation. |
fixef_sizes |
The size of each cluster. |
obsRemoved |
In the case there were clusters and some observations were removed because of only 0/1 outcome within a cluster, it gives the row numbers of the observations that were removed. |
fixef_removed |
In the case there were clusters and some observations were removed because of only 0/1 outcome within a cluster, it gives the list (for each cluster) of the cluster identifiers that were removed. |
theta |
In the case of a negative binomial estimation: the overdispersion parameter. |
@seealso
See also summary.fixest
to see the results with the appropriate standard-errors, fixef.fixest
to extract the cluster coefficients, and the functions esttable
and esttex
to visualize the results of multiple estimations.
And other estimation methods: feols
, femlm
, feglm
, fepois
, fenegbin
.
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.
Laurent Berge
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 cluster coefficients, and the functions esttable
and esttex
to visualize the results of multiple estimations.
And other estimation methods: feols
, feglm
, fepois
, feNmlm
.
# # 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)