Learn R Programming

capybara

About

tldr; If you have a 2-4GB dataset and you need to estimate a (generalized) linear model with a large number of fixed effects, this package is for you. It works with larger datasets as well and facilites computing clustered standard errors.

‘capybara’ is a fast and small footprint software that provides efficient functions for demeaning variables before conducting a GLM estimation. This technique is particularly useful when estimating linear models with multiple group fixed effects. It is a fork of the excellent Alpaca package created and maintained by Dr. Amrei Stammann. The software can estimate Exponential Family models (e.g., Poisson) and Negative Binomial models.

Traditional QR estimation can be unfeasible due to additional memory requirements. The method, which is based on Halperin (1962) vector projections offers important time and memory savings without compromising numerical stability in the estimation process.

The software heavily borrows from Gaure (2013) and Stammann (2018) works on OLS and GLM estimation with large fixed effects implemented in the ‘lfe’ and ‘alpaca’ packages. The differences are that ‘capybara’ does not use C nor Rcpp code, instead it uses cpp11 and cpp11armadillo.

The summary tables borrow from Stata outputs. I have also provided integrations with ‘broom’ to facilitate the inclusion of statistical tables in Quarto/Jupyter notebooks.

If this software is useful to you, please consider donating on Buy Me A Coffee. All donations will be used to continue improving capybara.

Installation

You can install the development version of capybara like so:

remotes::install_github("pachadotdev/capybara")

Examples

See the documentation: https://pacha.dev/capybara/.

Here is simple example of estimating a linear model and a Poisson model with fixed effects:

m1 <- felm(mpg ~ wt | cyl, mtcars)
m2 <- fepoisson(mpg ~ wt | cyl, mtcars)
summary_table(m1, m2, model_names = c("Linear", "Poisson"))

|     Variable     |       Linear        |      Poisson      |
|------------------|---------------------|-------------------|
| wt               |           -3.206*** |           -0.180* |
|                  |             (0.295) |           (0.072) |
|                  |                     |                   |
| Fixed effects    |                     |                   |
| cyl              |                 Yes |               Yes |
|                  |                     |                   |
| N                |                  32 |                32 |
| R-squared        |               0.837 |             0.616 |

Standard errors in parenthesis
Significance levels: *** p < 0.001; ** p < 0.01; * p < 0.05; . p < 0.1

Design choices

Capybara is full of trade-offs. I have used ‘data.table’ to benefit from in-place modifications. The model fitting is done on C++ side. While the code aims to be fast, I prefer to have some bottlenecks instead of low numerical stability or reinvent the wheel. Armadillo works great for the size of data and the models that I use for my research. The principle was: “He who gives up code safety for code speed deserves neither.” (Wickham, 2014).

Benchmarks

Median time and memory footprint for the different models in the book An Advanced Guide to Trade Policy Analysis.

ModelPackageMedian TimeMemory
PPMLAlpaca720.07 ms - 3302.64 MB - 3
PPMLBase R41.72 s - 42.73 GB - 4
PPMLCapybara405.89 ms - 219.22 MB - 1
PPMLFixest130.1 ms - 144.59 MB - 2
Trade DiversionAlpaca3.79 s - 3339.79 MB - 3
Trade DiversionBase R39.84 s - 42.6 GB - 4
Trade DiversionCapybara947.96 ms - 226.22 MB - 1
Trade DiversionFixest932.78 ms - 136.59 MB - 2
EndogeneityAlpaca2.65 s - 3306.27 MB - 3
EndogeneityBase R10.7 m - 411.94 GB - 4
EndogeneityCapybara1.32 s - 215.55 MB - 1
EndogeneityFixest225.64 ms - 128.08 MB - 2
Reverse CausalityAlpaca3.36 s - 3335.61 MB - 3
Reverse CausalityBase R10.69 m - 411.94 GB - 4
Reverse CausalityCapybara1.36 s - 217.73 MB - 1
Reverse CausalityFixest296.63 ms - 132.43 MB - 2
Phasing EffectsAlpaca4.6 s - 3393.86 MB - 3
Phasing EffectsBase R10.75 m - 411.95 GB - 4
Phasing EffectsCapybara1.57 s - 222.08 MB - 1
Phasing EffectsFixest471.1 ms - 141.12 MB - 2
GlobalizationAlpaca8.2 s - 3539.49 MB - 3
GlobalizationBase R10.79 m - 411.97 GB - 4
GlobalizationCapybara2.07 s - 232.98 MB - 1
GlobalizationFixest869.62 ms - 162.87 MB - 2

Changing the number of cores

Note that you can use Sys.setenv(CAPYBARA_NCORES = 4) (or other positive integers) to change the number of cores that capybara uses, here is an example of how it affects the performance

coresPPMLTrade Diversion
21.8s16.2s
41.5s14.0s
60.8s2.4s
80.4s0.9s

Installing with compiler optimizations

CRAN packages are built with the -O2 compiler flag, which is sufficient for most packages, including capybara. However, if you want to use the maximum compiler optimizations, you can do so by setting the -O3 compiler flag.

To do that, create a user Makevars file in your home directory (~/.R/Makevars) and add the following lines:

# Copy to ~/.R/Makevars if you want to override R's default optimization
CXXFLAGS = -O3
CXX11FLAGS = -O3
CXX14FLAGS = -O3
CXX17FLAGS = -O3
CXX20FLAGS = -O3

Additional optimizations can be enabled by setting the CAPYBARA_PORTABLE environment variable to "no" before installing the package. This will enable hardware-specific compiler flags that can significantly improve performance (sometimes 2-4x faster than just using portable flags).

Sys.setenv(CAPYBARA_OPTIMIZATIONS = "yes")

# CRAN version
install.packages("capybara", type = "source")

# Local version
install.packages(".", repos = NULL, type = "source")
# or
devtools::install()

This will determine if your hardware allows hardware-specific compiler flags that provide significant performance improvements (sometimes 2-4x faster than just using portable flags).

Code of Conduct

Please note that the capybara project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Acknowledgements

Thanks a lot to Prof. Yoto Yotov for reviewing the summary functions.

Copy Link

Version

Install

install.packages('capybara')

Monthly Downloads

568

Version

1.8.0

License

Apache License (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Mauricio Vargas Sepulveda

Last Published

August 27th, 2025

Functions in capybara (1.8.0)

vcov.feglm

Covariance matrix for GLMs
summary_table

Generate formatted regression tables
trade_panel

Trade Panel 1986-2006
fepoisson

Poisson model fitting high-dimensional with k-way fixed effects
bias_corr

Asymptotic bias correction after fitting binary choice models with a 1,2,3-way error component
capybara-package

Generalized Linear Models (GLMs) with high-dimensional k-way fixed effects
fenegbin

Negative Binomial model fitting with high-dimensional k-way fixed effects
apes

Compute average partial effects after fitting binary choice models with a 1,2,3-way error component
autoplot.feglm

Autoplot method for feglm objects
feglm

GLM fitting with high-dimensional k-way fixed effects
felm

LM fitting with high-dimensional k-way fixed effects
augment.feglm

Broom Integration
fit_control

Set feglm Control Parameters
vcov.felm

Covariance matrix for LMs
reexports

Objects exported from other packages