Gravity models in their traditional form are inspired by Newton law of gravitation:

\[ F_{ij}=G\frac{M_{i}M_{j}}{D^{2}_{ij}}. \]

The force \(F\) between two bodies \(i\) and \(j\) with \(i \neq j\) is proportional to the masses \(M\) of these bodies and inversely proportional to the square of their geographical distance \(D\). \(G\) is a constant and as such of no major concern.

The underlying idea of a traditional gravity model, shown for international trade, is equally simple:

\[ X_{ij}=G\frac{Y_{i}^{\beta_{1}}Y_{j}^{\beta_{2}}}{D_{ij}^{\beta_{3}}}. \]

The trade flow \(X\) is explained by \(Y_{i}\) and \(Y_{j}\) that are the masses of the exporting and importing country (e.g. the GDP) and \(D_{ij}\) that is the distance between the countries.

Dummy variables such as common borders \(contig\) or regional trade agreements \(rta\) can be added to the model. Let \(t_{ij}\) be the transaction cost defined as:

\[ t_{ij}= D_{ij} \exp(contig_{ij} + rta_{ij}) \]

So that the model with friction becomes:

\[ X_{ij}=G\frac{Y_{i}^{\beta_{1}}Y_{j}^{\beta_{2}}}{t_{ij}^{\beta_{3}}}. \]

A logarithmic operator can be applied to form a log-linear model and use a standard estimation methods such as OLS:

\[ \log X_{ij}=\beta_{0}\log G +\beta_{1}\log Y_{i}+\beta_{2}\log Y_{j}+\beta_{3}\log D_{ij}+\beta_{4}contig_{ij}+\beta_{5}rta_{ij} \]

Provided trade barriers exists, the econometric literature proposes the Multilateral Resistance model defined by the equations:

\[ X_{ij}=\frac{Y_{i}Y_{j}}{Y}\frac{t_{ij}^{1-\sigma}}{P_{j}^{1-\sigma}\Pi_{i}^{1-\sigma}} \] with \[ P_{i}^{1-\sigma}=\sum_{j}\frac{t_{ij}^{1-\sigma}}{\Pi_{j}^{1-\sigma}}\frac{Y_{j}}{Y}. \] and \[ \Pi_{j}^{1-\sigma}=\sum_{i}\frac{t_{ij}^{1-\sigma}}{P_{i}^{1-\sigma}}\frac{Y_{i}}{Y} \]

Basically the model proposes that the exports \(X_{ij}\) from \(i\) to \(j\) are determined by the supply factors in \(i\), \(Y_{i}\), and the demand factors in \(j\), \(Y_{j}\), as well as the transaction costs \(t_{ij}\).

Next to information on bilateral partners \(i\) and \(j\), information on the rest of the world is included in the gravity model with \(Y=\sum_{i} Y_{i}= \sum_{j} Y_{j}\) that represents the worldwide sum of incomes (e.g. the world’s GDP).

In this model \(\sigma\) represents the elasticity of substitution between all goods. A key assumption is to take a fixed value \(\sigma > 1\) in order to account for the preference for a variation of goods (e.g. in this model goods can be replaced for other similar goods).

The Multilateral Resistance terms are included via the terms \(P\), Inward Multilateral Resistance, and \(\Pi\), Outward Multilateral Resistance. The Inward Multilateral Resistance \(P_i\) is a function of the transaction costs of \(i\) to all trade partners \(j\). The Outward Multilateral Resistance \(\Pi_{j}\) is a function of the transaction costs of \(j\) to all trade partners \(i\) and their demand.

The Multilateral Resistance terms dependent on each other. Hence, the estimation of structural gravity models becomes complex.

To estimate gravity equations you need a square dataset including bilateral flows defined by the argument dependent_variable, a distance measure defined by the argument distance that is the key regressor, and other potential influences (e.g. contiguity and common currency) given as a vector in additional_regressors are required.

Some estimation methods require ISO codes or similar of type character variables to compute particular country effects. Make sure the origin and destination codes are of type “character”.

The rule of thumb for regressors or independent variables consists in:

- All dummy variables should be of type numeric (0/1).
- If an independent variable is defined as a ratio, it should be logged.

The user should perform some data cleaning beforehand to remove observations that contain entries that can distort estimates, notwithstanding the functions provided within this package will remove zero flows and distances.

See for gravity datasets and Stata code for estimating gravity models.

All the examples here are adapted from Wölwer, Breßlein, and Burgard (2018). We included some examples that require further explanation as they perform some data transforming and therefore the functions provide a simplification for the end user.

Double Demeaning, as introduced by Head and Mayer (2014), subtracts importer and exporter averages on the left and right hand side of the respective gravity equation, and all unilateral influences including the Multilateral Resistance terms vanish. Therefore, no unilateral variables may be added as independent variables for the estimation.

Our ddm function first logs the dependent variable and the distance variable. Afterwards, the dependent and independent variables are transformed in the following way (exemplary shown for trade flows, \(X_{ij}\)):

\[ (\log X_{ij})_{\text{DDM}} = (\log X_{ij}) - (\log X_{ij})_{\text{Origin Mean}} \\- (\log X_{ij})_{\text{Destination Mean}} + (\log X_{ij})_{\text{Mean}}. \]

One subtracts the mean value for the origin country and the mean value for the destination country and adds the overall mean value to the logged trade flows. This procedure is repeated for all dependent and independent variables. The transformed variables are then used for the estimation.

DDM is easily applied, but, as shown in , its very sensitive to missing data.

An example of how to apply the function ddm to an example dataset in gravity and the resulting output is shown in the following:

```
library(gravity)
fit <- ddm(
dependent_variable = "flow",
distance = "distw",
additional_regressors = c("rta", "comcur", "contig"),
code_origin = "iso_o",
code_destination = "iso_d",
data = gravity_no_zeros
)
summary(fit)
```

```
##
## Call:
## y_log_ddm ~ dist_log_ddm + rta_ddm + comcur_ddm + contig_ddm +
## 0
##
## Residuals:
## Min 1Q Median 3Q Max
## -20.9411 -1.2268 0.3032 1.5195 8.4538
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## dist_log_ddm -1.60334 0.03312 -48.407 <2e-16 ***
## rta_ddm 0.79727 0.07004 11.383 <2e-16 ***
## comcur_ddm 0.17376 0.14613 1.189 0.234
## contig_ddm 1.00184 0.11981 8.362 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.318 on 17084 degrees of freedom
## Multiple R-squared: 0.2541, Adjusted R-squared: 0.254
## F-statistic: 1455 on 4 and 17084 DF, p-value: < 2.2e-16
```

The package returns lm or glm objects instead of summaries. Doing that allows to use our functions in conjunction with broom or other packages, for example:

```
library(broom)
tidy(fit)
```

```
## # A tibble: 4 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 dist_log_ddm -1.60 0.0331 -48.4 0.
## 2 rta_ddm 0.797 0.0700 11.4 6.54e-30
## 3 comcur_ddm 0.174 0.146 1.19 2.34e- 1
## 4 contig_ddm 1.00 0.120 8.36 6.62e-17
```

`glance(fit)`

```
## # A tibble: 1 x 11
## r.squared adj.r.squared sigma statistic p.value df logLik AIC
## * <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
## 1 0.254 0.254 2.32 1455. 0 4 -38611. 77232.
## # ... with 3 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>
```

`augment(fit)`

```
## # A tibble: 17,088 x 12
## y_log_ddm dist_log_ddm rta_ddm comcur_ddm contig_ddm .fitted .se.fit
## * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -0.710 0.540 0.0175 0.0562 -0.0435 -0.885 0.0205
## 2 0.00279 -0.0201 0.0318 0.0501 -0.00474 0.0616 0.00754
## 3 -0.373 0.154 -0.197 -0.00870 -0.0502 -0.456 0.0126
## 4 -3.85 -0.758 -0.0408 0.0562 -0.0533 1.14 0.0298
## 5 0.301 0.223 -0.191 -0.00705 -0.0237 -0.534 0.0111
## 6 -3.45 0.599 -0.0747 -0.0747 -0.0762 -1.11 0.0176
## 7 1.80 -0.861 0.0562 0.0562 -0.0152 1.42 0.0276
## 8 -1.43 -0.0245 -0.127 0.0562 -0.0319 -0.0845 0.0135
## 9 3.10 -0.0260 -0.0195 0.0562 -0.0426 -0.00683 0.0104
## 10 -3.88 0.622 -0.0227 0.0562 -0.0486 -1.05 0.0217
## # ... with 17,078 more rows, and 5 more variables: .resid <dbl>,
## # .hat <dbl>, .sigma <dbl>, .cooksd <dbl>, .std.resid <dbl>
```

S. L. Baier and Bergstrand (2010) suggest a modification of the simple OLS that is easily implemented, allows for comparative statics and yields results close to those of NLS, called Bonus vetus OLS (BVU and BVW). They estimate gravity models in their additive form.

As unilateral income elasticities are assumed, flows are divided by the product of unilateral incomes. The dependent variable for the estimation is therefore \[\log\left(\frac{y}{inc_{o} \: inc_{d}}\right).\]

By applying a Taylor-series expansion and the assumption of symmetrical, bilateral trade costs, they develop a reduced gravity model in which multilateral and worldwide resistance enter exogenously.

S. L. Baier and Bergstrand (2010) distinguish two types of Bonus vetus estimations depending on how the Taylor-series is centered. One method, called BVU, uses simple averages while the other, called BVW, uses GDP weights. Depending on which method is used, the transaction costs are weighted differently. For advantages and disadvantages of both methods see Scott L. Baier and Bergstrand (2009) and S. L. Baier and Bergstrand (2010).

To give an example with simple averages (BVU), distance would be transformed to Multilateral and World Resistance in the following way: \[ MWR_{ij} = \frac{1}{N}\left(\sum_{i=1}^{N}\log D_{ij} \right)+\frac{1}{N}\left(\sum_{j=1}^{N}\log D_{ij} \right)-\frac{1}{N^{2}}\left(\sum_{i=1}^{N}\sum_{j=1}^{N}\log D_{ij} \right) \] with \(D_{ij}\) denoting the bilateral distance, \(N\) the number of countries and \((MWR)_{D_{ij}}\) the transformed variable adjusted for multilateral resistances.

When using weighted averages (BVW), the simple averages are replaced by GDP weights. The transformed variables are included as independent variables in the estimation. The resulting equation can be estimated by simple OLS.

An example of how to apply the functions bvu and bvw to an example dataset in gravity and the resulting output is shown in the following:

```
fit2 <- bvu(
dependent_variable = "flow",
distance = "distw",
additional_regressors = c("rta", "contig", "comcur"),
income_origin = "gdp_o",
income_destination = "gdp_d",
code_origin = "iso_o",
code_destination = "iso_d",
data = gravity_no_zeros
)
tidy(fit2)
```

```
## # A tibble: 5 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 7.47 0.0192 389. 0.
## 2 dist_log_mr -1.69 0.0359 -47.0 0.
## 3 rta_mr 0.582 0.0793 7.35 2.12e-13
## 4 contig_mr 1.00 0.131 7.62 2.75e-14
## 5 comcur_mr 0.191 0.167 1.14 2.53e- 1
```

`glance(fit2)`

```
## # A tibble: 1 x 11
## r.squared adj.r.squared sigma statistic p.value df logLik AIC
## * <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
## 1 0.229 0.229 2.51 1271. 0 5 -39958. 79928.
## # ... with 3 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>
```

```
fit3 <- bvw(
dependent_variable = "flow",
distance = "distw",
additional_regressors = c("rta", "comcur", "contig"),
income_origin = "gdp_o",
income_destination = "gdp_d",
code_origin = "iso_o",
code_destination = "iso_d",
data = gravity_no_zeros
)
tidy(fit3)
```

```
## # A tibble: 5 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 7.26 0.0546 133. 0.
## 2 dist_log_mr -0.634 0.0257 -24.7 1.09e-132
## 3 rta_mr 1.31 0.0670 19.5 4.07e- 84
## 4 comcur_mr -0.896 0.142 -6.31 2.86e- 10
## 5 contig_mr 1.38 0.126 10.9 1.11e- 27
```

`glance(fit3)`

```
## # A tibble: 1 x 11
## r.squared adj.r.squared sigma statistic p.value df logLik AIC
## * <dbl> <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl>
## 1 0.118 0.118 2.68 574. 0 5 -41106. 82225.
## # ... with 3 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>
```

Baier, S. L., and J. H. Bergstrand. 2010. “The Gravity Model in International Trade: Advances and Applications.” In, edited by Peter A. G. van Bergeijk and StevenEditors Brakman. Cambridge University Press. doi:10.1017/CBO9780511762109.

Baier, Scott L., and Jeffrey H. Bergstrand. 2009. “Bonus Vetus Ols: A Simple Method for Approximating International Trade-Cost Effects Using the Gravity Equation.” *Journal of International Economics* 77 (1): 77–85. doi:10.1016/j.jinteco.2008.10.004.

Head, Keith, and Thierry Mayer. 2014. “Chapter 3 - Gravity Equations: Workhorse,Toolkit, and Cookbook.” In *Handbook of International Economics*, edited by Gita Gopinath, Elhanan Helpman, and Kenneth Rogoff, 4:131–95. Handbook of International Economics. Elsevier. doi:10.1016/B978-0-444-54314-1.00003-3.

Wölwer, Anna-Lena, Martin Breßlein, and Jan Pablo Burgard. 2018. “Gravity Models in R.” *Austrian Journal of Statistics* 47 (4): 16–35. doi:10.17713/ajs.v47i4.688.