# Solutions Manual for An Advanced Guide to Trade Policy Analysis in R

*2022-04-06*

# Chapter 1 Introduction

## 1.1 Welcome

Welcome to the online solutions manual for for An Advanced Guide to Trade Policy Analysis (AGTPA).

This material provides a comprehensive explanation to reproduce the book’s results from Yotov et al. (2016) in R. We do not attempt to discuss the theory behind gravity models. The book thoroughly and the references cited through the chapters shall fill those details.

This manual points at two audiences:

- People who know R and are interested in learning about gravity models.
- People with no R knowledge who know gravity models theory.

We only assume that readers are familiar with linear regression and that they shall read Yotov et al. (2016) and Wickham and Grolemund (2016) alongside this material.

We replicated all the results from the book, obtained initially with Stata, in R as a way to contribute to Reproducible Research. R is free and available on almost every operating system. It is a beautiful tool for all statistical analyses.

## 1.2 Disclaimer

The views and opinions expressed in this thesis project are solely those of the author. They do not necessarily reflect the official position of the United Nations Economic and Social Commission for Asia and the Pacific or the Pontifical Catholic University of Chile.

## 1.3 Reproducing AGTPA results yourself

We created *tradepolicy*, an R package explicitly created to reproduce the results from AGTPA. It acts as a meta-package that calls other packages to use several functions that augment R capabilities. It features its own functions to compute clustered standard errors and much more.

The reader can get all the functions and datasets by running these lines from RStudio:

```
# stable version
install.packages("tradepolicy")
# development version
if (!require("remotes")) install.packages("remotes")
::install_github("pachadotdev/tradepolicy") remotes
```

## 1.4 Getting the most out of this material

The reader can clone the GitHub repository to obtain the editable RMarkdown files:

`git clone https://github.com/pachamaltese/tradepolicy.git`

Please read Bryan, Hester, and STAT 545 TAs (2020) in case of questions about *git* or *GitHub*.

## 1.5 Acknowledgments

This ebook improved noticeably from its early drafts thanks to valuable comments and corrections from two teams. United Nations: Jinbo Hao, Alexey Kravchenko, Chence Sun, Yoto Yotov. Catholic University of Chile: Luis Gutierrez, Leonardo Jofre, Jessica Pavani, Constanza Prado, Vanessa Reinoso.

For the second (present) edition of the ebook, the communication and contributions to other projects by Laurent Berge, Paulo F. Olivera, Oliver Reiter, Hernan Robledo and Matthieu Stigler were crucial to improving the computation time results.

Earlier versions of these solutions started during the development of the gravity R package, where Hrisyana Doytchinova’s comments and feedback were precious.

## 1.6 Changes concerning the first edition

The first edition was very conservative in avoiding external libraries (or packages). It only used the strict minimal packages such as *dplyr* for data cleaning and *sandwich* for clustered standard errors. We fitted all the models using base R, which was considerably slower than Stata, with a computation time around five to twenty times larger depending on the model link (or family) used.

The current edition uses *fixest* reduced the fitting for all the models by around 95%. The book’s first edition takes around three hours to fit all the models and produce the final HTML. The same process now takes around ten minutes, and we hope these time efficiencies benefit the readers. An additional improvement was to add additional explanations to the codes and review all the text grammar based on the feedback received after publishing the first edition.

While writing this second edition, a critical bug was detected and reported to improve *fixest*, which gave problems when having more than two fixed effects columns. The details for this are available on GitHub. The possibility of comparing with base R, reporting and getting fixes from the packages’ authors with transparent communication is a tremendous advantage from Open Source.

As Professor Yotov told this author once while writing his master’s thesis and the first edition: We should focus on the correctness of our results, then think about computational efficiency. He was right!

### References

*R for Data Science: Import, Tidy, Transform, Visualize, and Model Data*. " O’Reilly Media, Inc.".

*An Advanced Guide to Trade Policy Analysis: The Structural Gravity Model*. World Trade Organization Geneva.