Table Of ContentSPRINGER BRIEFS IN ECONOMICS
Massimiliano Porto
Using R for Trade
Policy Analysis
R Codes
for the UNCTAD
and WTO Practical
Guide
123
SpringerBriefs in Economics
SpringerBriefs present concise summaries of cutting-edge research and practical
applications across a wide spectrum of fields. Featuring compact volumes of 50
to 125 pages, the series covers a range of content from professional to academic.
Typicaltopicsmightinclude:
• Atimelyreportofstate-of-theartanalyticaltechniques
• A bridge between new research results, as published in journal articles, and a
contextualliteraturereview
• Asnapshotofahotoremergingtopic
• Anin-depthcasestudyorclinicalexample
• Apresentationofcoreconceptsthatstudentsmustunderstandinordertomake
independentcontributions
SpringerBriefs in Economics showcase emerging theory, empirical research, and
practicalapplicationinmicroeconomics,macroeconomics,economicpolicy,public
finance, econometrics, regional science, and related fields, from a global author
community.
Briefs are characterized by fast, global electronic dissemination, standard pub-
lishing contracts, standardized manuscript preparation and formatting guidelines,
andexpeditedproductionschedules.
Moreinformationaboutthisseriesathttp://www.springer.com/series/8876
Massimiliano Porto
Using R for Trade Policy
Analysis
R Codes for the UNCTAD and WTO
Practical Guide
MassimilianoPorto
KobeUniversity
Kobe,Japan
ISSN2191-5504 ISSN2191-5512 (electronic)
SpringerBriefsinEconomics
ISBN978-3-030-34528-0 ISBN978-3-030-34529-7 (eBook)
https://doi.org/10.1007/978-3-030-34529-7
©TheAuthor(s),underexclusivelicensetoSpringerNatureSwitzerlandAG2020
Thisworkissubjecttocopyright.AllrightsaresolelyandexclusivelylicensedbythePublisher,whether
thewholeorpartofthematerialisconcerned,specificallytherightsoftranslation,reprinting,reuse
ofillustrations,recitation,broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,and
transmissionorinformationstorageandretrieval,electronicadaptation,computersoftware,orbysimilar
ordissimilarmethodologynowknownorhereafterdeveloped.
Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublication
doesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevant
protectivelawsandregulationsandthereforefreeforgeneraluse.
Thepublisher,theauthors,andtheeditorsaresafetoassumethattheadviceandinformationinthisbook
arebelievedtobetrueandaccurateatthedateofpublication.Neitherthepublishernortheauthorsor
theeditorsgiveawarranty,expressedorimplied,withrespecttothematerialcontainedhereinorforany
errorsoromissionsthatmayhavebeenmade.Thepublisherremainsneutralwithregardtojurisdictional
claimsinpublishedmapsandinstitutionalaffiliations.
ThisSpringerimprintispublishedbytheregisteredcompanySpringerNatureSwitzerlandAG.
Theregisteredcompanyaddressis:Gewerbestrasse11,6330Cham,Switzerland
Abstract
The aim of this book is to spread the best practices of the UNCTAD and WTO
for trade analysis to the R users community. We accomplish this task by showing
how to replicate the UNCTAD and WTO’s Stata codes in the Practical Guide to
TradePolicyAnalysisbyusingR.Weselectsomeapplicationsandexercisesinthe
PracticalGuidetoTradePolicyAnalysisandexplainhowtoimplementthecodesin
R.ThisbooktargetsreaderswithabasicknowledgeofR.Itisparticularlysuitable
forStatausers.
v
Contents
1 Introduction ................................................................... 1
1.1 StructureoftheBook.................................................... 1
1.2 PackagestoInstall....................................................... 2
1.3 GoodPracticeandNotation............................................. 3
2 AnalyzingTradeFlows....................................................... 5
2.1 OpennessAcrossCountries............................................. 5
2.2 GeographicalOrientationofExports................................... 18
2.3 SectoralOrientationofExports......................................... 25
2.4 OverlapTradeandSimilarityIndex.................................... 29
2.5 TermsofTrade........................................................... 35
2.6 IntensiveandExtensiveMargins ....................................... 38
3 AnalyzingTradeTariffs...................................................... 45
3.1 SummaryofTariffStatistics............................................ 45
3.2 DeterminantsofTariffs.................................................. 51
3.3 AnalyzingPreferentialMarketAccess................................. 57
4 TheGravityModelofTrade................................................. 65
4.1 BuildingtheDatabase................................................... 69
4.2 EstimatingtheGravityModel .......................................... 78
4.3 ExportingRegressionOutput........................................... 80
A ANon-technicalIntroductiontoRandRStudio .......................... 83
A.1 RStudio .................................................................. 83
A.1.1 LaunchingaNewProject ...................................... 83
A.1.2 OpeninganRScript............................................ 86
A.1.3 InstallingandLoadingPackages............................... 87
A.2 ObjectsandBasicOperations........................................... 87
A.2.1 VectorandMatrix .............................................. 88
A.2.2 DataFrame...................................................... 92
A.2.3 ClassandStructure............................................. 93
vii
viii Contents
A.3 LogicalOperators ....................................................... 95
A.4 ResourcesforR.......................................................... 95
A.4.1 CRANTaskViews ............................................. 95
A.4.2 OnlineGuidesforR............................................ 95
A.4.3 Books ........................................................... 96
A.4.4 RCommunity................................................... 96
Bibliography....................................................................... 97
Index................................................................................ 99
List of Figures
Fig.2.1 Comparingbasicplotandggplotlayout.(a)plotfunction.
(b)ggplotfunction ..................................................... 9
Fig.2.2 Addingaquadraticfittoggplot ....................................... 11
Fig.2.3 Addinganx interceptlinetoggplot .................................. 13
Fig.2.4 Adding(a)labelsand(b)texttoggplot .............................. 15
Fig.2.5 Moreelaboratedplotswithggplot .................................... 24
Fig.2.6 Barplotwithggplot ................................................... 28
Fig.2.7 Scatterplotwith(a)linearand(b)quadraticfittedlineswith
ggplot ................................................................... 34
Fig.2.8 Lineplotwithggplot .................................................. 37
Fig.2.9 Adoubley-axisplot ................................................... 43
Fig.3.1 Histogramwithggplot ................................................. 48
Fig.3.2 Densityplotwithggplot ............................................... 50
Fig.3.3 Facetswithggplot ..................................................... 51
Fig.3.4 Barplotandfacetswithggplot ........................................ 63
Fig.A.1 RStudio’sinterface ..................................................... 84
Fig.A.2 Launchanewproject(1) .............................................. 84
Fig.A.3 Launchanewproject(2) .............................................. 85
Fig.A.4 Launchanewproject(3) .............................................. 85
Fig.A.5 OpenanRscript ....................................................... 86
ix
List of Tables
Table 2.1 OpennessforG20Countries,2016 .................................. 6
Table 3.1 Regression output of the determinant of tariffs with
stargazer .......................................................... 56
Table 4.1 Regressionoutputofthegravitymodelwithstargazer ........ 81
xi