Gravity & Flow Models
PPML and negative binomial gravity models with high-dimensional fixed effects.
SpatialEconometrics.SpatialGravityModels.BilateralFE — Type
BilateralFE()Both origin and destination fixed effects. The default.
This is the theory-consistent specification: structural gravity implies origin and destination terms that absorb both multilateral resistances, and omitting them is the classic gold-medal error that biases the distance and border coefficients. They are swept out by alternating projection, so the cost stays manageable as the number of units grows.
Recover the absorbed terms with multilateral_resistance.
References
Anderson, J. & van Wincoop, E. (2003). Gravity with gravitas. American Economic Review, 93(1).
SpatialEconometrics.SpatialGravityModels.GravityFE — Type
GravityFEWhich fixed effects a gravity equation absorbs. Subtypes: OriginFE, DestinationFE, BilateralFE.
SpatialEconometrics.SpatialGravityModels.GravityModel — Type
GravityModelA fitted gravity / flow model, returned by fit_gravity. Implements the StatsAPI.RegressionModel interface.
Fields
variant:PoissonGravityorNegBinGravity.coef,vcov,coefnames: the structural coefficients onX. The fixed effects are not in here.origin_fe,dest_fe: the absorbed fixed effects, i.e. the multilateral resistance terms — seemultilateral_resistance.alpha: negative binomial dispersion;NaNfor Poisson.loglik,deviance,pseudo_R2: fit measures.residuals,fitted: on the scale of the flows, not logs.N_obs,N_origin,N_dest: observation and unit counts.converged,iterations: state of the IRLS loop. Checkconverged.
SpatialEconometrics.SpatialGravityModels.GravityVariant — Type
GravityVariantWhich count model a gravity equation is estimated with. Subtypes: PoissonGravity and NegBinGravity.
SpatialEconometrics.SpatialGravityModels.NegBinGravity — Type
NegBinGravity()Negative binomial estimation of a gravity equation, adding a dispersion parameter alpha to accommodate variance above the Poisson mean.
More efficient than PoissonGravity when the overdispersion is real, but it buys that by assuming the variance function, so it is less robust to getting that assumption wrong. Fitted starting from the Poisson estimates. The estimated alpha is on the fitted model; near zero means the Poisson restriction was adequate.
See also fit_gravity.
SpatialEconometrics.SpatialGravityModels.OriginFE — Type
OriginFE()Origin fixed effects only — one per sending unit.
Absorbs everything about the origin, including its outward multilateral resistance, but leaves destination characteristics in the error.
See also BilateralFE.
SpatialEconometrics.SpatialGravityModels.PoissonGravity — Type
PoissonGravity()Poisson pseudo-maximum likelihood (PPML) estimation of a gravity equation.
The standard choice, for two reasons. Trade and migration flow data are full of zeros, which log-linear estimation simply drops; and under heteroskedasticity — the norm for flows — log-linear OLS is inconsistent because E[log y] != log E[y]. PPML is consistent under either, needing only the conditional mean to be correctly specified, and handles zeros natively.
See also NegBinGravity, fit_gravity.
References
Santos Silva, J. M. C. & Tenreyro, S. (2006). The log of gravity. Review of Economics and Statistics, 88(4).
SpatialEconometrics.SpatialGravityModels.fit_gravity — Function
fit_gravity(variant, flows, X, origin_id, dest_id; fe = BilateralFE(),
varnames = nothing, maxiter = 100, tol = 1e-8)Fit a gravity / flow model by pseudo-maximum likelihood, absorbing high-dimensional origin and destination fixed effects.
Estimates E[flows] = exp(X*beta + origin FE + destination FE) by IRLS, with the fixed effects swept out by alternating projection rather than expanded into dummies, so the number of units is not a constraint.
Arguments
variant:PoissonGravityorNegBinGravity.flows: the dependent flow variable, on its own scale, not logged. Zeros are fine and should be kept.X:N_obs x Kbilateral covariates — log distance, contiguity, common language and so on. No intercept, and nothing that varies only by origin or only by destination, since the fixed effects absorb that.origin_id,dest_id: integer unit indices per observation.
Keywords
fe: aGravityFE;BilateralFEby default and generally what theory calls for.varnames: covariate names; defaults tox1, x2, ....maxiter,tol: IRLS iteration cap and convergence tolerance.
Returns
A GravityModel.
Examples
m = fit_gravity(PoissonGravity(), flows, X, orig, dest;
varnames = ["log_dist", "contiguous"])
m.converged || @warn "IRLS did not converge"
coef(m)
multilateral_resistance(m)Do not log the flows and do not drop the zeros. Doing either is the practice PPML exists to replace.
References
Santos Silva, J. M. C. & Tenreyro, S. (2006). Review of Economics and Statistics, 88(4).
SpatialEconometrics.SpatialGravityModels.multilateral_resistance — Function
multilateral_resistance(model) -> NamedTupleRecover the fitted fixed effects as (; outward, inward).
In structural gravity the origin and destination fixed effects are the multilateral resistance terms: outward measures how hard it is for each origin to reach all markets, inward how accessible each destination is from all sources. They are nuisance parameters for estimating beta but often the quantity of substantive interest.
Both vectors are identified only up to a normalization, so compare values within a vector rather than reading levels.
Examples
mr = multilateral_resistance(m)
sortperm(mr.inward, rev = true) # most accessible destinationsSee also GravityModel, BilateralFE.