matchingMarkets-package: An R package for the analysis of stable matchings.


The matchingMarkets package contains R, C++ and Java code for stable matching algorithms and the estimation of structural models that correct for the sample selection bias of observed outcomes in matching markets.

Matching is concerned with who transacts with whom, and how. For example, who works at which job, which students go to which school, who forms a workgroup with whom, and so on.

The empirical analysis of matching markets is naturally subject to sample selection problems. If agents match assortatively on characteristics unobserved to the analyst but correlated with both the exogenous variable and the outcome of interest, regression estimates will generally be biased.

The matchingMarkets package comprises

  1. Bayes estimators. The estimators implemented in function stabit and stabit2 correct for the selection bias from endogenous matching.

    The current package version provides solutions for two commonly observed matching processes: (i) the group formation problem with fixed group sizes and (ii) the college admissions problem. These processes determine which matches are observed – and which are not – and this is a sample selection problem.

  2. Post-estimation tools. Setting mfx=TRUE in the summary function computes marginal effects from coefficients in binary outcome and selection equations and khb implements the Karlson-Holm-Breen test for confounding due to sample selection.

  3. Design matrix generation. The estimators are based on independent variables for all feasible, i.e., observed and counterfactual, matches in the market. Generating the characteristics of all feasible matches from individual-level data is a combinatorial problem. The package returns design matrices based on pre-specified transformations to generate counterfactual matches.

  4. Algorithms. The package also contains three matching algorithms that can be used to simulated matching data: hri: A constraint model (Posser, 2014) for the stable marriage and college admissions problem, a.k.a. hospital/residents problem (see Gale and Shapley, 1962). sri: A constraint model for the stable roommates problem (see Gusfield and Irving, 1989). ttc: The top-trading-cycles algorithm for the housing market problem. These can be used to obtain stable matchings from simulated or real preference data (see Shapley and Scarf, 1974).

  5. Data. In addition to the baac00 dataset from borrowing groups in Thailands largest agricultural lending program, the package provides functions stabsim and stabsim2 to simulate one's own data from one-sided and two-sided matching markets.

Frequently Asked Questions

How to cite this package

Whenever using this package, please cite as

Klein, T. (2016). matchingMarkets: Structural Estimator and Algorithms for the Analysis of Stable Matchings. R package version 0.3-2.


Thilo Klein


Bresnahan, T. and Reiss, P. (1991). Empirical models of discrete games. Journal of Econometrics, 48(1-2):57–81.

Gale, D. and Shapley, L.S. (1962). College admissions and the stability of marriage. The American Mathematical Monthly, 69(1):9–15.

Gusfield, D.M. and R.W. Irving (1989). The stable marriage problem: Structure and algorithms, MIT Press.

Heckman, J. (1979). Sample selection bias as a specification error. Econometrica, 47(1):153–161.

Prosser, P. (2014). Stable Roommates and Constraint Programming. Lecture Notes in Computer Science, CPAIOR 2014 Edition. Springer International Publishing, 8451: 15–28.

Pycia, M. (2012). Stability and preference alignment in matching and coalition formation. Econometrica, 80(1):323–362.

Shapley, L. and H. Scarf (1974). On cores and indivisibility. Journal of Mathematical Economics, 1(1):23–37.

Sorensen, M. (2007). How smart is smart money? A two-sided matching model of venture capital. The Journal of Finance, 62(6):2725–2762.

See Also


Questions? Problems? Suggestions? or email at

All documentation is copyright its authors; we didn't write any of that.