# Packages ----
library(archive)
library(readr)
library(dplyr)
library(tidyr)
library(purrr)
library(knitr)
# Extract ----
<- "first-edition/Chapter-2.zip"
fzip <- gsub("\\.zip$", "", fzip)
dout
if (!dir.exists(dout)) {
archive_extract(fzip, dir = dout)
}
# Read ----
<- paste0(dout, "/trefler.rds")
fout <- paste0(dout, "/trefler_desc.rds")
fout2
if (!file.exists(fout)) {
<- read_csv("first-edition/Chapter-2/hov_pub.csv", col_names = F) %>%
trefler rename(
country = X1,
factor = X2,
at = X3,
v = X4,
y = X5,
b = X6,
ypc = X7,
pop = X8
)
# Transform ----
# see https://www.stata.com/manuals/rsummarize.pdf
# https://www.stata.com/manuals/degen.pdf
# https://www.stata.com/manuals/degen.pdf
# Create an auxiliary table for delta values
<- tibble(
delta_values country = c(
"Bangladesh", "Pakistan", "Indonesia", "Sri Lanka", "Thailand",
"Colombia", "Panama", "Yugoslavia", "Portugal", "Uruguay", "Greece",
"Ireland", "Spain", "Israel", "Hong Kong", "New Zealand", "Austria",
"Singapore", "Italy", "UK", "Japan", "Belgium", "Trinidad", "Netherlands",
"Finland", "Denmark", "West Germany", "France", "Sweden", "Norway",
"Switzerland", "Canada", "USA"
),delta = c(
0.03, 0.09, 0.10, 0.09, 0.17, 0.16, 0.28, 0.29, 0.14, 0.11, 0.45,
0.55, 0.42, 0.49, 0.40, 0.38, 0.60, 0.48, 0.60, 0.58, 0.70, 0.65, 0.47,
0.72, 0.65, 0.73, 0.78, 0.74, 0.57, 0.69, 0.79, 0.55, 1
)
)
<- trefler %>%
trefler mutate(ypc_max = max(ypc)) %>%
mutate(
ratio = case_when(
!= "Italy" ~ ypc / ypc_max,
country == "Italy" ~ (ypc / ypc_max) + 0.0001
country
)%>%
) select(-ypc_max) %>%
arrange(ratio) %>%
group_by(ratio) %>%
mutate(indexc = cur_group_id()) %>%
group_by(factor) %>%
mutate(indexf = cur_group_id()) %>%
ungroup() %>%
left_join(delta_values)
# Labels ----
# Create a separate table with the variables description
<- tibble(
trefler_desc variable = c(
"country", "factor", "at", "v", "y", "b", "ypc", "indexc",
"indexf"
),description = c(
"Name of the country", "Name of the factor",
"Factor content of trade F=A*T", "Endowment", "GDP, World Bank, y=p*Q",
"Trade balance, World Bank b=p*T", "GDP per capita, PWT",
"Country indentifier", "Factor Indentifier"
)
)
# Save ----
saveRDS(trefler, fout)
saveRDS(trefler_desc, fout2)
else {
} <- readRDS(fout)
trefler <- readRDS(fout2)
trefler_desc }
Chapter 2. The Heckscher-Ohlin Model
In these exercises, you will reproduce some of the empirical results from Trefler (1993, 1995). To complete the exercise, the Excel file hov_pub.csv
should be stored in the directory Chapter-2
. After this, run the STATA program hov_pub.do
, which will create a new STATA data file trefler.dta
.
Read and transform the data
Feenstra’s code
data into Stata *
* This is to read the
set mem 30m
insheet using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler.csv *
* insheet using "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\hov_pub.csv"
rename v1 country
rename v2 factor
rename v3 AT
rename v4 V
rename v5 Y
rename v6 B
rename v7 YPC
rename v8 POP
index *
* create country quietly summarize YPC
local maxYPC=_result(6)
gen ratio=YPC/`maxYPC'
replace ratio=ratio+0.0001 if country=="Italy"
sort ratio
egen indexc=group(ratio)
factor index *
* create sort factor
egen indexf=group(factor)
include delta *
*
gen delta=1
replace delta=0.03 if country=="Bangladesh"
replace delta=0.09 if country=="Pakistan"
replace delta=0.10 if country=="Indonesia"
replace delta=0.09 if country=="Sri Lanka"
replace delta=0.17 if country=="Thailand"
replace delta=0.16 if country=="Colombia"
replace delta=0.28 if country=="Panama"
replace delta=0.29 if country=="Yugoslavia"
replace delta=0.14 if country=="Portugal"
replace delta=0.11 if country=="Uruguay"
replace delta=0.45 if country=="Greece"
replace delta=0.55 if country=="Ireland"
replace delta=0.42 if country=="Spain"
replace delta=0.49 if country=="Israel"
replace delta=0.40 if country=="Hong Kong"
replace delta=0.38 if country=="New Zealand"
replace delta=0.60 if country=="Austria"
replace delta=0.48 if country=="Singapore"
replace delta=0.60 if country=="Italy"
replace delta=0.58 if country=="UK"
replace delta=0.70 if country=="Japan"
replace delta=0.65 if country=="Belgium"
replace delta=0.47 if country=="Trinidad"
replace delta=0.72 if country=="Netherlands"
replace delta=0.65 if country=="Finland"
replace delta=0.73 if country=="Denmark"
replace delta=0.78 if country=="West Germany"
replace delta=0.74 if country=="France"
replace delta=0.57 if country=="Sweden"
replace delta=0.69 if country=="Norway"
replace delta=0.79 if country=="Switzerland"
replace delta=0.55 if country=="Canada"
replace delta=1 if country=="USA"
compress
label var country "Name of the country"
label var factor "Name of the factor"
label var AT "Factor content of trade F=A*T"
label var V "Endowment"
label var Y "GDP, World Bank, y=p*Q"
label var B "Trade balance, World Bank b=p*T"
label var YPC "GDP per capita, PWT"
label var indexc "Country indentifier"
label var indexf "Factor Indentifier"
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler,replace *
* save "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler", replace
exit
Output:
data into Stata *
. * This is to read the set mem 30m
.
memory allocation
Current
memory usage
current value description (1M = 1024k)
settable
--------------------------------------------------------------------set maxvar 5000 max. variables allowed 1.909M
set memory 30M max. data space 30.000M
set matsize 400 max. RHS vars in models 1.254M
-----------
33.163M
insheet using Z:\home\pacha\github\advanced-international-trade\first-edition\Ch
.
> apter-2\hov_pub.csvobs)
(8 vars, 297
rename v1 country
.
rename v2 factor
.
rename v3 AT
.
rename v4 V
.
rename v5 Y
.
rename v6 B
.
rename v7 YPC
.
rename v8 POP
.
. index *
. * create country quietly summarize YPC
.
local maxYPC=_result(6)
.
gen ratio=YPC/`maxYPC'
.
replace ratio=ratio+0.0001 if country=="Italy"
. real changes made)
(9
. sort ratio
.
egen indexc=group(ratio)
.
. factor index *
. * create sort factor
.
egen indexf=group(factor)
.
. include delta *
. *
. gen delta=1
.
replace delta=0.03 if country=="Bangladesh"
. real changes made)
(9
replace delta=0.09 if country=="Pakistan"
. real changes made)
(9
replace delta=0.10 if country=="Indonesia"
. real changes made)
(9
replace delta=0.09 if country=="Sri Lanka"
. real changes made)
(9
replace delta=0.17 if country=="Thailand"
. real changes made)
(9
replace delta=0.16 if country=="Colombia"
. real changes made)
(9
replace delta=0.28 if country=="Panama"
. real changes made)
(9
replace delta=0.29 if country=="Yugoslavia"
. real changes made)
(9
replace delta=0.14 if country=="Portugal"
. real changes made)
(9
replace delta=0.11 if country=="Uruguay"
. real changes made)
(9
replace delta=0.45 if country=="Greece"
. real changes made)
(9
replace delta=0.55 if country=="Ireland"
. real changes made)
(9
replace delta=0.42 if country=="Spain"
. real changes made)
(9
replace delta=0.49 if country=="Israel"
. real changes made)
(9
replace delta=0.40 if country=="Hong Kong"
. real changes made)
(9
replace delta=0.38 if country=="New Zealand"
. real changes made)
(9
replace delta=0.60 if country=="Austria"
. real changes made)
(9
replace delta=0.48 if country=="Singapore"
. real changes made)
(9
replace delta=0.60 if country=="Italy"
. real changes made)
(9
replace delta=0.58 if country=="UK"
. real changes made)
(9
replace delta=0.70 if country=="Japan"
. real changes made)
(9
replace delta=0.65 if country=="Belgium"
. real changes made)
(9
replace delta=0.47 if country=="Trinidad"
. real changes made)
(9
replace delta=0.72 if country=="Netherlands"
. real changes made)
(9
replace delta=0.65 if country=="Finland"
. real changes made)
(9
replace delta=0.73 if country=="Denmark"
. real changes made)
(9
replace delta=0.78 if country=="West Germany"
. real changes made)
(9
replace delta=0.74 if country=="France"
. real changes made)
(9
replace delta=0.57 if country=="Sweden"
. real changes made)
(9
replace delta=0.69 if country=="Norway"
. real changes made)
(9
replace delta=0.79 if country=="Switzerland"
. real changes made)
(9
replace delta=0.55 if country=="Canada"
. real changes made)
(9
replace delta=1 if country=="USA"
. real changes made)
(0
. compress
. float now int
YPC was float now byte
indexc was float now byte
indexf was
. label var country "Name of the country"
.
label var factor "Name of the factor"
.
label var AT "Factor content of trade F=A*T"
.
label var V "Endowment"
.
label var Y "GDP, World Bank, y=p*Q"
.
label var B "Trade balance, World Bank b=p*T"
.
label var YPC "GDP per capita, PWT"
.
label var indexc "Country indentifier"
.
label var indexf "Factor Indentifier"
.
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\t
. replace
> refler,
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\tre
> fler.dta saved
. exit
.
end of do-file
My code
Exercise 1
Given identical technologies across countries, run the program sign_rank_1.do
to conduct the sign test, rank test, and test for missing trade. Use the results in sign_rank_1.log
to replicate columns (2) and (4) in Table 2.5.
Feenstra’s code
program is to conduct sign test, Rank test and Missing trade test *
* This
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\sign_rank_1.log, replace *
* log using "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\sign_rank_1.log", replace
set mem 30m
use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler, clear *
* use "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler", clear
of country in the dataset *
* number egen C=max(indexc)
egen F=max(indexf)
level of Yw, Bw and Vw *
* Calculate the world egen Yww=sum(Y)
gen Yw=Yww/F
egen Bww=sum(B)
gen Bw=Bww/F
egen Vfw=sum(V), by(indexf)
* Calculate country share Sc *gen Sc=(Y-B)/(Yw-Bw)
eq.2 in Trefler (1995)*
* Calculate epsilon(fc) and sigma^2(f) according to gen Efc=AT-(V-Sc*Vfw)
for a given factor *
* Construct the average epsilon egen total=sum(Efc),by(indexf)
gen ave=total/C
weight *
* Construct sigma^2 and the
egen tot=sum((Efc-ave)^2), by(indexf)
gen sigma2f=tot/(C-1)
codebook sigma2f
gen sigmaf=sqrt(sigma2f)
gen weight=sigmaf*sqrt(Sc)
weight, convert all the data *
* Using the
gen trAT=AT/(sigmaf*sqrt(Sc))
gen trV=V/(sigmaf*sqrt(Sc))
gen trY=Y/sqrt(Sc)
gen trB=B/sqrt(Sc)
gen trVfw=Vfw/sigmaf
gen AThat=trV-Sc*trVfw
gen AThat2=(V-Sc*Vfw)/weight
be .28 *
* Correlation, should
corr trAT AThat2
*************
* Sign Test *
*************
sort indexc
by indexc: count if trAT*AThat2>0
count if trAT*AThat2>0
display _result(1)/_N
*****************
* Missing Trade *
*****************
for the missing trade, should be .032 *
* Checking
quietly summarize trAT
local varAT=_result(4)
quietly summarize AThat
local varHat=_result(4)
quietly summarize AThat2
local varHat2=_result(4)
display `varAT'/`varHat'
display `varAT'/`varHat2'
**************
* Rank Tests *
**************
keep country indexc indexf trAT AThat2
sort indexc indexf
reshape wide trAT AThat2, i(indexc) j(indexf)
local i=1
while `i'<9{
local j=`i'+1
while `j'<=9{
gen rank`i'`j'=((trAT`i'-trAT`j')*(AThat2`i'-AThat2`j')>0)
local j=`j'+1
}local i=`i'+1
}
keep country indexc rank*
reshape long rank, i(indexc) j(factor)
egen r1=sum(rank), by(indexc)
gen r2=r1/36
collapse r2,by(indexc country)
sum r2
list
log close
exit
Output:
program is to conduct sign test, Rank test and Missing trade test *
. * This
. capture log close
.
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
. r-2\sign_rank_1.log, replace
>
----------------------------------------------------------------------------------name: <unnamed>
log: Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
log
> er-2\sign_rank_1.log type: text
on: 19 Jun 2024, 15:29:23
opened
. set mem 30m
.
memory allocation
Current
memory usage
current value description (1M = 1024k)
settable
--------------------------------------------------------------------set maxvar 5000 max. variables allowed 1.909M
set memory 30M max. data space 30.000M
set matsize 400 max. RHS vars in models 1.254M
-----------
33.163M
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\tr
. clear
> efler,
. of country in the dataset *
. * number egen C=max(indexc)
.
egen F=max(indexf)
.
. level of Yw, Bw and Vw *
. * Calculate the world egen Yww=sum(Y)
.
gen Yw=Yww/F
.
egen Bww=sum(B)
.
gen Bw=Bww/F
.
egen Vfw=sum(V), by(indexf)
.
.
. * Calculate country share Sc *gen Sc=(Y-B)/(Yw-Bw)
.
. eq.2 in Trefler (1995)*
. * Calculate epsilon(fc) and sigma^2(f) according to gen Efc=AT-(V-Sc*Vfw)
.
. for a given factor *
. * Construct the average epsilon egen total=sum(Efc),by(indexf)
.
gen ave=total/C
.
. weight *
. * Construct sigma^2 and the
. egen tot=sum((Efc-ave)^2), by(indexf)
.
gen sigma2f=tot/(C-1)
.
. codebook sigma2f
.
----------------------------------------------------------------------------------
sigma2f (unlabeled)
----------------------------------------------------------------------------------
type: numeric (float)
range: [98198290,7.112e+22] units: 10
unique values: 9 missing .: 0/297
tabulation: Freq. Value
33 98198288
33 2.419e+08
33 7.455e+11
33 9.191e+11
33 1.210e+12
33 4.383e+12
33 2.106e+13
33 1.009e+14
33 7.112e+22
gen sigmaf=sqrt(sigma2f)
.
gen weight=sigmaf*sqrt(Sc)
.
. weight, convert all the data *
. * Using the
. gen trAT=AT/(sigmaf*sqrt(Sc))
.
gen trV=V/(sigmaf*sqrt(Sc))
.
gen trY=Y/sqrt(Sc)
.
gen trB=B/sqrt(Sc)
.
gen trVfw=Vfw/sigmaf
.
. gen AThat=trV-Sc*trVfw
.
gen AThat2=(V-Sc*Vfw)/weight
.
. be .28 *
. * Correlation, should
. corr trAT AThat2
. obs=297)
(
| trAT AThat2
-------------+------------------
trAT | 1.0000
AThat2 | 0.2823 1.0000
.
. *************
. * Sign Test *
. *************
. sort indexc
.
by indexc: count if trAT*AThat2>0
.
----------------------------------------------------------------------------------
-> indexc = 1
3
----------------------------------------------------------------------------------
-> indexc = 2
3
----------------------------------------------------------------------------------
-> indexc = 3
2
----------------------------------------------------------------------------------
-> indexc = 4
2
----------------------------------------------------------------------------------
-> indexc = 5
2
----------------------------------------------------------------------------------
-> indexc = 6
3
----------------------------------------------------------------------------------
-> indexc = 7
3
----------------------------------------------------------------------------------
-> indexc = 8
5
----------------------------------------------------------------------------------
-> indexc = 9
2
----------------------------------------------------------------------------------
-> indexc = 10
9
----------------------------------------------------------------------------------
-> indexc = 11
1
----------------------------------------------------------------------------------
-> indexc = 12
6
----------------------------------------------------------------------------------
-> indexc = 13
2
----------------------------------------------------------------------------------
-> indexc = 14
6
----------------------------------------------------------------------------------
-> indexc = 15
6
----------------------------------------------------------------------------------
-> indexc = 16
4
----------------------------------------------------------------------------------
-> indexc = 17
5
----------------------------------------------------------------------------------
-> indexc = 18
5
----------------------------------------------------------------------------------
-> indexc = 19
6
----------------------------------------------------------------------------------
-> indexc = 20
6
----------------------------------------------------------------------------------
-> indexc = 21
7
----------------------------------------------------------------------------------
-> indexc = 22
6
----------------------------------------------------------------------------------
-> indexc = 23
6
----------------------------------------------------------------------------------
-> indexc = 24
4
----------------------------------------------------------------------------------
-> indexc = 25
3
----------------------------------------------------------------------------------
-> indexc = 26
4
----------------------------------------------------------------------------------
-> indexc = 27
5
----------------------------------------------------------------------------------
-> indexc = 28
3
----------------------------------------------------------------------------------
-> indexc = 29
4
----------------------------------------------------------------------------------
-> indexc = 30
4
----------------------------------------------------------------------------------
-> indexc = 31
8
----------------------------------------------------------------------------------
-> indexc = 32
5
----------------------------------------------------------------------------------
-> indexc = 33
8
. count if trAT*AThat2>0
.
148
display _result(1)/_N
.
.4983165
.
. *****************
. * Missing Trade *
. *****************
. for the missing trade, should be .032 *
. * Checking
. quietly summarize trAT
.
local varAT=_result(4)
.
quietly summarize AThat
.
local varHat=_result(4)
.
quietly summarize AThat2
.
local varHat2=_result(4)
.
display `varAT'/`varHat'
.
.03375119
display `varAT'/`varHat2'
.
.031999
.
. **************
. * Rank Tests *
. **************
. keep country indexc indexf trAT AThat2
.
. sort indexc indexf
.
reshape wide trAT AThat2, i(indexc) j(indexf)
. note: j = 1 2 3 4 5 6 7 8 9)
(
long -> wide
Data
-----------------------------------------------------------------------------of obs. 297 -> 33
Number of variables 5 -> 20
Number variable (9 values) indexf -> (dropped)
j
xij variables:
trAT -> trAT1 trAT2 ... trAT9
AThat2 -> AThat21 AThat22 ... AThat29
-----------------------------------------------------------------------------
. local i=1
.
while `i'<9{
. local j=`i'+1
2. while `j'<=9{
3. gen rank`i'`j'=((trAT`i'-trAT`j')*(AThat2`i'-AThat2`j')>0)
4. local j=`j'+1
5.
6. }local i=`i'+1
7.
8. }
. keep country indexc rank*
.
reshape long rank, i(indexc) j(factor)
. note: j = 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47
(
> 48 49 56 57 58 59 67 68 69 78 79 89)
wide -> long
Data
-----------------------------------------------------------------------------of obs. 33 -> 1188
Number of variables 38 -> 4
Number variable (36 values) -> factor
j
xij variables:rank
rank12 rank13 ... rank89 ->
-----------------------------------------------------------------------------
egen r1=sum(rank), by(indexc)
.
gen r2=r1/36
.
collapse r2,by(indexc country)
.
sum r2
.
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
r2 | 33 .6026936 .1721445 .0833333 .9166667
list
.
+----------------------------------+
| indexc country r2 |
|----------------------------------|
1. | 1 Bangladesh .75 |
2. | 2 Pakistan .7222222 |
3. | 3 Indonesia .6666667 |
4. | 4 Sri Lanka .4166667 |
5. | 5 Thailand .6944444 |
|----------------------------------|
6. | 6 Colombia .8055556 |
7. | 7 Panama .5555556 |
8. | 8 Yugoslavia .4444444 |
9. | 9 Portugal .5277778 |
10. | 10 Uruguay .7222222 |
|----------------------------------|
11. | 11 Greece .4722222 |
12. | 12 Ireland .5277778 |
13. | 13 Spain .3888889 |
14. | 14 Israel .3888889 |
15. | 15 Hong Kong .8333333 |
|----------------------------------|
16. | 16 New Zealand .5277778 |
17. | 17 Austria .5277778 |
18. | 18 Singapore .6111111 |
19. | 19 Italy .7777778 |
20. | 20 UK .5833333 |
|----------------------------------|
21. | 21 Japan .7777778 |
22. | 22 Belgium .6111111 |
23. | 23 Trinidad .5 |
24. | 24 Netherlands .5277778 |
25. | 25 Finland .4722222 |
|----------------------------------|
26. | 26 Denmark .5277778 |
27. | 27 West Germany .8055556 |
28. | 28 France .0833333 |
29. | 29 Sweden .6666667 |
30. | 30 Norway .6111111 |
|----------------------------------|
31. | 31 Switzerland .5555556 |
32. | 32 Canada .8888889 |
33. | 33 USA .9166667 |
+----------------------------------+
. log close
. name: <unnamed>
log: Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
log
> er-2\sign_rank_1.log type: text
on: 19 Jun 2024, 15:29:31
closed
----------------------------------------------------------------------------------
exit
.
end of do-file
My code
# Transform ----
<- readRDS(fout) %>%
trefler # Number of country in the dataset
mutate(
c = max(indexc),
f = max(indexf)
%>%
) # Calculate the world level of Yw, Bw and Vw
mutate(
yww = sum(y),
yw = yww / f,
bww = sum(b),
bw = bww / f
%>%
) group_by(indexf) %>%
mutate(
vfw = sum(v)
%>%
) ungroup() %>%
# Calculate country share Sc
mutate(
sc = (y - b) / (yw - bw)
%>%
) # Calculate epsilon(fc) and sigma^2(f) according to eq.2 in Trefler (1995)
mutate(
efc = at - (v - sc * vfw)
%>%
) # Construct the average epsilon for a given factor
group_by(indexf) %>%
mutate(ave = sum(efc) / c) %>%
# Construct sigma^2 and the weight
mutate(
sigma2f = sum((efc - ave)^2) / (c - 1),
sigmaf = sqrt(sigma2f),
weight = sigmaf * sqrt(sc)
%>%
) ungroup() %>%
# Using the weight, convert all the data
mutate(
trat = at / (sigmaf * sqrt(sc)),
athat2 = (v - (sc * vfw)) / weight
%>%
) arrange(country)
# Correlation, should be .28
%>%
trefler select(trat, athat2) %>%
cor()
trat athat2
trat 1.0000000 0.2822883
athat2 0.2822883 1.0000000
# Sign Test ----
%>%
trefler group_by(country, indexc) %>%
summarize(
p = sum(trat * athat2 > 0) / n()
)
# A tibble: 33 × 3
# Groups: country [33]
country indexc p
<chr> <int> <dbl>
1 Austria 17 0.556
2 Bangladesh 1 0.333
3 Belgium 22 0.667
4 Canada 32 0.556
5 Colombia 6 0.333
6 Denmark 26 0.444
7 Finland 25 0.333
8 France 28 0.333
9 Greece 11 0.111
10 Hong Kong 15 0.667
# ℹ 23 more rows
%>%
trefler summarize(
p = sum(trat * athat2 > 0) / n()
)
# A tibble: 1 × 1
p
<dbl>
1 0.498
# Missing Trade ----
# Checking for the missing trade, should be .032
%>%
trefler summarize(
varat = var(trat),
varhat2 = var(athat2)
%>%
) mutate(
varat_varhat2 = varat / varhat2
)
# A tibble: 1 × 3
varat varhat2 varat_varhat2
<dbl> <dbl> <dbl>
1 1.61 50.3 0.0320
# Rank Tests ----
<- trefler %>%
trefler_wide select(country, indexc, indexf, trat, athat2) %>%
arrange(indexc, indexf) %>%
pivot_wider(
names_from = indexf,
values_from = c(trat, athat2)
)
# This would be too long
# trefler_wide <- trefler_wide %>%
# mutate(
# rank12 = (trat_1 - trat_2) * (athat2_1 - athat2_2) > 0,
# rank13 = (trat_1 - trat_3) * (athat2_1 - athat2_3) > 0,
# ...
# rank89 = (trat_8 - trat_9) * (athat2_8 - athat2_9) > 0,
# )
# create all relevant trat_1 - trat_2, trat_1 - trat_3, etc.
<- expand_grid(
ranks x = 1:8,
y = 1:9
%>%
) filter(x < y)
# The syntax here is based on internal R developer functions, but these
# allow to create columns with minimal lines of code and avoids more complicated
# bracket syntax
<- map2_df(
trefler_rank pull(ranks, x),
pull(ranks, y),
function(x, y) {
%>%
trefler_wide mutate(
name = paste0("rank", x, y),
value = (!!sym(paste0("trat_", x)) - !!sym(paste0("trat_", y))) *
!!sym(paste0("athat2_", x)) - !!sym(paste0("athat2_", y))) > 0
(%>%
) select(country, indexc, name, value)
}%>%
) group_by(country, indexc) %>%
summarise(r1 = sum(value)) %>%
mutate(r2 = r1 / 36)
trefler_rank
# A tibble: 33 × 4
# Groups: country [33]
country indexc r1 r2
<chr> <int> <int> <dbl>
1 Austria 17 19 0.528
2 Bangladesh 1 27 0.75
3 Belgium 22 22 0.611
4 Canada 32 32 0.889
5 Colombia 6 29 0.806
6 Denmark 26 19 0.528
7 Finland 25 17 0.472
8 France 28 3 0.0833
9 Greece 11 17 0.472
10 Hong Kong 15 30 0.833
# ℹ 23 more rows
%>%
trefler_rank pull(r2) %>%
mean()
[1] 0.6026936
Extra step: formatting the table
%>%
trefler group_by(country, indexc) %>%
summarize(
p = sum(trat * athat2 > 0) / n()
%>%
) arrange(indexc) %>% # same order as in the book
select(country, sign_hov = p) %>%
left_join(
%>%
trefler_rank select(country, rank_hov = r2)
%>%
) bind_rows(
%>%
trefler summarize(
p = sum(trat * athat2 > 0) / n()
%>%
) mutate(
country = "All countries",
rank_hov = mean(pull(trefler_rank, r2))
%>%
) select(country, sign_hov = p, rank_hov)
%>%
) mutate_if(is.numeric, round, 2) %>% # same no of decimals as in the book
kable()
country | sign_hov | rank_hov |
---|---|---|
Bangladesh | 0.33 | 0.75 |
Pakistan | 0.33 | 0.72 |
Indonesia | 0.22 | 0.67 |
Sri Lanka | 0.22 | 0.42 |
Thailand | 0.22 | 0.69 |
Colombia | 0.33 | 0.81 |
Panama | 0.33 | 0.56 |
Yugoslavia | 0.56 | 0.44 |
Portugal | 0.22 | 0.53 |
Uruguay | 1.00 | 0.72 |
Greece | 0.11 | 0.47 |
Ireland | 0.67 | 0.53 |
Spain | 0.22 | 0.39 |
Israel | 0.67 | 0.39 |
Hong Kong | 0.67 | 0.83 |
New Zealand | 0.44 | 0.53 |
Austria | 0.56 | 0.53 |
Singapore | 0.56 | 0.61 |
Italy | 0.67 | 0.78 |
UK | 0.67 | 0.58 |
Japan | 0.78 | 0.78 |
Belgium | 0.67 | 0.61 |
Trinidad | 0.67 | 0.50 |
Netherlands | 0.44 | 0.53 |
Finland | 0.33 | 0.47 |
Denmark | 0.44 | 0.53 |
West Germany | 0.56 | 0.81 |
France | 0.33 | 0.08 |
Sweden | 0.44 | 0.67 |
Norway | 0.44 | 0.61 |
Switzerland | 0.89 | 0.56 |
Canada | 0.56 | 0.89 |
USA | 0.89 | 0.92 |
All countries | 0.50 | 0.60 |
Exercise 2
Given uniform technological differences across countries, run the program sign_rank_2.do
to redo the sign test, rank test, and missing trade. Use the results in sign_rank_2.log
to replicate column (3) and (5), given column (6) in Table 2.5.
Feenstra’s code
program is to conduct sign test, Rank test and Missing trade test *
* This using delta *
*
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\sign_rank_2.log, replace *
* log using "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\sign_rank_2.log", replace
set mem 30m
use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler, clear *
* use "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler", clear
of country in the dataset *
* number egen C=max(indexc)
egen F=max(indexf)
level of Yw, Bw and Vw *
* Calculate the world egen Yww=sum(Y)
gen Yw=Yww/F
egen Bww=sum(B)
gen Bw=Bww/F
egen Vfw=sum(V), by(indexf)
egen Vw=sum(delta*V), by(indexf)
* Calculate country share Sc *gen Sc=(Y-B)/(Yw-Bw)
eq.2 in Trefler (1995)
* Calculate epsilon(fc) and sigma^2(f) according to
gen Efc=delta*AT-(delta*V-Sc*Vw)
for a given factor *
* Construct the average epsilon egen total=sum(Efc),by(indexf)
gen ave=total/C
weight *
* Construct sigma^2 and the
egen tot=sum((Efc-ave)^2), by(indexf)
gen sigma2f=tot/(C-1)
codebook sigma2f
gen sigmaf=sqrt(sigma2f)
gen weight=sigmaf*sqrt(Sc)
weight, convert all the data *
* Using the
gen trAT=delta*AT/weight
gen AThat2=(delta*V-Sc*Vw)/weight
* Correlation *
corr trAT AThat2
*************
* Sign Test *
*************
sort indexc
by indexc: count if trAT*AThat2>0
count if trAT*AThat2>0
display _result(1)/_N
*****************
* Missing Trade *
*****************
quietly summarize trAT
local varAT=_result(4)
quietly summarize AThat
local varHat=_result(4)
quietly summarize AThat2
local varHat2=_result(4)
display `varAT'/`varHat'
display `varAT'/`varHat2'
*************
* Rank Test *
*************
keep country indexc indexf trAT AThat2
sort indexc indexf
reshape wide trAT AThat2, i(indexc) j(indexf)
local i=1
while `i'<9{
local j=`i'+1
while `j'<=9{
gen rank`i'`j'=((trAT`i'-trAT`j')*(AThat2`i'-AThat2`j')>0)
local j=`j'+1
}local i=`i'+1
}
keep country indexc rank*
reshape long rank, i(indexc) j(factor)
egen r1=sum(rank), by(indexc)
gen r2=r1/36
collapse r2,by(indexc country)
sum r2
list
log close
exit
Output:
program is to conduct sign test, Rank test and Missing trade test *
. * This using delta *
. *
. capture log close
.
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
. r-2\sign_rank_2.log, replace
> note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
(r-2\sign_rank_2.log not found)
>
----------------------------------------------------------------------------------name: <unnamed>
log: Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
log
> er-2\sign_rank_2.log type: text
on: 19 Jun 2024, 15:30:38
opened
. set mem 30m
.
memory allocation
Current
memory usage
current value description (1M = 1024k)
settable
--------------------------------------------------------------------set maxvar 5000 max. variables allowed 1.909M
set memory 30M max. data space 30.000M
set matsize 400 max. RHS vars in models 1.254M
-----------
33.163M
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\tr
. clear
> efler,
. of country in the dataset *
. * number egen C=max(indexc)
.
egen F=max(indexf)
.
. level of Yw, Bw and Vw *
. * Calculate the world egen Yww=sum(Y)
.
gen Yw=Yww/F
.
egen Bww=sum(B)
.
gen Bw=Bww/F
.
egen Vfw=sum(V), by(indexf)
.
egen Vw=sum(delta*V), by(indexf)
.
.
. * Calculate country share Sc *gen Sc=(Y-B)/(Yw-Bw)
.
. eq.2 in Trefler (1995)
. * Calculate epsilon(fc) and sigma^2(f) according to
. gen Efc=delta*AT-(delta*V-Sc*Vw)
.
. for a given factor *
. * Construct the average epsilon egen total=sum(Efc),by(indexf)
.
gen ave=total/C
.
. weight *
. * Construct sigma^2 and the
. egen tot=sum((Efc-ave)^2), by(indexf)
.
gen sigma2f=tot/(C-1)
.
. codebook sigma2f
.
----------------------------------------------------------------------------------
sigma2f (unlabeled)
----------------------------------------------------------------------------------
type: numeric (float)
range: [51579730,3.418e+21] units: 10
unique values: 9 missing .: 0/297
tabulation: Freq. Value
33 51579728
33 3.926e+08
33 6.172e+10
33 1.211e+11
33 1.883e+11
33 2.320e+11
33 2.662e+11
33 2.113e+12
33 3.418e+21
gen sigmaf=sqrt(sigma2f)
.
gen weight=sigmaf*sqrt(Sc)
.
. weight, convert all the data *
. * Using the
. gen trAT=delta*AT/weight
.
gen AThat2=(delta*V-Sc*Vw)/weight
.
.
. * Correlation *
. corr trAT AThat2
. obs=297)
(
| trAT AThat2
-------------+------------------
trAT | 1.0000
AThat2 | 0.4100 1.0000
.
. *************
. * Sign Test *
. *************
. sort indexc
.
by indexc: count if trAT*AThat2>0
.
----------------------------------------------------------------------------------
-> indexc = 1
7
----------------------------------------------------------------------------------
-> indexc = 2
6
----------------------------------------------------------------------------------
-> indexc = 3
6
----------------------------------------------------------------------------------
-> indexc = 4
5
----------------------------------------------------------------------------------
-> indexc = 5
6
----------------------------------------------------------------------------------
-> indexc = 6
8
----------------------------------------------------------------------------------
-> indexc = 7
7
----------------------------------------------------------------------------------
-> indexc = 8
6
----------------------------------------------------------------------------------
-> indexc = 9
7
----------------------------------------------------------------------------------
-> indexc = 10
1
----------------------------------------------------------------------------------
-> indexc = 11
5
----------------------------------------------------------------------------------
-> indexc = 12
4
----------------------------------------------------------------------------------
-> indexc = 13
7
----------------------------------------------------------------------------------
-> indexc = 14
8
----------------------------------------------------------------------------------
-> indexc = 15
8
----------------------------------------------------------------------------------
-> indexc = 16
2
----------------------------------------------------------------------------------
-> indexc = 17
6
----------------------------------------------------------------------------------
-> indexc = 18
9
----------------------------------------------------------------------------------
-> indexc = 19
3
----------------------------------------------------------------------------------
-> indexc = 20
7
----------------------------------------------------------------------------------
-> indexc = 21
6
----------------------------------------------------------------------------------
-> indexc = 22
7
----------------------------------------------------------------------------------
-> indexc = 23
9
----------------------------------------------------------------------------------
-> indexc = 24
4
----------------------------------------------------------------------------------
-> indexc = 25
4
----------------------------------------------------------------------------------
-> indexc = 26
4
----------------------------------------------------------------------------------
-> indexc = 27
6
----------------------------------------------------------------------------------
-> indexc = 28
3
----------------------------------------------------------------------------------
-> indexc = 29
4
----------------------------------------------------------------------------------
-> indexc = 30
4
----------------------------------------------------------------------------------
-> indexc = 31
8
----------------------------------------------------------------------------------
-> indexc = 32
2
----------------------------------------------------------------------------------
-> indexc = 33
5
. count if trAT*AThat2>0
.
184
display _result(1)/_N
.
.61952862
.
. *****************
. * Missing Trade *
. *****************
. quietly summarize trAT
.
local varAT=_result(4)
.
quietly summarize AThat
.
local varHat=_result(4)
.
quietly summarize AThat2
.
local varHat2=_result(4)
.
display `varAT'/`varHat'
.
.07061819
display `varAT'/`varHat2'
.
.07061819
.
. *************
. * Rank Test *
. *************
. keep country indexc indexf trAT AThat2
.
. sort indexc indexf
.
reshape wide trAT AThat2, i(indexc) j(indexf)
. note: j = 1 2 3 4 5 6 7 8 9)
(
long -> wide
Data
-----------------------------------------------------------------------------of obs. 297 -> 33
Number of variables 5 -> 20
Number variable (9 values) indexf -> (dropped)
j
xij variables:
trAT -> trAT1 trAT2 ... trAT9
AThat2 -> AThat21 AThat22 ... AThat29
-----------------------------------------------------------------------------
. local i=1
.
while `i'<9{
. local j=`i'+1
2. while `j'<=9{
3. gen rank`i'`j'=((trAT`i'-trAT`j')*(AThat2`i'-AThat2`j')>0)
4. local j=`j'+1
5.
6. }local i=`i'+1
7.
8. }
. keep country indexc rank*
.
reshape long rank, i(indexc) j(factor)
. note: j = 12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47
(
> 48 49 56 57 58 59 67 68 69 78 79 89)
wide -> long
Data
-----------------------------------------------------------------------------of obs. 33 -> 1188
Number of variables 38 -> 4
Number variable (36 values) -> factor
j
xij variables:rank
rank12 rank13 ... rank89 ->
-----------------------------------------------------------------------------
egen r1=sum(rank), by(indexc)
.
gen r2=r1/36
.
collapse r2,by(indexc country)
.
sum r2
.
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
r2 | 33 .6153199 .1508913 .1944444 .8611111
list
.
+----------------------------------+
| indexc country r2 |
|----------------------------------|
1. | 1 Bangladesh .7777778 |
2. | 2 Pakistan .7777778 |
3. | 3 Indonesia .6666667 |
4. | 4 Sri Lanka .6666667 |
5. | 5 Thailand .7222222 |
|----------------------------------|
6. | 6 Colombia .8611111 |
7. | 7 Panama .7777778 |
8. | 8 Yugoslavia .6111111 |
9. | 9 Portugal .5833333 |
10. | 10 Uruguay .5277778 |
|----------------------------------|
11. | 11 Greece .75 |
12. | 12 Ireland .3888889 |
13. | 13 Spain .6944444 |
14. | 14 Israel .6944444 |
15. | 15 Hong Kong .7222222 |
|----------------------------------|
16. | 16 New Zealand .6111111 |
17. | 17 Austria .4444444 |
18. | 18 Singapore .6111111 |
19. | 19 Italy .6666667 |
20. | 20 UK .6388889 |
|----------------------------------|
21. | 21 Japan .7777778 |
22. | 22 Belgium .5277778 |
23. | 23 Trinidad .5277778 |
24. | 24 Netherlands .4722222 |
25. | 25 Finland .5 |
|----------------------------------|
26. | 26 Denmark .4166667 |
27. | 27 West Germany .7777778 |
28. | 28 France .1944444 |
29. | 29 Sweden .3611111 |
30. | 30 Norway .7777778 |
|----------------------------------|
31. | 31 Switzerland .5 |
32. | 32 Canada .5555556 |
33. | 33 USA .7222222 |
+----------------------------------+
. log close
. name: <unnamed>
log: Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
log
> er-2\sign_rank_2.log type: text
on: 19 Jun 2024, 15:30:44
closed
----------------------------------------------------------------------------------
exit
.
end of do-file
My code
# Transform ----
<- readRDS(fout) %>%
trefler # Number of country in the dataset
mutate(
c = max(indexc),
f = max(indexf)
%>%
) # Calculate the world level of Yw, Bw and Vw
mutate(
yww = sum(y),
yw = yww / f,
bww = sum(b),
bw = bww / f
%>%
) group_by(indexf) %>%
mutate(
vfw = sum(v),
vw = sum(delta * v)
%>%
) ungroup() %>%
# Calculate country share Sc
mutate(
sc = (y - b) / (yw - bw)
%>%
) # Calculate epsilon(fc) and sigma^2(f) according to eq.2 in Trefler (1995)
mutate(
efc = delta * at - (delta * v - sc * vw)
%>%
) # Construct the average epsilon for a given factor
group_by(indexf) %>%
mutate(ave = sum(efc) / c) %>%
# Construct sigma^2 and the weight
mutate(
sigma2f = sum((efc - ave)^2) / (c - 1),
sigmaf = sqrt(sigma2f),
weight = sigmaf * sqrt(sc)
%>%
) ungroup() %>%
# Using the weight, convert all the data
mutate(
trat = delta * at / weight,
athat2 = (delta * v - (sc * vw)) / weight
)
# Correlation should be .41
%>%
trefler select(trat, athat2) %>%
cor()
trat athat2
trat 1.0000000 0.4099617
athat2 0.4099617 1.0000000
# Sign Test ----
%>%
trefler group_by(country, indexc) %>%
summarize(
p = sum(trat * athat2 > 0) / n()
)
# A tibble: 33 × 3
# Groups: country [33]
country indexc p
<chr> <int> <dbl>
1 Austria 17 0.667
2 Bangladesh 1 0.778
3 Belgium 22 0.778
4 Canada 32 0.222
5 Colombia 6 0.889
6 Denmark 26 0.444
7 Finland 25 0.444
8 France 28 0.333
9 Greece 11 0.556
10 Hong Kong 15 0.889
# ℹ 23 more rows
%>%
trefler summarize(
p = sum(trat * athat2 > 0) / n()
)
# A tibble: 1 × 1
p
<dbl>
1 0.620
# Missing Trade ----
# Checking for the missing trade, should be .07
%>%
trefler summarize(
varat = var(trat),
varhat2 = var(athat2)
%>%
) mutate(
varat_varhat2 = varat / varhat2
)
# A tibble: 1 × 3
varat varhat2 varat_varhat2
<dbl> <dbl> <dbl>
1 1.34 19.0 0.0706
# Rank Tests ----
<- trefler %>%
trefler_wide select(country, indexc, indexf, trat, athat2) %>%
arrange(indexc, indexf) %>%
pivot_wider(
names_from = indexf,
values_from = c(trat, athat2)
)
<- expand_grid(
ranks x = 1:8,
y = 1:9
%>%
) filter(x < y)
<- map2_df(
trefler_rank pull(ranks, x),
pull(ranks, y),
function(x, y) {
%>%
trefler_wide mutate(
name = paste0("rank", x, y),
value = (!!sym(paste0("trat_", x)) - !!sym(paste0("trat_", y))) *
!!sym(paste0("athat2_", x)) - !!sym(paste0("athat2_", y))) > 0
(%>%
) select(country, indexc, name, value)
}%>%
) group_by(country, indexc) %>%
summarise(r1 = sum(value)) %>%
mutate(r2 = r1 / 36)
trefler_rank
# A tibble: 33 × 4
# Groups: country [33]
country indexc r1 r2
<chr> <int> <int> <dbl>
1 Austria 17 16 0.444
2 Bangladesh 1 28 0.778
3 Belgium 22 19 0.528
4 Canada 32 20 0.556
5 Colombia 6 31 0.861
6 Denmark 26 15 0.417
7 Finland 25 18 0.5
8 France 28 7 0.194
9 Greece 11 27 0.75
10 Hong Kong 15 26 0.722
# ℹ 23 more rows
%>%
trefler_rank pull(r2) %>%
mean()
[1] 0.6153199
Extra step: formatting the table
%>%
trefler group_by(country, indexc) %>%
summarize(
p = sum(trat * athat2 > 0) / n()
%>%
) arrange(indexc) %>% # same order as in the book
select(country, sign_hov = p) %>%
left_join(
%>%
trefler_rank select(country, rank_hov = r2)
%>%
) bind_rows(
%>%
trefler summarize(
p = sum(trat * athat2 > 0) / n()
%>%
) mutate(
country = "All countries",
rank_hov = mean(pull(trefler_rank, r2))
%>%
) select(country, sign_hov = p, rank_hov)
%>%
) mutate_if(is.numeric, round, 2) %>% # same no of decimals as in the book
kable()
country | sign_hov | rank_hov |
---|---|---|
Bangladesh | 0.78 | 0.78 |
Pakistan | 0.67 | 0.78 |
Indonesia | 0.67 | 0.67 |
Sri Lanka | 0.56 | 0.67 |
Thailand | 0.67 | 0.72 |
Colombia | 0.89 | 0.86 |
Panama | 0.78 | 0.78 |
Yugoslavia | 0.67 | 0.61 |
Portugal | 0.78 | 0.58 |
Uruguay | 0.11 | 0.53 |
Greece | 0.56 | 0.75 |
Ireland | 0.44 | 0.39 |
Spain | 0.78 | 0.69 |
Israel | 0.89 | 0.69 |
Hong Kong | 0.89 | 0.72 |
New Zealand | 0.22 | 0.61 |
Austria | 0.67 | 0.44 |
Singapore | 1.00 | 0.61 |
Italy | 0.33 | 0.67 |
UK | 0.78 | 0.64 |
Japan | 0.67 | 0.78 |
Belgium | 0.78 | 0.53 |
Trinidad | 1.00 | 0.53 |
Netherlands | 0.44 | 0.47 |
Finland | 0.44 | 0.50 |
Denmark | 0.44 | 0.42 |
West Germany | 0.67 | 0.78 |
France | 0.33 | 0.19 |
Sweden | 0.44 | 0.36 |
Norway | 0.44 | 0.78 |
Switzerland | 0.89 | 0.50 |
Canada | 0.22 | 0.56 |
USA | 0.56 | 0.72 |
All countries | 0.62 | 0.62 |
Notes
The Stata code that I run returns the same values as R. However:
- Austria should have values 0.67 and 0.47. I got 0.67 and 0.44.
- France should have values 0.33 and 0.22. I got 0.33 and 0.19.
- Switzerland should have values 0.89 and 0.47. I got 0.89 and 0.50.
Exercise 3
Allowing all factors in each country to have different productivities, now run the program compute_pi.do
to compute factor productivities pi_.log
or alternatively in the data files pi_1.dta, pi_2.dta, pi_3.dta, pi_4.dta
to answer the following:
- Which factor has the most negative productivities estimated?
- What is the correlation between the estimated labor productivity and the productivities of other factors? What is the correlation between each factor productivity and GDP per-capita (which you can find in the file
trefler.dta
)?
Feenstra’s code
program is to compute pai, the factor productivity *
* This
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi.log,replace *
* log using "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi.log", replace
set mem 30m
use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler, clear *
* use "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\trefler", clear
of country in the dataset *
* number egen C=max(indexc)
egen F=max(indexf)
level of Yw, Bw and Vw *
* Calculate the world egen Yww=sum(Y)
gen Yw=Yww/F
egen Bww=sum(B)
gen Bw=Bww/F
egen Vfw=sum(V), by(indexf)
* Calculate country share Sc *gen Sc=(Y-B)/(Yw-Bw)
eq.2 in Trefler (1995)
* Calculate epsilon(fc) and sigma^2(f) according to gen Efc=AT-(V-Sc*Vfw)
for a given factor *
* Construct the average epsilon egen total=sum(Efc),by(indexf)
gen ave=total/C
weight *
* Construct sigma^2 and the
egen tot=sum((Efc-ave)^2), by(indexf)
gen sigma2f=tot/(C-1)
codebook sigma2f
gen sigmaf=sqrt(sigma2f)
gen weight=sigmaf*sqrt(Sc)
weight, convert all the data *
* Using the
gen trAT=AT/(sigmaf*sqrt(Sc))
gen trV=V/(sigmaf*sqrt(Sc))
gen trY=Y/sqrt(Sc)
gen trB=B/sqrt(Sc)
gen trVfw=Vfw/sigmaf
gen AThat=trV-Sc*trVfw
gen AThat2=(V-Sc*Vfw)/weight
* Construct Aggregate Labor Endowment *
preserve
keep if indexf==7 |indexf==8 | indexf==9
gen en=2
replace en=3 if indexf==8
replace en=4 if indexf==9
keep country factor AT V en indexc Sc
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_189,replace *
* save "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_189", replace
restore
preserve
drop if indexf==7 |indexf==8 | indexf==9
egen v_l=sum(V), by(country)
egen AT_l=sum(AT), by(country)
drop V AT factor
rename v_l V
rename AT_l AT
gen en=1
collapse (mean)AT V en indexc Sc, by(country)
gen str5 factor="Labor"
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_L,replace *
* save "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_L", replace
restore
use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_189,clear *
* use "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_189", clear
append using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_L *
* append using "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\indexf_L"
sort indexc en
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi,replace *
* save "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi", replace
************************************factor productivity *
* Compute Pi:
************************************
use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi, clear *
* use "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi", clear
gen p_0=1
gen p_1=1
local i=1
while `i'<=4{
preserve
keep if en==`i'
local j=1
while `j'<51{
replace p_0=p_1
gen Vp=p_0*V
egen Vpw=sum(Vp)
replace p_1=(AT+Sc*Vpw)/V
replace p_1=1 if country=="USA"
drop Vp Vpw
local j=`j'+1
}keep en country indexc p_1
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_`i',replace *
* save "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_`i'", replace
restore
local i=`i'+1
}
local i=1
while `i'<=4{
use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_`i',clear *
* use "Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_`i'", clear
sort en indexc
by en: list en indexc country p_1
local i=`i'+1
}
log close
exit
Output:
program is to compute pai, the factor productivity *
. * This
. capture log close
.
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
. r-2\pi.log,replace
>
----------------------------------------------------------------------------------name: <unnamed>
log: Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
pi.log
> er-2\log type: text
on: 19 Jun 2024, 15:32:25
opened
. set mem 30m
.
memory allocation
Current
memory usage
current value description (1M = 1024k)
settable
--------------------------------------------------------------------set maxvar 5000 max. variables allowed 1.909M
set memory 30M max. data space 30.000M
set matsize 400 max. RHS vars in models 1.254M
-----------
33.163M
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\tr
. clear
> efler,
. of country in the dataset *
. * number egen C=max(indexc)
.
egen F=max(indexf)
.
. level of Yw, Bw and Vw *
. * Calculate the world egen Yww=sum(Y)
.
gen Yw=Yww/F
.
egen Bww=sum(B)
.
gen Bw=Bww/F
.
egen Vfw=sum(V), by(indexf)
.
.
. * Calculate country share Sc *gen Sc=(Y-B)/(Yw-Bw)
.
. eq.2 in Trefler (1995)
. * Calculate epsilon(fc) and sigma^2(f) according to gen Efc=AT-(V-Sc*Vfw)
.
. for a given factor *
. * Construct the average epsilon egen total=sum(Efc),by(indexf)
.
gen ave=total/C
.
. weight *
. * Construct sigma^2 and the
. egen tot=sum((Efc-ave)^2), by(indexf)
.
gen sigma2f=tot/(C-1)
.
. codebook sigma2f
.
----------------------------------------------------------------------------------
sigma2f (unlabeled)
----------------------------------------------------------------------------------
type: numeric (float)
range: [98198290,7.112e+22] units: 10
unique values: 9 missing .: 0/297
tabulation: Freq. Value
33 98198288
33 2.419e+08
33 7.455e+11
33 9.191e+11
33 1.210e+12
33 4.383e+12
33 2.106e+13
33 1.009e+14
33 7.112e+22
gen sigmaf=sqrt(sigma2f)
.
gen weight=sigmaf*sqrt(Sc)
.
. weight, convert all the data *
. * Using the
. gen trAT=AT/(sigmaf*sqrt(Sc))
.
gen trV=V/(sigmaf*sqrt(Sc))
.
gen trY=Y/sqrt(Sc)
.
gen trB=B/sqrt(Sc)
.
gen trVfw=Vfw/sigmaf
.
. gen AThat=trV-Sc*trVfw
.
gen AThat2=(V-Sc*Vfw)/weight
.
.
. * Construct Aggregate Labor Endowment *
. preserve
.
keep if indexf==7 |indexf==8 | indexf==9
.
(198 observations deleted)
gen en=2
.
replace en=3 if indexf==8
. real changes made)
(33
replace en=4 if indexf==9
. real changes made)
(33
keep country factor AT V en indexc Sc
.
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\i
. replace
> ndexf_189,
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\ind
> exf_189.dta saved
. restore
.
. preserve
.
drop if indexf==7 |indexf==8 | indexf==9
.
(99 observations deleted)
. egen v_l=sum(V), by(country)
.
egen AT_l=sum(AT), by(country)
.
. drop V AT factor
.
rename v_l V
.
rename AT_l AT
.
gen en=1
.
collapse (mean)AT V en indexc Sc, by(country)
.
gen str5 factor="Labor"
.
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\i
. replace
> ndexf_L,
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\ind
> exf_L.dta saved
. restore
.
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\in
. clear
> dexf_189,
append using Z:\home\pacha\github\advanced-international-trade\first-edition\Cha
.
> pter-2\indexf_Lbyte now float
indexc was
sort indexc en
.
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\p
. replace
> i,pi.
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\
> dta saved
.
. ************************************factor productivity *
. * Compute Pi:
. ************************************
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi
. clear
> ,
. gen p_0=1
.
gen p_1=1
.
. local i=1
.
while `i'<=4{
. preserve
2. keep if en==`i'
3. local j=1
4. while `j'<51{
5. replace p_0=p_1
6. gen Vp=p_0*V
7. egen Vpw=sum(Vp)
8. replace p_1=(AT+Sc*Vpw)/V
9. replace p_1=1 if country=="USA"
10. drop Vp Vpw
11. local j=`j'+1
12.
13. }keep en country indexc p_1
14. save Z:\home\pacha\github\advanced-international-trade\first-edition\
15. `i',replace
> Chapter-2\pi_restore
16. local i=`i'+1
17.
18. }
(99 observations deleted)real changes made)
(0 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(30 real change made)
(1 real changes made)
(29 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_
> 1.dta saved
(99 observations deleted)real changes made)
(0 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_
> 2.dta saved
(99 observations deleted)real changes made)
(0 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(32 real change made)
(1 real changes made)
(31 real changes made)
(31 real change made)
(1 real changes made)
(30 real changes made)
(29 real change made)
(1 real changes made)
(28 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_
> 3.dta saved
(99 observations deleted)real changes made)
(0 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(33 real change made)
(1 real changes made)
(32 real changes made)
(28 real change made)
(1 real changes made)
(27 real changes made)
(31 real change made)
(1 real changes made)
(30 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1 real changes made)
(0 real change made)
(1 real change made)
(1
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-2\pi_
> 4.dta saved
. local i=1
.
while `i'<=4{
. use Z:\home\pacha\github\advanced-international-trade\first-edition\C
2. `i',clear
> hapter-2\pi_sort en indexc
3. by en: list en indexc country p_1
4. local i=`i'+1
5.
6. }
----------------------------------------------------------------------------------
-> en = 1
+---------------------------------------+
| en indexc country p_1 |
|---------------------------------------|
1. | 1 1 Bangladesh .0165219 |
2. | 1 2 Pakistan .0394124 |
3. | 1 3 Indonesia .0432824 |
4. | 1 4 Sri Lanka .0344432 |
5. | 1 5 Thailand .048902 |
|---------------------------------------|
6. | 1 6 Colombia .1940833 |
7. | 1 7 Panama .2279774 |
8. | 1 8 Yugoslavia .1997569 |
9. | 1 9 Portugal .1759864 |
10. | 1 10 Uruguay .2029693 |
|---------------------------------------|
11. | 1 11 Greece .3140718 |
12. | 1 12 Ireland .4166084 |
13. | 1 13 Spain .4172725 |
14. | 1 14 Israel .6131932 |
15. | 1 15 Hong Kong .3781684 |
|---------------------------------------|
16. | 1 16 New Zealand .5850298 |
17. | 1 17 Austria .5694186 |
18. | 1 18 Singapore .4873306 |
19. | 1 19 Italy .5907437 |
20. | 1 20 UK .6332356 |
|---------------------------------------|
21. | 1 21 Japan .604921 |
22. | 1 22 Belgium .7233988 |
23. | 1 23 Trinidad .448344 |
24. | 1 24 Netherlands .7933354 |
25. | 1 25 Finland .6969619 |
|---------------------------------------|
26. | 1 26 Denmark .7061784 |
27. | 1 27 West Germany .7691833 |
28. | 1 28 France .735665 |
29. | 1 29 Sweden .7270576 |
30. | 1 30 Norway .7816517 |
|---------------------------------------|
31. | 1 31 Switzerland .9317446 |
32. | 1 32 Canada .7799865 |
33. | 1 33 USA 1 |
+---------------------------------------+
----------------------------------------------------------------------------------
-> en = 2
+---------------------------------------+
| en indexc country p_1 |
|---------------------------------------|
1. | 2 1 Bangladesh .9119021 |
2. | 2 2 Pakistan .4867497 |
3. | 2 3 Indonesia .250653 |
4. | 2 4 Sri Lanka .1259789 |
5. | 2 5 Thailand .3929541 |
|---------------------------------------|
6. | 2 6 Colombia .4856291 |
7. | 2 7 Panama .3224079 |
8. | 2 8 Yugoslavia .2985477 |
9. | 2 9 Portugal .297107 |
10. | 2 10 Uruguay .4060566 |
|---------------------------------------|
11. | 2 11 Greece .4401574 |
12. | 2 12 Ireland .4783922 |
13. | 2 13 Spain .519489 |
14. | 2 14 Israel .5049116 |
15. | 2 15 Hong Kong .5150965 |
|---------------------------------------|
16. | 2 16 New Zealand .6197254 |
17. | 2 17 Austria .542198 |
18. | 2 18 Singapore .3209652 |
19. | 2 19 Italy .5122213 |
20. | 2 20 UK .8159872 |
|---------------------------------------|
21. | 2 21 Japan .6502131 |
22. | 2 22 Belgium .6457499 |
23. | 2 23 Trinidad .4193396 |
24. | 2 24 Netherlands .7513015 |
25. | 2 25 Finland .6453199 |
|---------------------------------------|
26. | 2 26 Denmark .703564 |
27. | 2 27 West Germany .6629313 |
28. | 2 28 France .6150761 |
29. | 2 29 Sweden .9445322 |
30. | 2 30 Norway .6199874 |
|---------------------------------------|
31. | 2 31 Switzerland .6079721 |
32. | 2 32 Canada .7359194 |
33. | 2 33 USA 1 |
+---------------------------------------+
----------------------------------------------------------------------------------
-> en = 3
+----------------------------------------+
| en indexc country p_1 |
|----------------------------------------|
1. | 3 1 Bangladesh .0230836 |
2. | 3 2 Pakistan .0975686 |
3. | 3 3 Indonesia .1641388 |
4. | 3 4 Sri Lanka .2300839 |
5. | 3 5 Thailand .3002517 |
|----------------------------------------|
6. | 3 6 Colombia .5963939 |
7. | 3 7 Panama .5834956 |
8. | 3 8 Yugoslavia .3189657 |
9. | 3 9 Portugal -.383313 |
10. | 3 10 Uruguay .5709916 |
|----------------------------------------|
11. | 3 11 Greece .6072472 |
12. | 3 12 Ireland 1.480371 |
13. | 3 13 Spain .2365766 |
14. | 3 14 Israel 2.517195 |
15. | 3 15 Hong Kong -229.073 |
|----------------------------------------|
16. | 3 16 New Zealand 7.520329 |
17. | 3 17 Austria 1.159659 |
18. | 3 18 Singapore -25.49224 |
19. | 3 19 Italy .6419317 |
20. | 3 20 UK 1.47654 |
|----------------------------------------|
21. | 3 21 Japan 4.292305 |
22. | 3 22 Belgium .4575562 |
23. | 3 23 Trinidad -.904206 |
24. | 3 24 Netherlands 9.091455 |
25. | 3 25 Finland .67896 |
|----------------------------------------|
26. | 3 26 Denmark 1.759899 |
27. | 3 27 West Germany 1.054174 |
28. | 3 28 France 1.558952 |
29. | 3 29 Sweden .907351 |
30. | 3 30 Norway 1.812922 |
|----------------------------------------|
31. | 3 31 Switzerland 3.74253 |
32. | 3 32 Canada .4738446 |
33. | 3 33 USA 1 |
+----------------------------------------+
----------------------------------------------------------------------------------
-> en = 4
+----------------------------------------+
| en indexc country p_1 |
|----------------------------------------|
1. | 4 1 Bangladesh 1.256177 |
2. | 4 2 Pakistan .4731916 |
3. | 4 3 Indonesia .4576648 |
4. | 4 4 Sri Lanka .7949094 |
5. | 4 5 Thailand 16.93923 |
|----------------------------------------|
6. | 4 6 Colombia .1271541 |
7. | 4 7 Panama .2973391 |
8. | 4 8 Yugoslavia .6397428 |
9. | 4 9 Portugal 1.639775 |
10. | 4 10 Uruguay .0984165 |
|----------------------------------------|
11. | 4 11 Greece .5059133 |
12. | 4 12 Ireland .6129137 |
13. | 4 13 Spain 1.017669 |
14. | 4 14 Israel 2.15672 |
15. | 4 15 Hong Kong -879.5381 |
|----------------------------------------|
16. | 4 16 New Zealand .3673215 |
17. | 4 17 Austria 1.975304 |
18. | 4 18 Singapore 795.1004 |
19. | 4 19 Italy 3.145272 |
20. | 4 20 UK 2.242249 |
|----------------------------------------|
21. | 4 21 Japan 100.5984 |
22. | 4 22 Belgium 6.886858 |
23. | 4 23 Trinidad 4.62197 |
24. | 4 24 Netherlands 13.09784 |
25. | 4 25 Finland 22.29664 |
|----------------------------------------|
26. | 4 26 Denmark 28.72834 |
27. | 4 27 West Germany 6.864776 |
28. | 4 28 France 3.166397 |
29. | 4 29 Sweden 7.64686 |
30. | 4 30 Norway 34.59857 |
|----------------------------------------|
31. | 4 31 Switzerland 3.335021 |
32. | 4 32 Canada .9541135 |
33. | 4 33 USA 1 |
+----------------------------------------+
. log close
. name: <unnamed>
log: Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
pi.log
> er-2\log type: text
on: 19 Jun 2024, 15:32:49
closed
----------------------------------------------------------------------------------
. exit
.
end of do-file
My code
<- readRDS(fout) %>%
trefler # Number of country in the dataset
mutate(
c = max(indexc),
f = max(indexf)
%>%
) # Calculate the world level of Yw, Bw and Vw
mutate(
yww = sum(y),
yw = yww / f,
bww = sum(b),
bw = bww / f
%>%
) group_by(indexf) %>%
mutate(
vfw = sum(v),
vw = sum(delta * v)
%>%
) ungroup() %>%
# Calculate country share Sc
mutate(
sc = (y - b) / (yw - bw)
%>%
) # Calculate epsilon(fc) and sigma^2(f) according to eq.2 in Trefler (1995)
mutate(
efc = delta * at - (delta * v - sc * vw)
%>%
) # Construct the average epsilon for a given factor
group_by(indexf) %>%
mutate(ave = sum(efc) / c) %>%
# Construct sigma^2 and the weight
mutate(
sigma2f = sum((efc - ave)^2) / (c - 1),
sigmaf = sqrt(sigma2f),
weight = sigmaf * sqrt(sc)
%>%
) ungroup() %>%
# Using the weight, convert all the data
mutate(
trat = delta * at / weight,
athat2 = (delta * v - (sc * vw)) / weight
)
# No need to save changes and restore
<- trefler %>%
trefler2 # Construct Aggregate Labor Endowment
filter(indexf %in% 7:9) %>%
mutate(
en = case_when(
== 7 ~ 2,
indexf == 8 ~ 3,
indexf == 9 ~ 4
indexf
)%>%
) select(country, factor, indexf, at, v, en, indexc, sc)
<- trefler %>%
trefler3 filter(!(indexf %in% 7:9)) %>%
group_by(country) %>%
# No need to create v_l and at_l to then rename and then collapse
summarise(
v = sum(v),
at = sum(at),
sc = mean(sc),
indexc = mean(indexc)
%>%
) mutate(
en = 1,
factor = "Labor"
)
<- trefler2 %>%
trefler3 select(-indexf) %>%
bind_rows(trefler3) %>%
arrange(indexc, en)
# Compute Pi: factor productivity
# No need to save intermediate outputs, we proceed with iteration
<- map_df(
trefler4 %>%
trefler3 distinct(en) %>%
pull(),
function(x) {
<- trefler3 %>%
d filter(en == x) %>%
mutate(p0 = 1, p1 = 1)
<- 50
iter
for (i in seq_len(iter)) {
<- d %>%
d mutate(
p0 = p1,
vp = p0 * v,
vpw = sum(vp),
p1 = ifelse(country == "USA", 1, (at + sc * vpw) / v)
%>%
) select(-vp, -vpw)
}
# Tidy alternative to the for loop
# d <- accumulate(seq_len(iter), .init = d, ~ .x %>%
# mutate(
# p0 = p1,
# p1 = ifelse(country == "USA", 1, (at + sc * sum(p1 * v)) / v)
# )) %>%
# .[[iter + 1]]
%>%
d select(country, indexc, en, p1)
} )
Extra step: formatting the tables
%>%
trefler4 arrange(country, en) %>%
kable()
country | indexc | en | p1 |
---|---|---|---|
Austria | 17 | 1 | 0.5694186 |
Austria | 17 | 2 | 0.5421980 |
Austria | 17 | 3 | 1.1596592 |
Austria | 17 | 4 | 1.9753040 |
Bangladesh | 1 | 1 | 0.0165219 |
Bangladesh | 1 | 2 | 0.9119021 |
Bangladesh | 1 | 3 | 0.0230836 |
Bangladesh | 1 | 4 | 1.2561775 |
Belgium | 22 | 1 | 0.7233988 |
Belgium | 22 | 2 | 0.6457500 |
Belgium | 22 | 3 | 0.4575561 |
Belgium | 22 | 4 | 6.8868596 |
Canada | 32 | 1 | 0.7799865 |
Canada | 32 | 2 | 0.7359194 |
Canada | 32 | 3 | 0.4738445 |
Canada | 32 | 4 | 0.9541137 |
Colombia | 6 | 1 | 0.1940833 |
Colombia | 6 | 2 | 0.4856291 |
Colombia | 6 | 3 | 0.5963939 |
Colombia | 6 | 4 | 0.1271541 |
Denmark | 26 | 1 | 0.7061784 |
Denmark | 26 | 2 | 0.7035640 |
Denmark | 26 | 3 | 1.7598990 |
Denmark | 26 | 4 | 28.7283398 |
Finland | 25 | 1 | 0.6969619 |
Finland | 25 | 2 | 0.6453199 |
Finland | 25 | 3 | 0.6789600 |
Finland | 25 | 4 | 22.2966445 |
France | 28 | 1 | 0.7356650 |
France | 28 | 2 | 0.6150762 |
France | 28 | 3 | 1.5589519 |
France | 28 | 4 | 3.1663974 |
Greece | 11 | 1 | 0.3140718 |
Greece | 11 | 2 | 0.4401574 |
Greece | 11 | 3 | 0.6072472 |
Greece | 11 | 4 | 0.5059134 |
Hong Kong | 15 | 1 | 0.3781684 |
Hong Kong | 15 | 2 | 0.5150965 |
Hong Kong | 15 | 3 | -229.0729738 |
Hong Kong | 15 | 4 | -879.5376414 |
Indonesia | 3 | 1 | 0.0432824 |
Indonesia | 3 | 2 | 0.2506530 |
Indonesia | 3 | 3 | 0.1641388 |
Indonesia | 3 | 4 | 0.4576649 |
Ireland | 12 | 1 | 0.4166084 |
Ireland | 12 | 2 | 0.4783923 |
Ireland | 12 | 3 | 1.4803708 |
Ireland | 12 | 4 | 0.6129137 |
Israel | 14 | 1 | 0.6131932 |
Israel | 14 | 2 | 0.5049116 |
Israel | 14 | 3 | 2.5171947 |
Israel | 14 | 4 | 2.1567210 |
Italy | 19 | 1 | 0.5907437 |
Italy | 19 | 2 | 0.5122213 |
Italy | 19 | 3 | 0.6419316 |
Italy | 19 | 4 | 3.1452732 |
Japan | 21 | 1 | 0.6049210 |
Japan | 21 | 2 | 0.6502130 |
Japan | 21 | 3 | 4.2923043 |
Japan | 21 | 4 | 100.5984017 |
Netherlands | 24 | 1 | 0.7933355 |
Netherlands | 24 | 2 | 0.7513015 |
Netherlands | 24 | 3 | 9.0914542 |
Netherlands | 24 | 4 | 13.0978390 |
New Zealand | 16 | 1 | 0.5850298 |
New Zealand | 16 | 2 | 0.6197254 |
New Zealand | 16 | 3 | 7.5203295 |
New Zealand | 16 | 4 | 0.3673216 |
Norway | 30 | 1 | 0.7816517 |
Norway | 30 | 2 | 0.6199874 |
Norway | 30 | 3 | 1.8129219 |
Norway | 30 | 4 | 34.5985787 |
Pakistan | 2 | 1 | 0.0394124 |
Pakistan | 2 | 2 | 0.4867497 |
Pakistan | 2 | 3 | 0.0975686 |
Pakistan | 2 | 4 | 0.4731917 |
Panama | 7 | 1 | 0.2279774 |
Panama | 7 | 2 | 0.3224079 |
Panama | 7 | 3 | 0.5834955 |
Panama | 7 | 4 | 0.2973391 |
Portugal | 9 | 1 | 0.1759864 |
Portugal | 9 | 2 | 0.2971070 |
Portugal | 9 | 3 | -0.3833130 |
Portugal | 9 | 4 | 1.6397757 |
Singapore | 18 | 1 | 0.4873306 |
Singapore | 18 | 2 | 0.3209652 |
Singapore | 18 | 3 | -25.4922493 |
Singapore | 18 | 4 | 795.1006524 |
Spain | 13 | 1 | 0.4172726 |
Spain | 13 | 2 | 0.5194890 |
Spain | 13 | 3 | 0.2365766 |
Spain | 13 | 4 | 1.0176692 |
Sri Lanka | 4 | 1 | 0.0344433 |
Sri Lanka | 4 | 2 | 0.1259789 |
Sri Lanka | 4 | 3 | 0.2300838 |
Sri Lanka | 4 | 4 | 0.7949096 |
Sweden | 29 | 1 | 0.7270576 |
Sweden | 29 | 2 | 0.9445322 |
Sweden | 29 | 3 | 0.9073509 |
Sweden | 29 | 4 | 7.6468612 |
Switzerland | 31 | 1 | 0.9317447 |
Switzerland | 31 | 2 | 0.6079722 |
Switzerland | 31 | 3 | 3.7425295 |
Switzerland | 31 | 4 | 3.3350218 |
Thailand | 5 | 1 | 0.0489020 |
Thailand | 5 | 2 | 0.3929541 |
Thailand | 5 | 3 | 0.3002516 |
Thailand | 5 | 4 | 16.9392348 |
Trinidad | 23 | 1 | 0.4483440 |
Trinidad | 23 | 2 | 0.4193396 |
Trinidad | 23 | 3 | -0.9042060 |
Trinidad | 23 | 4 | 4.6219780 |
UK | 20 | 1 | 0.6332355 |
UK | 20 | 2 | 0.8159872 |
UK | 20 | 3 | 1.4765402 |
UK | 20 | 4 | 2.2422499 |
USA | 33 | 1 | 1.0000000 |
USA | 33 | 2 | 1.0000000 |
USA | 33 | 3 | 1.0000000 |
USA | 33 | 4 | 1.0000000 |
Uruguay | 10 | 1 | 0.2029693 |
Uruguay | 10 | 2 | 0.4060566 |
Uruguay | 10 | 3 | 0.5709917 |
Uruguay | 10 | 4 | 0.0984165 |
West Germany | 27 | 1 | 0.7691833 |
West Germany | 27 | 2 | 0.6629313 |
West Germany | 27 | 3 | 1.0541740 |
West Germany | 27 | 4 | 6.8647780 |
Yugoslavia | 8 | 1 | 0.1997569 |
Yugoslavia | 8 | 2 | 0.2985477 |
Yugoslavia | 8 | 3 | 0.3189656 |
Yugoslavia | 8 | 4 | 0.6397429 |