Package {fereg}


Title: Fast Fixed-Effects Estimation with the 'fixest' Interface
Version: 0.4.0
Description: Ordinary least squares, two-stage least squares, Poisson and logit regressions with any number of high-dimensional fixed effects, interactions of fixed effects, varying slopes, weights and iid, heteroskedasticity-robust (HC1, HC2, HC3), multiway-clustered, CRV3, Newey-West, Driscoll-Kraay or Conley standard errors. The formula syntax, arguments, results and methods follow the 'fixest' package and reproduce its estimates. The computations run in a parallel 'Rust' library that eliminates the largest fixed effect exactly and solves for the others by preconditioned conjugate gradient.
License: MIT + file LICENSE
URL: https://github.com/leoliu0/fereg
BugReports: https://github.com/leoliu0/fereg/issues
Depends: R (≥ 4.2)
Imports: stats
Suggests: fixest, testthat (≥ 3.0.0), data.table, generics, Matrix
SystemRequirements: Cargo (Rust's package manager), rustc (>= 1.85)
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Config/roxygen2/version: 8.1.1
NeedsCompilation: yes
Packaged: 2026-10-14 12:13:57 UTC; leo
Author: Leo Liu [aut, cre, cph], Authors of the bundled Rust crates [ctb, cph] (see inst/AUTHORS for each crate's authors and license)
Maintainer: Leo Liu <auleo.liu@gmail.com>
Repository: CRAN
Date/Publication: 2026-10-22 16:30:02 UTC

fereg: fast fixed-effects estimation with fixest's interface

Description

OLS, IV/2SLS, Poisson and logit estimation with any number of fixed effects, interactions and varying slopes, weights and iid / heteroskedasticity-robust / multiway-clustered variances. The interface and the numbers are those of fixest (feols(), fepois(), feglm(), etable(), fixef(), ...); estimation runs in a parallel Rust core that eliminates the largest fixed effect exactly and solves the rest by preconditioned conjugate gradient.

Details

Options: fereg.nthreads (threads, 0 = all cores; default all cores, capped at OMP_THREAD_LIMIT, and 2 under R CMD check) and fereg.notes (print notes on removed observations).

Author(s)

Maintainer: Leo Liu auleo.liu@gmail.com [copyright holder]

Authors:

Other contributors:

See Also

Useful links:


Table of estimation results

Description

Side-by-side table of several estimations in fixest's etable() layout: coefficients with standard errors in parentheses and significance stars, fixed effects, variance type and fit statistics.

Usage

etable(
  ...,
  vcov = NULL,
  ssc = NULL,
  tex = FALSE,
  digits = 4,
  digits.stats = 5,
  fitstat = NULL,
  keep = NULL,
  drop = NULL,
  family = NULL
)

Arguments

...

fereg or fereg_multi objects (named arguments set the column titles).

vcov, ssc

Variance for every model (see summary()); vcov may also be a list with one variance per model, as in fixest.

tex

Return LaTeX code instead of a data frame.

digits

Significant digits of coefficients and standard errors.

digits.stats

Digits of the fit statistics, as in fixest: a number (significant digits, at least one decimal for large values), or "rN" (round to N decimals) / "sN".

fitstat

Fit statistics (see fitstat()). The default is fixest's: observations, R2 and within R2 (adjusted R2 without fixed effects) when every model is OLS, else observations, squared correlation, pseudo R2 and BIC.

keep, drop

Character vectors of regular expressions (Perl syntax) selecting the coefficient rows, as in fixest: keep shows the coefficients matching any of them, then drop removes those matching any of them. Names are matched as displayed (the intercept is "Constant"); a leading "%" matches the original names instead ("(Intercept)"), and a leading "!" negates the expression.

family

Show the row "Family" (OLS, Poisson, Logit); by default only when the models are of different families.

Value

A data frame (class fereg_etable) or, with tex = TRUE, a character string (class fereg_etable_tex).

Examples

data(trade_toy, package = "fereg")
m1 <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
m2 <- fepois(count ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
etable(m1, m2, vcov = ~id)
etable(m1, tex = TRUE)

Fixed-effects OLS and IV estimation

Description

Drop-in replacement for fixest::feols(): same formula syntax, arguments and defaults, same numbers. Estimation runs in the fereg Rust core.

Usage

feols(
  fml,
  data,
  vcov,
  weights,
  offset,
  subset,
  split,
  fsplit,
  cluster,
  se,
  ssc,
  panel.id,
  fixef.rm = "perfect_fit",
  fixef.tol = 1e-08,
  fixef.iter = 16000,
  collin.tol = 1e-09,
  nthreads = getOption("fereg.nthreads", default_threads()),
  lean = FALSE,
  notes = getOption("fereg.notes", TRUE),
  ...
)

Arguments

fml

A formula y ~ x1 + x2 | fe1 + fe2 | endo ~ inst. The fixed-effects part accepts interactions fe1^fe2 and varying slopes fe[x] (fixed effect and slope) or fe[[x]] (slope only). The right-hand side accepts i(), factor(), I(), functions of variables, interactions and -1. Several dependent variables c(y1, y2) and the stepwise functions sw(), sw0(), csw(), csw0() (right-hand side and fixed-effects part) give multiple estimations.

data

A data frame (or a list of equal-length vectors).

vcov

Variance type, as in fixest: a string ("iid", the default; "hetero" (HC1), "hc2", "hc3", "cluster" (first panel.id variable, else first fixed effect), "twoway", "threeway", "fourway", "NW" / "newey_west", "DK" / "driscoll_kraay", "conley", and fereg's extension "crv3"; case and unique abbreviations are accepted), a one-sided formula (~cl, ~cl1 + cl2, up to four dimensions, ~cl1^cl2 clusters on the interaction), a formula type ~ variables (NW ~ id + period, NW(2) ~ id + period, NW ~ period (time series without panel.id), DK(2) ~ period, conley(100) ~ lat + lon, conley("60mi") ~ lat + lon, crv3 ~ g1 + g2; + ssc(...) among the variables sets the small-sample correction) or a request object (see vcov_requests). Variables of Newey-West and Driscoll-Kraay variances default to panel.id, Conley coordinates are guessed from the column names. Rows with missing values in a variance variable are removed (counted as "vcov" in the NA note).

weights

Observation weights: a one-sided formula (~w), a column name or a vector.

offset

Offset on the link scale: a one-sided formula, a column name or a vector.

subset

Rows to use: logical or integer vector, or a one-sided formula (~year > 2000).

split, fsplit

A one-sided formula (or column name): one estimation per value of the variable; fsplit adds the full sample first.

cluster, se

Former fixest ways of requesting a variance type (cluster = ~g, se = "hetero").

ssc

Small-sample correction, see ssc().

panel.id

Panel identifiers, as in fixest: a formula ~unit + time or a string "unit,time". They are the default unit and time of Newey-West / Driscoll-Kraay variances and the default cluster of vcov = "cluster". (fereg has no lag operators.)

fixef.rm

"perfect_fit" (default; singletons, plus fixed-effect groups with a perfectly fitted outcome in GLMs), "singleton" or "none".

fixef.tol, fixef.iter

Tolerance and iteration cap of the fixed-effects solver.

collin.tol

Collinearity tolerance (fixest rule).

nthreads

Number of threads; 0 = all logical cores. Defaults to option fereg.nthreads, else all cores, capped at OMP_THREAD_LIMIT when set (2 under R CMD check, i.e. when ⁠_R_CHECK_LIMIT_CORES_⁠ is set).

lean

If TRUE, residuals, fitted values and the estimation state are not kept (post-hoc variances then need a new estimation).

notes

Print notes about removed observations (option fereg.notes, default TRUE).

...

Not used; unknown arguments are an error.

Value

An object of class fereg (or fereg_multi for multiple estimations).

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
m
summary(m, vcov = ~id)

Fixed-effects Poisson and logit estimation

Description

Drop-in replacements for fixest::fepois() and fixest::feglm() (Poisson and logit families).

Usage

fepois(
  fml,
  data,
  vcov,
  offset,
  weights,
  subset,
  split,
  fsplit,
  cluster,
  se,
  ssc,
  panel.id,
  fixef.rm = "perfect_fit",
  fixef.tol = 1e-08,
  fixef.iter = 16000,
  glm.iter = 25,
  glm.tol = 1e-08,
  collin.tol = 1e-09,
  nthreads = getOption("fereg.nthreads", default_threads()),
  lean = FALSE,
  notes = getOption("fereg.notes", TRUE),
  ...
)

feglm(
  fml,
  data,
  family = "logit",
  vcov,
  offset,
  weights,
  subset,
  split,
  fsplit,
  cluster,
  se,
  ssc,
  panel.id,
  fixef.rm = "perfect_fit",
  fixef.tol = 1e-08,
  fixef.iter = 16000,
  glm.iter = 25,
  glm.tol = 1e-08,
  collin.tol = 1e-09,
  nthreads = getOption("fereg.nthreads", default_threads()),
  lean = FALSE,
  notes = getOption("fereg.notes", TRUE),
  ...
)

Arguments

fml

A formula y ~ x1 + x2 | fe1 + fe2 | endo ~ inst. The fixed-effects part accepts interactions fe1^fe2 and varying slopes fe[x] (fixed effect and slope) or fe[[x]] (slope only). The right-hand side accepts i(), factor(), I(), functions of variables, interactions and -1. Several dependent variables c(y1, y2) and the stepwise functions sw(), sw0(), csw(), csw0() (right-hand side and fixed-effects part) give multiple estimations.

data

A data frame (or a list of equal-length vectors).

vcov

Variance type, as in fixest: a string ("iid", the default; "hetero" (HC1), "hc2", "hc3", "cluster" (first panel.id variable, else first fixed effect), "twoway", "threeway", "fourway", "NW" / "newey_west", "DK" / "driscoll_kraay", "conley", and fereg's extension "crv3"; case and unique abbreviations are accepted), a one-sided formula (~cl, ~cl1 + cl2, up to four dimensions, ~cl1^cl2 clusters on the interaction), a formula type ~ variables (NW ~ id + period, NW(2) ~ id + period, NW ~ period (time series without panel.id), DK(2) ~ period, conley(100) ~ lat + lon, conley("60mi") ~ lat + lon, crv3 ~ g1 + g2; + ssc(...) among the variables sets the small-sample correction) or a request object (see vcov_requests). Variables of Newey-West and Driscoll-Kraay variances default to panel.id, Conley coordinates are guessed from the column names. Rows with missing values in a variance variable are removed (counted as "vcov" in the NA note).

offset

Offset on the link scale: a one-sided formula, a column name or a vector.

weights

Observation weights: a one-sided formula (~w), a column name or a vector.

subset

Rows to use: logical or integer vector, or a one-sided formula (~year > 2000).

split, fsplit

A one-sided formula (or column name): one estimation per value of the variable; fsplit adds the full sample first.

cluster, se

Former fixest ways of requesting a variance type (cluster = ~g, se = "hetero").

ssc

Small-sample correction, see ssc().

panel.id

Panel identifiers, as in fixest: a formula ~unit + time or a string "unit,time". They are the default unit and time of Newey-West / Driscoll-Kraay variances and the default cluster of vcov = "cluster". (fereg has no lag operators.)

fixef.rm

"perfect_fit" (default; singletons, plus fixed-effect groups with a perfectly fitted outcome in GLMs), "singleton" or "none".

fixef.tol, fixef.iter

Tolerance and iteration cap of the fixed-effects solver.

glm.tol, glm.iter

IRLS convergence tolerance (relative deviance change) and iteration cap.

collin.tol

Collinearity tolerance (fixest rule).

nthreads

Number of threads; 0 = all logical cores. Defaults to option fereg.nthreads, else all cores, capped at OMP_THREAD_LIMIT when set (2 under R CMD check, i.e. when ⁠_R_CHECK_LIMIT_CORES_⁠ is set).

lean

If TRUE, residuals, fitted values and the estimation state are not kept (post-hoc variances then need a new estimation).

notes

Print notes about removed observations (option fereg.notes, default TRUE).

...

Not used; unknown arguments are an error.

family

"poisson" / poisson() or "logit" / "binomial" / binomial().

Value

An object of class fereg (or fereg_multi).

Examples

data(trade_toy, package = "fereg")
p <- fepois(count ~ x1 | id + year, trade_toy, nthreads = 2)
p

Methods for fereg objects

Description

Accessors, post-hoc variances, predictions and fixed effects of feols(), fepois() and feglm() results, as in fixest.

Usage

## S3 method for class 'fereg'
summary(
  object,
  vcov = NULL,
  ssc = NULL,
  cluster = NULL,
  se = NULL,
  stage = 2,
  ...
)

## S3 method for class 'fereg'
coef(object, ...)

## S3 method for class 'fereg'
vcov(
  object,
  vcov = NULL,
  ssc = NULL,
  attr = FALSE,
  cluster = NULL,
  se = NULL,
  ...
)

## S3 method for class 'fereg'
confint(
  object,
  parm,
  level = 0.95,
  vcov = NULL,
  ssc = NULL,
  cluster = NULL,
  se = NULL,
  ...
)

## S3 method for class 'fereg'
nobs(object, ...)

## S3 method for class 'fereg'
residuals(
  object,
  type = c("response", "deviance", "pearson", "working"),
  na.rm = TRUE,
  ...
)

## S3 method for class 'fereg'
fitted(object, type = c("response", "link"), na.rm = TRUE, ...)

## S3 method for class 'fereg'
predict(object, newdata, type = c("response", "link"), ...)

## S3 method for class 'fereg'
logLik(object, ...)

## S3 method for class 'fereg'
AIC(object, ..., k = 2)

## S3 method for class 'fereg'
BIC(object, ...)

## S3 method for class 'fereg'
deviance(object, ...)

## S3 method for class 'fereg'
sigma(object, ...)

Arguments

object

A fereg object.

vcov, ssc, cluster, se

Post-hoc variance (same forms as in feols(), including the request objects of vcov_requests); computed from the retained estimation state without re-estimating, except CRV3, whose leave-one-cluster-out refits re-estimate the model on the estimation data (found again from the call). NULL keeps the estimation's variance.

stage

IV only: 1 first stage(s), 2 second stage (default), 1:2 both.

...

Not used.

attr

Add the attributes type, df.K, df.t (and G) to the matrix.

parm

Coefficients (names or indices); all by default.

level

Confidence level.

type

resid(): "response", "deviance", "pearson" or "working"; fitted() / predict(): "response" or "link".

na.rm

FALSE returns one value per row of the estimation data (NA for removed rows).

newdata

A data frame for predictions; fixed-effect levels absent at estimation give NA.

k

AIC penalty per parameter.

Value

summary(): the fereg object with the requested variance (coefficient table in ⁠$coeftable⁠); for IV with stage = 1 or 1:2, the requested stages (a fereg_multi when there are several). coef(): named vector of coefficients. vcov(): variance matrix. confint(): data frame of lower and upper bounds, one row per coefficient. nobs(): number of observations. resid(), fitted(), predict(): numeric vectors. logLik(): object of class logLik. AIC(), BIC(), deviance(), sigma(): numbers.

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
summary(m, vcov = "hetero")
coef(m)
vcov(m, vcov = ~id)
confint(m, level = 0.9)
head(predict(m, newdata = trade_toy))
c(AIC = AIC(m), BIC = BIC(m))

Multiple estimations

Description

A fereg_multi object is a list of fereg objects (one per estimation, fixest order: sample, fixed effects, dependent variable, right-hand side). Methods apply to every estimation.

Usage

## S3 method for class 'fereg_multi'
print(x, ...)

## S3 method for class 'fereg_multi'
x[i]

## S3 method for class 'fereg_multi'
as.list(x, ...)

## S3 method for class 'fereg_multi'
summary(object, ...)

## S3 method for class 'fereg_multi'
coef(object, ...)

## S3 method for class 'fereg_multi'
nobs(object, ...)

Arguments

x, object

A fereg_multi object.

...

Passed to the methods of each estimation.

i

Indices or names of estimations.

Value

print(): x, invisibly. [ and summary(): a fereg_multi object. as.list(): a list of fereg objects. coef(): data frame with one row per estimation and one column per coefficient. nobs(): number of observations of each estimation.

Examples

data(trade_toy, package = "fereg")
ms <- feols(y ~ csw(x1, x2) | id + year, trade_toy, nthreads = 2)
ms
coef(ms)
nobs(ms)
summary(ms[2], vcov = ~id)

Fit statistics

Description

Same types and names as fixest's fitstat(): n, rmse, r2, ar2, wr2, war2, ll, pr2, apr2, cor2 (sq.cor), aic, bic, f, my, g and, for IV, ivf1 (ivf), ivwald1 (ivwald), wh and sargan.

Usage

fitstat(x, type, simplify = FALSE, ...)

Arguments

x

A fereg object.

type

A character vector of types, or a one-sided formula (~ r2 + ivf1).

simplify

Return a plain vector when every statistic is a single number.

...

Not used.

Value

An object of class fereg_fitstat (a named list), or a vector if simplify.

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
fitstat(m, ~ n + r2 + wr2)

Fixed effects of an estimation

Description

Fixed-effect coefficients with fixest's normalization: the first fixed effect is free, and in every connected component one level of each other fixed effect is set to 0 (counted in the references attribute). Varying slopes are listed as fe[[x]].

Usage

fixef(object, ...)

Arguments

object

A fereg object.

...

Not used.

Value

A list (class fereg.fixef) of named vectors, one per fixed effect / slope.

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
fe <- fixef(m)
summary(fe)
head(fe$year)

R-squared variants

Description

R-squared variants

Usage

r2(x, type = "all")

Arguments

x

A fereg object.

type

Types among "r2", "ar2", "wr2", "war2", "pr2", "apr2", "cor2", or "all".

Value

A named numeric vector (NA where a type does not apply).

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
r2(m)

Coefficient table accessors

Description

se(), tstat(), pvalue() and coeftable() as in fixest.

Usage

se(object, ...)

tstat(object, ...)

pvalue(object, ...)

coeftable(object, ...)

Arguments

object

A fereg (or fereg_multi) object.

...

Passed to summary() (vcov, ssc, cluster).

Value

Named vectors, or the coefficient matrix for coeftable().

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
se(m)
pvalue(m, vcov = "hetero")
coeftable(m, vcov = ~id)

Small-sample correction

Description

Same arguments and defaults as fixest's ssc(). As in fixest, the correction depends on the variance type: heteroskedasticity-robust, Newey-West, Driscoll-Kraay and Conley variances use K.fixef = "full" (no nesting), Newey-West and Driscoll-Kraay apply G.adj with G = the number of time periods, Conley uses n / (n - K), and HC2 / HC3 use no correction at all.

Usage

ssc(
  K.adj = TRUE,
  K.fixef = "nonnested",
  K.exact = FALSE,
  G.adj = TRUE,
  G.df = "min",
  t.df = "min",
  adj,
  fixef.K,
  cluster.adj,
  cluster.df
)

Arguments

K.adj

Multiply the variance by (n - 1) / (n - K) (iid, clustered, Newey-West, Driscoll-Kraay) or n / (n - K) (heteroskedasticity-robust, Conley).

K.fixef

How fixed effects count in K: "nonnested" (fixed effects nested in a cluster variable do not count), "full" or "none".

K.exact

Count the exact number of redundant fixed-effect coefficients.

G.adj

Multiply clustered variances by G / (G - 1) (Newey-West and Driscoll-Kraay: T / (T - 1) with T time periods).

G.df

"min" (one factor with the smallest number of clusters) or "conventional" (each cluster dimension uses its own G).

t.df

Degrees of freedom of the t distribution with clustered, Newey-West and Driscoll-Kraay variances: "min" (G - 1, or T - 1), "conventional" (n - K) or a number.

adj, fixef.K, cluster.adj, cluster.df

Former fixest names of K.adj, K.fixef, G.adj and G.df.

Value

An object of class ssc.type.

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 + x2 | id + year, trade_toy, nthreads = 2)
summary(m, vcov = ~id, ssc = ssc(K.adj = FALSE, G.adj = FALSE))

Simulated panel for examples

Description

A small simulated panel: 600 observations of 60 units over 10 years.

Usage

data(trade_toy)

Format

A data frame with columns id, year, x1, x2, z (instrument), xe (endogenous regressor), y, count (Poisson outcome) and binary (0/1 outcome).


Variance requests

Description

fixest's request objects and helpers for the vcov argument of feols(), fepois(), feglm(), summary(), vcov(), confint() and etable(). They return objects of class fixest_vcov_request (the same structure as fixest's, so both packages' requests are accepted). Called with an estimation as first argument, the ⁠vcov_*()⁠ functions return its variance matrix instead, as in fixest.

Usage

NW(lag = NULL)

newey_west(lag = NULL)

DK(lag = NULL)

driscoll_kraay(lag = NULL)

conley(cutoff = NULL, pixel = NULL, distance = NULL)

vcov_hetero(
  x,
  type = "hc1",
  exact = TRUE,
  boot.size = NULL,
  ssc = NULL,
  vcov_fix = TRUE
)

vcov_cluster(x, cluster = NULL, ssc = NULL, vcov_fix = TRUE)

vcov_crv3(x, cluster = NULL, ssc = NULL)

vcov_NW(x, unit = NULL, time = NULL, lag = NULL, ssc = NULL, vcov_fix = TRUE)

vcov_DK(x, time = NULL, lag = NULL, ssc = NULL, vcov_fix = TRUE)

vcov_conley(
  x,
  lat = NULL,
  lon = NULL,
  cutoff = NULL,
  pixel = 0,
  distance = "triangular",
  ssc = NULL,
  vcov_fix = TRUE
)

Arguments

lag

Number of lags (NULL = the default rule).

cutoff, pixel, distance

Conley options, see Details.

x

An estimation (fereg object), or omitted to build a request. As in fixest, when the first argument is not an estimation it is taken as the next argument (vcov_NW("id", "t")).

type

vcov_hetero(): "hc1", "hc2" or "hc3".

exact, boot.size

vcov_hetero(): fereg always computes the exact leverage (exact = FALSE is not supported; boot.size is not used).

ssc

A small-sample correction (ssc()); overrides the ssc argument of the caller.

vcov_fix

Must be TRUE (fereg always applies fixest's positive-definiteness fix).

cluster

Cluster variables: a character vector or a one-sided formula.

unit, time

Unit and time variables of Newey-West / Driscoll-Kraay (names or one-sided formulas); by default the variables of panel.id.

lat, lon

Latitude and longitude variables (names or one-sided formulas, degrees).

Details

Value

A fixest_vcov_request object, or the variance matrix of x.

Examples

data(trade_toy, package = "fereg")
m <- feols(y ~ x1 | id + year, trade_toy, panel.id = ~id + year, nthreads = 2)
summary(m, vcov = NW(1))
summary(m, vcov = DK ~ year)
summary(m, vcov = vcov_hetero(type = "hc2"))