Working paper · · Command version 1.1.1
xtgamfe: Fixed-effects gamma regression with a log link for strictly positive panel outcomes in Stata
M. Christopher Roebuck. RxEconomics working paper, November 2026 (command version 1.1.1).
Health care spending among people who use care is strictly positive and heavily skewed to the right. In cross-sectional work, gamma regression with a log link has become a standard way to model it, because it models mean costs directly in dollars. When the same people are followed over time (in multi-year claims extracts or linked survey panels, for example), a fixed effect for each person can absorb characteristics that are roughly constant over time—baseline severity, health behaviors, and the propensity to seek care. In this working paper, I describe xtgamfe, which fits that model in Stata as an ordinary panel command, and set out when its estimates can be relied on. Readers interested in the details (the proofs, the simulation evidence, and the full illustration with survey data) will find them in the working paper.
Get the paper and the command
Download the working paper (PDF, 90 pages, 0.8 MB)
The command is available from the Statistical Software Components (SSC) archive. In Stata, type:
ssc install xtgamfe, allThe all option also copies the example dataset.
Why a fixed-effects gamma command
Official Stata does not offer fixed-effects gamma regression as an ordinary panel estimator, in the way that xtpoisson, fe offers the Poisson. The same coefficients can, of course, be computed with existing tools (glm with an indicator for every panel, an auxiliary Poisson regression fit with ppmlhdfe, or xtspj), but each supplies part of what an applied analysis needs. glm with indicators, for instance, takes about half a minute with 1,000 panels, and the shape estimates from glm and xtspj are not consistent when the number of periods is fixed.
In one step, xtgamfe:
- reports the coefficients on their natural scale, with standard errors clustered on panels or on a level in which they are nested;
- accepts frequency, sampling, and importance weights, factor variables, and time-series operators;
- estimates the gamma shape in two ways that, under the gamma model, remain consistent when the number of periods is fixed;
- returns fitted means, fixed effects, and average partial effects in the units of the outcome, with standard errors that allow for the estimation of the fixed effects; and
- offers postestimation checks, including a bootstrap comparison with fixed-effects Poisson (
estat fepoisson) and a Park regression of the residuals (estat park).
When it helps, and when fixed-effects Poisson is safer
The gamma coefficients are consistent, with the number of periods fixed, when each period’s error has the same expected share of its panel’s total (a condition I call share centering). Errors that are exchangeable across periods satisfy it, whereas serial dependence, dispersion that changes over time, and a variance not proportional to the squared mean can violate it. In balanced panels whose errors are independent of the regressors, however, period indicators can absorb a common serial or dispersion pattern.
The payoff is precision. In simulations in which the variance was proportional to the squared mean and panel levels differed widely, fixed-effects Poisson had 1.7 to 2.7 times the root-mean-squared error of the gamma estimator, and in the Medical Expenditure Panel Survey (MEPS) illustration, on the same observations, its standard errors were 1.5 times as large.
The cost is robustness. Fixed-effects Poisson needs only a correctly specified mean and strictly exogenous regressors (with no condition on how the errors of a panel relate to one another), and it accommodates zeros. Indeed, when the variance was proportional to another power of the mean, the gamma estimator was biased and Poisson much less so. An analyst who wants a single estimate with the fewest assumptions should, therefore, report Poisson; the gamma estimator, however, is worth fitting beside it.
A quick example
The package includes a simulated dataset of 200 panels observed for four periods (true coefficients of 0.5 and −0.3, and a gamma shape of 2):
. ssc install xtgamfe, all
. sysuse xtgamfe_example, clear
. xtset id time
. xtgamfe cost x1 x2 i.time
. estat ape x1 x2, ybarThe coefficient on x1 is 0.499, and its average partial effect from estat ape, ybar is 6.50 outcome units per unit of x1 (standard error 0.84), which is the coefficient times the mean outcome of 13.0. In a model with period indicators, these effects (computed from the observed outcome) are the ones to report, for reasons the paper explains.
What to report
My recommendations amount to a short workflow:
- Fit
xtgamfe, with period indicators when the panel has a common time dimension. In unbalanced panels, report which periods each panel is observed in (the pattern of observation), because the choice of period indicators depends on it. - Fit fixed-effects Poisson regression to the same observations and report both.
estat fepoissontests the difference (it does not allow weights), and a material difference calls for checking the mean specification, strict exogeneity, and share centering. - From a model with period indicators, report the average partial effects of
estat ape, ybar. - Read the slope of
estat parkand a difference between the two shape estimates as warnings, not as tests of share centering. - With few clusters, use the
smalloption orvce(jackknife); for effects on the dollar scale whose sign is not in doubt, consider the interval on the log scale.
The paper’s illustration shows why step 2 matters. Using MEPS data for a diabetes cohort, it relates positive nondrug spending to the days supplied of medication in the prior round. xtgamfe puts that association within one standard error of zero, whereas fixed-effects Poisson on the same observations puts it about 2.2 standard errors above zero. A further check then finds that same-round medication use also predicts spending, which casts doubt on the lag-only specification itself, the kind of problem a material difference between the two estimators can signal.
Limitations
The command covers one set of fixed effects, a log link, and strictly positive outcomes. It does not treat zeros (for which a two-part model and the Poisson estimator are among the alternatives), and dropping them changes what is estimated; moreover, if whether an outcome is positive depends on the same shock that determines its size, the estimates can be biased. The estimator also requires strict exogeneity (each period’s error has mean one given the regressors of every period), so it does not accommodate lagged outcomes or regressors that respond to past outcomes, and weights that vary within panel can bias the coefficients even when the gamma model holds.
Intervals for effects on the level of the outcome covered the true value in as few as 86 percent of replications with 250 panels when the fixed effects were widely dispersed. Finally, the test suite of version 1.1.1 has been run in StataNow 19.5 MP on Linux and Windows and in Stata 15.1 MP on Windows, and not in other releases or editions of Stata.
Applying it to claims data
Panel models of health care costs come up in claims-based studies that follow the same members across years. For help applying them to client-provided, licensed, or public-use data, see our real-world evidence and claims analytics services.
Abstract
Positive health care costs are heavily skewed to the right, gamma regression with a log link is a leading choice for modeling them in cross-section, and panels that follow the same persons over time (multi-year claims extracts, linked survey panels) let cost analyses control for unobserved person-level characteristics. This paper describes xtgamfe, a Stata command for fixed-effects gamma regression with a log link, which also reports two estimates of the gamma shape and average partial effects in the units of the outcome.
Its coefficients are consistent with the number of periods fixed when each period’s error has the same expected share of its panel’s total, given the fixed effect and the regressors and offsets of all periods. Errors that are exchangeable across periods satisfy this condition, whereas serial dependence, dispersion that changes over time, and a variance not proportional to the squared mean can violate it; in balanced panels whose error vectors are independent of the regressors, period indicators can absorb a common serial or dispersion pattern.
In simulations in which the variance was proportional to the squared mean and panel levels differed widely, fixed-effects Poisson had 1.7 to 2.7 times the root-mean-squared error of the gamma estimator, whereas with other powers of the mean the gamma estimator was biased and Poisson, which is consistent there, much less so.
An illustration with Medical Expenditure Panel Survey data relates positive nondrug spending to the days supplied of medication in the prior round. Under the main coding, the xtgamfe estimate is within one standard error of zero in both a diabetes and an antipsychotic cohort, whereas the diabetes estimate of fixed-effects Poisson differs from it (nominal bootstrap p-values of 0.017 and 0.009 in the two models), and same-round medication use also predicts spending in that cohort, which casts doubt on the lag-only specification. For applied work on health care costs, the results argue for reporting not just the gamma estimates but also fixed-effects Poisson on the same observations, and for reading a material difference between them as evidence against the share condition, the mean specification, or strict exogeneity.
Keywords: xtgamfe; gamma regression; fixed effects; panel data; health care costs
Replication materials
The replication materials (the test suite, the simulation drivers with their output, and the code that rebuilds the MEPS analysis files from the public sources) are available from the author on request. To request them, use the contact page.
Suggested citation
Roebuck, M. Christopher. 2026. “xtgamfe: Fixed-effects gamma regression with a log link for strictly positive panel outcomes in Stata.” RxEconomics working paper, November 2026 (command version 1.1.1). https://www.rxeconomics.com/publications/xtgamfe