Chapter 8. Import Quotas and Export Subsidies

Empirical exercise

This exercise is to reproduce regression results Feenstra (1988a). To complete the exercise, the files “car_7990.dta” and “truck_7990.dta” should be stored in Chapter-8.

Documentation

Data Description for Feenstra (1988a):

  1. car.dta: This STATA file contains car’s various characteristics, such as weight (wght), width (wdth), height (hght), horse power (hs), four-wheel (four), transmission (tran), power steering (ps) and air condition (ac) for 1979-1990. It also includes each model’s sales (quan) and price.
  2. truck.dta: This STATA file contains truck’s various characteristics, such as weight (wght), width (wdth), height (hght), horse power (hs), four-wheel (four), transmission (tran), power steering (ps) and air condition (ac) for 1979-1990. It also includes each model’s sale (quan) and price, though sales are missing after 1985.

Exercise 1

Run the programs pindex_c.do, pindex_t.do, unit_value.do to reproduce the price indexes and unit-values for cars and trucks in Table 8.3. What formula is used for the price indexes, and how does this differ from the unit-values?

Feenstra’s code

pindex_c.do

clear
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\pindex_c.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_7990,clear
keep year model type price quan

preserve
keep if year==80
keep model type price quan
ren price price_80
ren quan quan_80
sort model
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_80,replace
restore

sort model
merge model using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_80
keep if _merge==3
drop _merge

sort year model
egen value_80=sum(price_80*quan_80), by(year)
egen value_cp=sum(price*quan_80),by(year)
gen lasp=value_cp/value_80

egen value_c=sum(price*quan), by(year)
egen value_cq=sum(price_80*quan), by(year)
gen pasp=value_c/value_cq
 
gen pindex=(lasp*pasp)^0.5

collapse (mean) pindex, by(year)
list

log close

Output:

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\pindex_c.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\pindex_c.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\pindex_c.log
  log type:  text
 opened on:  19 Jun 2024, 18:40:59

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\ca
> r_7990,clear

. keep year model type price quan

. 
. preserve

. keep if year==80
(260 observations deleted)

. keep model type price quan

. ren price price_80

. ren quan quan_80

. sort model

. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\c
> ar_80,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car
> _80.dta saved

. restore

. 
. sort model

. merge model using Z:\home\pacha\github\advanced-international-trade\first-editio
> n\Chapter-8\car_80
(note: you are using old merge syntax; see [R] merge for new syntax)
variable model does not uniquely identify observations in the master data

. keep if _merge==3
(55 observations deleted)

. drop _merge

. 
. sort year model

. egen value_80=sum(price_80*quan_80), by(year)

. egen value_cp=sum(price*quan_80),by(year)

. gen lasp=value_cp/value_80

. 
. egen value_c=sum(price*quan), by(year)

. egen value_cq=sum(price_80*quan), by(year)

. gen pasp=value_c/value_cq

.  
. gen pindex=(lasp*pasp)^0.5

. 
. collapse (mean) pindex, by(year)

. list

     +-----------------+
     | year     pindex |
     |-----------------|
  1. |   79      .9663 |
  2. |   80          1 |
  3. |   81   1.197522 |
  4. |   82   1.289163 |
  5. |   83   1.312988 |
     |-----------------|
  6. |   84   1.389093 |
  7. |   85   1.477987 |
  8. |   86   1.668846 |
  9. |   87   1.885685 |
 10. |   88    2.04798 |
     |-----------------|
 11. |   89   2.082082 |
 12. |   90    2.10851 |
     +-----------------+

. 
. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\pindex_c.log
  log type:  text
 closed on:  19 Jun 2024, 18:41:01
----------------------------------------------------------------------------------

. 
end of do-file

pindex_t.do

clear
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\pindex_t.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_7990,clear
keep year model type price quan

preserve
keep if year==80
keep model type price quan
ren price price_80
ren quan quan_80
sort model
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_80,replace
restore

sort model
merge model using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_80
keep if _merge==3
drop _merge

sort year model
egen value_80=sum(price_80*quan_80), by(year)
egen value_cp=sum(price*quan_80),by(year)
gen lasp=value_cp/value_80

egen value_c=sum(price*quan), by(year)
egen value_cq=sum(price_80*quan), by(year)
gen pasp=value_c/value_cq
 
gen pindex=(lasp*pasp)^0.5

collapse (mean) pindex, by(year)
list

log close

Output:

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\pindex_t.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\pindex_t.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\pindex_t.log
  log type:  text
 opened on:  19 Jun 2024, 18:59:29

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tr
> uck_7990,clear

. keep year model type price quan

. 
. preserve

. keep if year==80
(110 observations deleted)

. keep model type price quan

. ren price price_80

. ren quan quan_80

. sort model

. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\t
> ruck_80,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tru
> ck_80.dta saved

. restore

. 
. sort model

. merge model using Z:\home\pacha\github\advanced-international-trade\first-editio
> n\Chapter-8\truck_80
(note: you are using old merge syntax; see [R] merge for new syntax)
variable model does not uniquely identify observations in the master data

. keep if _merge==3
(38 observations deleted)

. drop _merge

. 
. sort year model

. egen value_80=sum(price_80*quan_80), by(year)

. egen value_cp=sum(price*quan_80),by(year)

. gen lasp=value_cp/value_80

. 
. egen value_c=sum(price*quan), by(year)

. egen value_cq=sum(price_80*quan), by(year)

. gen pasp=value_c/value_cq
(21 missing values generated)

.  
. gen pindex=(lasp*pasp)^0.5
(21 missing values generated)

. 
. collapse (mean) pindex, by(year)

. list

     +-----------------+
     | year     pindex |
     |-----------------|
  1. |   79    .970215 |
  2. |   80          1 |
  3. |   81   1.278085 |
  4. |   82   1.309353 |
  5. |   83   1.210055 |
     |-----------------|
  6. |   84   1.230847 |
  7. |   85   1.252268 |
  8. |   86          . |
  9. |   87          . |
 10. |   88          . |
     |-----------------|
 11. |   89          . |
 12. |   90          . |
     +-----------------+

. 
. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\pindex_t.log
  log type:  text
 closed on:  19 Jun 2024, 18:59:31
----------------------------------------------------------------------------------

. 
end of do-file

unit_value.do

clear
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\unit_value.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_7990,clear

gen value=price*quan
egen aquan=mean(quan), by(year)
egen avalue=mean(value), by(year)
gen uvalue=avalue/aquan
collapse (mean) uvalue, by(year)
list
clear

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_7990,clear
gen value=price*quan
egen aquan=mean(quan), by(year)
egen avalue=mean(value), by(year)
gen uvalue=avalue/aquan
collapse (mean) uvalue, by(year)
list
clear

log close
exit

Output:

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\unit_value.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\unit_value.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\unit_value.log
  log type:  text
 opened on:  19 Jun 2024, 19:13:08

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\ca
> r_7990,clear

. 
. gen value=price*quan

. egen aquan=mean(quan), by(year)

. egen avalue=mean(value), by(year)

. gen uvalue=avalue/aquan

. collapse (mean) uvalue, by(year)

. list

     +-----------------+
     | year     uvalue |
     |-----------------|
  1. |   79   4945.603 |
  2. |   80   5174.757 |
  3. |   81   6210.741 |
  4. |   82   6834.219 |
  5. |   83   7069.203 |
     |-----------------|
  6. |   84   7518.311 |
  7. |   85   8153.146 |
  8. |   86   9391.989 |
  9. |   87   10396.91 |
 10. |   88    10840.6 |
     |-----------------|
 11. |   89   10492.76 |
 12. |   90   11112.68 |
     +-----------------+

. clear

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tr
> uck_7990,clear

. gen value=price*quan

. egen aquan=mean(quan), by(year)

. egen avalue=mean(value), by(year)

. gen uvalue=avalue/aquan
(45 missing values generated)

. collapse (mean) uvalue, by(year)

. list

     +-----------------+
     | year     uvalue |
     |-----------------|
  1. |   79   4794.177 |
  2. |   80   4937.291 |
  3. |   81   6313.539 |
  4. |   82   6426.264 |
  5. |   83   6134.027 |
     |-----------------|
  6. |   84   6246.728 |
  7. |   85   6249.927 |
  8. |   86          . |
  9. |   87          . |
 10. |   88          . |
     |-----------------|
 11. |   89          . |
 12. |   90          . |
     +-----------------+

. clear

. 
. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\unit_value.log
  log type:  text
 closed on:  19 Jun 2024, 19:13:10
----------------------------------------------------------------------------------

. exit

end of do-file

My code

pindex_c.do

# Packages ----

library(archive)
library(haven)
library(dplyr)
library(tidyr)
library(broom)
library(knitr)

# Extract ----

fzip <- "first-edition/Chapter-8.zip"
dout <- gsub("\\.zip$", "", fzip)

if (!dir.exists(dout)) {
  archive_extract(fzip, dir = dout)
}

# Read and transform ----

fout <- paste0(dout, "/feenstra_88.rds")

if (!file.exists(fout)) {
  feenstra_88 <- list(
    car_7990 = read_dta(paste0(dout, "/car_7990.dta")),
    truck_7990 = read_dta(paste0(dout, "/truck_7990.dta"))
  )

  saveRDS(feenstra_88, fout)
} else {
  feenstra_88 <- readRDS(fout)
}

car_7990 <- feenstra_88$car_7990 %>%
  select(year, model, type, price, quan) %>%
  arrange(year, model)

car_80 <- car_7990 %>%
  filter(year == 80) %>%
  select(model, type, price, quan) %>%
  rename(price_80 = price, quan_80 = quan)

car_7990 <- car_7990 %>%
  inner_join(car_80) %>%
  group_by(year) %>%
  mutate(
    value_80 = sum(price_80 * quan_80),
    value_cp = sum(price * quan_80)
  ) %>%
  ungroup() %>%
  mutate(lasp = value_cp / value_80)

car_7990 <- car_7990 %>%
  group_by(year) %>%
  mutate(
    value_c = sum(price * quan),
    value_cq = sum(price_80 * quan)
  ) %>%
  ungroup() %>%
  mutate(pasp = value_c / value_cq)

car_7990 %>%
  mutate(pindex = sqrt(lasp * pasp)) %>%
  group_by(year) %>%
  summarise(pindex = mean(pindex))
# A tibble: 12 × 2
    year pindex
   <dbl>  <dbl>
 1    79  0.966
 2    80  1    
 3    81  1.20 
 4    82  1.29 
 5    83  1.31 
 6    84  1.39 
 7    85  1.48 
 8    86  1.67 
 9    87  1.89 
10    88  2.05 
11    89  2.08 
12    90  2.11 

pindex_t.do

truck_7990 <- feenstra_88$truck_7990 %>%
  select(year, model, type, price, quan) %>%
  arrange(year, model)

truck_80 <- truck_7990 %>%
  filter(year == 80) %>%
  select(model, type, price, quan) %>%
  rename(price_80 = price, quan_80 = quan)

truck_7990 <- truck_7990 %>%
  inner_join(truck_80) %>%
  group_by(year) %>%
  mutate(
    value_80 = sum(price_80 * quan_80),
    value_cp = sum(price * quan_80)
  ) %>%
  ungroup() %>%
  mutate(lasp = value_cp / value_80)

truck_7990 <- truck_7990 %>%
  group_by(year) %>%
  mutate(
    value_c = sum(price * quan),
    value_cq = sum(price_80 * quan)
  ) %>%
  ungroup() %>%
  mutate(pasp = value_c / value_cq)

truck_7990 %>%
  mutate(pindex = sqrt(lasp * pasp)) %>%
  group_by(year) %>%
  summarise(pindex = mean(pindex))
# A tibble: 12 × 2
    year  pindex
   <dbl>   <dbl>
 1    79   0.970
 2    80   1    
 3    81   1.28 
 4    82   1.31 
 5    83   1.21 
 6    84   1.23 
 7    85   1.25 
 8    86 NaN    
 9    87 NaN    
10    88 NaN    
11    89 NaN    
12    90 NaN    

unit_value.do

car_7990 <- feenstra_88$car_7990 %>%
  mutate(value = price * quan) %>%
  group_by(year) %>%
  mutate(
    aquan = mean(quan),
    avalue = mean(value),
    uvalue = avalue / aquan
  )

truck_7990 <- feenstra_88$truck_7990 %>%
  mutate(value = price * quan) %>%
  group_by(year) %>%
  mutate(
    aquan = mean(quan),
    avalue = mean(value),
    uvalue = avalue / aquan
  )

car_7990 %>%
  group_by(year) %>%
  summarise(uvalue = mean(uvalue))
# A tibble: 12 × 2
    year uvalue
   <dbl>  <dbl>
 1    79  4946.
 2    80  5175.
 3    81  6211.
 4    82  6834.
 5    83  7069.
 6    84  7518.
 7    85  8153.
 8    86  9392.
 9    87 10397.
10    88 10841.
11    89 10493.
12    90 11113.
truck_7990 %>%
  group_by(year) %>%
  summarise(uvalue = mean(uvalue))
# A tibble: 12 × 2
    year uvalue
   <dbl>  <dbl>
 1    79  4794.
 2    80  4937.
 3    81  6314.
 4    82  6426.
 5    83  6134.
 6    84  6247.
 7    85  6250.
 8    86   NaN 
 9    87   NaN 
10    88   NaN 
11    89   NaN 
12    90   NaN 

Exercise 2

Run the program car_reg.do to reproduce the column (1) in Table 8.4, and the program truck_reg.do to reproduce column (2). What weights are being used in the regression, and how does this affect the results?

Feenstra’s code

car_reg.do


clear
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_reg.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_7990,clear
drop if year>=86

tab year, gen(yd)

program define nlcar
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
exit
}
replace `1'=$a3*yd3+$a4*yd4+$a5*yd5+$a6*yd6+$a7*yd7/*
*/+exp($b2*yd2+$b3*yd3+$b4*yd4+$b5*yd5+$b6*yd6+$b7*yd7/*
*/+$c1*wght+$c2*wdth+$c3*hght+$c4*hp+$c5*tran+$c6*ps+$c7*ac+$c8)

end

nl car price

gen winv=1/exp(_b[b2]*yd2+_b[b3]*yd3+_b[b4]*yd4+_b[b5]*yd5+_b[b6]*yd6+_b[b7]*yd7/*
*/+_b[c1]*wght+_b[c2]*wdth+_b[c3]*hght+_b[c4]*hp+_b[c5]*tran+_b[c6]*ps+_b[c7]*ac+_b[c8])

gen nprice=price*winv
gen nyd3=yd3*winv
gen nyd4=yd4*winv
gen nyd5=yd5*winv
gen nyd6=yd6*winv
gen nyd7=yd7*winv

program define nlncar
    version 7.0
    if "`1'"=="?"{
        global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8"
        global a3=1
        global a4=1
        global a5=1 
        global a6=1
        global a7=1
        global b2=.1    
        global b3=.1
        global b4=.1
        global b5=.1
        global b6=.1    
        global b7=.1
        global c1=.1
        global c2=.1
        global c3=.1
        global c4=.1
        global c5=.1
        global c6=.1
        global c7=.1
        global c8=.1
        exit
    }
    replace `1'=$a3*nyd3+$a4*nyd4+$a5*nyd5+$a6*nyd6+$a7*nyd7/*
*/+winv*exp($b2*yd2+$b3*yd3+$b4*yd4+$b5*yd5+$b6*yd6+$b7*yd7/*
*/+$c1*wght+$c2*wdth+$c3*hght+$c4*hp+$c5*tran+$c6*ps+$c7*ac+$c8)

end

nl ncar nprice
clear
program drop _all
log close
exit

Output:

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\car_reg.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\car_reg.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\car_reg.log
  log type:  text
 opened on:  19 Jun 2024, 19:28:58

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\ca
> r_7990,clear

. drop if year>=86
(105 observations deleted)

. 
. tab year, gen(yd)

       year |      Freq.     Percent        Cum.
------------+-----------------------------------
         79 |         21       11.73       11.73
         80 |         24       13.41       25.14
         81 |         24       13.41       38.55
         82 |         24       13.41       51.96
         83 |         26       14.53       66.48
         84 |         29       16.20       82.68
         85 |         31       17.32      100.00
------------+-----------------------------------
      Total |        179      100.00

. 
. program define nlcar
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global b2=.1
 10. global b3=.1
 11. global b4=.1
 12. global b5=.1
 13. global b6=.1
 14. global b7=.1
 15. global c1=.1
 16. global c2=.1
 17. global c3=.1
 18. global c4=.1
 19. global c5=.1
 20. global c6=.1
 21. global c7=.1
 22. global c8=.1
 23. exit
 24. }
 25. replace `1'=$a3*yd3+$a4*yd4+$a5*yd5+$a6*yd6+$a7*yd7/*
> */+exp($b2*yd2+$b3*yd3+$b4*yd4+$b5*yd5+$b6*yd6+$b7*yd7/*
> */+$c1*wght+$c2*wdth+$c3*hght+$c4*hp+$c5*tran+$c6*ps+$c7*ac+$c8)
 26. 
. end

. 
. nl car price
(obs = 179)

Iteration 0:   residual SS =  1.16e+10
...
Iteration 9:   residual SS =  8.39e+07
Iteration 10:  residual SS =  8.39e+07

      Source |       SS       df       MS            Number of obs =       179
-------------+------------------------------         F( 19,   160) =   1158.52
       Model |  1.1541e+10    19   607414473         Prob > F      =    0.0000
    Residual |    83888568   160   524303.55         R-squared     =    0.9928
-------------+------------------------------         Adj R-squared =    0.9919
       Total |  1.1625e+10   179  64942813.2         Root MSE      =  724.0881
                                                     Res. dev.     =  2845.293
(car)
------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |  -294.7452   723.3322    -0.41   0.684    -1723.255    1133.765
          a4 |  -304.9631   714.5517    -0.43   0.670    -1716.132    1106.206
          a5 |  -137.3977   700.8446    -0.20   0.845    -1521.497    1246.701
          a6 |   72.87527   695.4052     0.10   0.917    -1300.482    1446.232
          a7 |  -103.4007   742.9475    -0.14   0.889    -1570.649    1363.848
          b2 |   .0099678   .0362994     0.27   0.784      -.06172    .0816555
          b3 |   .1522578   .1022847     1.49   0.139    -.0497444      .35426
          b4 |   .1899382   .0965319     1.97   0.051    -.0007027    .3805792
          b5 |   .1540892   .0959124     1.61   0.110    -.0353283    .3435068
          b6 |    .172972   .0944704     1.83   0.069    -.0135977    .3595417
          b7 |   .2274326   .0945034     2.41   0.017     .0407977    .4140675
          c1 |  -.0007315   .1021984    -0.01   0.994    -.2025632    .2011002
          c2 |   .3681958   .1019742     3.61   0.000     .1668067    .5695849
          c3 |  -.1431584   .0519385    -2.76   0.007    -.2457317    -.040585
          c4 |   .6070451   .0665196     9.13   0.000     .4756756    .7384147
          c5 |   .1315757   .0264479     4.97   0.000     .0793437    .1838078
          c6 |   .0515819   .0218212     2.36   0.019     .0084872    .0946766
          c7 |   .1635465   .0228493     7.16   0.000     .1184213    .2086717
          c8 |    6.73024   .5719055    11.77   0.000     5.600783    7.859697
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. gen winv=1/exp(_b[b2]*yd2+_b[b3]*yd3+_b[b4]*yd4+_b[b5]*yd5+_b[b6]*yd6+_b[b7]*yd7
> /*
> */+_b[c1]*wght+_b[c2]*wdth+_b[c3]*hght+_b[c4]*hp+_b[c5]*tran+_b[c6]*ps+_b[c7]*ac
> +_b[c8])

. 
. gen nprice=price*winv

. gen nyd3=yd3*winv

. gen nyd4=yd4*winv

. gen nyd5=yd5*winv

. gen nyd6=yd6*winv

. gen nyd7=yd7*winv

. 
. program define nlncar
  1.         version 7.0
  2.         if "`1'"=="?"{
  3.                 global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c
> 6 c7 c8"
  4.                 global a3=1
  5.                 global a4=1
  6.                 global a5=1     
  7.                 global a6=1
  8.                 global a7=1
  9.                 global b2=.1    
 10.                 global b3=.1
 11.                 global b4=.1
 12.                 global b5=.1
 13.                 global b6=.1    
 14.                 global b7=.1
 15.                 global c1=.1
 16.                 global c2=.1
 17.                 global c3=.1
 18.                 global c4=.1
 19.                 global c5=.1
 20.                 global c6=.1
 21.                 global c7=.1
 22.                 global c8=.1
 23.                 exit
 24.         }
 25.         replace `1'=$a3*nyd3+$a4*nyd4+$a5*nyd5+$a6*nyd6+$a7*nyd7/*
> */+winv*exp($b2*yd2+$b3*yd3+$b4*yd4+$b5*yd5+$b6*yd6+$b7*yd7/*
> */+$c1*wght+$c2*wdth+$c3*hght+$c4*hp+$c5*tran+$c6*ps+$c7*ac+$c8)
 26. 
. end

. 
. nl ncar nprice
(obs = 179)

Iteration 0:   residual SS =  174.4452
...
Iteration 13:  residual SS =  1.672068
Iteration 14:  residual SS =  1.672068

      Source |       SS       df       MS            Number of obs =       179
-------------+------------------------------         F( 19,   160) =    871.41
       Model |  173.025768    19  9.10661939         Prob > F      =    0.0000
    Residual |  1.67206775   160  .010450423         R-squared     =    0.9904
-------------+------------------------------         Adj R-squared =    0.9893
       Total |  174.697836   179  .975965565         Root MSE      =  .1022273
                                                     Res. dev.     = -328.5451
(ncar)
------------------------------------------------------------------------------
      nprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   410.4256   619.1744     0.66   0.508    -812.3829    1633.234
          a4 |    378.927   650.2562     0.58   0.561     -905.265    1663.119
          a5 |   770.6115   601.2054     1.28   0.202    -416.7099    1957.933
          a6 |   624.3817   618.9925     1.01   0.315    -598.0676    1846.831
          a7 |  -122.9148   719.4492    -0.17   0.865    -1543.756    1297.927
          b2 |   .0089535   .0309918     0.29   0.773    -.0522522    .0701592
          b3 |   .0474345   .1093808     0.43   0.665    -.1685818    .2634509
          b4 |   .0938457    .108274     0.87   0.387    -.1199848    .3076761
          b5 |   .0195742   .1052628     0.19   0.853    -.1883094    .2274579
          b6 |   .0918745   .1019572     0.90   0.369     -.109481      .29323
          b7 |   .2189293   .1012639     2.16   0.032      .018943    .4189156
          c1 |   .0254744   .1207686     0.21   0.833    -.2130317    .2639805
          c2 |   .3565755   .1083292     3.29   0.001      .142636     .570515
          c3 |  -.0630705   .0554954    -1.14   0.257    -.1726685    .0465275
          c4 |   .6899452   .0852231     8.10   0.000      .521638    .8582524
          c5 |   .1428588   .0244762     5.84   0.000     .0945207    .1911968
          c6 |   .0581011   .0272835     2.13   0.035     .0042188    .1119834
          c7 |   .1473354    .033027     4.46   0.000     .0821103    .2125606
          c8 |   6.328371   .5830815    10.85   0.000     5.176842    7.479899
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. clear

. program drop _all

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\car_reg.log
  log type:  text
 closed on:  19 Jun 2024, 19:29:06
----------------------------------------------------------------------------------

. exit

end of do-file

truck_reg.do

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_reg.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_reg.log not found)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_reg.log
  log type:  text
 opened on:  18 Oct 2024, 17:38:02

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_7990,clear

. 
. drop if year>=86
(45 observations deleted)

. gen lnprice=log(price)

. 
. tab year, gen(yd)

       year |      Freq.     Percent        Cum.
------------+-----------------------------------
         79 |         10       13.33       13.33
         80 |         10       13.33       26.67
         81 |         11       14.67       41.33
         82 |         11       14.67       56.00
         83 |         10       13.33       69.33
         84 |         11       14.67       84.00
         85 |         12       16.00      100.00
------------+-----------------------------------
      Total |         75      100.00

. 
. program define nltruck_1
  1. version 7.0
  2.         if "`1'"=="?"{
  3.                 global S_1"a2 a3 a4 a5 a6 a7 b1 b2 b3 b4 b5 b6 b7 c1"
  4.                 global a2=1
  5.                 global a3=1
  6.                 global a4=1
  7.                 global a5=1
  8.                 global a6=1
  9.                 global a7=1
 10.                 global b1=1
 11.                 global b2=1
 12.                 global b3=1
 13.                 global b4=1
 14.                 global b5=1
 15.                 global b6=1
 16.                 global b7=1
 17.                 global c1=1
 18.                 exit
 19.         }
 20.         replace `1'=exp($a2*yd2+$a3*yd3+$a4*yd4+$a5*yd5+$a6*yd6+$a7*yd7/*
> */+$b1*weight+$b2*wdth+$b3*hght+$b4*hp+$b5*tran+$b6*ps+$b7*four+$c1*1)
 21. end

. 
. nl truck_1 price
(obs = 75)

Iteration 0:   residual SS =  2.50e+17
Iteration 1:   residual SS =  3.38e+16
Iteration 2:   residual SS =  4.57e+15
Iteration 3:   residual SS =  6.18e+14
Iteration 4:   residual SS =  8.35e+13
Iteration 5:   residual SS =  1.12e+13
Iteration 6:   residual SS =  1.50e+12
Iteration 7:   residual SS =  1.96e+11
Iteration 8:   residual SS =  2.44e+10
Iteration 9:   residual SS =  2.65e+09
Iteration 10:  residual SS =  2.09e+08
Iteration 11:  residual SS =  2.03e+07
Iteration 12:  residual SS =  1.53e+07
Iteration 13:  residual SS =  1.53e+07
Iteration 14:  residual SS =  1.53e+07
Iteration 15:  residual SS =  1.53e+07
Iteration 16:  residual SS =  1.53e+07

      Source |       SS       df       MS            Number of obs =        75
-------------+------------------------------         F( 14,    61) =   1004.27
       Model |  3.5299e+09    14   252133551         Prob > F      =    0.0000
    Residual |  15314779.4    61  251061.957         R-squared     =    0.9957
-------------+------------------------------         Adj R-squared =    0.9947
       Total |  3.5452e+09    75  47269126.6         Root MSE      =  501.0608
                                                     Res. dev.     =  1129.854
(truck_1)
------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a2 |  -.0210733   .0423824    -0.50   0.621    -.1058222    .0636756
          a3 |   .1734177   .0382242     4.54   0.000     .0969837    .2498518
          a4 |   .2001205   .0385602     5.19   0.000     .1230145    .2772264
          a5 |   .1454809   .0400948     3.63   0.001     .0653064    .2256555
          a6 |   .1825347   .0394306     4.63   0.000     .1036883    .2613811
          a7 |   .1592055    .042285     3.77   0.000     .0746514    .2437595
          b1 |   .4398295   .0564709     7.79   0.000     .3269091      .55275
          b2 |     .08571   .1119267     0.77   0.447    -.1381014    .3095213
          b3 |  -.0487976   .0470736    -1.04   0.304     -.142927    .0453318
          b4 |   .2482011   .1089575     2.28   0.026     .0303272     .466075
          b5 |   .0341161   .0249591     1.37   0.177    -.0157928    .0840249
          b6 |   .0942558   .0337561     2.79   0.007     .0267564    .1617552
          b7 |   .2292275   .0271818     8.43   0.000     .1748741    .2835808
          c1 |   7.536871   .5461675    13.80   0.000     6.444741       8.629
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. gen winv=1/exp(_b[a2]*yd2+_b[a3]*yd3+_b[a4]*yd4+_b[a5]*yd5+_b[a6]*yd6+_b[a7]*yd7/*
> */+_b[b1]*weight+_b[b2]*wdth+_b[b3]*hght+_b[b4]*hp+_b[b5]*tran+_b[b6]*ps+_b[b7]*four+_b[c1]*1)

. 
. gen nprice=price*winv

. 
. program define nltruck_2
  1.         version 7.0
  2.         if "`1'"=="?"{
  3.                 global S_1"a2 a3 a4 a5 a6 a7 b1 b2 b3 b4 b5 b6 b7 c1"
  4.                 global a2=1
  5.                 global a3=1
  6.                 global a4=1
  7.                 global a5=1
  8.                 global a6=1
  9.                 global a7=1
 10.                 global b1=1
 11.                 global b2=1
 12.                 global b3=1
 13.                 global b4=1
 14.                 global b5=1
 15.                 global b6=1
 16.                 global b7=1
 17.                 global c1=1
 18.                 exit
 19.         }
 20.         replace `1'=winv*exp($a2*yd2+$a3*yd3+$a4*yd4+$a5*yd5+$a6*yd6+$a7*yd7/*
> */+$b1*weight+$b2*wdth+$b3*hght+$b4*hp+$b5*tran+$b6*ps+$b7*four+$c1*1)
 21. end

. 
. nl truck_2 nprice
(obs = 75)

Iteration 0:   residual SS =  2.27e+09
Iteration 1:   residual SS =  3.07e+08
Iteration 2:   residual SS =  4.15e+07
Iteration 3:   residual SS =   5608900
Iteration 4:   residual SS =  757532.6
Iteration 5:   residual SS =  101968.1
Iteration 6:   residual SS =  13611.98
Iteration 7:   residual SS =   1784.24
Iteration 8:   residual SS =  224.5781
Iteration 9:   residual SS =  24.89367
Iteration 10:  residual SS =  2.123931
Iteration 11:  residual SS =  .3646277
Iteration 12:  residual SS =  .3191061
Iteration 13:  residual SS =  .3190177
Iteration 14:  residual SS =  .3190176
Iteration 15:  residual SS =  .3190176
Iteration 16:  residual SS =  .3190176

      Source |       SS       df       MS            Number of obs =        75
-------------+------------------------------         F( 14,    61) =   1023.93
       Model |  74.9691322    14  5.35493802         Prob > F      =    0.0000
    Residual |  .319017551    61  .005229796         R-squared     =    0.9958
-------------+------------------------------         Adj R-squared =    0.9948
       Total |  75.2881498    75    1.003842         Root MSE      =  .0723173
                                                     Res. dev.     =  -196.659
(truck_2)
------------------------------------------------------------------------------
      nprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a2 |   .0041382   .0326606     0.13   0.900    -.0611707    .0694472
          a3 |   .2093262   .0318227     6.58   0.000     .1456929    .2729596
          a4 |   .2295212   .0329551     6.96   0.000     .1636235     .295419
          a5 |   .1715058   .0348772     4.92   0.000     .1017645    .2412471
          a6 |   .1898806   .0347216     5.47   0.000     .1204505    .2593108
          a7 |   .1803588   .0370176     4.87   0.000     .1063376      .25438
          b1 |   .4115021   .0823391     5.00   0.000      .246855    .5761493
          b2 |   .0141524   .1005163     0.14   0.888    -.1868423    .2151472
          b3 |   .0054683   .0515606     0.11   0.916    -.0976334      .10857
          b4 |   .1979945   .1020843     1.94   0.057    -.0061357    .4021247
          b5 |   .0294082     .02481     1.19   0.240    -.0202025    .0790188
          b6 |   .0901756   .0435822     2.07   0.043     .0030275    .1773236
          b7 |   .2159646   .0267321     8.08   0.000     .1625105    .2694187
          c1 |   7.705805   .4974887    15.49   0.000     6.711015    8.700595
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. clear

. 
. program drop _all

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_reg.log
  log type:  text
 closed on:  18 Oct 2024, 17:38:02
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

. exit

end of do-file

My code

car_reg.do

library(minpack.lm) # for nlsLM()

car_7990 <- feenstra_88$car_7990 %>%
  filter(year < 86) %>%
  mutate(yd = factor(year)) %>%
  group_by(yd) %>%
  mutate(id = row_number()) %>%
  ungroup()

car_7990 %>%
  group_by(yd) %>%
  summarise(freq = n()) %>%
  ungroup() %>%
  mutate(
    percent = freq / sum(freq) * 100,
    cum = cumsum(percent)
  )
# A tibble: 7 × 4
  yd     freq percent   cum
  <fct> <int>   <dbl> <dbl>
1 79       21    11.7  11.7
2 80       24    13.4  25.1
3 81       24    13.4  38.5
4 82       24    13.4  52.0
5 83       26    14.5  66.5
6 84       29    16.2  82.7
7 85       31    17.3 100  
# expand the yd variable to 0/1 columns
# in R, you cannot multiply a factor/categorical variable

car_7990 <- car_7990 %>%
  mutate(yd_ind = as.integer(yd)) %>%
  mutate(yd_ind = as.factor(yd_ind - min(yd_ind) + 1))

car_7990_dummy <- model.matrix(~ yd_ind - 1, data = car_7990)
colnames(car_7990_dummy) <- gsub("_ind", "", colnames(car_7990_dummy))

car_7990 <- car_7990 %>%
  bind_cols(as.data.frame(car_7990_dummy))

# fit the non-linear specification with initial values
# nls() fails, so I use nlsLM() that implements the Levenberg-Marquardt
# algorithm

initial_values <- list(
  a3 = 1, a4 = 1, a5 = 1, a6 = 1, a7 = 1,
  b2 = 0.1, b3 = 0.1, b4 = 0.1, b5 = 0.1, b6 = 0.1, b7 = 0.1,
  c1 = 0.1, c2 = 0.1, c3 = 0.1, c4 = 0.1, c5 = 0.1, c6 = 0.1, c7 = 0.1,
  c8 = 0.1
)

fit <- nlsLM(
  price ~ a3 * yd3 + a4 * yd4 + a5 * yd5 + a6 * yd6 + a7 * yd7 +
    exp(
      b2 * yd2 + b3 * yd3 + b4 * yd4 + b5 * yd5 + b6 * yd6 + b7 * yd7 +
        c1 * wght + c2 * wdth + c3 * hght + c4 * hp + c5 * tran + c6 * ps +
        c7 * ac + c8
    ),
  data = car_7990,
  start = initial_values
)

summary(fit)

Formula: price ~ a3 * yd3 + a4 * yd4 + a5 * yd5 + a6 * yd6 + a7 * yd7 + 
    exp(b2 * yd2 + b3 * yd3 + b4 * yd4 + b5 * yd5 + b6 * yd6 + 
        b7 * yd7 + c1 * wght + c2 * wdth + c3 * hght + c4 * hp + 
        c5 * tran + c6 * ps + c7 * ac + c8)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
a3 -2.947e+02  7.233e+02  -0.407 0.684236    
a4 -3.049e+02  7.145e+02  -0.427 0.670137    
a5 -1.374e+02  7.008e+02  -0.196 0.844867    
a6  7.293e+01  6.954e+02   0.105 0.916608    
a7 -1.033e+02  7.429e+02  -0.139 0.889554    
b2  9.967e-03  3.630e-02   0.275 0.783991    
b3  1.523e-01  1.023e-01   1.489 0.138586    
b4  1.899e-01  9.653e-02   1.968 0.050842 .  
b5  1.541e-01  9.591e-02   1.607 0.110130    
b6  1.730e-01  9.447e-02   1.831 0.068972 .  
b7  2.274e-01  9.450e-02   2.407 0.017243 *  
c1 -6.998e-04  1.022e-01  -0.007 0.994545    
c2  3.682e-01  1.020e-01   3.610 0.000409 ***
c3 -1.432e-01  5.194e-02  -2.756 0.006522 ** 
c4  6.070e-01  6.652e-02   9.126 2.93e-16 ***
c5  1.316e-01  2.645e-02   4.975 1.67e-06 ***
c6  5.158e-02  2.182e-02   2.364 0.019283 *  
c7  1.635e-01  2.285e-02   7.157 2.81e-11 ***
c8  6.730e+00  5.719e-01  11.768  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 724.1 on 160 degrees of freedom

Number of iterations to convergence: 19 
Achieved convergence tolerance: 1.49e-08
fit_coef <- coef(fit)

car_7990 <- car_7990 %>%
  mutate(
    winv = 1 / exp(fit_coef["b2"] * yd2 + fit_coef["b3"] * yd3 +
      fit_coef["b4"] * yd4 + fit_coef["b5"] * yd5 + fit_coef["b6"] * yd6 +
      fit_coef["b7"] * yd7 + fit_coef["c1"] * wght + fit_coef["c2"] * wdth +
      fit_coef["c3"] * hght + fit_coef["c4"] * hp + fit_coef["c5"] * tran +
      fit_coef["c6"] * ps + fit_coef["c7"] * ac + fit_coef["c8"]),
    nprice = price * winv,
    nyd3 = yd3 * winv,
    nyd4 = yd4 * winv,
    nyd5 = yd5 * winv,
    nyd6 = yd6 * winv,
    nyd7 = yd7 * winv
  )

fit2 <- nlsLM(
  nprice ~ a3 * nyd3 + a4 * nyd4 + a5 * nyd5 + a6 * nyd6 + a7 * nyd7 +
    winv * exp(
      b2 * yd2 + b3 * yd3 + b4 * yd4 + b5 * yd5 + b6 * yd6 + b7 * yd7 +
        c1 * wght + c2 * wdth + c3 * hght + c4 * hp + c5 * tran + c6 * ps +
        c7 * ac + c8
    ),
  data = car_7990,
  start = initial_values,
  # increase the number of iterations to avoid convergence issues
  control = nls.lm.control(maxiter = 1000, maxfev = 10000)
)

summary(fit2)

Formula: nprice ~ a3 * nyd3 + a4 * nyd4 + a5 * nyd5 + a6 * nyd6 + a7 * 
    nyd7 + winv * exp(b2 * yd2 + b3 * yd3 + b4 * yd4 + b5 * yd5 + 
    b6 * yd6 + b7 * yd7 + c1 * wght + c2 * wdth + c3 * hght + 
    c4 * hp + c5 * tran + c6 * ps + c7 * ac + c8)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
a3  4.105e+02  6.192e+02   0.663  0.50834    
a4  3.789e+02  6.503e+02   0.583  0.56087    
a5  7.706e+02  6.012e+02   1.282  0.20176    
a6  6.244e+02  6.190e+02   1.009  0.31465    
a7 -1.229e+02  7.194e+02  -0.171  0.86455    
b2  8.954e-03  3.099e-02   0.289  0.77303    
b3  4.743e-02  1.094e-01   0.434  0.66515    
b4  9.384e-02  1.083e-01   0.867  0.38740    
b5  1.957e-02  1.053e-01   0.186  0.85274    
b6  9.188e-02  1.020e-01   0.901  0.36887    
b7  2.189e-01  1.013e-01   2.162  0.03211 *  
c1  2.548e-02  1.208e-01   0.211  0.83316    
c2  3.566e-01  1.083e-01   3.292  0.00123 ** 
c3 -6.306e-02  5.550e-02  -1.136  0.25748    
c4  6.899e-01  8.522e-02   8.096 1.36e-13 ***
c5  1.429e-01  2.448e-02   5.837 2.88e-08 ***
c6  5.810e-02  2.728e-02   2.130  0.03474 *  
c7  1.473e-01  3.303e-02   4.461 1.53e-05 ***
c8  6.328e+00  5.831e-01  10.853  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1022 on 160 degrees of freedom

Number of iterations to convergence: 18 
Achieved convergence tolerance: 1.49e-08

truck_reg.do

truck_7990 <- feenstra_88$truck_7990 %>%
  filter(year < 86) %>%
  mutate(
    lnprice = log(price),
    yd = factor(year)
  ) %>%
  group_by(yd) %>%
  mutate(id = row_number()) %>%
  ungroup() %>%
  rename(wght = weight)

truck_7990 %>%
  group_by(yd) %>%
  summarise(freq = n()) %>%
  ungroup() %>%
  mutate(
    percent = freq / sum(freq) * 100,
    cum = cumsum(percent)
  )
# A tibble: 7 × 4
  yd     freq percent   cum
  <fct> <int>   <dbl> <dbl>
1 79       10    13.3  13.3
2 80       10    13.3  26.7
3 81       11    14.7  41.3
4 82       11    14.7  56  
5 83       10    13.3  69.3
6 84       11    14.7  84  
7 85       12    16   100  
# I will use b8 instead of c1 to avoid extra tidying later
initial_values <- list(
  a2 = 1, a3 = 1, a4 = 1, a5 = 1, a6 = 1, a7 = 1,
  b1 = 1, b2 = 1, b3 = 1, b4 = 1, b5 = 1, b6 = 1, b7 = 1, b8 = 1
)

truck_7990 <- truck_7990 %>%
  mutate(yd_ind = as.integer(yd)) %>%
  mutate(yd_ind = as.factor(yd_ind - min(yd_ind) + 1))

truck_7990_dummy <- model.matrix(~ yd_ind - 1, data = truck_7990)

colnames(truck_7990_dummy) <- gsub("_ind", "", colnames(truck_7990_dummy))

truck_7990 <- truck_7990 %>%
  bind_cols(as.data.frame(truck_7990_dummy))

fit3 <- nlsLM(
  lnprice ~ exp(a2 * yd2 + a3 * yd3 + a4 * yd4 + a5 * yd5 + a6 * yd6 +
    a7 * yd7 + b1 * wght + b2 * wdth + b3 * hght + b4 * hp + b5 * tran +
    b6 * ps + b7 * four + b8),
  data = truck_7990,
  start = initial_values
)

summary(fit3)

Formula: lnprice ~ exp(a2 * yd2 + a3 * yd3 + a4 * yd4 + a5 * yd5 + a6 * 
    yd6 + a7 * yd7 + b1 * wght + b2 * wdth + b3 * hght + b4 * 
    hp + b5 * tran + b6 * ps + b7 * four + b8)

Parameters:
    Estimate Std. Error t value Pr(>|t|)    
a2 0.0005705  0.0037259   0.153   0.8788    
a3 0.0240717  0.0036050   6.677 8.28e-09 ***
a4 0.0263975  0.0037132   7.109 1.50e-09 ***
a5 0.0199685  0.0039124   5.104 3.50e-06 ***
a6 0.0221336  0.0038661   5.725 3.37e-07 ***
a7 0.0207545  0.0041371   5.017 4.83e-06 ***
b1 0.0444571  0.0087268   5.094 3.63e-06 ***
b2 0.0030967  0.0112786   0.275   0.7846    
b3 0.0004542  0.0056452   0.080   0.9361    
b4 0.0235098  0.0114640   2.051   0.0446 *  
b5 0.0035386  0.0027552   1.284   0.2039    
b6 0.0097044  0.0046752   2.076   0.0421 *  
b7 0.0246971  0.0029674   8.323 1.23e-11 ***
b8 2.0445709  0.0557319  36.686  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.07091 on 61 degrees of freedom

Number of iterations to convergence: 23 
Achieved convergence tolerance: 1.49e-08
fit_coef <- coef(fit3)

truck_7990 <- truck_7990 %>%
  mutate(
    winv = 1 / exp(fit_coef["a2"] * yd2 + fit_coef["a3"] * yd3 +
      fit_coef["a4"] * yd4 + fit_coef["a5"] * yd5 + fit_coef["a6"] * yd6 +
      fit_coef["a7"] * yd7 + fit_coef["b1"] * wght + fit_coef["b2"] * wdth +
      fit_coef["b3"] * hght + fit_coef["b4"] * hp + fit_coef["b5"] * tran +
      fit_coef["b6"] * ps + fit_coef["b7"] * four + fit_coef["b8"]),
    nprice = price * winv
  )

fit4 <- nlsLM(
  nprice ~ winv * exp(a2 * yd2 + a3 * yd3 + a4 * yd4 + a5 * yd5 + a6 * yd6 +
    a7 * yd7 + b1 * wght + b2 * wdth + b3 * hght + b4 * hp + b5 * tran +
    b6 * ps + b7 * four + b8),
  data = truck_7990,
  start = initial_values,
  control = nls.lm.control(maxiter = 1000, maxfev = 10000)
)

summary(fit4)

Formula: nprice ~ winv * exp(a2 * yd2 + a3 * yd3 + a4 * yd4 + a5 * yd5 + 
    a6 * yd6 + a7 * yd7 + b1 * wght + b2 * wdth + b3 * hght + 
    b4 * hp + b5 * tran + b6 * ps + b7 * four + b8)

Parameters:
   Estimate Std. Error t value Pr(>|t|)    
a2 -0.01752    0.04115  -0.426 0.671808    
a3  0.17842    0.03740   4.771 1.19e-05 ***
a4  0.20406    0.03782   5.396 1.18e-06 ***
a5  0.14882    0.03941   3.777 0.000363 ***
a6  0.18356    0.03874   4.738 1.33e-05 ***
a7  0.16232    0.04151   3.910 0.000234 ***
b1  0.43760    0.05855   7.473 3.55e-10 ***
b2  0.07716    0.11049   0.698 0.487617    
b3 -0.04245    0.04740  -0.896 0.373973    
b4  0.23968    0.10844   2.210 0.030841 *  
b5  0.03410    0.02494   1.367 0.176684    
b6  0.09343    0.03465   2.696 0.009053 ** 
b7  0.22790    0.02709   8.412 8.62e-12 ***
b8  7.55794    0.53930  14.014  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 56.58 on 61 degrees of freedom

Number of iterations to convergence: 15 
Achieved convergence tolerance: 1.49e-08

Extra step: formatting the tables

# function to avoid repeating the same code four times
reshape_estimates <- function(fit, vehicle, term_prefix, term_mappings) {
  tidy(fit) %>%
    filter(term %in% paste0(term_prefix, 1:8)) %>%
    mutate(
      term = case_when(
        term == paste0(term_prefix, "1") ~ term_mappings[1],
        term == paste0(term_prefix, "2") ~ term_mappings[2],
        term == paste0(term_prefix, "3") ~ term_mappings[3],
        term == paste0(term_prefix, "4") ~ term_mappings[4],
        term == paste0(term_prefix, "5") ~ term_mappings[5],
        term == paste0(term_prefix, "6") ~ term_mappings[6],
        term == paste0(term_prefix, "7") ~ term_mappings[7],
        term == paste0(term_prefix, "8") ~ term_mappings[8]
      )
    ) %>%
    select(term, estimate, std.error) %>%
    pivot_longer(
      cols = c(estimate, std.error), names_to = "stat",
      values_to = "value"
    ) %>%
    mutate(
      stat = if_else(stat == "estimate", "Estimate", "Std. Error"),
      value = round(value, 3)
    ) %>%
    rename(!!sym(vehicle) := value)
}

car_vars <- c(
  "Weight (tons)", "Width (feet)", "Height (feet)", "Horsepower (100)",
  "Transmission (5-speed or auto)", "Power steering", "Air conditioning", "Constant"
)

truck_vars <- c(
  "Weight (tons)", "Width (feet)", "Height (feet)", "Horsepower (100)",
  "Transmission (5-speed or auto)", "Power steering", "Four-wheel drive", "Constant"
)

ordered_vars <- unique(c(car_vars, truck_vars))
ordered_vars <- c("Constant", ordered_vars[ordered_vars != "Constant"])

table_84_1 <- reshape_estimates(fit2, "Car", "c", car_vars) %>%
  full_join(reshape_estimates(fit4, "Truck", "b", truck_vars)) %>%
  # mutate term to factor + keep Constant at the beginning
  mutate(
    term = factor(term, levels = ordered_vars)
  ) %>%
  arrange(term)

colnames(table_84_1) <- c("Variable", "Statistic", "Car", "Truck")

kable(table_84_1)
Variable Statistic Car Truck
Constant Estimate 6.328 7.558
Constant Std. Error 0.583 0.539
Weight (tons) Estimate 0.025 0.438
Weight (tons) Std. Error 0.121 0.059
Width (feet) Estimate 0.357 0.077
Width (feet) Std. Error 0.108 0.110
Height (feet) Estimate -0.063 -0.042
Height (feet) Std. Error 0.055 0.047
Horsepower (100) Estimate 0.690 0.240
Horsepower (100) Std. Error 0.085 0.108
Transmission (5-speed or auto) Estimate 0.143 0.034
Transmission (5-speed or auto) Std. Error 0.024 0.025
Power steering Estimate 0.058 0.093
Power steering Std. Error 0.027 0.035
Air conditioning Estimate 0.147 NA
Air conditioning Std. Error 0.033 NA
Four-wheel drive Estimate NA 0.228
Four-wheel drive Std. Error NA 0.027

Exercise 3

Pooling car and truck data, run system_7985.do to reproduce columns (3) and (4) in Table 8.4 with the constraints specified in equation (8.22). How are these constraints built into the program for the nonlinear regression?

Feenstra’s code

capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\system_7985.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_7990,clear
gen ve=2
rename wdth wdth_t
rename hght hght_t
rename weight wght_t
rename hp hp_t
rename four four_t
rename tran tran_t
rename ps ps_t
rename ac ac_t
drop if year>=86

save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_temp,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_7990,clear
drop if year>=86
gen ve=1
rename wdth wdth_c
rename hght hght_c
rename wght wght_c
rename hp hp_c
rename four four_c
rename tran tran_c
rename ps ps_c
rename ac ac_c

append using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_temp

replace wdth_c=0 if wdth_c==.
replace hght_c=0 if hght_c==.
replace wght_c=0 if wght_c==.
replace hp_c=0 if hp_c==.
replace four_c=0 if four_c==.
replace tran_c=0 if tran_c==.
replace ps_c=0 if ps_c==.
replace ac_c=0 if ac_c==.
replace wdth_t=0 if wdth_t==.
replace hght_t=0 if hght_t==.
replace wght_t=0 if wght_t==.
replace hp_t=0 if hp_t==.
replace four_t=0 if four_t==.
replace tran_t=0 if tran_t==.
replace ps_t=0 if ps_t==.
replace ac_t=0 if ac_t==.

tab year, gen(yd)

gen cyd2=yd2
gen tyd2=yd2
replace cyd2=0 if type=="JT"
replace tyd2=0 if type~="JT"

gen cyd3=yd3
gen tyd3=yd3
replace cyd3=0 if type=="JT"
replace tyd3=0 if type~="JT"

gen cyd4=yd4
gen tyd4=yd4
replace cyd4=0 if type=="JT"
replace tyd4=0 if type~="JT"

gen cyd5=yd5
gen tyd5=yd5
replace cyd5=0 if type=="JT"
replace tyd5=0 if type~="JT"

gen cyd6=yd6
gen tyd6=yd6
replace cyd6=0 if type=="JT"
replace tyd6=0 if type~="JT"

gen cyd7=yd7
gen tyd7=yd7
replace cyd7=0 if type=="JT"
replace tyd7=0 if type~="JT"

program define nlct_1
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2 d3 d4 d5 d6 d7 d8"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
global c9=.1
global d1=.1
global d2=.1
global d3=.1
global d4=.1
global d5=.1
global d6=.1
global d7=.1
global d8=.1
exit
}

replace `1'=$a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
*/+exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
*/+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
*/+$c8)+exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)*tyd6+$c9*tyd7/*
*/+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)

end

nl ct_1 price

gen winv=1/exp(_b[b2]*cyd2+_b[b3]*cyd3+_b[b4]*cyd4+_b[b5]*cyd5+_b[b6]*cyd6+_b[b7]*cyd7/*
*/+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c+_b[c5]*tran_c+_b[c6]*ps_c/*
*/+_b[c7]*ac_c+_b[c8])

replace winv=1/exp((_b[b2]+0.16)*tyd2+(_b[b3]+.16)*tyd3+(_b[b4]+.16)*tyd4/*
*/+(_b[b5]+.16)*tyd5+(_b[b6]+.16)*tyd6+_b[c9]*tyd7/*
*/+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
*/+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t+_b[d8]) if ve==2

gen nprice=price*winv


program define nlct_2
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2 d3 d4 d5 d6 d7 d8"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
global c9=.1
global d1=.1
global d2=.1
global d3=.1
global d4=.1
global d5=.1
global d6=.1
global d7=.1
global d8=.1
exit
}
replace `1'=winv*($a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7)/*
*/+winv*exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
*/+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
*/+$c8)+winv*exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)*tyd6+$c9*tyd7/*
*/+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)

end

nl ct_2 nprice

program drop _all
log close
exit

Output:

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\system_7985.log,replace
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\system_7985.log
  log type:  text
 opened on:  19 Jun 2024, 21:59:23

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tr
> uck_7990,clear

. gen ve=2

. rename wdth wdth_t

. rename hght hght_t

. rename weight wght_t

. rename hp hp_t

. rename four four_t

. rename tran tran_t

. rename ps ps_t

. rename ac ac_t

. drop if year>=86
(45 observations deleted)

. 
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\t
> ruck_temp,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tru
> ck_temp.dta saved

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\ca
> r_7990,clear

. drop if year>=86
(105 observations deleted)

. gen ve=1

. rename wdth wdth_c

. rename hght hght_c

. rename wght wght_c

. rename hp hp_c

. rename four four_c

. rename tran tran_c

. rename ps ps_c

. rename ac ac_c

. 
. append using Z:\home\pacha\github\advanced-international-trade\first-edition\Cha
> pter-8\truck_temp

. 
. replace wdth_c=0 if wdth_c==.
(75 real changes made)

. replace hght_c=0 if hght_c==.
(75 real changes made)

. replace wght_c=0 if wght_c==.
(75 real changes made)

. replace hp_c=0 if hp_c==.
(75 real changes made)

. replace four_c=0 if four_c==.
(75 real changes made)

. replace tran_c=0 if tran_c==.
(75 real changes made)

. replace ps_c=0 if ps_c==.
(75 real changes made)

. replace ac_c=0 if ac_c==.
(75 real changes made)

. replace wdth_t=0 if wdth_t==.
(179 real changes made)

. replace hght_t=0 if hght_t==.
(179 real changes made)

. replace wght_t=0 if wght_t==.
(179 real changes made)

. replace hp_t=0 if hp_t==.
(179 real changes made)

. replace four_t=0 if four_t==.
(179 real changes made)

. replace tran_t=0 if tran_t==.
(179 real changes made)

. replace ps_t=0 if ps_t==.
(179 real changes made)

. replace ac_t=0 if ac_t==.
(179 real changes made)

. 
. tab year, gen(yd)

       year |      Freq.     Percent        Cum.
------------+-----------------------------------
         79 |         31       12.20       12.20
         80 |         34       13.39       25.59
         81 |         35       13.78       39.37
         82 |         35       13.78       53.15
         83 |         36       14.17       67.32
         84 |         40       15.75       83.07
         85 |         43       16.93      100.00
------------+-----------------------------------
      Total |        254      100.00

. 
. gen cyd2=yd2

. gen tyd2=yd2

. replace cyd2=0 if type=="JT"
(8 real changes made)

. replace tyd2=0 if type~="JT"
(26 real changes made)

. 
. gen cyd3=yd3

. gen tyd3=yd3

. replace cyd3=0 if type=="JT"
(9 real changes made)

. replace tyd3=0 if type~="JT"
(26 real changes made)

. 
. gen cyd4=yd4

. gen tyd4=yd4

. replace cyd4=0 if type=="JT"
(9 real changes made)

. replace tyd4=0 if type~="JT"
(26 real changes made)

. 
. gen cyd5=yd5

. gen tyd5=yd5

. replace cyd5=0 if type=="JT"
(7 real changes made)

. replace tyd5=0 if type~="JT"
(29 real changes made)

. 
. gen cyd6=yd6

. gen tyd6=yd6

. replace cyd6=0 if type=="JT"
(7 real changes made)

. replace tyd6=0 if type~="JT"
(33 real changes made)

. 
. gen cyd7=yd7

. gen tyd7=yd7

. replace cyd7=0 if type=="JT"
(7 real changes made)

. replace tyd7=0 if type~="JT"
(36 real changes made)

. 
. program define nlct_1
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2
>  d3 d4 d5 d6 d7 d8"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global b2=.1
 10. global b3=.1
 11. global b4=.1
 12. global b5=.1
 13. global b6=.1
 14. global b7=.1
 15. global c1=.1
 16. global c2=.1
 17. global c3=.1
 18. global c4=.1
 19. global c5=.1
 20. global c6=.1
 21. global c7=.1
 22. global c8=.1
 23. global c9=.1
 24. global d1=.1
 25. global d2=.1
 26. global d3=.1
 27. global d4=.1
 28. global d5=.1
 29. global d6=.1
 30. global d7=.1
 31. global d8=.1
 32. exit
 33. }
 34. 
. replace `1'=$a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
> */+exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
> */+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
> */+$c8)+exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)*tyd6
> +$c9*tyd7/*
> */+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)
 35. 
. end

. 
. nl ct_1 price
(obs = 254)

Iteration 0:   residual SS =  1.51e+10
...
Iteration 24:  residual SS =  1.01e+08
Iteration 25:  residual SS =  1.01e+08

      Source |       SS       df       MS            Number of obs =       254
-------------+------------------------------         F( 28,   226) =   1210.09
       Model |  1.5069e+10    28   538194036         Prob > F      =    0.0000
    Residual |   100515047   226  444756.844         R-squared     =    0.9934
-------------+------------------------------         Adj R-squared =    0.9926
       Total |  1.5170e+10   254  59724204.9         Root MSE      =  666.9009
                                                     Res. dev.     =  3994.496
(ct_1)
------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   254.0918   317.1045     0.80   0.424    -370.7678    878.9513
          a4 |   505.1058   325.6388     1.55   0.122    -136.5708    1146.782
          a5 |   708.9466   317.3172     2.23   0.026       83.668    1334.225
          a6 |   1082.054   322.4415     3.36   0.001     446.6773     1717.43
          a7 |   1063.014   373.9703     2.84   0.005     326.0995    1799.929
          b2 |   .0040633   .0329008     0.12   0.902    -.0607683    .0688948
          b3 |   .0756038    .049061     1.54   0.125    -.0210716    .1722792
          b4 |   .0828575   .0490467     1.69   0.093    -.0137897    .1795048
          b5 |   .0327776    .050707     0.65   0.519    -.0671415    .1326967
          b6 |   .0351224   .0514768     0.68   0.496    -.0663135    .1365582
          b7 |   .0825554   .0557608     1.48   0.140    -.0273221    .1924329
          c1 |   .0326037   .1117298     0.29   0.771    -.1875618    .2527691
          c2 |    .385946   .1204171     3.21   0.002     .1486621    .6232299
          c3 |  -.1929238   .0568197    -3.40   0.001     -.304888   -.0809596
          c4 |    .715212   .0736084     9.72   0.000     .5701655    .8602585
          c5 |   .1578585     .03381     4.67   0.000     .0912354    .2244816
          c6 |   .0641097   .0247061     2.59   0.010     .0154259    .1127934
          c7 |   .1829447   .0242773     7.54   0.000     .1351059    .2307836
          c8 |   6.599831   .7305317     9.03   0.000     5.160306    8.039356
          c9 |   .1171992   .0634238     1.85   0.066    -.0077785    .2421769
          d1 |   .4917133   .0795807     6.18   0.000     .3348983    .6485283
          d2 |   .2622177   .1757206     1.49   0.137    -.0840426    .6084779
          d3 |  -.0451278   .0677055    -0.67   0.506    -.1785426     .088287
          d4 |   .3030767   .1701249     1.78   0.076    -.0321571    .6383106
          d5 |   .0397822   .0362731     1.10   0.274    -.0316947     .111259
          d6 |   .0967659   .0501832     1.93   0.055    -.0021208    .1956526
          d7 |   .3052359   .0715711     4.26   0.000     .1642038     .446268
          d8 |   6.299805   .9622249     6.55   0.000     4.403726    8.195885
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. gen winv=1/exp(_b[b2]*cyd2+_b[b3]*cyd3+_b[b4]*cyd4+_b[b5]*cyd5+_b[b6]*cyd6+_b[b7
> ]*cyd7/*
> */+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c+_b[c5]*tran_c+_b[c6]*ps
> _c/*
> */+_b[c7]*ac_c+_b[c8])

. 
. replace winv=1/exp((_b[b2]+0.16)*tyd2+(_b[b3]+.16)*tyd3+(_b[b4]+.16)*tyd4/*
> */+(_b[b5]+.16)*tyd5+(_b[b6]+.16)*tyd6+_b[c9]*tyd7/*
> */+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
> */+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t+_b[d8]) if ve==2
(75 real changes made)

. 
. gen nprice=price*winv

. 
. 
. program define nlct_2
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2
>  d3 d4 d5 d6 d7 d8"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global b2=.1
 10. global b3=.1
 11. global b4=.1
 12. global b5=.1
 13. global b6=.1
 14. global b7=.1
 15. global c1=.1
 16. global c2=.1
 17. global c3=.1
 18. global c4=.1
 19. global c5=.1
 20. global c6=.1
 21. global c7=.1
 22. global c8=.1
 23. global c9=.1
 24. global d1=.1
 25. global d2=.1
 26. global d3=.1
 27. global d4=.1
 28. global d5=.1
 29. global d6=.1
 30. global d7=.1
 31. global d8=.1
 32. exit
 33. }
 34. replace `1'=winv*($a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7)/*
> */+winv*exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
> */+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
> */+$c8)+winv*exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)
> *tyd6+$c9*tyd7/*
> */+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)
 35. 
. end

. 
. nl ct_2 nprice
(obs = 254)

Iteration 0:   residual SS =  353.5865
...
Iteration 20:  residual SS =  2.957295
Iteration 21:  residual SS =  2.957295

      Source |       SS       df       MS            Number of obs =       254
-------------+------------------------------         F( 28,   226) =    958.75
       Model |  351.277591    28  12.5456282         Prob > F      =    0.0000
    Residual |  2.95729549   226  .013085378         R-squared     =    0.9917
-------------+------------------------------         Adj R-squared =    0.9906
       Total |  354.234886   254  1.39462554         Root MSE      =  .1143913
                                                     Res. dev.     = -410.2562
(ct_2)
------------------------------------------------------------------------------
      nprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   367.9829    239.059     1.54   0.125    -103.0867    839.0525
          a4 |    600.553   254.2261     2.36   0.019     99.59628     1101.51
          a5 |   887.9399   243.5428     3.65   0.000     408.0348    1367.845
          a6 |   1104.433   251.0245     4.40   0.000     609.7847     1599.08
          a7 |   855.7993   366.2444     2.34   0.020     134.1088     1577.49
          b2 |   .0130387   .0303131     0.43   0.668    -.0466937    .0727711
          b3 |   .0674477   .0425176     1.59   0.114     -.016334    .1512294
          b4 |   .0701234   .0441821     1.59   0.114    -.0169382     .157185
          b5 |  -.0005627   .0470779    -0.01   0.990    -.0933305    .0922051
          b6 |   .0179989   .0473905     0.38   0.704    -.0753848    .1113826
          b7 |   .1007712   .0641587     1.57   0.118    -.0256545    .2271969
          c1 |   .0492474   .1311096     0.38   0.708    -.2091061     .307601
          c2 |   .3908689   .1383663     2.82   0.005     .1182159     .663522
          c3 |  -.0618204   .0611492    -1.01   0.313    -.1823158     .058675
          c4 |   .8103277   .1038045     7.81   0.000     .6057792    1.014876
          c5 |   .1779391   .0355273     5.01   0.000     .1079319    .2479463
          c6 |   .0693687   .0304968     2.27   0.024     .0092743     .129463
          c7 |   .1567051   .0360384     4.35   0.000     .0856908    .2277193
          c8 |   5.839036   .8621144     6.77   0.000     4.140226    7.537847
          c9 |   .1059852   .0550545     1.93   0.055    -.0025007    .2144711
          d1 |   .4759918   .1172145     4.06   0.000     .2450187    .7069649
          d2 |   .2120021   .1419991     1.49   0.137    -.0678094    .4918136
          d3 |  -.0270369    .074052    -0.37   0.715    -.1729575    .1188837
          d4 |     .24435   .1516866     1.61   0.109    -.0545508    .5432508
          d5 |   .0530065   .0356148     1.49   0.138     -.017173     .123186
          d6 |   .0575178   .0639555     0.90   0.369    -.0685076    .1835431
          d7 |   .2971909   .0566882     5.24   0.000      .185486    .4088959
          d8 |   6.630805   .7652819     8.66   0.000     5.122804    8.138805
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. program drop _all

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\system_7985.log
  log type:  text
 closed on:  19 Jun 2024, 21:59:28
----------------------------------------------------------------------------------

. exit

end of do-file

My code

car_7990 <- feenstra_88$car_7990 %>%
  filter(year < 86) %>%
  mutate(ve = 1) %>%
  rename(
    wdth_c = wdth, hght_c = hght, wght_c = wght, hp_c = hp, four_c = four,
    tran_c = tran, ps_c = ps, ac_c = ac
  )

truck_7990 <- feenstra_88$truck_7990 %>%
  filter(year < 86) %>%
  mutate(ve = 2) %>%
  rename(
    wdth_t = wdth, hght_t = hght, wght_t = weight, hp_t = hp, four_t = four,
    tran_t = tran, ps_t = ps, ac_t = ac
  )

truck_7990 <- truck_7990 %>%
  bind_rows(car_7990) %>%
  mutate(
    wdth_c = ifelse(is.na(wdth_c), 0, wdth_c),
    hght_c = ifelse(is.na(hght_c), 0, hght_c),
    wght_c = ifelse(is.na(wght_c), 0, wght_c),
    hp_c = ifelse(is.na(hp_c), 0, hp_c),
    four_c = ifelse(is.na(four_c), 0, four_c),
    tran_c = ifelse(is.na(tran_c), 0, tran_c),
    ps_c = ifelse(is.na(ps_c), 0, ps_c),
    ac_c = ifelse(is.na(ac_c), 0, ac_c),
    wdth_t = ifelse(is.na(wdth_t), 0, wdth_t),
    hght_t = ifelse(is.na(hght_t), 0, hght_t),
    wght_t = ifelse(is.na(wght_t), 0, wght_t),
    hp_t = ifelse(is.na(hp_t), 0, hp_t),
    four_t = ifelse(is.na(four_t), 0, four_t),
    tran_t = ifelse(is.na(tran_t), 0, tran_t),
    ps_t = ifelse(is.na(ps_t), 0, ps_t),
    ac_t = ifelse(is.na(ac_t), 0, ac_t)
  )

truck_7990 %>%
  group_by(year) %>%
  summarise(freq = n()) %>%
  ungroup() %>%
  mutate(
    percent = freq / sum(freq) * 100,
    cum = cumsum(percent)
  )
# A tibble: 7 × 4
   year  freq percent   cum
  <dbl> <int>   <dbl> <dbl>
1    79    31    12.2  12.2
2    80    34    13.4  25.6
3    81    35    13.8  39.4
4    82    35    13.8  53.1
5    83    36    14.2  67.3
6    84    40    15.7  83.1
7    85    43    16.9 100  
# expand the yd variable

truck_7990 <- truck_7990 %>%
  mutate(yd = as.factor(year - min(year) + 1))

truck_7990_dummy <- model.matrix(~ yd - 1, data = truck_7990)

truck_7990 <- truck_7990 %>%
  bind_cols(as.data.frame(truck_7990_dummy))

# generate the cyd and tyd variables

truck_7990 <- truck_7990 %>%
  mutate(
    cyd2 = ifelse(type == "JT", 0, yd2),
    cyd3 = ifelse(type == "JT", 0, yd3),
    cyd4 = ifelse(type == "JT", 0, yd4),
    cyd5 = ifelse(type == "JT", 0, yd5),
    cyd6 = ifelse(type == "JT", 0, yd6),
    cyd7 = ifelse(type == "JT", 0, yd7),
    tyd2 = ifelse(type != "JT", 0, yd2),
    tyd3 = ifelse(type != "JT", 0, yd3),
    tyd4 = ifelse(type != "JT", 0, yd4),
    tyd5 = ifelse(type != "JT", 0, yd5),
    tyd6 = ifelse(type != "JT", 0, yd6),
    tyd7 = ifelse(type != "JT", 0, yd7)
  )

# fit the models

initial_values <- list(
  a3 = 1, a4 = 1, a5 = 1, a6 = 1, a7 = 1,
  b2 = 0.1, b3 = 0.1, b4 = 0.1, b5 = 0.1, b6 = 0.1, b7 = 0.1,
  c1 = 0.1, c2 = 0.1, c3 = 0.1, c4 = 0.1, c5 = 0.1, c6 = 0.1, c7 = 0.1,
  c8 = 0.1, c9 = 0.1,
  d1 = 0.1, d2 = 0.1, d3 = 0.1, d4 = 0.1, d5 = 0.1, d6 = 0.1, d7 = 0.1,
  d8 = 0.1
)

fit <- nlsLM(
  price ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7 +
    exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 + b7 * cyd7 +
      c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c + c5 * tran_c +
      c6 * ps_c + c7 * ac_c + c8) +
    exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + (b4 + 0.16) * tyd4 +
      (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + c9 * tyd7 +
      d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t +
      d6 * ps_t + d7 * four_t + d8),
  data = truck_7990,
  start = initial_values
)

summary(fit)

Formula: price ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * 
    cyd7 + exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + 
    b6 * cyd6 + b7 * cyd7 + c1 * wght_c + c2 * wdth_c + c3 * 
    hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + c7 * ac_c + 
    c8) + exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + (b4 + 0.16) * 
    tyd4 + (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + c9 * tyd7 + 
    d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * 
    tran_t + d6 * ps_t + d7 * four_t + d8)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
a3  2.542e+02  3.171e+02   0.802 0.423598    
a4  5.052e+02  3.256e+02   1.552 0.122150    
a5  7.090e+02  3.173e+02   2.235 0.026415 *  
a6  1.082e+03  3.224e+02   3.356 0.000926 ***
a7  1.063e+03  3.739e+02   2.843 0.004876 ** 
b2  4.065e-03  3.290e-02   0.124 0.901779    
b3  7.559e-02  4.906e-02   1.541 0.124732    
b4  8.284e-02  4.904e-02   1.689 0.092555 .  
b5  3.276e-02  5.070e-02   0.646 0.518840    
b6  3.510e-02  5.147e-02   0.682 0.495934    
b7  8.254e-02  5.576e-02   1.480 0.140197    
c1  3.261e-02  1.117e-01   0.292 0.770715    
c2  3.860e-01  1.204e-01   3.205 0.001545 ** 
c3 -1.929e-01  5.683e-02  -3.395 0.000811 ***
c4  7.152e-01  7.362e-02   9.715  < 2e-16 ***
c5  1.579e-01  3.382e-02   4.668 5.21e-06 ***
c6  6.412e-02  2.471e-02   2.595 0.010082 *  
c7  1.830e-01  2.428e-02   7.535 1.16e-12 ***
c8  6.599e+00  7.307e-01   9.032  < 2e-16 ***
c9  1.172e-01  6.342e-02   1.848 0.065932 .  
d1  4.917e-01  7.958e-02   6.179 2.97e-09 ***
d2  2.621e-01  1.757e-01   1.492 0.137107    
d3 -4.513e-02  6.770e-02  -0.667 0.505752    
d4  3.031e-01  1.701e-01   1.782 0.076158 .  
d5  3.978e-02  3.627e-02   1.097 0.273899    
d6  9.676e-02  5.018e-02   1.928 0.055072 .  
d7  3.052e-01  7.156e-02   4.265 2.94e-05 ***
d8  6.300e+00  9.620e-01   6.549 3.85e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 666.9 on 226 degrees of freedom

Number of iterations to convergence: 30 
Achieved convergence tolerance: 1.49e-08
truck_7990 <- truck_7990 %>%
  mutate(
    winv = case_when(
      ve == 1 ~ 1 / exp(coef(fit)["b2"] * cyd2 + coef(fit)["b3"] * cyd3 +
        coef(fit)["b4"] * cyd4 + coef(fit)["b5"] * cyd5 +
        coef(fit)["b6"] * cyd6 + coef(fit)["b7"] * cyd7 +
        coef(fit)["c1"] * wght_c + coef(fit)["c2"] * wdth_c +
        coef(fit)["c3"] * hght_c + coef(fit)["c4"] * hp_c +
        coef(fit)["c5"] * tran_c + coef(fit)["c6"] * ps_c +
        coef(fit)["c7"] * ac_c + coef(fit)["c8"]),
      ve == 2 ~ 1 / exp((coef(fit)["b2"] + 0.16) * tyd2 +
        (coef(fit)["b3"] + 0.16) * tyd3 + (coef(fit)["b4"] + 0.16) * tyd4 +
        (coef(fit)["b5"] + 0.16) * tyd5 + (coef(fit)["b6"] + 0.16) * tyd6 +
        coef(fit)["c9"] * tyd7 + coef(fit)["d1"] * wght_t +
        coef(fit)["d2"] * wdth_t + coef(fit)["d3"] * hght_t +
        coef(fit)["d4"] * hp_t + coef(fit)["d5"] * tran_t +
        coef(fit)["d6"] * ps_t + coef(fit)["d7"] * four_t +
        coef(fit)["d8"])
    ),
    nprice = price * winv
  )

fit2 <- nlsLM(
  nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7) +
    winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 +
      b7 * cyd7 + c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c +
      c5 * tran_c + c6 * ps_c + c7 * ac_c + c8) +
    winv * exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + (b4 + 0.16) * tyd4 +
      (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + c9 * tyd7 + d1 * wght_t +
      d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * ps_t +
      d7 * four_t + d8),
  data = truck_7990,
  start = initial_values
)

summary(fit2)

Formula: nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + 
    a7 * cyd7) + winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + 
    b5 * cyd5 + b6 * cyd6 + b7 * cyd7 + c1 * wght_c + c2 * wdth_c + 
    c3 * hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + c7 * 
    ac_c + c8) + winv * exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + 
    (b4 + 0.16) * tyd4 + (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + 
    c9 * tyd7 + d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * 
    hp_t + d5 * tran_t + d6 * ps_t + d7 * four_t + d8)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
a3  3.679e+02  2.391e+02   1.539 0.125219    
a4  6.005e+02  2.543e+02   2.362 0.019041 *  
a5  8.879e+02  2.436e+02   3.645 0.000332 ***
a6  1.104e+03  2.511e+02   4.399 1.68e-05 ***
a7  8.556e+02  3.663e+02   2.336 0.020379 *  
b2  1.304e-02  3.031e-02   0.430 0.667488    
b3  6.745e-02  4.252e-02   1.586 0.114086    
b4  7.013e-02  4.419e-02   1.587 0.113886    
b5 -5.484e-04  4.708e-02  -0.012 0.990718    
b6  1.801e-02  4.740e-02   0.380 0.704268    
b7  1.008e-01  6.416e-02   1.571 0.117554    
c1  4.925e-02  1.311e-01   0.376 0.707523    
c2  3.908e-01  1.383e-01   2.825 0.005153 ** 
c3 -6.182e-02  6.114e-02  -1.011 0.313088    
c4  8.103e-01  1.038e-01   7.807 2.17e-13 ***
c5  1.779e-01  3.552e-02   5.009 1.10e-06 ***
c6  6.936e-02  3.049e-02   2.275 0.023864 *  
c7  1.567e-01  3.604e-02   4.348 2.08e-05 ***
c8  5.839e+00  8.619e-01   6.775 1.07e-10 ***
c9  1.060e-01  5.506e-02   1.925 0.055494 .  
d1  4.760e-01  1.172e-01   4.060 6.76e-05 ***
d2  2.121e-01  1.420e-01   1.493 0.136773    
d3 -2.704e-02  7.406e-02  -0.365 0.715380    
d4  2.443e-01  1.517e-01   1.611 0.108659    
d5  5.301e-02  3.562e-02   1.488 0.138112    
d6  5.751e-02  6.396e-02   0.899 0.369534    
d7  2.972e-01  5.670e-02   5.242 3.64e-07 ***
d8  6.630e+00  7.655e-01   8.661 8.98e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1144 on 226 degrees of freedom

Number of iterations to convergence: 31 
Achieved convergence tolerance: 1.49e-08

Extra step: formatting the tables

table_84_2 <- tidy(fit2) %>%
  select(term, estimate, std.error) %>%
  mutate(
    vehicle = substr(term, 1, 1),
    vehicle = case_when(
      vehicle == "c" ~ "car",
      vehicle == "d" ~ "truck",
      TRUE ~ "Exclude"
    )
  ) %>%
  filter(vehicle != "Exclude") %>%
  mutate(
    term = case_when(
      term == "c1" | term == "d1" ~ "Weight (tons)",
      term == "c2" | term == "d2" ~ "Width (feet)",
      term == "c3" | term == "d3" ~ "Height (feet)",
      term == "c4" | term == "d4" ~ "Horsepower (100)",
      term == "c5" | term == "d5" ~ "Transmission (5-speed or auto)",
      term == "c6" | term == "d6" ~ "Power steering",
      term == "c7"                ~ "Air conditioning",
      term == "d7"                ~ "Four-wheel drive",
      term == "c8" | term == "d8" ~ "Constant",
      TRUE ~ "Exclude"
    )
  ) %>%
  filter(term != "Exclude") %>%
  pivot_longer(cols = c(estimate, std.error), names_to = "stat") %>%
  pivot_wider(names_from = "vehicle", values_from = "value") %>%
  mutate_if(is.numeric, ~ round(., 3)) %>%
  mutate(
    term = factor(term, levels = c(
      "Constant", "Weight (tons)", "Width (feet)", "Height (feet)",
      "Horsepower (100)", "Transmission (5-speed or auto)", "Power steering",
      "Air conditioning", "Four-wheel drive"
    )),
    stat = if_else(stat == "estimate", "Estimate", "Std. Error")
  ) %>%
  arrange(term)

colnames(table_84_2) <- c("Variable", "Statistic", "Car", "Truck")

kable(table_84_2)
Variable Statistic Car Truck
Constant Estimate 5.839 6.630
Constant Std. Error 0.862 0.765
Weight (tons) Estimate 0.049 0.476
Weight (tons) Std. Error 0.131 0.117
Width (feet) Estimate 0.391 0.212
Width (feet) Std. Error 0.138 0.142
Height (feet) Estimate -0.062 -0.027
Height (feet) Std. Error 0.061 0.074
Horsepower (100) Estimate 0.810 0.244
Horsepower (100) Std. Error 0.104 0.152
Transmission (5-speed or auto) Estimate 0.178 0.053
Transmission (5-speed or auto) Std. Error 0.036 0.036
Power steering Estimate 0.069 0.058
Power steering Std. Error 0.030 0.064
Air conditioning Estimate 0.157 NA
Air conditioning Std. Error 0.036 NA
Four-wheel drive Estimate NA 0.297
Four-wheel drive Std. Error NA 0.057

Exercise 4

Run the programs unit_quality.do, qindex_c.do, qindex_t.do to reproduce the quality indexes and unit-qualities for cars and trucks in Table 8.3. What formula is used for the quality of each model and the quality indexes, and how does this differ from the unit-quality?

Feenstra’s code

unit_quality.do

capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\unit_quality.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_7990,clear
drop if year>=86
gen ve=2
rename quan quan_t
rename wdth wdth_t
rename hght hght_t
rename weight wght_t
rename hp hp_t
rename four four_t
rename tran tran_t
rename ps ps_t
rename ac ac_t

save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_temp,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_7990,clear
drop if year>=86
gen ve=1
rename quan quan_c
rename wdth wdth_c
rename hght hght_c
rename wght wght_c
rename hp hp_c
rename four four_c
rename tran tran_c
rename ps ps_c
rename ac ac_c

append using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_temp

replace wdth_c=0 if wdth_c==.
replace hght_c=0 if hght_c==.
replace wght_c=0 if wght_c==.
replace hp_c=0 if hp_c==.
replace four_c=0 if four_c==.
replace tran_c=0 if tran_c==.
replace ps_c=0 if ps_c==.
replace ac_c=0 if ac_c==.
replace wdth_t=0 if wdth_t==.
replace hght_t=0 if hght_t==.
replace wght_t=0 if wght_t==.
replace hp_t=0 if hp_t==.
replace four_t=0 if four_t==.
replace tran_t=0 if tran_t==.
replace ps_t=0 if ps_t==.
replace ac_t=0 if ac_t==.

tab year, gen(yd)

gen cyd2=yd2
gen tyd2=yd2
replace cyd2=0 if type=="JT"
replace tyd2=0 if type~="JT"

gen cyd3=yd3
gen tyd3=yd3
replace cyd3=0 if type=="JT"
replace tyd3=0 if type~="JT"

gen cyd4=yd4
gen tyd4=yd4
replace cyd4=0 if type=="JT"
replace tyd4=0 if type~="JT"

gen cyd5=yd5
gen tyd5=yd5
replace cyd5=0 if type=="JT"
replace tyd5=0 if type~="JT"

gen cyd6=yd6
gen tyd6=yd6
replace cyd6=0 if type=="JT"
replace tyd6=0 if type~="JT"

gen cyd7=yd7
gen tyd7=yd7
replace cyd7=0 if type=="JT"
replace tyd7=0 if type~="JT"

program define nlct_1
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2 d3 d4 d5 d6 d7 d8"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
global c9=.1
global d1=.1
global d2=.1
global d3=.1
global d4=.1
global d5=.1
global d6=.1
global d7=.1
global d8=.1
exit
}

replace `1'=$a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
*/+exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
*/+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
*/+$c8)+exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)*tyd6+$c9*tyd7/*
*/+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)

end

nl ct_1 price

gen winv=1/exp(_b[b2]*cyd2+_b[b3]*cyd3+_b[b4]*cyd4+_b[b5]*cyd5+_b[b6]*cyd6+_b[b7]*cyd7/*
*/+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c+_b[c5]*tran_c+_b[c6]*ps_c/*
*/+_b[c7]*ac_c+_b[c8])

replace winv=1/exp((_b[b2]+0.16)*tyd2+(_b[b3]+.16)*tyd3+(_b[b4]+.16)*tyd4/*
*/+(_b[b5]+.16)*tyd5+(_b[b6]+.16)*tyd6+_b[c9]*tyd7/*
*/+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
*/+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t+_b[d8]) if ve==2

gen nprice=price*winv


program define nlct_2
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2 d3 d4 d5 d6 d7 d8"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
global c9=.1
global d1=.1
global d2=.1
global d3=.1
global d4=.1
global d5=.1
global d6=.1
global d7=.1
global d8=.1
exit
}
replace `1'=winv*($a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7)/*
*/+winv*exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
*/+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
*/+$c8)+winv*exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)*tyd6+$c9*tyd7/*
*/+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)

end

nl ct_2 nprice


preserve
keep if ve==1

gen quality=exp(_b[c8]+_b[b2]+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c/*
*/+_b[c5]*tran_c+_b[c6]*ps_c+_b[c7]*ac_c)

save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\quality_c,replace

gen nvalue=quality*quan_c
egen aquan=mean(quan_c), by(year)
egen anvalue=mean(nvalue),by(year)
gen uquality=anvalue/aquan
collapse (mean)uquality, by(year)
list
restore

preserve
keep if ve==2

gen quality=exp(_b[d8]+_b[b2]+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
*/+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t)

save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\quality_t,replace

gen nvalue=quality*quan_t
egen aquan=mean(quan_t),by(year)
egen anvalue=mean(nvalue), by(year)
gen uquality=anvalue/aquan
collapse (mean)uquality, by(year)
list
restore

program drop _all
log close
exit

Output:

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\unit_quality.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\unit_quality.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\unit_quality.log
  log type:  text
 opened on:  19 Jun 2024, 23:03:32

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tr
> uck_7990,clear

. drop if year>=86
(45 observations deleted)

. gen ve=2

. rename quan quan_t

. rename wdth wdth_t

. rename hght hght_t

. rename weight wght_t

. rename hp hp_t

. rename four four_t

. rename tran tran_t

. rename ps ps_t

. rename ac ac_t

. 
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\t
> ruck_temp,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tru
> ck_temp.dta saved

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\ca
> r_7990,clear

. drop if year>=86
(105 observations deleted)

. gen ve=1

. rename quan quan_c

. rename wdth wdth_c

. rename hght hght_c

. rename wght wght_c

. rename hp hp_c

. rename four four_c

. rename tran tran_c

. rename ps ps_c

. rename ac ac_c

. 
. append using Z:\home\pacha\github\advanced-international-trade\first-edition\Cha
> pter-8\truck_temp

. 
. replace wdth_c=0 if wdth_c==.
(75 real changes made)

. replace hght_c=0 if hght_c==.
(75 real changes made)

. replace wght_c=0 if wght_c==.
(75 real changes made)

. replace hp_c=0 if hp_c==.
(75 real changes made)

. replace four_c=0 if four_c==.
(75 real changes made)

. replace tran_c=0 if tran_c==.
(75 real changes made)

. replace ps_c=0 if ps_c==.
(75 real changes made)

. replace ac_c=0 if ac_c==.
(75 real changes made)

. replace wdth_t=0 if wdth_t==.
(179 real changes made)

. replace hght_t=0 if hght_t==.
(179 real changes made)

. replace wght_t=0 if wght_t==.
(179 real changes made)

. replace hp_t=0 if hp_t==.
(179 real changes made)

. replace four_t=0 if four_t==.
(179 real changes made)

. replace tran_t=0 if tran_t==.
(179 real changes made)

. replace ps_t=0 if ps_t==.
(179 real changes made)

. replace ac_t=0 if ac_t==.
(179 real changes made)

. 
. tab year, gen(yd)

       year |      Freq.     Percent        Cum.
------------+-----------------------------------
         79 |         31       12.20       12.20
         80 |         34       13.39       25.59
         81 |         35       13.78       39.37
         82 |         35       13.78       53.15
         83 |         36       14.17       67.32
         84 |         40       15.75       83.07
         85 |         43       16.93      100.00
------------+-----------------------------------
      Total |        254      100.00

. 
. gen cyd2=yd2

. gen tyd2=yd2

. replace cyd2=0 if type=="JT"
(8 real changes made)

. replace tyd2=0 if type~="JT"
(26 real changes made)

. 
. gen cyd3=yd3

. gen tyd3=yd3

. replace cyd3=0 if type=="JT"
(9 real changes made)

. replace tyd3=0 if type~="JT"
(26 real changes made)

. 
. gen cyd4=yd4

. gen tyd4=yd4

. replace cyd4=0 if type=="JT"
(9 real changes made)

. replace tyd4=0 if type~="JT"
(26 real changes made)

. 
. gen cyd5=yd5

. gen tyd5=yd5

. replace cyd5=0 if type=="JT"
(7 real changes made)

. replace tyd5=0 if type~="JT"
(29 real changes made)

. 
. gen cyd6=yd6

. gen tyd6=yd6

. replace cyd6=0 if type=="JT"
(7 real changes made)

. replace tyd6=0 if type~="JT"
(33 real changes made)

. 
. gen cyd7=yd7

. gen tyd7=yd7

. replace cyd7=0 if type=="JT"
(7 real changes made)

. replace tyd7=0 if type~="JT"
(36 real changes made)

. 
. program define nlct_1
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2
>  d3 d4 d5 d6 d7 d8"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global b2=.1
 10. global b3=.1
 11. global b4=.1
 12. global b5=.1
 13. global b6=.1
 14. global b7=.1
 15. global c1=.1
 16. global c2=.1
 17. global c3=.1
 18. global c4=.1
 19. global c5=.1
 20. global c6=.1
 21. global c7=.1
 22. global c8=.1
 23. global c9=.1
 24. global d1=.1
 25. global d2=.1
 26. global d3=.1
 27. global d4=.1
 28. global d5=.1
 29. global d6=.1
 30. global d7=.1
 31. global d8=.1
 32. exit
 33. }
 34. 
. replace `1'=$a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
> */+exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
> */+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
> */+$c8)+exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)*tyd6
> +$c9*tyd7/*
> */+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)
 35. 
. end

. 
. nl ct_1 price
(obs = 254)

Iteration 0:   residual SS =  1.51e+10
...
Iteration 24:  residual SS =  1.01e+08
Iteration 25:  residual SS =  1.01e+08

      Source |       SS       df       MS            Number of obs =       254
-------------+------------------------------         F( 28,   226) =   1210.09
       Model |  1.5069e+10    28   538194036         Prob > F      =    0.0000
    Residual |   100515047   226  444756.844         R-squared     =    0.9934
-------------+------------------------------         Adj R-squared =    0.9926
       Total |  1.5170e+10   254  59724204.9         Root MSE      =  666.9009
                                                     Res. dev.     =  3994.496
(ct_1)
------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   254.0918   317.1045     0.80   0.424    -370.7678    878.9513
          a4 |   505.1058   325.6388     1.55   0.122    -136.5708    1146.782
          a5 |   708.9466   317.3172     2.23   0.026       83.668    1334.225
          a6 |   1082.054   322.4415     3.36   0.001     446.6773     1717.43
          a7 |   1063.014   373.9703     2.84   0.005     326.0995    1799.929
          b2 |   .0040633   .0329008     0.12   0.902    -.0607683    .0688948
          b3 |   .0756038    .049061     1.54   0.125    -.0210716    .1722792
          b4 |   .0828575   .0490467     1.69   0.093    -.0137897    .1795048
          b5 |   .0327776    .050707     0.65   0.519    -.0671415    .1326967
          b6 |   .0351224   .0514768     0.68   0.496    -.0663135    .1365582
          b7 |   .0825554   .0557608     1.48   0.140    -.0273221    .1924329
          c1 |   .0326037   .1117298     0.29   0.771    -.1875618    .2527691
          c2 |    .385946   .1204171     3.21   0.002     .1486621    .6232299
          c3 |  -.1929238   .0568197    -3.40   0.001     -.304888   -.0809596
          c4 |    .715212   .0736084     9.72   0.000     .5701655    .8602585
          c5 |   .1578585     .03381     4.67   0.000     .0912354    .2244816
          c6 |   .0641097   .0247061     2.59   0.010     .0154259    .1127934
          c7 |   .1829447   .0242773     7.54   0.000     .1351059    .2307836
          c8 |   6.599831   .7305317     9.03   0.000     5.160306    8.039356
          c9 |   .1171992   .0634238     1.85   0.066    -.0077785    .2421769
          d1 |   .4917133   .0795807     6.18   0.000     .3348983    .6485283
          d2 |   .2622177   .1757206     1.49   0.137    -.0840426    .6084779
          d3 |  -.0451278   .0677055    -0.67   0.506    -.1785426     .088287
          d4 |   .3030767   .1701249     1.78   0.076    -.0321571    .6383106
          d5 |   .0397822   .0362731     1.10   0.274    -.0316947     .111259
          d6 |   .0967659   .0501832     1.93   0.055    -.0021208    .1956526
          d7 |   .3052359   .0715711     4.26   0.000     .1642038     .446268
          d8 |   6.299805   .9622249     6.55   0.000     4.403726    8.195885
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. gen winv=1/exp(_b[b2]*cyd2+_b[b3]*cyd3+_b[b4]*cyd4+_b[b5]*cyd5+_b[b6]*cyd6+_b[b7
> ]*cyd7/*
> */+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c+_b[c5]*tran_c+_b[c6]*ps
> _c/*
> */+_b[c7]*ac_c+_b[c8])

. 
. replace winv=1/exp((_b[b2]+0.16)*tyd2+(_b[b3]+.16)*tyd3+(_b[b4]+.16)*tyd4/*
> */+(_b[b5]+.16)*tyd5+(_b[b6]+.16)*tyd6+_b[c9]*tyd7/*
> */+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
> */+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t+_b[d8]) if ve==2
(75 real changes made)

. 
. gen nprice=price*winv

. 
. 
. program define nlct_2
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2
>  d3 d4 d5 d6 d7 d8"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global b2=.1
 10. global b3=.1
 11. global b4=.1
 12. global b5=.1
 13. global b6=.1
 14. global b7=.1
 15. global c1=.1
 16. global c2=.1
 17. global c3=.1
 18. global c4=.1
 19. global c5=.1
 20. global c6=.1
 21. global c7=.1
 22. global c8=.1
 23. global c9=.1
 24. global d1=.1
 25. global d2=.1
 26. global d3=.1
 27. global d4=.1
 28. global d5=.1
 29. global d6=.1
 30. global d7=.1
 31. global d8=.1
 32. exit
 33. }
 34. replace `1'=winv*($a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7)/*
> */+winv*exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
> */+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
> */+$c8)+winv*exp($b2*tyd2+($b3+.16)*tyd3+($b4+.16)*tyd4+($b5+.16)*tyd5+($b6+.16)
> *tyd6+$c9*tyd7/*
> */+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)
 35. 
. end

. 
. nl ct_2 nprice
(obs = 254)

Iteration 0:   residual SS =  353.5865
...
Iteration 20:  residual SS =  2.957295
Iteration 21:  residual SS =  2.957295

      Source |       SS       df       MS            Number of obs =       254
-------------+------------------------------         F( 28,   226) =    958.75
       Model |  351.277591    28  12.5456282         Prob > F      =    0.0000
    Residual |  2.95729549   226  .013085378         R-squared     =    0.9917
-------------+------------------------------         Adj R-squared =    0.9906
       Total |  354.234886   254  1.39462554         Root MSE      =  .1143913
                                                     Res. dev.     = -410.2562
(ct_2)
------------------------------------------------------------------------------
      nprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   367.9829    239.059     1.54   0.125    -103.0867    839.0525
          a4 |    600.553   254.2261     2.36   0.019     99.59628     1101.51
          a5 |   887.9399   243.5428     3.65   0.000     408.0348    1367.845
          a6 |   1104.433   251.0245     4.40   0.000     609.7847     1599.08
          a7 |   855.7993   366.2444     2.34   0.020     134.1088     1577.49
          b2 |   .0130387   .0303131     0.43   0.668    -.0466937    .0727711
          b3 |   .0674477   .0425176     1.59   0.114     -.016334    .1512294
          b4 |   .0701234   .0441821     1.59   0.114    -.0169382     .157185
          b5 |  -.0005627   .0470779    -0.01   0.990    -.0933305    .0922051
          b6 |   .0179989   .0473905     0.38   0.704    -.0753848    .1113826
          b7 |   .1007712   .0641587     1.57   0.118    -.0256545    .2271969
          c1 |   .0492474   .1311096     0.38   0.708    -.2091061     .307601
          c2 |   .3908689   .1383663     2.82   0.005     .1182159     .663522
          c3 |  -.0618204   .0611492    -1.01   0.313    -.1823158     .058675
          c4 |   .8103277   .1038045     7.81   0.000     .6057792    1.014876
          c5 |   .1779391   .0355273     5.01   0.000     .1079319    .2479463
          c6 |   .0693687   .0304968     2.27   0.024     .0092743     .129463
          c7 |   .1567051   .0360384     4.35   0.000     .0856908    .2277193
          c8 |   5.839036   .8621144     6.77   0.000     4.140226    7.537847
          c9 |   .1059852   .0550545     1.93   0.055    -.0025007    .2144711
          d1 |   .4759918   .1172145     4.06   0.000     .2450187    .7069649
          d2 |   .2120021   .1419991     1.49   0.137    -.0678094    .4918136
          d3 |  -.0270369    .074052    -0.37   0.715    -.1729575    .1188837
          d4 |     .24435   .1516866     1.61   0.109    -.0545508    .5432508
          d5 |   .0530065   .0356148     1.49   0.138     -.017173     .123186
          d6 |   .0575178   .0639555     0.90   0.369    -.0685076    .1835431
          d7 |   .2971909   .0566882     5.24   0.000      .185486    .4088959
          d8 |   6.630805   .7652819     8.66   0.000     5.122804    8.138805
------------------------------------------------------------------------------
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. 
. preserve

. keep if ve==1
(75 observations deleted)

. 
. gen quality=exp(_b[c8]+_b[b2]+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*h
> p_c/*
> */+_b[c5]*tran_c+_b[c6]*ps_c+_b[c7]*ac_c)

. 
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\q
> uality_c,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qua
> lity_c.dta saved

. 
. gen nvalue=quality*quan_c

. egen aquan=mean(quan_c), by(year)

. egen anvalue=mean(nvalue),by(year)

. gen uquality=anvalue/aquan

. collapse (mean)uquality, by(year)

. list

     +-----------------+
     | year   uquality |
     |-----------------|
  1. |   79   4360.772 |
  2. |   80   4472.789 |
  3. |   81   4867.316 |
  4. |   82   5252.845 |
  5. |   83   5636.667 |
     |-----------------|
  6. |   84    5862.13 |
  7. |   85   6130.052 |
     +-----------------+

. restore

. 
. preserve

. keep if ve==2
(179 observations deleted)

. 
. gen quality=exp(_b[d8]+_b[b2]+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*h
> p_t/*
> */+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t)

. 
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\q
> uality_t,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qua
> lity_t.dta saved

. 
. gen nvalue=quality*quan_t

. egen aquan=mean(quan_t),by(year)

. egen anvalue=mean(nvalue), by(year)

. gen uquality=anvalue/aquan

. collapse (mean)uquality, by(year)

. list

     +-----------------+
     | year   uquality |
     |-----------------|
  1. |   79   4626.836 |
  2. |   80   4637.583 |
  3. |   81   4791.255 |
  4. |   82   4929.792 |
  5. |   83   4997.103 |
     |-----------------|
  6. |   84   5009.971 |
  7. |   85   5432.772 |
     +-----------------+

. restore

. 
. program drop _all

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\unit_quality.log
  log type:  text
 closed on:  19 Jun 2024, 23:03:38
----------------------------------------------------------------------------------

. exit

end of do-file

qindex_c.do

clear
capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qindex_c.log,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\quality_c,clear
keep year model type quality quan_c
ren quan_c quan

preserve
keep if year==80
keep model type quality quan
ren quality quality_80
ren quan quan_80
sort model
save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\quality_80_c,replace
restore

sort model
merge model using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\quality_80_c
keep if _merge==3
drop _merge

sort year model
egen value_80=sum(quality_80*quan_80), by(year)
egen value_cp=sum(quality*quan_80), by(year)
gen lasp=value_cp/value_80

egen value_c=sum(quality*quan),by(year)
egen value_cq=sum(quality_80*quan), by(year)
gen pasp=value_c/value_cq

gen qindex=(lasp*pasp)^0.5
collapse (mean) qindex, by(year)

list
log close

Output:

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\qindex_c.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\qindex_c.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\qindex_c.log
  log type:  text
 opened on:  19 Jun 2024, 23:06:51

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qu
> ality_c,clear

. keep year model type quality quan_c

. ren quan_c quan

. 
. preserve

. keep if year==80
(155 observations deleted)

. keep model type quality quan

. ren quality quality_80

. ren quan quan_80

. sort model

. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\q
> uality_80_c,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qua
> lity_80_c.dta saved

. restore

. 
. sort model

. merge model using Z:\home\pacha\github\advanced-international-trade\first-editio
> n\Chapter-8\quality_80_c
(note: you are using old merge syntax; see [R] merge for new syntax)
variable model does not uniquely identify observations in the master data

. keep if _merge==3
(23 observations deleted)

. drop _merge

. 
. sort year model

. egen value_80=sum(quality_80*quan_80), by(year)

. egen value_cp=sum(quality*quan_80), by(year)

. gen lasp=value_cp/value_80

. 
. egen value_c=sum(quality*quan),by(year)

. egen value_cq=sum(quality_80*quan), by(year)

. gen pasp=value_c/value_cq

. 
. gen qindex=(lasp*pasp)^0.5

. collapse (mean) qindex, by(year)

. 
. list

     +-----------------+
     | year     qindex |
     |-----------------|
  1. |   79    .986878 |
  2. |   80          1 |
  3. |   81   1.085813 |
  4. |   82    1.15183 |
  5. |   83   1.212675 |
     |-----------------|
  6. |   84   1.266379 |
  7. |   85   1.305744 |
     +-----------------+

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\qindex_c.log
  log type:  text
 closed on:  19 Jun 2024, 23:06:53
----------------------------------------------------------------------------------

. 
end of do-file

qindex_t.do

. clear

. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\qindex_t.log,replace
(note: file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\qindex_t.log not found)
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\qindex_t.log
  log type:  text
 opened on:  19 Jun 2024, 23:09:00

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qu
> ality_t,clear

. keep year model type quality quan_t

. ren quan_t quan

. 
. preserve

. keep if year==80
(65 observations deleted)

. keep model type quality quan

. ren quality quality_80

. ren quan quan_80

. sort model

. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\q
> uality_80_t,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\qua
> lity_80_t.dta saved

. restore

. 
. sort model

. merge model using Z:\home\pacha\github\advanced-international-trade\first-editio
> n\Chapter-8\quality_80_t
(note: you are using old merge syntax; see [R] merge for new syntax)
variable model does not uniquely identify observations in the master data

. keep if _merge==3
(14 observations deleted)

. drop _merge

. 
. sort year model

. egen value_80=sum(quality_80*quan_80), by(year)

. egen value_cp=sum(quality*quan_80), by(year)

. gen lasp=value_cp/value_80

. 
. egen value_c=sum(quality*quan),by(year)

. egen value_cq=sum(quality_80*quan), by(year)

. gen pasp=value_c/value_cq

. 
. gen qindex=(lasp*pasp)^0.5

. collapse (mean) qindex, by(year)

. 
. list

     +-----------------+
     | year     qindex |
     |-----------------|
  1. |   79   .9956951 |
  2. |   80          1 |
  3. |   81   1.035043 |
  4. |   82   1.058717 |
  5. |   83   1.057227 |
     |-----------------|
  6. |   84   1.053449 |
  7. |   85   1.160353 |
     +-----------------+

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\qindex_t.log
  log type:  text
 closed on:  19 Jun 2024, 23:09:01
----------------------------------------------------------------------------------

. 
end of do-file

My code

unit_quality.do

truck_7990 <- feenstra_88$truck_7990 %>%
  filter(year < 86) %>%
  mutate(ve = 2) %>%
  rename(
    quan_t = quan,
    wdth_t = wdth,
    hght_t = hght,
    wght_t = weight,
    hp_t = hp,
    four_t = four,
    tran_t = tran,
    ps_t = ps,
    ac_t = ac
  )

car_7990 <- feenstra_88$car_7990 %>%
  filter(year < 86) %>%
  mutate(ve = 1) %>%
  rename(
    quan_c = quan,
    wdth_c = wdth,
    hght_c = hght,
    wght_c = wght,
    hp_c = hp,
    four_c = four,
    tran_c = tran,
    ps_c = ps,
    ac_c = ac
  )

truck_7990 <- truck_7990 %>%
  bind_rows(car_7990) %>%
  mutate(
    wdth_c = ifelse(is.na(wdth_c), 0, wdth_c),
    hght_c = ifelse(is.na(hght_c), 0, hght_c),
    wght_c = ifelse(is.na(wght_c), 0, wght_c),
    hp_c = ifelse(is.na(hp_c), 0, hp_c),
    four_c = ifelse(is.na(four_c), 0, four_c),
    tran_c = ifelse(is.na(tran_c), 0, tran_c),
    ps_c = ifelse(is.na(ps_c), 0, ps_c),
    ac_c = ifelse(is.na(ac_c), 0, ac_c),
    wdth_t = ifelse(is.na(wdth_t), 0, wdth_t),
    hght_t = ifelse(is.na(hght_t), 0, hght_t),
    wght_t = ifelse(is.na(wght_t), 0, wght_t),
    hp_t = ifelse(is.na(hp_t), 0, hp_t),
    four_t = ifelse(is.na(four_t), 0, four_t),
    tran_t = ifelse(is.na(tran_t), 0, tran_t),
    ps_t = ifelse(is.na(ps_t), 0, ps_t),
    ac_t = ifelse(is.na(ac_t), 0, ac_t)
  )

truck_7990 %>%
  group_by(year) %>%
  summarise(freq = n()) %>%
  ungroup() %>%
  mutate(
    percent = freq / sum(freq) * 100,
    cum = cumsum(percent)
  )
# A tibble: 7 × 4
   year  freq percent   cum
  <dbl> <int>   <dbl> <dbl>
1    79    31    12.2  12.2
2    80    34    13.4  25.6
3    81    35    13.8  39.4
4    82    35    13.8  53.1
5    83    36    14.2  67.3
6    84    40    15.7  83.1
7    85    43    16.9 100  
truck_7990 <- truck_7990 %>%
  mutate(yd = as.factor(year - min(year) + 1))

truck_7990_dummy <- model.matrix(~ yd - 1, data = truck_7990)

truck_7990 <- truck_7990 %>%
  bind_cols(as.data.frame(truck_7990_dummy))

truck_7990 <- truck_7990 %>%
  mutate(
    cyd2 = ifelse(type == "JT", 0, yd2),
    cyd3 = ifelse(type == "JT", 0, yd3),
    cyd4 = ifelse(type == "JT", 0, yd4),
    cyd5 = ifelse(type == "JT", 0, yd5),
    cyd6 = ifelse(type == "JT", 0, yd6),
    cyd7 = ifelse(type == "JT", 0, yd7),
    tyd2 = ifelse(type != "JT", 0, yd2),
    tyd3 = ifelse(type != "JT", 0, yd3),
    tyd4 = ifelse(type != "JT", 0, yd4),
    tyd5 = ifelse(type != "JT", 0, yd5),
    tyd6 = ifelse(type != "JT", 0, yd6),
    tyd7 = ifelse(type != "JT", 0, yd7)
  )

initial_values <- list(
  a3 = 1, a4 = 1, a5 = 1, a6 = 1, a7 = 1,
  b2 = 0.1, b3 = 0.1, b4 = 0.1, b5 = 0.1, b6 = 0.1, b7 = 0.1,
  c1 = 0.1, c2 = 0.1, c3 = 0.1, c4 = 0.1, c5 = 0.1, c6 = 0.1, c7 = 0.1,
  c8 = 0.1, c9 = 0.1,
  d1 = 0.1, d2 = 0.1, d3 = 0.1, d4 = 0.1, d5 = 0.1, d6 = 0.1, d7 = 0.1,
  d8 = 0.1
)

fit <- nlsLM(
  price ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7 +
    exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 +
      b7 * cyd7 + c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c +
      c5 * tran_c + c6 * ps_c + c7 * ac_c + c8) +
    exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + (b4 + 0.16) * tyd4 +
      (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + c9 * tyd7 + d1 * wght_t +
      d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * ps_t +
      d7 * four_t + d8),
  data = truck_7990,
  start = initial_values
)

summary(fit)

Formula: price ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * 
    cyd7 + exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + 
    b6 * cyd6 + b7 * cyd7 + c1 * wght_c + c2 * wdth_c + c3 * 
    hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + c7 * ac_c + 
    c8) + exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + (b4 + 0.16) * 
    tyd4 + (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + c9 * tyd7 + 
    d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * 
    tran_t + d6 * ps_t + d7 * four_t + d8)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
a3  2.542e+02  3.171e+02   0.802 0.423598    
a4  5.052e+02  3.256e+02   1.552 0.122150    
a5  7.090e+02  3.173e+02   2.235 0.026415 *  
a6  1.082e+03  3.224e+02   3.356 0.000926 ***
a7  1.063e+03  3.739e+02   2.843 0.004876 ** 
b2  4.065e-03  3.290e-02   0.124 0.901779    
b3  7.559e-02  4.906e-02   1.541 0.124732    
b4  8.284e-02  4.904e-02   1.689 0.092555 .  
b5  3.276e-02  5.070e-02   0.646 0.518840    
b6  3.510e-02  5.147e-02   0.682 0.495934    
b7  8.254e-02  5.576e-02   1.480 0.140197    
c1  3.261e-02  1.117e-01   0.292 0.770715    
c2  3.860e-01  1.204e-01   3.205 0.001545 ** 
c3 -1.929e-01  5.683e-02  -3.395 0.000811 ***
c4  7.152e-01  7.362e-02   9.715  < 2e-16 ***
c5  1.579e-01  3.382e-02   4.668 5.21e-06 ***
c6  6.412e-02  2.471e-02   2.595 0.010082 *  
c7  1.830e-01  2.428e-02   7.535 1.16e-12 ***
c8  6.599e+00  7.307e-01   9.032  < 2e-16 ***
c9  1.172e-01  6.342e-02   1.848 0.065932 .  
d1  4.917e-01  7.958e-02   6.179 2.97e-09 ***
d2  2.621e-01  1.757e-01   1.492 0.137107    
d3 -4.513e-02  6.770e-02  -0.667 0.505752    
d4  3.031e-01  1.701e-01   1.782 0.076158 .  
d5  3.978e-02  3.627e-02   1.097 0.273899    
d6  9.676e-02  5.018e-02   1.928 0.055072 .  
d7  3.052e-01  7.156e-02   4.265 2.94e-05 ***
d8  6.300e+00  9.620e-01   6.549 3.85e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 666.9 on 226 degrees of freedom

Number of iterations to convergence: 30 
Achieved convergence tolerance: 1.49e-08
fit_coef <- coef(fit)

truck_7990 <- truck_7990 %>%
  mutate(
    winv = case_when(
      ve == 1 ~ 1 / exp(fit_coef["b2"] * cyd2 + fit_coef["b3"] * cyd3 +
        fit_coef["b4"] * cyd4 + fit_coef["b5"] * cyd5 +
        fit_coef["b6"] * cyd6 + fit_coef["b7"] * cyd7 +
        fit_coef["c1"] * wght_c + fit_coef["c2"] * wdth_c +
        fit_coef["c3"] * hght_c + fit_coef["c4"] * hp_c +
        fit_coef["c5"] * tran_c + fit_coef["c6"] * ps_c +
        fit_coef["c7"] * ac_c + fit_coef["c8"]),
      ve == 2 ~ 1 / exp((fit_coef["b2"] + 0.16) * tyd2 +
        (fit_coef["b3"] + 0.16) * tyd3 + (fit_coef["b4"] + 0.16) * tyd4 +
        (fit_coef["b5"] + 0.16) * tyd5 + (fit_coef["b6"] + 0.16) * tyd6 +
        fit_coef["c9"] * tyd7 + fit_coef["d1"] * wght_t +
        fit_coef["d2"] * wdth_t + fit_coef["d3"] * hght_t +
        fit_coef["d4"] * hp_t + fit_coef["d5"] * tran_t +
        fit_coef["d6"] * ps_t + fit_coef["d7"] * four_t + fit_coef["d8"])
    ),
    nprice = price * winv
  )

fit2 <- nlsLM(
  nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7) +
    winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 +
      b7 * cyd7 + c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c +
      c5 * tran_c + c6 * ps_c + c7 * ac_c + c8) +
    winv * exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + (b4 + 0.16) * tyd4 +
      (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + c9 * tyd7 + d1 * wght_t +
      d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * ps_t +
      d7 * four_t + d8),
  data = truck_7990,
  start = initial_values
)

summary(fit2)

Formula: nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + 
    a7 * cyd7) + winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + 
    b5 * cyd5 + b6 * cyd6 + b7 * cyd7 + c1 * wght_c + c2 * wdth_c + 
    c3 * hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + c7 * 
    ac_c + c8) + winv * exp(b2 * tyd2 + (b3 + 0.16) * tyd3 + 
    (b4 + 0.16) * tyd4 + (b5 + 0.16) * tyd5 + (b6 + 0.16) * tyd6 + 
    c9 * tyd7 + d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * 
    hp_t + d5 * tran_t + d6 * ps_t + d7 * four_t + d8)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
a3  3.679e+02  2.391e+02   1.539 0.125219    
a4  6.005e+02  2.543e+02   2.362 0.019041 *  
a5  8.879e+02  2.436e+02   3.645 0.000332 ***
a6  1.104e+03  2.511e+02   4.399 1.68e-05 ***
a7  8.556e+02  3.663e+02   2.336 0.020379 *  
b2  1.304e-02  3.031e-02   0.430 0.667488    
b3  6.745e-02  4.252e-02   1.586 0.114086    
b4  7.013e-02  4.419e-02   1.587 0.113886    
b5 -5.484e-04  4.708e-02  -0.012 0.990718    
b6  1.801e-02  4.740e-02   0.380 0.704268    
b7  1.008e-01  6.416e-02   1.571 0.117554    
c1  4.925e-02  1.311e-01   0.376 0.707523    
c2  3.908e-01  1.383e-01   2.825 0.005153 ** 
c3 -6.182e-02  6.114e-02  -1.011 0.313088    
c4  8.103e-01  1.038e-01   7.807 2.17e-13 ***
c5  1.779e-01  3.552e-02   5.009 1.10e-06 ***
c6  6.936e-02  3.049e-02   2.275 0.023864 *  
c7  1.567e-01  3.604e-02   4.348 2.08e-05 ***
c8  5.839e+00  8.619e-01   6.775 1.07e-10 ***
c9  1.060e-01  5.506e-02   1.925 0.055494 .  
d1  4.760e-01  1.172e-01   4.060 6.76e-05 ***
d2  2.121e-01  1.420e-01   1.493 0.136773    
d3 -2.704e-02  7.406e-02  -0.365 0.715380    
d4  2.443e-01  1.517e-01   1.611 0.108659    
d5  5.301e-02  3.562e-02   1.488 0.138112    
d6  5.751e-02  6.396e-02   0.899 0.369534    
d7  2.972e-01  5.670e-02   5.242 3.64e-07 ***
d8  6.630e+00  7.655e-01   8.661 8.98e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1144 on 226 degrees of freedom

Number of iterations to convergence: 31 
Achieved convergence tolerance: 1.49e-08
fit2_coef <- coef(fit2)
truck_7990_2 <- truck_7990 %>%
  filter(ve == 1) %>%
  mutate(
    quality = exp(fit2_coef["c8"] + fit2_coef["b2"] + fit2_coef["c1"] * wght_c +
      fit2_coef["c2"] * wdth_c + fit2_coef["c3"] * hght_c +
      fit2_coef["c4"] * hp_c + fit2_coef["c5"] * tran_c +
      fit2_coef["c6"] * ps_c + fit2_coef["c7"] * ac_c),
    nvalue = quality * quan_c
  )

car_uquality <- truck_7990_2 %>%
  group_by(year) %>%
  summarise(
    aquan = mean(quan_c),
    anvalue = mean(nvalue)
  ) %>%
  ungroup() %>%
  mutate(uquality = anvalue / aquan) %>%
  group_by(year) %>%
  summarise(mean_uquality = mean(uquality))

car_uquality
# A tibble: 7 × 2
   year mean_uquality
  <dbl>         <dbl>
1    79         4361.
2    80         4473.
3    81         4868.
4    82         5253.
5    83         5637.
6    84         5862.
7    85         6130.
truck_7990_3 <- truck_7990 %>%
  filter(ve == 2) %>%
  mutate(
    quality = exp(fit2_coef["d8"] + fit2_coef["b2"] + fit2_coef["d1"] * wght_t +
      fit2_coef["d2"] * wdth_t + fit2_coef["d3"] * hght_t +
      fit2_coef["d4"] * hp_t + fit2_coef["d5"] * tran_t +
      fit2_coef["d6"] * ps_t + fit2_coef["d7"] * four_t),
    nvalue = quality * quan_t
  )

truck_uquality <- truck_7990_3 %>%
  group_by(year) %>%
  summarise(
    aquan = mean(quan_t),
    anvalue = mean(nvalue)
  ) %>%
  ungroup() %>%
  mutate(uquality = anvalue / aquan) %>%
  group_by(year) %>%
  summarise(mean_uquality = mean(uquality))

truck_uquality
# A tibble: 7 × 2
   year mean_uquality
  <dbl>         <dbl>
1    79         4627.
2    80         4637.
3    81         4791.
4    82         4930.
5    83         4997.
6    84         5010.
7    85         5433.

qindex_c.do

car_7990_2_80 <- truck_7990_2 %>%
  filter(year == 80) %>%
  select(model, type, quality, quan_c) %>%
  rename(
    quality_80 = quality,
    quan_80 = quan_c
  )

car_qindex <- truck_7990_2 %>%
  select(year, model, type, quality, quan_c) %>%
  inner_join(car_7990_2_80, by = c("model", "type")) %>%
  group_by(year, model) %>%
  mutate(
    value_80 = sum(quality_80 * quan_80),
    value_cp = sum(quality * quan_80),
    lasp = value_cp / value_80
  ) %>%
  group_by(year) %>%
  mutate(
    value_c = sum(quality * quan_c),
    value_cq = sum(quality_80 * quan_c),
    pasp = value_c / value_cq,
    qindex = sqrt(lasp * pasp)
  ) %>%
  summarise(mean_qindex = mean(qindex))

car_qindex
# A tibble: 7 × 2
   year mean_qindex
  <dbl>       <dbl>
1    79       0.985
2    80       1    
3    81       1.09 
4    82       1.14 
5    83       1.20 
6    84       1.26 
7    85       1.33 

qindex_t.do

truck_7990_3_80 <- truck_7990_3 %>%
  filter(year == 80) %>%
  select(model, type, quality, quan_t) %>%
  rename(
    quality_80 = quality,
    quan_80 = quan_t
  )

truck_qindex <- truck_7990_3 %>%
  select(year, model, type, quality, quan_t) %>%
  inner_join(truck_7990_3_80, by = c("model", "type")) %>%
  group_by(year, model) %>%
  mutate(
    value_80 = sum(quality_80 * quan_80),
    value_tp = sum(quality * quan_80),
    lasp = value_tp / value_80
  ) %>%
  group_by(year) %>%
  mutate(
    value_t = sum(quality * quan_t),
    value_tq = sum(quality_80 * quan_t),
    pasp = value_t / value_tq,
    qindex = sqrt(lasp * pasp)
  ) %>%
  summarise(mean_qindex = mean(qindex))

truck_qindex
# A tibble: 7 × 2
   year mean_qindex
  <dbl>       <dbl>
1    79       0.983
2    80       1    
3    81       1.03 
4    82       1.06 
5    83       1.05 
6    84       1.07 
7    85       1.17 

Extra step: formatting the tables

# function to avoid code quadruplication
reshape_qindex <- function(data, mean_qindex_col, group, measurement) {
  data %>%
    rename(mean_qindex = !!mean_qindex_col) %>%
    mutate(
      qindex_pct = (mean_qindex - lag(mean_qindex)) / mean_qindex
    ) %>%
    pivot_longer(
      cols = c(mean_qindex, qindex_pct),
      names_to = "metric",
      values_to = "value"
    ) %>%
    pivot_wider(names_from = year, values_from = value) %>%
    mutate(type = group) %>%
    mutate(metric = c(measurement, paste(measurement, "(% Change)"))) %>%
    select(type, metric, everything()) %>%
    mutate_if(is.numeric, ~ round(., 3))
}

table83 <- reshape_qindex(car_uquality, "mean_uquality", "Japanese Cars", "Unit quality") %>%
  bind_rows(
    reshape_qindex(car_qindex, "mean_qindex", "Japanese Cars", "Quality index")
  ) %>%
  bind_rows(
    reshape_qindex(truck_uquality, "mean_uquality", "Japanese Trucks", "Unit quality")
  ) %>%
  bind_rows(
    reshape_qindex(truck_qindex, "mean_qindex", "Japanese Trucks", "Quality index")
  )

colnames(table83) <- c("Type", "Metric", paste0("19", 79:85))

kable(table83)
Type Metric 1979 1980 1981 1982 1983 1984 1985
Japanese Cars Unit quality 4361.162 4473.178 4867.702 5253.221 5637.037 5862.490 6130.408
Japanese Cars Unit quality (% Change) NA 0.025 0.081 0.073 0.068 0.038 0.044
Japanese Cars Quality index 0.985 1.000 1.085 1.144 1.204 1.263 1.328
Japanese Cars Quality index (% Change) NA 0.015 0.079 0.052 0.049 0.047 0.049
Japanese Trucks Unit quality 4626.649 4637.404 4791.084 4929.624 4996.917 5009.782 5432.585
Japanese Trucks Unit quality (% Change) NA 0.002 0.032 0.028 0.013 0.003 0.078
Japanese Trucks Quality index 0.983 1.000 1.032 1.056 1.054 1.068 1.167
Japanese Trucks Quality index (% Change) NA 0.017 0.031 0.023 -0.002 0.013 0.085

Exercise 5

Pooling car and truck data, run system_nocom.do and system_wcon.do to reproduce columns (1) - (4) in Table 8.5, with constraints specified in equation (8.22).

Feenstra’s code

system_nocom.do

In the original code:

gen cyd10=yd10
gen tyd10=yd10
replace cyd7=10 if type=="JT"
replace tyd7=10 if type~="JT"

gen cyd11=yd11
gen tyd11=yd11
replace cyd7=11 if type=="JT"
replace tyd7=11 if type~="JT"

gen cyd12=yd12
gen tyd12=yd12
replace cyd7=12 if type=="JT"
replace tyd7=12 if type~="JT"

Can be simplified to:

gen cyd10=yd10
gen tyd10=yd10

gen cyd11=yd11
gen tyd11=yd11

gen cyd12=yd12
gen tyd12=yd12

replace cyd7=12 if type=="JT"
replace tyd7=12 if type~="JT"

The original code is:

capture log close
log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\system_nocon.log,replace

set matsize 400

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_7990,clear
gen ve=2
rename wdth wdth_t
rename hght hght_t
rename weight wght_t
rename hp hp_t
rename four four_t
rename tran tran_t
rename ps ps_t
rename ac ac_t

save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_temp,replace

use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\car_7990,clear
gen ve=1
rename wdth wdth_c
rename hght hght_c
rename wght wght_c
rename hp hp_c
rename four four_c
rename tran tran_c
rename ps ps_c
rename ac ac_c

append using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\truck_temp

replace wdth_c=0 if wdth_c==.
replace hght_c=0 if hght_c==.
replace wght_c=0 if wght_c==.
replace hp_c=0 if hp_c==.
replace four_c=0 if four_c==.
replace tran_c=0 if tran_c==.
replace ps_c=0 if ps_c==.
replace ac_c=0 if ac_c==.
replace wdth_t=0 if wdth_t==.
replace hght_t=0 if hght_t==.
replace wght_t=0 if wght_t==.
replace hp_t=0 if hp_t==.
replace four_t=0 if four_t==.
replace tran_t=0 if tran_t==.
replace ps_t=0 if ps_t==.
replace ac_t=0 if ac_t==.

tab year, gen(yd)

gen cyd2=yd2
gen tyd2=yd2
replace cyd2=0 if type=="JT"
replace tyd2=0 if type~="JT"

gen cyd3=yd3
gen tyd3=yd3
replace cyd3=0 if type=="JT"
replace tyd3=0 if type~="JT"

gen cyd4=yd4
gen tyd4=yd4
replace cyd4=0 if type=="JT"
replace tyd4=0 if type~="JT"

gen cyd5=yd5
gen tyd5=yd5
replace cyd5=0 if type=="JT"
replace tyd5=0 if type~="JT"

gen cyd6=yd6
gen tyd6=yd6
replace cyd6=0 if type=="JT"
replace tyd6=0 if type~="JT"

gen cyd7=yd7
gen tyd7=yd7
replace cyd7=0 if type=="JT"
replace tyd7=0 if type~="JT"

gen cyd8=yd8
gen tyd8=yd8
replace cyd8=0 if type=="JT"
replace tyd8=0 if type~="JT"

gen cyd9=yd9
gen tyd9=yd9
replace cyd9=0 if type=="JT"
replace tyd9=0 if type~="JT"

gen cyd10=yd10
gen tyd10=yd10
replace cyd7=10 if type=="JT"
replace tyd7=10 if type~="JT"

gen cyd11=yd11
gen tyd11=yd11
replace cyd7=11 if type=="JT"
replace tyd7=11 if type~="JT"

gen cyd12=yd12
gen tyd12=yd12
replace cyd7=12 if type=="JT"
replace tyd7=12 if type~="JT"

program define nlct_1
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 c1 c2 c3 c4 c5 c6 c7 c8 d1 d2 d3 d4 d5 d6 d7 d8 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global a8=1
global a9=1
global a10=1
global a11=1
global a12=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global b8=.1
global b9=.1
global b10=.1
global b11=.1
global b12=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
global d1=.1
global d2=.1
global d3=.1
global d4=.1
global d5=.1
global d6=.1
global d7=.1
global d8=.1
global e2=.1
global e3=.1
global e4=.1
global e5=.1
global e6=.1
global e7=.1
global e8=.1
global e9=.1
global e10=.1
global e11=.1
global e12=.1
exit
}

replace `1'=$a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
*/+$a8*cyd8+$a9*cyd9+$a10*cyd10+$a11*cyd11+$a12*cyd12/*
*/+exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
*/+$b8*cyd8+$b9*cyd9+$b10*cyd10+$b11*cyd11+$b12*cyd12/*
*/+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
*/+$c8)+exp($e2*tyd2+$e3*tyd3+$e4*tyd4+$e5*tyd5+$e6*tyd6+$e7*tyd7/*
*/+$e8*tyd8+$e9*tyd9+$e10*tyd10+$e11*tyd11+$e12*tyd12/*
*/+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)

end

nl ct_1 price

gen winv=1/exp(_b[b2]*cyd2+_b[b3]*cyd3+_b[b4]*cyd4+_b[b5]*cyd5+_b[b6]*cyd6+_b[b7]*cyd7/*
*/+_b[b8]*cyd8+_b[b9]*cyd9+_b[b10]*cyd10+_b[b11]*cyd11+_b[b12]*cyd12/*
*/+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c+_b[c5]*tran_c+_b[c6]*ps_c/*
*/+_b[c7]*ac_c+_b[c8])

replace winv=1/exp(_b[e2]*tyd2+_b[e3]*tyd3+_b[e4]*tyd4+_b[e5]*tyd5+_b[e6]*tyd6/*
*/+_b[e7]*tyd7+_b[e8]*tyd8+_b[e9]*tyd9+_b[e10]*tyd10+_b[e11]*tyd11+_b[e12]*tyd12/*
*/+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
*/+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t+_b[d8]) if ve==2

gen nprice=price*winv


program define nlct_2
version 7.0
if "`1'"=="?"{
global S_1 "a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 c1 c2 c3 c4 c5 c6 c7 c8 d1 d2 d3 d4 d5 d6 d7 d8 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12"
global a3=1
global a4=1
global a5=1
global a6=1
global a7=1
global a8=1
global a9=1
global a10=1
global a11=1
global a12=1
global b2=.1
global b3=.1
global b4=.1
global b5=.1
global b6=.1
global b7=.1
global b8=.1
global b9=.1
global b10=.1
global b11=.1
global b12=.1
global c1=.1
global c2=.1
global c3=.1
global c4=.1
global c5=.1
global c6=.1
global c7=.1
global c8=.1
global d1=.1
global d2=.1
global d3=.1
global d4=.1
global d5=.1
global d6=.1
global d7=.1
global d8=.1
global e2=.1
global e3=.1
global e4=.1
global e5=.1
global e6=.1
global e7=.1
global e8=.1
global e9=.1
global e10=.1
global e11=.1
global e12=.1
exit
}

replace `1'=winv*($a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
*/+$a8*cyd8+$a9*cyd9+$a10*cyd10+$a11*cyd11+$a12*cyd12)/*
*/+winv*exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
*/+$b8*cyd8+$b9*cyd9+$b10*cyd10+$b11*cyd11+$b12*cyd12/*
*/+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
*/+$c8)+winv*exp($e2*tyd2+$e3*tyd3+$e4*tyd4+$e5*tyd5+$e6*tyd6+$e7*tyd7/*
*/+$e8*tyd8+$e9*tyd9+$e10*tyd10+$e11*tyd11+$e12*tyd12/*
*/+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)

end

nl ct_2 nprice

program drop _all
log close
exit

Output:


. capture log close

. log using Z:\home\pacha\github\advanced-international-trade\first-edition\Chapte
> r-8\system_nocon.log,replace
----------------------------------------------------------------------------------
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\system_nocon.log
  log type:  text
 opened on:   6 Jul 2024, 23:01:56

. 
. set matsize 400

Current memory allocation

                    current                                 memory usage
    settable          value     description                 (1M = 1024k)
    --------------------------------------------------------------------
    set maxvar         5000     max. variables allowed           1.909M
    set memory           50M    max. data space                 50.000M
    set matsize         400     max. RHS vars in models          1.254M
                                                            -----------
                                                                53.163M

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tr
> uck_7990,clear

. gen ve=2

. rename wdth wdth_t

. rename hght hght_t

. rename weight wght_t

. rename hp hp_t

. rename four four_t

. rename tran tran_t

. rename ps ps_t

. rename ac ac_t

. 
. save Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\t
> ruck_temp,replace
file Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\tru
> ck_temp.dta saved

. 
. use Z:\home\pacha\github\advanced-international-trade\first-edition\Chapter-8\ca
> r_7990,clear

. gen ve=1

. rename wdth wdth_c

. rename hght hght_c

. rename wght wght_c

. rename hp hp_c

. rename four four_c

. rename tran tran_c

. rename ps ps_c

. rename ac ac_c

. 
. append using Z:\home\pacha\github\advanced-international-trade\first-edition\Cha
> pter-8\truck_temp

. 
. replace wdth_c=0 if wdth_c==.
(120 real changes made)

. replace hght_c=0 if hght_c==.
(120 real changes made)

. replace wght_c=0 if wght_c==.
(120 real changes made)

. replace hp_c=0 if hp_c==.
(120 real changes made)

. replace four_c=0 if four_c==.
(120 real changes made)

. replace tran_c=0 if tran_c==.
(120 real changes made)

. replace ps_c=0 if ps_c==.
(120 real changes made)

. replace ac_c=0 if ac_c==.
(120 real changes made)

. replace wdth_t=0 if wdth_t==.
(284 real changes made)

. replace hght_t=0 if hght_t==.
(284 real changes made)

. replace wght_t=0 if wght_t==.
(284 real changes made)

. replace hp_t=0 if hp_t==.
(284 real changes made)

. replace four_t=0 if four_t==.
(284 real changes made)

. replace tran_t=0 if tran_t==.
(284 real changes made)

. replace ps_t=0 if ps_t==.
(284 real changes made)

. replace ac_t=0 if ac_t==.
(284 real changes made)

. 
. tab year, gen(yd)

       year |      Freq.     Percent        Cum.
------------+-----------------------------------
         79 |         31        7.67        7.67
         80 |         34        8.42       16.09
         81 |         35        8.66       24.75
         82 |         35        8.66       33.42
         83 |         36        8.91       42.33
         84 |         40        9.90       52.23
         85 |         43       10.64       62.87
         86 |         33        8.17       71.04
         87 |         35        8.66       79.70
         88 |         30        7.43       87.13
         89 |         26        6.44       93.56
         90 |         26        6.44      100.00
------------+-----------------------------------
      Total |        404      100.00

. 
. gen cyd2=yd2

. gen tyd2=yd2

. replace cyd2=0 if type=="JT"
(8 real changes made)

. replace tyd2=0 if type~="JT"
(26 real changes made)

. 
. gen cyd3=yd3

. gen tyd3=yd3

. replace cyd3=0 if type=="JT"
(9 real changes made)

. replace tyd3=0 if type~="JT"
(26 real changes made)

. 
. gen cyd4=yd4

. gen tyd4=yd4

. replace cyd4=0 if type=="JT"
(9 real changes made)

. replace tyd4=0 if type~="JT"
(26 real changes made)

. 
. gen cyd5=yd5

. gen tyd5=yd5

. replace cyd5=0 if type=="JT"
(7 real changes made)

. replace tyd5=0 if type~="JT"
(29 real changes made)

. 
. gen cyd6=yd6

. gen tyd6=yd6

. replace cyd6=0 if type=="JT"
(7 real changes made)

. replace tyd6=0 if type~="JT"
(33 real changes made)

. 
. gen cyd7=yd7

. gen tyd7=yd7

. replace cyd7=0 if type=="JT"
(7 real changes made)

. replace tyd7=0 if type~="JT"
(36 real changes made)

. 
. gen cyd8=yd8

. gen tyd8=yd8

. replace cyd8=0 if type=="JT"
(7 real changes made)

. replace tyd8=0 if type~="JT"
(26 real changes made)

. 
. gen cyd9=yd9

. gen tyd9=yd9

. replace cyd9=0 if type=="JT"
(6 real changes made)

. replace tyd9=0 if type~="JT"
(29 real changes made)

. 
. gen cyd10=yd10

. gen tyd10=yd10

. replace cyd7=10 if type=="JT"
(84 real changes made)

. replace tyd7=10 if type~="JT"
(320 real changes made)

. 
. gen cyd11=yd11

. gen tyd11=yd11

. replace cyd7=11 if type=="JT"
(84 real changes made)

. replace tyd7=11 if type~="JT"
(320 real changes made)

. 
. gen cyd12=yd12

. gen tyd12=yd12

. replace cyd7=12 if type=="JT"
(84 real changes made)

. replace tyd7=12 if type~="JT"
(320 real changes made)

. 
. program define nlct_1
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 
> b12 c1 c2 c3 c4 c5 c6 c7 c8 d1 d2 d3 d4 d5 d6 d7 d8 e2 e3 e4 e5 e6 e7 e8 e9 e10 
> e11 e12"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global a8=1
 10. global a9=1
 11. global a10=1
 12. global a11=1
 13. global a12=1
 14. global b2=.1
 15. global b3=.1
 16. global b4=.1
 17. global b5=.1
 18. global b6=.1
 19. global b7=.1
 20. global b8=.1
 21. global b9=.1
 22. global b10=.1
 23. global b11=.1
 24. global b12=.1
 25. global c1=.1
 26. global c2=.1
 27. global c3=.1
 28. global c4=.1
 29. global c5=.1
 30. global c6=.1
 31. global c7=.1
 32. global c8=.1
 33. global d1=.1
 34. global d2=.1
 35. global d3=.1
 36. global d4=.1
 37. global d5=.1
 38. global d6=.1
 39. global d7=.1
 40. global d8=.1
 41. global e2=.1
 42. global e3=.1
 43. global e4=.1
 44. global e5=.1
 45. global e6=.1
 46. global e7=.1
 47. global e8=.1
 48. global e9=.1
 49. global e10=.1
 50. global e11=.1
 51. global e12=.1
 52. exit
 53. }
 54. 
. replace `1'=$a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
> */+$a8*cyd8+$a9*cyd9+$a10*cyd10+$a11*cyd11+$a12*cyd12/*
> */+exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
> */+$b8*cyd8+$b9*cyd9+$b10*cyd10+$b11*cyd11+$b12*cyd12/*
> */+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
> */+$c8)+exp($e2*tyd2+$e3*tyd3+$e4*tyd4+$e5*tyd5+$e6*tyd6+$e7*tyd7/*
> */+$e8*tyd8+$e9*tyd9+$e10*tyd10+$e11*tyd11+$e12*tyd12/*
> */+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)
 55. 
. end

. 
. nl ct_1 price
(obs = 404)

Iteration 0:   residual SS =  3.68e+10
...
Iteration 24:  residual SS =  3.13e+08
Iteration 25:  residual SS =  3.13e+08

      Source |       SS       df       MS            Number of obs =       404
-------------+------------------------------         F( 46,   357) =    147.07
       Model |  5.9297e+09    46   128906902         Prob > F      =    0.0000
    Residual |   312918865   357  876523.431         R-squared     =    0.9499
-------------+------------------------------         Adj R-squared =    0.9434
       Total |  6.2426e+09   403  15490412.8         Root MSE      =  936.2283
                                                     Res. dev.     =  6624.758
(ct_1)
------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   277.0869   551.2618     0.50   0.616    -807.0417    1361.215
          a4 |   21.66828   535.0463     0.04   0.968    -1030.571    1073.907
          a5 |   147.2889   494.2067     0.30   0.766    -824.6333    1119.211
          a6 |    556.957   484.3219     1.15   0.251    -395.5256     1509.44
          a7 |  -7.057291   101.5543    -0.07   0.945    -206.7772    192.6626
          a8 |   771.0952   521.6698     1.48   0.140     -254.837    1797.027
          a9 |    1191.93   517.0276     2.31   0.022     175.1272    2208.733
         a10 |   657.8229   728.4691     0.90   0.367    -774.8072    2090.453
         a11 |  -450.8028   817.0406    -0.55   0.581     -2057.62    1156.015
         a12 |  -433.4064   772.5761    -0.56   0.575    -1952.779    1085.966
          b2 |  -.0841009     .04192    -2.01   0.046    -.1665421   -.0016597
          b3 |  -.0056866   .0864787    -0.07   0.948    -.1757583    .1643851
          b4 |   .0872793   .0747481     1.17   0.244    -.0597227    .2342813
          b5 |   .0666161   .0692594     0.96   0.337    -.0695916    .2028239
          b6 |   .0749652   .0668452     1.12   0.263    -.0564948    .2064251
          b7 |   .1526226   .0314581     4.85   0.000     .0907562    .2144891
          b8 |   .1437711    .065557     2.19   0.029     .0148446    .2726975
          b9 |   .1988908    .062616     3.18   0.002     .0757482    .3220335
         b10 |   .2857005   .0719646     3.97   0.000     .1441727    .4272282
         b11 |   .4104976   .0690287     5.95   0.000     .2747436    .5462515
         b12 |   .3679665   .0684655     5.37   0.000     .2333201    .5026129
          c1 |   .4662111   .0865907     5.38   0.000     .2959191    .6365031
          c2 |   .5300141   .0640388     8.28   0.000     .4040735    .6559547
          c3 |  -.3625191   .0443666    -8.17   0.000    -.4497719   -.2752663
          c4 |   .2502107   .0387283     6.46   0.000     .1740464     .326375
          c5 |    .063748   .0203705     3.13   0.002     .0236867    .1038093
          c6 |    .108309   .0205511     5.27   0.000     .0678925    .1487255
          c7 |   .1667295   .0205709     8.11   0.000     .1262742    .2071848
          c8 |   6.664738   .3769454    17.68   0.000     5.923426    7.406051
          d1 |   .4827909   .0948538     5.09   0.000     .2962486    .6693333
          d2 |   .6237576   .3207663     1.94   0.053    -.0070714    1.254587
          d3 |  -.1758063   .0807852    -2.18   0.030    -.3346811   -.0169316
          d4 |   .4714717   .1961516     2.40   0.017     .0857138    .8572296
          d5 |   .0368238   .0705556     0.52   0.602     -.101933    .1755805
          d6 |   .0595696    .060131     0.99   0.323    -.0586858     .177825
          d7 |    .184348   .0926225     1.99   0.047     .0021937    .3665023
          d8 |   2.773037   1.807071     1.53   0.126    -.7808051     6.32688
          e2 |  -2.59e+09          .        .       .            .           .
          e3 |   1.002811   .5449131     1.84   0.067    -.0688323    2.074454
          e4 |   1.089538   .5484653     1.99   0.048     .0109092    2.168167
          e5 |   .7809413   .5636633     1.39   0.167    -.3275767    1.889459
          e6 |   .8176126   .5623188     1.45   0.147    -.2882611    1.923486
          e7 |   .1963533   .0542407     3.62   0.000     .0896819    .3030248
          e8 |   .9909852   .5576036     1.78   0.076    -.1056155    2.087586
          e9 |   1.011894   .5669185     1.78   0.075    -.1030257    2.126813
         e10 |   .0766676   .1003499     0.76   0.445    -.1206836    .2740188
         e11 |   .3305375   .0754574     4.38   0.000     .1821406    .4789343
         e12 |   .4674833   .0740926     6.31   0.000     .3217704    .6131961
------------------------------------------------------------------------------
* Parameter e2 taken as constant term in model & ANOVA table
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. gen winv=1/exp(_b[b2]*cyd2+_b[b3]*cyd3+_b[b4]*cyd4+_b[b5]*cyd5+_b[b6]*cyd6+_b[b7
> ]*cyd7/*
> */+_b[b8]*cyd8+_b[b9]*cyd9+_b[b10]*cyd10+_b[b11]*cyd11+_b[b12]*cyd12/*
> */+_b[c1]*wght_c+_b[c2]*wdth_c+_b[c3]*hght_c+_b[c4]*hp_c+_b[c5]*tran_c+_b[c6]*ps
> _c/*
> */+_b[c7]*ac_c+_b[c8])

. 
. replace winv=1/exp(_b[e2]*tyd2+_b[e3]*tyd3+_b[e4]*tyd4+_b[e5]*tyd5+_b[e6]*tyd6/*
> */+_b[e7]*tyd7+_b[e8]*tyd8+_b[e9]*tyd9+_b[e10]*tyd10+_b[e11]*tyd11+_b[e12]*tyd12
> /*
> */+_b[d1]*wght_t+_b[d2]*wdth_t+_b[d3]*hght_t+_b[d4]*hp_t/*
> */+_b[d5]*tran_t+_b[d6]*ps_t+_b[d7]*four_t+_b[d8]) if ve==2
(120 real changes made, 8 to missing)

. 
. gen nprice=price*winv
(8 missing values generated)

. 
. 
. program define nlct_2
  1. version 7.0
  2. if "`1'"=="?"{
  3. global S_1 "a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 
> b12 c1 c2 c3 c4 c5 c6 c7 c8 d1 d2 d3 d4 d5 d6 d7 d8 e2 e3 e4 e5 e6 e7 e8 e9 e10 
> e11 e12"
  4. global a3=1
  5. global a4=1
  6. global a5=1
  7. global a6=1
  8. global a7=1
  9. global a8=1
 10. global a9=1
 11. global a10=1
 12. global a11=1
 13. global a12=1
 14. global b2=.1
 15. global b3=.1
 16. global b4=.1
 17. global b5=.1
 18. global b6=.1
 19. global b7=.1
 20. global b8=.1
 21. global b9=.1
 22. global b10=.1
 23. global b11=.1
 24. global b12=.1
 25. global c1=.1
 26. global c2=.1
 27. global c3=.1
 28. global c4=.1
 29. global c5=.1
 30. global c6=.1
 31. global c7=.1
 32. global c8=.1
 33. global d1=.1
 34. global d2=.1
 35. global d3=.1
 36. global d4=.1
 37. global d5=.1
 38. global d6=.1
 39. global d7=.1
 40. global d8=.1
 41. global e2=.1
 42. global e3=.1
 43. global e4=.1
 44. global e5=.1
 45. global e6=.1
 46. global e7=.1
 47. global e8=.1
 48. global e9=.1
 49. global e10=.1
 50. global e11=.1
 51. global e12=.1
 52. exit
 53. }
 54. 
. replace `1'=winv*($a3*cyd3+$a4*cyd4+$a5*cyd5+$a6*cyd6+$a7*cyd7/*
> */+$a8*cyd8+$a9*cyd9+$a10*cyd10+$a11*cyd11+$a12*cyd12)/*
> */+winv*exp($b2*cyd2+$b3*cyd3+$b4*cyd4+$b5*cyd5+$b6*cyd6+$b7*cyd7/*
> */+$b8*cyd8+$b9*cyd9+$b10*cyd10+$b11*cyd11+$b12*cyd12/*
> */+$c1*wght_c+$c2*wdth_c+$c3*hght_c+$c4*hp_c+$c5*tran_c+$c6*ps_c+$c7*ac_c/*
> */+$c8)+winv*exp($e2*tyd2+$e3*tyd3+$e4*tyd4+$e5*tyd5+$e6*tyd6+$e7*tyd7/*
> */+$e8*tyd8+$e9*tyd9+$e10*tyd10+$e11*tyd11+$e12*tyd12/*
> */+$d1*wght_t+$d2*wdth_t+$d3*hght_t+$d4*hp_t+$d5*tran_t+$d6*ps_t+$d7*four_t+$d8)
 55. 
. end

. 
. nl ct_2 nprice
(obs = 396)

Iteration 0:   residual SS =  3738.817
...
Iteration 32:  residual SS =  15.11498
Iteration 33:  residual SS =  15.11498

      Source |       SS       df       MS            Number of obs =       396
-------------+------------------------------         F( 46,   349) =   1037.51
       Model |  2066.95212    46  44.9337418         Prob > F      =    0.0000
    Residual |  15.1149771   349   .04330939         R-squared     =    0.9927
-------------+------------------------------         Adj R-squared =    0.9918
       Total |   2082.0671   395  5.27105594         Root MSE      =  .2081091
                                                     Res. dev.     =  -169.429
(ct_2)
------------------------------------------------------------------------------
      nprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          a3 |   165.6435   524.1069     0.32   0.752    -865.1618    1196.449
          a4 |   255.2019   575.0953     0.44   0.657    -875.8867     1386.29
          a5 |    382.162   544.9258     0.70   0.484    -689.5897    1453.914
          a6 |    561.334   534.9376     1.05   0.295    -490.7731    1613.441
          a7 |    32.3127   99.45926     0.32   0.745    -163.3022    227.9276
          a8 |    427.688   694.9132     0.62   0.539    -939.0566    1794.433
          a9 |   1338.024   667.4732     2.00   0.046     25.24802      2650.8
         a10 |   1354.805   1272.661     1.06   0.288    -1148.245    3857.854
         a11 |   3732.851    1148.03     3.25   0.001     1474.923    5990.778
         a12 |   4310.495   998.1988     4.32   0.000     2347.253    6273.737
          b2 |   -.053796    .124424    -0.43   0.666    -.2985112    .1909192
          b3 |   .1024193   .1947552     0.53   0.599    -.2806223    .4854609
          b4 |   .1508038   .1964602     0.77   0.443    -.2355911    .5371987
          b5 |    .104406   .1879434     0.56   0.579    -.2652381      .47405
          b6 |    .161327   .1825493     0.88   0.377    -.1977082    .5203622
          b7 |    .299865   .1270196     2.36   0.019     .0500448    .5496851
          b8 |   .4180167   .1963952     2.13   0.034     .0317497    .8042836
          b9 |   .4275378    .190593     2.24   0.026     .0526824    .8023932
         b10 |    .473833   .1983748     2.39   0.017     .0836725    .8639936
         b11 |   .3078561    .206895     1.49   0.138    -.0990619    .7147741
         b12 |   .1790811   .2035596     0.88   0.380    -.2212767     .579439
          c1 |   .6665196   .3118486     2.14   0.033     .0531805    1.279859
          c2 |   1.066573   .3592669     2.97   0.003     .3599729    1.773174
          c3 |  -.3333017   .1511001    -2.21   0.028    -.6304831   -.0361203
          c4 |   .4277256   .1382984     3.09   0.002     .1557224    .6997287
          c5 |   .2157146   .0848215     2.54   0.011     .0488889    .3825402
          c6 |   .1948046   .0696014     2.80   0.005     .0579136    .3316957
          c7 |   .1545815   .0715104     2.16   0.031      .013936     .295227
          c8 |   2.456385    2.09751     1.17   0.242    -1.668965    6.581735
          d1 |   .5464099   .0950598     5.75   0.000     .3594477    .7333721
          d2 |   .0486571   .0432501     1.13   0.261    -.0364066    .1337208
          d3 |  -.0695537   .0449695    -1.55   0.123     -.157999    .0188915
          d4 |   .3998404   .0759349     5.27   0.000     .2504929    .5491879
          d5 |   .0875427   .0184166     4.75   0.000     .0513213    .1237641
          d6 |    .039679    .075099     0.53   0.598    -.1080246    .1873827
          d7 |  -.0740434   .1046904    -0.71   0.480    -.2799468      .13186
          d8 |   7.377294   .2976717    24.78   0.000     6.791838     7.96275
          e2 |         .1          .        .       .            .           .
          e3 |   .2699934   .0481155     5.61   0.000     .1753605    .3646263
          e4 |   .2654374    .048439     5.48   0.000     .1701684    .3607065
          e5 |   .1633878    .033666     4.85   0.000      .097174    .2296016
          e6 |   .1719965   .0352488     4.88   0.000     .1026697    .2413233
          e7 |   .0506695    .015178     3.34   0.001     .0208177    .0805213
          e8 |   .1550894   .0392545     3.95   0.000     .0778843    .2322944
          e9 |   .1318401   .0385509     3.42   0.001     .0560188    .2076615
         e10 |   -.047726   .2001646    -0.24   0.812    -.4414067    .3459547
         e11 |  -.5569037   .3050303    -1.83   0.069    -1.156833    .0430251
         e12 |  -.6348534    .314223    -2.02   0.044    -1.252862   -.0168445
------------------------------------------------------------------------------
* Parameter e2 taken as constant term in model & ANOVA table
 (SEs, P values, CIs, and correlations are asymptotic approximations)

. 
. program drop _all

. log close
      name:  <unnamed>
       log:  Z:\home\pacha\github\advanced-international-trade\first-edition\Chapt
> er-8\system_nocon.log
  log type:  text
 closed on:   6 Jul 2024, 23:02:18
----------------------------------------------------------------------------------

. exit

end of do-file

My code

system_nocom.do

truck_7990 <- feenstra_88$truck_7990 %>%
  mutate(ve = 2) %>%
  rename(
    wdth_t = wdth, hght_t = hght, wght_t = weight, hp_t = hp, four_t = four,
    tran_t = tran, ps_t = ps, ac_t = ac
  )

car_7990 <- feenstra_88$car_7990 %>%
  mutate(ve = 1) %>%
  rename(
    wdth_c = wdth, hght_c = hght, wght_c = wght, hp_c = hp, four_c = four,
    tran_c = tran, ps_c = ps, ac_c = ac
  )

truck_7990 <- truck_7990 %>%
  bind_rows(car_7990) %>%
  mutate(
    wdth_c = ifelse(is.na(wdth_c), 0, wdth_c),
    hght_c = ifelse(is.na(hght_c), 0, hght_c),
    wght_c = ifelse(is.na(wght_c), 0, wght_c),
    hp_c = ifelse(is.na(hp_c), 0, hp_c),
    four_c = ifelse(is.na(four_c), 0, four_c),
    tran_c = ifelse(is.na(tran_c), 0, tran_c),
    ps_c = ifelse(is.na(ps_c), 0, ps_c),
    ac_c = ifelse(is.na(ac_c), 0, ac_c),
    wdth_t = ifelse(is.na(wdth_t), 0, wdth_t),
    hght_t = ifelse(is.na(hght_t), 0, hght_t),
    wght_t = ifelse(is.na(wght_t), 0, wght_t),
    hp_t = ifelse(is.na(hp_t), 0, hp_t),
    four_t = ifelse(is.na(four_t), 0, four_t),
    tran_t = ifelse(is.na(tran_t), 0, tran_t),
    ps_t = ifelse(is.na(ps_t), 0, ps_t),
    ac_t = ifelse(is.na(ac_t), 0, ac_t)
  )

truck_7990 %>%
  group_by(year) %>%
  summarise(freq = n()) %>%
  ungroup() %>%
  mutate(
    percent = freq / sum(freq) * 100,
    cum = cumsum(percent)
  )
# A tibble: 12 × 4
    year  freq percent    cum
   <dbl> <int>   <dbl>  <dbl>
 1    79    31    7.67   7.67
 2    80    34    8.42  16.1 
 3    81    35    8.66  24.8 
 4    82    35    8.66  33.4 
 5    83    36    8.91  42.3 
 6    84    40    9.90  52.2 
 7    85    43   10.6   62.9 
 8    86    33    8.17  71.0 
 9    87    35    8.66  79.7 
10    88    30    7.43  87.1 
11    89    26    6.44  93.6 
12    90    26    6.44 100   
truck_7990 <- truck_7990 %>%
  mutate(yd = as.factor(year - min(year) + 1))

truck_7990_dummy <- model.matrix(~ yd - 1, data = truck_7990)

truck_7990 <- truck_7990 %>%
  bind_cols(as.data.frame(truck_7990_dummy))

truck_7990 <- truck_7990 %>%
  mutate(
    cyd2 = ifelse(type == "JT", 0, yd2),
    cyd3 = ifelse(type == "JT", 0, yd3),
    cyd4 = ifelse(type == "JT", 0, yd4),
    cyd5 = ifelse(type == "JT", 0, yd5),
    cyd6 = ifelse(type == "JT", 0, yd6),
    cyd7 = ifelse(type == "JT", 12, yd7),
    cyd8 = ifelse(type == "JT", 0, yd8),
    cyd9 = ifelse(type == "JT", 0, yd9),
    cyd10 = yd10,
    cyd11 = yd11,
    cyd12 = yd12,
    tyd2 = ifelse(type != "JT", 0, yd2),
    tyd3 = ifelse(type != "JT", 0, yd3),
    tyd4 = ifelse(type != "JT", 0, yd4),
    tyd5 = ifelse(type != "JT", 0, yd5),
    tyd6 = ifelse(type != "JT", 0, yd6),
    tyd7 = ifelse(type != "JT", 12, yd7),
    tyd8 = ifelse(type != "JT", 0, yd8),
    tyd9 = ifelse(type != "JT", 0, yd9),
    tyd10 = yd10,
    tyd11 = yd11,
    tyd12 = yd12
  )
initial_values <- list(
  a3 = 1, a4 = 1, a5 = 1, a6 = 1, a7 = 1, a8 = 1, a9 = 1, a10 = 1, a11 = 1,
  a12 = 1,
  b2 = 0.1, b3 = 0.1, b4 = 0.1, b5 = 0.1, b6 = 0.1, b7 = 0.1, b8 = 0.1,
  b9 = 0.1, b10 = 0.1, b11 = 0.1, b12 = 0.1,
  c1 = 0.1, c2 = 0.1, c3 = 0.1, c4 = 0.1, c5 = 0.1, c6 = 0.1, c7 = 0.1,
  c8 = 0.1,
  d1 = 0.1, d2 = 0.1, d3 = 0.1, d4 = 0.1, d5 = 0.1, d6 = 0.1, d7 = 0.1,
  d8 = 0.1,
  e2 = 0.1, e3 = 0.1, e4 = 0.1, e5 = 0.1, e6 = 0.1, e7 = 0.1, e8 = 0.1,
  e9 = 0.1, e10 = 0.1, e11 = 0.1, e12 = 0.1
)

fit <- nlsLM(
  price ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7 +
    a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + a12 * cyd12 +
    exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 + b7 * cyd7 +
      b8 * cyd8 + b9 * cyd9 + b10 * cyd10 + b11 * cyd11 + b12 * cyd12 +
      c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c + c5 * tran_c +
      c6 * ps_c + c7 * ac_c + c8) +
    exp(e2 * tyd2 + e3 * tyd3 + e4 * tyd4 + e5 * tyd5 + e6 * tyd6 + e7 * tyd7 +
      e8 * tyd8 + e9 * tyd9 + e10 * tyd10 + e11 * tyd11 + e12 * tyd12 +
      d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t +
      d6 * ps_t + d7 * four_t + d8),
  data = truck_7990,
  start = initial_values,
  # adjust parameters to account to slow convergence
  control = nls.lm.control(maxiter = 1000, maxfev = 10000)
)

summary(fit)

Formula: price ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * 
    cyd7 + a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + 
    a12 * cyd12 + exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * 
    cyd5 + b6 * cyd6 + b7 * cyd7 + b8 * cyd8 + b9 * cyd9 + b10 * 
    cyd10 + b11 * cyd11 + b12 * cyd12 + c1 * wght_c + c2 * wdth_c + 
    c3 * hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + c7 * 
    ac_c + c8) + exp(e2 * tyd2 + e3 * tyd3 + e4 * tyd4 + e5 * 
    tyd5 + e6 * tyd6 + e7 * tyd7 + e8 * tyd8 + e9 * tyd9 + e10 * 
    tyd10 + e11 * tyd11 + e12 * tyd12 + d1 * wght_t + d2 * wdth_t + 
    d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * ps_t + d7 * 
    four_t + d8)

Parameters:
      Estimate Std. Error t value Pr(>|t|)    
a3   2.596e+02  5.491e+02   0.473  0.63663    
a4   2.159e+01  5.320e+02   0.041  0.96766    
a5   1.385e+02  4.921e+02   0.282  0.77849    
a6   5.621e+02  4.804e+02   1.170  0.24277    
a7  -2.020e+01  1.035e+02  -0.195  0.84547    
a8   7.438e+02  5.206e+02   1.429  0.15399    
a9   1.175e+03  5.175e+02   2.270  0.02378 *  
a10  7.411e+02  7.438e+02   0.996  0.31973    
a11 -4.744e+02  8.253e+02  -0.575  0.56579    
a12 -5.577e+02  7.820e+02  -0.713  0.47623    
b2  -8.778e-02  4.246e-02  -2.067  0.03943 *  
b3  -6.198e-03  8.692e-02  -0.071  0.94319    
b4   8.501e-02  7.549e-02   1.126  0.26086    
b5   6.504e-02  7.007e-02   0.928  0.35391    
b6   7.187e-02  6.775e-02   1.061  0.28952    
b7   1.512e-01  3.346e-02   4.519 8.46e-06 ***
b8   1.445e-01  6.653e-02   2.173  0.03046 *  
b9   1.986e-01  6.371e-02   3.117  0.00198 ** 
b10  2.789e-01  7.397e-02   3.771  0.00019 ***
b11  4.093e-01  7.059e-02   5.799 1.48e-08 ***
b12  3.709e-01  6.936e-02   5.347 1.60e-07 ***
c1   4.691e-01  8.868e-02   5.289 2.15e-07 ***
c2   5.440e-01  7.051e-02   7.715 1.23e-13 ***
c3  -3.623e-01  4.558e-02  -7.949 2.51e-14 ***
c4   2.486e-01  3.892e-02   6.388 5.25e-10 ***
c5   6.424e-02  2.068e-02   3.106  0.00205 ** 
c6   1.090e-01  2.102e-02   5.188 3.59e-07 ***
c7   1.667e-01  2.075e-02   8.033 1.41e-14 ***
c8   6.581e+00  4.161e-01  15.814  < 2e-16 ***
d1   4.809e-01  9.247e-02   5.200 3.36e-07 ***
d2   5.697e-01  2.951e-01   1.930  0.05436 .  
d3  -1.723e-01  7.918e-02  -2.176  0.03021 *  
d4   4.761e-01  1.876e-01   2.538  0.01157 *  
d5   4.939e-02  6.314e-02   0.782  0.43462    
d6   5.430e-02  5.794e-02   0.937  0.34930    
d7   1.797e-01  9.133e-02   1.968  0.04986 *  
d8   3.724e+00  1.851e+00   2.012  0.04497 *  
e2  -2.764e-01  6.097e-01  -0.453  0.65061    
e3   6.840e-01  4.056e-01   1.687  0.09255 .  
e4   7.412e-01  4.242e-01   1.747  0.08141 .  
e5   5.277e-01  3.852e-01   1.370  0.17157    
e6   5.536e-01  3.897e-01   1.421  0.15624    
e7   1.408e-01  6.125e-02   2.298  0.02212 *  
e8   6.340e-01  4.319e-01   1.468  0.14302    
e9   6.354e-01  4.490e-01   1.415  0.15788    
e10  5.881e-02  1.012e-01   0.581  0.56144    
e11  3.243e-01  7.533e-02   4.305 2.16e-05 ***
e12  4.668e-01  7.319e-02   6.378 5.59e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 936.3 on 356 degrees of freedom

Number of iterations to convergence: 42 
Achieved convergence tolerance: 1.49e-08
fit_coef <- coef(fit)

truck_7990 <- truck_7990 %>%
  mutate(
    winv = case_when(
      ve == 1 ~ 1 / exp(fit_coef["b2"] * cyd2 + fit_coef["b3"] * cyd3 +
        fit_coef["b4"] * cyd4 + fit_coef["b5"] * cyd5 + fit_coef["b6"] * cyd6 +
        fit_coef["b7"] * cyd7 + fit_coef["b8"] * cyd8 + fit_coef["b9"] * cyd9 +
        fit_coef["b10"] * cyd10 + fit_coef["b11"] * cyd11 +
        fit_coef["b12"] * cyd12 + fit_coef["c1"] * wght_c +
        fit_coef["c2"] * wdth_c + fit_coef["c3"] * hght_c +
        fit_coef["c4"] * hp_c + fit_coef["c5"] * tran_c +
        fit_coef["c6"] * ps_c + fit_coef["c7"] * ac_c + fit_coef["c8"]),
      ve == 2 ~ 1 / exp(fit_coef["e2"] * tyd2 + fit_coef["e3"] * tyd3 +
        fit_coef["e4"] * tyd4 + fit_coef["e5"] * tyd5 + fit_coef["e6"] * tyd6 +
        fit_coef["e7"] * tyd7 + fit_coef["e8"] * tyd8 + fit_coef["e9"] * tyd9 +
        fit_coef["e10"] * tyd10 + fit_coef["e11"] * tyd11 +
        fit_coef["e12"] * tyd12 + fit_coef["d1"] * wght_t +
        fit_coef["d2"] * wdth_t + fit_coef["d3"] * hght_t +
        fit_coef["d4"] * hp_t + fit_coef["d5"] * tran_t +
        fit_coef["d6"] * ps_t + fit_coef["d7"] * four_t + fit_coef["d8"]),
    ),
    nprice = price * winv
  )

fit2 <- nlsLM(
  nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7 +
    a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + a12 * cyd12) +
    winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 +
      b7 * cyd7 + b8 * cyd8 + b9 * cyd9 + b10 * cyd10 + b11 * cyd11 +
      b12 * cyd12 + c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c +
      c5 * tran_c + c6 * ps_c + c7 * ac_c + c8) +
    winv * exp(e2 * tyd2 + e3 * tyd3 + e4 * tyd4 + e5 * tyd5 + e6 * tyd6 +
      e7 * tyd7 + e8 * tyd8 + e9 * tyd9 + e10 * tyd10 + e11 * tyd11 +
      e12 * tyd12 + d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t +
      d5 * tran_t + d6 * ps_t + d7 * four_t + d8),
  data = truck_7990,
  start = initial_values,
  control = nls.lm.control(maxiter = 1000, maxfev = 10000)
)

summary(fit2)

Formula: nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + 
    a7 * cyd7 + a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + 
    a12 * cyd12) + winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + 
    b5 * cyd5 + b6 * cyd6 + b7 * cyd7 + b8 * cyd8 + b9 * cyd9 + 
    b10 * cyd10 + b11 * cyd11 + b12 * cyd12 + c1 * wght_c + c2 * 
    wdth_c + c3 * hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + 
    c7 * ac_c + c8) + winv * exp(e2 * tyd2 + e3 * tyd3 + e4 * 
    tyd4 + e5 * tyd5 + e6 * tyd6 + e7 * tyd7 + e8 * tyd8 + e9 * 
    tyd9 + e10 * tyd10 + e11 * tyd11 + e12 * tyd12 + d1 * wght_t + 
    d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * 
    ps_t + d7 * four_t + d8)

Parameters:
      Estimate Std. Error t value Pr(>|t|)    
a3   2.558e+02  4.717e+02   0.542 0.587994    
a4   3.602e+02  5.124e+02   0.703 0.482532    
a5   5.100e+02  4.823e+02   1.057 0.291011    
a6   6.946e+02  4.770e+02   1.456 0.146214    
a7   6.024e+01  9.338e+01   0.645 0.519292    
a8   6.719e+02  6.023e+02   1.116 0.265324    
a9   1.317e+03  5.831e+02   2.259 0.024505 *  
a10  8.590e+02  1.067e+03   0.805 0.421102    
a11  8.514e+02  1.135e+03   0.750 0.453477    
a12  8.496e+02  1.123e+03   0.756 0.449875    
b2  -4.359e-02  6.986e-02  -0.624 0.533015    
b3   6.083e-02  1.322e-01   0.460 0.645668    
b4   9.350e-02  1.340e-01   0.698 0.485782    
b5   5.018e-02  1.275e-01   0.393 0.694188    
b6   9.386e-02  1.238e-01   0.758 0.448940    
b7   2.288e-01  7.452e-02   3.071 0.002300 ** 
b8   2.835e-01  1.330e-01   2.131 0.033774 *  
b9   3.347e-01  1.255e-01   2.667 0.008003 ** 
b10  4.105e-01  1.389e-01   2.956 0.003328 ** 
b11  4.014e-01  1.454e-01   2.761 0.006058 ** 
b12  3.202e-01  1.418e-01   2.258 0.024519 *  
c1   4.797e-01  2.005e-01   2.392 0.017269 *  
c2   8.180e-01  2.096e-01   3.903 0.000114 ***
c3  -2.666e-01  9.648e-02  -2.763 0.006022 ** 
c4   4.243e-01  9.175e-02   4.624 5.27e-06 ***
c5   1.603e-01  5.221e-02   3.070 0.002302 ** 
c6   1.633e-01  4.580e-02   3.567 0.000411 ***
c7   1.522e-01  4.696e-02   3.241 0.001306 ** 
c8   4.049e+00  1.249e+00   3.243 0.001296 ** 
d1   4.943e-01  9.277e-02   5.328 1.77e-07 ***
d2   7.392e-02  6.277e-02   1.178 0.239734    
d3  -3.155e-02  5.003e-02  -0.631 0.528681    
d4   3.573e-01  8.138e-02   4.390 1.49e-05 ***
d5   9.111e-02  2.193e-02   4.154 4.10e-05 ***
d6   8.898e-03  5.962e-02   0.149 0.881454    
d7   8.680e-02  1.017e-01   0.853 0.394034    
d8   6.958e+00  4.030e-01  17.264  < 2e-16 ***
e2  -6.496e-03  1.990e-02  -0.326 0.744316    
e3   3.050e-01  5.749e-02   5.306 1.98e-07 ***
e4   3.021e-01  5.829e-02   5.182 3.68e-07 ***
e5   1.918e-01  4.357e-02   4.402 1.42e-05 ***
e6   2.001e-01  4.508e-02   4.439 1.21e-05 ***
e7   5.227e-02  1.667e-02   3.135 0.001862 ** 
e8   2.051e-01  5.229e-02   3.923 0.000105 ***
e9   1.869e-01  5.223e-02   3.578 0.000395 ***
e10  5.322e-02  1.533e-01   0.347 0.728665    
e11  1.145e-01  1.635e-01   0.700 0.484378    
e12  2.044e-01  1.760e-01   1.161 0.246236    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1606 on 356 degrees of freedom

Number of iterations to convergence: 40 
Achieved convergence tolerance: 1.49e-08

system_nocom.do

fit3 <- nlsLM(
  nprice ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7 +
    a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + a12 * cyd12 +
    exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 + b7 * cyd7 +
      b8 * cyd8 + b9 * cyd9 + b10 * cyd10 + b11 * cyd11 + b12 * cyd12 +
      c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c + c5 * tran_c +
      c6 * ps_c + c7 * ac_c + c8) +
    exp(e2 * tyd2 + e3 * tyd3 + e4 * tyd4 + e5 * tyd5 + e6 * tyd6 + e7 * tyd7 +
      e8 * tyd8 + e9 * tyd9 + e10 * tyd10 + e11 * tyd11 + e12 * tyd12 +
      d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t +
      d6 * ps_t + d7 * four_t + d8),
  data = truck_7990,
  start = initial_values,
  control = nls.lm.control(maxiter = 1000, maxfev = 10000)
)

summary(fit3)

Formula: nprice ~ a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * 
    cyd7 + a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + 
    a12 * cyd12 + exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * 
    cyd5 + b6 * cyd6 + b7 * cyd7 + b8 * cyd8 + b9 * cyd9 + b10 * 
    cyd10 + b11 * cyd11 + b12 * cyd12 + c1 * wght_c + c2 * wdth_c + 
    c3 * hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + c7 * 
    ac_c + c8) + exp(e2 * tyd2 + e3 * tyd3 + e4 * tyd4 + e5 * 
    tyd5 + e6 * tyd6 + e7 * tyd7 + e8 * tyd8 + e9 * tyd9 + e10 * 
    tyd10 + e11 * tyd11 + e12 * tyd12 + d1 * wght_t + d2 * wdth_t + 
    d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * ps_t + d7 * 
    four_t + d8)

Parameters:
      Estimate Std. Error t value Pr(>|t|)    
a3   2.624e-02  4.140e-01   0.063 0.949491    
a4   8.697e-02  4.183e-01   0.208 0.835437    
a5  -8.312e-02  4.548e-01  -0.183 0.855098    
a6  -1.323e-01  4.526e-01  -0.292 0.770199    
a7   2.344e-01  1.482e-02  15.813  < 2e-16 ***
a8   3.986e-01  3.092e-01   1.289 0.198254    
a9   5.016e-01  2.736e-01   1.833 0.067611 .  
a10  5.424e-01  3.662e-01   1.481 0.139457    
a11  9.750e-01  2.681e-01   3.637 0.000316 ***
a12  9.776e-01  3.251e-01   3.007 0.002822 ** 
b2   9.341e-02  5.898e-02   1.584 0.114102    
b3   1.123e-01  4.512e-01   0.249 0.803509    
b4  -1.732e-02  5.231e-01  -0.033 0.973612    
b5   1.847e-01  4.491e-01   0.411 0.681186    
b6   2.756e-01  3.967e-01   0.695 0.487667    
b7  -2.069e-01  7.952e-02  -2.602 0.009669 ** 
b8  -3.091e-01  5.138e-01  -0.602 0.547748    
b9  -3.748e-01  4.893e-01  -0.766 0.444214    
b10 -6.060e-01  7.450e-01  -0.813 0.416582    
b11 -1.343e+01  2.169e+05   0.000 0.999951    
b12 -7.439e+00  6.761e+02  -0.011 0.991227    
c1  -3.546e-01  1.945e-01  -1.823 0.069162 .  
c2  -3.473e-01  1.416e-01  -2.453 0.014648 *  
c3   3.854e-01  1.320e-01   2.919 0.003736 ** 
c4   3.652e-01  1.556e-01   2.346 0.019500 *  
c5   8.554e-02  4.351e-02   1.966 0.050062 .  
c6  -4.309e-02  4.620e-02  -0.933 0.351609    
c7  -4.548e-02  5.820e-02  -0.781 0.435036    
c8  -9.251e-03  9.580e-02  -0.097 0.923125    
d1  -1.943e-01  3.195e-01  -0.608 0.543525    
d2  -9.550e-01  1.593e-01  -5.995 5.00e-09 ***
d3   8.712e-01  1.543e-01   5.646 3.36e-08 ***
d4   9.527e-02  1.540e-01   0.619 0.536598    
d5   1.438e-01  5.331e-02   2.697 0.007324 ** 
d6   8.713e-02  2.299e+00   0.038 0.969795    
d7  -1.475e+00  3.011e+00  -0.490 0.624490    
d8   1.564e+00  8.814e-01   1.775 0.076812 .  
e2   6.017e-01  5.035e-02  11.951  < 2e-16 ***
e3  -2.292e+00  7.305e-01  -3.138 0.001844 ** 
e4  -1.671e+01  1.301e+06   0.000 0.999990    
e5  -1.534e+00  2.857e-01  -5.371 1.42e-07 ***
e6  -1.676e+00  3.323e-01  -5.043 7.30e-07 ***
e7  -2.786e-01  5.847e-02  -4.765 2.76e-06 ***
e8  -1.922e+01  1.849e+07   0.000 0.999999    
e9  -1.872e+01  1.117e+07   0.000 0.999999    
e10  3.423e-02  1.998e-01   0.171 0.864101    
e11 -1.734e+00  6.989e-01  -2.482 0.013532 *  
e12 -1.259e+01  4.935e+04   0.000 0.999797    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1665 on 356 degrees of freedom

Number of iterations to convergence: 135 
Achieved convergence tolerance: 1.49e-08
fit4 <- nlsLM(
  nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + a7 * cyd7 +
    a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + a12 * cyd12) +
    winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + b5 * cyd5 + b6 * cyd6 +
      b7 * cyd7 + b8 * cyd8 + b9 * cyd9 + b10 * cyd10 + b11 * cyd11 +
      b12 * cyd12 + c1 * wght_c + c2 * wdth_c + c3 * hght_c + c4 * hp_c +
      c5 * tran_c + c6 * ps_c + c7 * ac_c + c8) +
    winv * exp(e2 * tyd2 + e3 * tyd3 + e4 * tyd4 + e5 * tyd5 + e6 * tyd6 +
      e7 * tyd7 + e8 * tyd8 + e9 * tyd9 + e10 * tyd10 + e11 * tyd11 +
      e12 * tyd12 + d1 * wght_t + d2 * wdth_t + d3 * hght_t + d4 * hp_t +
      d5 * tran_t + d6 * ps_t + d7 * four_t + d8),
  data = truck_7990,
  start = initial_values,
  control = nls.lm.control(maxiter = 1000, maxfev = 10000)
)

summary(fit4)

Formula: nprice ~ winv * (a3 * cyd3 + a4 * cyd4 + a5 * cyd5 + a6 * cyd6 + 
    a7 * cyd7 + a8 * cyd8 + a9 * cyd9 + a10 * cyd10 + a11 * cyd11 + 
    a12 * cyd12) + winv * exp(b2 * cyd2 + b3 * cyd3 + b4 * cyd4 + 
    b5 * cyd5 + b6 * cyd6 + b7 * cyd7 + b8 * cyd8 + b9 * cyd9 + 
    b10 * cyd10 + b11 * cyd11 + b12 * cyd12 + c1 * wght_c + c2 * 
    wdth_c + c3 * hght_c + c4 * hp_c + c5 * tran_c + c6 * ps_c + 
    c7 * ac_c + c8) + winv * exp(e2 * tyd2 + e3 * tyd3 + e4 * 
    tyd4 + e5 * tyd5 + e6 * tyd6 + e7 * tyd7 + e8 * tyd8 + e9 * 
    tyd9 + e10 * tyd10 + e11 * tyd11 + e12 * tyd12 + d1 * wght_t + 
    d2 * wdth_t + d3 * hght_t + d4 * hp_t + d5 * tran_t + d6 * 
    ps_t + d7 * four_t + d8)

Parameters:
      Estimate Std. Error t value Pr(>|t|)    
a3   2.558e+02  4.717e+02   0.542 0.587994    
a4   3.602e+02  5.124e+02   0.703 0.482532    
a5   5.100e+02  4.823e+02   1.057 0.291011    
a6   6.946e+02  4.770e+02   1.456 0.146214    
a7   6.024e+01  9.338e+01   0.645 0.519292    
a8   6.719e+02  6.023e+02   1.116 0.265324    
a9   1.317e+03  5.831e+02   2.259 0.024505 *  
a10  8.590e+02  1.067e+03   0.805 0.421102    
a11  8.514e+02  1.135e+03   0.750 0.453477    
a12  8.496e+02  1.123e+03   0.756 0.449875    
b2  -4.359e-02  6.986e-02  -0.624 0.533015    
b3   6.083e-02  1.322e-01   0.460 0.645668    
b4   9.350e-02  1.340e-01   0.698 0.485782    
b5   5.018e-02  1.275e-01   0.393 0.694188    
b6   9.386e-02  1.238e-01   0.758 0.448940    
b7   2.288e-01  7.452e-02   3.071 0.002300 ** 
b8   2.835e-01  1.330e-01   2.131 0.033774 *  
b9   3.347e-01  1.255e-01   2.667 0.008003 ** 
b10  4.105e-01  1.389e-01   2.956 0.003328 ** 
b11  4.014e-01  1.454e-01   2.761 0.006058 ** 
b12  3.202e-01  1.418e-01   2.258 0.024519 *  
c1   4.797e-01  2.005e-01   2.392 0.017269 *  
c2   8.180e-01  2.096e-01   3.903 0.000114 ***
c3  -2.666e-01  9.648e-02  -2.763 0.006022 ** 
c4   4.243e-01  9.175e-02   4.624 5.27e-06 ***
c5   1.603e-01  5.221e-02   3.070 0.002302 ** 
c6   1.633e-01  4.580e-02   3.567 0.000411 ***
c7   1.522e-01  4.696e-02   3.241 0.001306 ** 
c8   4.049e+00  1.249e+00   3.243 0.001296 ** 
d1   4.943e-01  9.277e-02   5.328 1.77e-07 ***
d2   7.392e-02  6.277e-02   1.178 0.239734    
d3  -3.155e-02  5.003e-02  -0.631 0.528681    
d4   3.573e-01  8.138e-02   4.390 1.49e-05 ***
d5   9.111e-02  2.193e-02   4.154 4.10e-05 ***
d6   8.898e-03  5.962e-02   0.149 0.881454    
d7   8.680e-02  1.017e-01   0.853 0.394034    
d8   6.958e+00  4.030e-01  17.264  < 2e-16 ***
e2  -6.496e-03  1.990e-02  -0.326 0.744316    
e3   3.050e-01  5.749e-02   5.306 1.98e-07 ***
e4   3.021e-01  5.829e-02   5.182 3.68e-07 ***
e5   1.918e-01  4.357e-02   4.402 1.42e-05 ***
e6   2.001e-01  4.508e-02   4.439 1.21e-05 ***
e7   5.227e-02  1.667e-02   3.135 0.001862 ** 
e8   2.051e-01  5.229e-02   3.923 0.000105 ***
e9   1.869e-01  5.223e-02   3.578 0.000395 ***
e10  5.322e-02  1.533e-01   0.347 0.728665    
e11  1.145e-01  1.635e-01   0.700 0.484378    
e12  2.044e-01  1.760e-01   1.161 0.246236    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1606 on 356 degrees of freedom

Number of iterations to convergence: 40 
Achieved convergence tolerance: 1.49e-08

Extra step: formatting the tables

I need to replace variables such as “c1” with “Weight (tons)” to replicate Table 3.5. I can do this by looking at the coefficients of the variables in the non-linear regression function.

# function to avoid repeating the same code four times
reshape_estimates <- function(fit, vehicle, term_prefix, term_mappings) {
  tidy(fit) %>%
    filter(term %in% paste0(term_prefix, 1:8)) %>%
    mutate(
      term = case_when(
        term == paste0(term_prefix, "1") ~ term_mappings[1],
        term == paste0(term_prefix, "2") ~ term_mappings[2],
        term == paste0(term_prefix, "3") ~ term_mappings[3],
        term == paste0(term_prefix, "4") ~ term_mappings[4],
        term == paste0(term_prefix, "5") ~ term_mappings[5],
        term == paste0(term_prefix, "6") ~ term_mappings[6],
        term == paste0(term_prefix, "7") ~ term_mappings[7],
        term == paste0(term_prefix, "8") ~ term_mappings[8]
      )
    ) %>%
    select(term, estimate, std.error) %>%
    pivot_longer(
      cols = c(estimate, std.error), names_to = "stat",
      values_to = "value"
    ) %>%
    mutate(
      stat = if_else(stat == "estimate", "Estimate", "Std. Error"),
      value = round(value, 3)
    ) %>%
    rename(!!sym(vehicle) := value)
}

car_vars <- c(
  "Weight (tons)", "Width (feet)", "Height (feet)", "Horsepower (100)",
  "Transmission (5-speed or auto)", "Power steering", "Air conditioning", "Constant"
)

truck_vars <- c(
  "Weight (tons)", "Width (feet)", "Height (feet)", "Horsepower (100)",
  "Transmission (5-speed or auto)", "Power steering", "Four-wheel drive", "Constant"
)

ordered_vars <- unique(c(car_vars, truck_vars))
ordered_vars <- c("Constant", ordered_vars[!ordered_vars %in% "Constant"])

table_85 <- reshape_estimates(fit2, "cars", "c", car_vars) %>%
  full_join(reshape_estimates(fit2, "trucks", "d", truck_vars)) %>%
  full_join(reshape_estimates(fit4, "cars", "c", car_vars),
    by = c("term", "stat")
  ) %>%
  full_join(reshape_estimates(fit4, "trucks", "d", truck_vars),
    by = c("term", "stat")
  ) %>%
  mutate(
    term = factor(term, levels = ordered_vars)
  ) %>%
  arrange(term)

colnames(table_85) <- c(
  "Variable", "Statistic", "Cars (No Con)", "Trucks (No Con)",
  "Cars (W Con)", "Trucks (W Con)"
)

kable(table_85)
Variable Statistic Cars (No Con) Trucks (No Con) Cars (W Con) Trucks (W Con)
Constant Estimate 4.049 6.958 4.049 6.958
Constant Std. Error 1.249 0.403 1.249 0.403
Weight (tons) Estimate 0.480 0.494 0.480 0.494
Weight (tons) Std. Error 0.201 0.093 0.201 0.093
Width (feet) Estimate 0.818 0.074 0.818 0.074
Width (feet) Std. Error 0.210 0.063 0.210 0.063
Height (feet) Estimate -0.267 -0.032 -0.267 -0.032
Height (feet) Std. Error 0.096 0.050 0.096 0.050
Horsepower (100) Estimate 0.424 0.357 0.424 0.357
Horsepower (100) Std. Error 0.092 0.081 0.092 0.081
Transmission (5-speed or auto) Estimate 0.160 0.091 0.160 0.091
Transmission (5-speed or auto) Std. Error 0.052 0.022 0.052 0.022
Power steering Estimate 0.163 0.009 0.163 0.009
Power steering Std. Error 0.046 0.060 0.046 0.060
Air conditioning Estimate 0.152 NA 0.152 NA
Air conditioning Std. Error 0.047 NA 0.047 NA
Four-wheel drive Estimate NA 0.087 NA 0.087
Four-wheel drive Std. Error NA 0.102 NA 0.102