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, 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

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
restore

sort model
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

> r-8\pindex_c.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\pindex_c.log
log type:  text
opened on:  19 Jun 2024, 18:40:59

.
> 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

> ar_80,replace
> _80.dta saved

. restore

.
. sort model

> 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>
> 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

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
restore

sort model
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

> r-8\pindex_t.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\pindex_t.log
log type:  text
opened on:  19 Jun 2024, 18:59:29

.
> 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

> ruck_80,replace
> ck_80.dta saved

. restore

.
. sort model

> 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>
> 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

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

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

> r-8\unit_value.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\unit_value.log
log type:  text
opened on:  19 Jun 2024, 19:13:08

.
> 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

.
> 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>
> 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)

# Extract ----

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

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

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

if (!file.exists(fout)) {
feenstra_88 <- list(
)

saveRDS(feenstra_88, fout)
} else {
}

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

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

> r-8\car_reg.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\car_reg.log
log type:  text
opened on:  19 Jun 2024, 19:28:58

.
> 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
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
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>
> er-8\car_reg.log
log type:  text
closed on:  19 Jun 2024, 19:29:06
----------------------------------------------------------------------------------

. exit

end of do-file``````

My code

truck_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 = 100)
)

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``````

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

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

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

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

> r-8\system_7985.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\system_7985.log
log type:  text
opened on:  19 Jun 2024, 21:59:23

.
> 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)

.
> ruck_temp,replace
> ck_temp.dta saved

.
> 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

.
> pter-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)

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"

. 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
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
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

.
. 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
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>
> er-8\system_7985.log
log type:  text
closed on:  19 Jun 2024, 21:59:28
----------------------------------------------------------------------------------

. exit

end of do-file``````

My code

``````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
)

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 <- 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 = case_when(type == "JT" ~ 0, TRUE ~ yd2),
tyd2 = case_when(type != "JT" ~ 0, TRUE ~ yd2),
cyd3 = case_when(type == "JT" ~ 0, TRUE ~ yd3),
tyd3 = case_when(type != "JT" ~ 0, TRUE ~ yd3),
cyd4 = case_when(type == "JT" ~ 0, TRUE ~ yd4),
tyd4 = case_when(type != "JT" ~ 0, TRUE ~ yd4),
cyd5 = case_when(type == "JT" ~ 0, TRUE ~ yd5),
tyd5 = case_when(type != "JT" ~ 0, TRUE ~ yd5),
cyd6 = case_when(type == "JT" ~ 0, TRUE ~ yd6),
tyd6 = case_when(type != "JT" ~ 0, TRUE ~ yd6),
cyd7 = case_when(type == "JT" ~ 0, TRUE ~ yd7),
tyd7 = case_when(type != "JT" ~ 0, TRUE ~ 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``````

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

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

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

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)

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)

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

> r-8\unit_quality.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\unit_quality.log
log type:  text
opened on:  19 Jun 2024, 23:03:32

.
> 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

.
> ruck_temp,replace
> ck_temp.dta saved

.
> 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

.
> pter-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)

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"

. 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
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
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

.
. 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
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)

.
> uality_c,replace
> 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)

.
> uality_t,replace
> 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>
> 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

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
restore

sort model
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

> r-8\qindex_c.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\qindex_c.log
log type:  text
opened on:  19 Jun 2024, 23:06:51

.
> 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

> uality_80_c,replace
> lity_80_c.dta saved

. restore

.
. sort model

> 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>
> 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

> r-8\qindex_t.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\qindex_t.log
log type:  text
opened on:  19 Jun 2024, 23:09:00

.
> 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

> uality_80_t,replace
> lity_80_t.dta saved

. restore

.
. sort model

> 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>
> 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 = case_when(
type == "JT" ~ 0,
TRUE ~ yd2
),
tyd2 = case_when(
type != "JT" ~ 0,
TRUE ~ yd2
),
cyd3 = case_when(
type == "JT" ~ 0,
TRUE ~ yd3
),
tyd3 = case_when(
type != "JT" ~ 0,
TRUE ~ yd3
),
cyd4 = case_when(
type == "JT" ~ 0,
TRUE ~ yd4
),
tyd4 = case_when(
type != "JT" ~ 0,
TRUE ~ yd4
),
cyd5 = case_when(
type == "JT" ~ 0,
TRUE ~ yd5
),
tyd5 = case_when(
type != "JT" ~ 0,
TRUE ~ yd5
),
cyd6 = case_when(
type == "JT" ~ 0,
TRUE ~ yd6
),
tyd6 = case_when(
type != "JT" ~ 0,
TRUE ~ yd6
),
cyd7 = case_when(
type == "JT" ~ 0,
TRUE ~ yd7
),
tyd7 = case_when(
type != "JT" ~ 0,
TRUE ~ 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
)

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))``````
``````# 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_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))``````
``````# 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

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

truck_7990_2 %>%
select(year, model, type, quality, quan_c) %>%
inner_join(truck_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))``````
``````# 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_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))``````
``````# 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 ``````

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

I think there is a typo in the original code, in specific in the lines:

``````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"``````

To me, it is not clear why overwriting `cyd7` three times instead of replacing with “12” in one step. I corrected `cyd10`, `cyd11`, and `cyd12` below.

``````capture log close

set matsize 400

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

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

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 cyd10=0 if type=="JT"
replace tyd10=0 if type~="JT"

gen cyd11=yd11
gen tyd11=yd11
replace cyd11=0 if type=="JT"
replace tyd11=0 if type~="JT"

gen cyd12=yd12
gen tyd12=yd12
replace cyd12=0 if type=="JT"
replace tyd12=0 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

> r-8\system_nocon.log,replace
----------------------------------------------------------------------------------
name:  <unnamed>
> er-8\system_nocon.log
log type:  text
opened on:  24 Jun 2024, 00:33:59

.
. 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

.
> 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

.
> ruck_temp,replace
> ck_temp.dta saved

.
> 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

.
> pter-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)

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"

. 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 cyd10=0 if type=="JT"

. replace tyd10=0 if type~="JT"

.
. gen cyd11=yd11

. gen tyd11=yd11

. replace cyd11=0 if type=="JT"

. replace tyd11=0 if type~="JT"

.
. gen cyd12=yd12

. gen tyd12=yd12

. replace cyd12=0 if type=="JT"

. replace tyd12=0 if type~="JT"

.
. 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.69e+10
...
Iteration 59:  residual SS =  3.48e+08
Iteration 60:  residual SS =  3.48e+08

Source |       SS       df       MS            Number of obs =       404
-------------+------------------------------         F( 45,   358) =    134.73
Model |  5.8946e+09    45   130990377         Prob > F      =    0.0000
Residual |   348069386   358  972260.856         R-squared     =    0.9442
Total |  6.2426e+09   403  15490412.8         Root MSE      =  986.0329
Res. dev.     =  6667.767
(ct_1)
------------------------------------------------------------------------------
price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
a3 |  -591.4105   1021.533    -0.58   0.563    -2600.371     1417.55
a4 |  -1145.046   1057.554    -1.08   0.280    -3224.844    934.7527
a5 |  -824.1875    1016.79    -0.81   0.418    -2823.819    1175.444
a6 |  -592.6532   1010.202    -0.59   0.558    -2579.329    1394.023
a7 |  -1306.657     1083.9    -1.21   0.229    -3438.268    824.9532
a8 |  -98.11828   1087.933    -0.09   0.928    -2237.661    2041.425
a9 |  -257.0587   1177.715    -0.22   0.827    -2573.168     2059.05
a10 |  -413.3716   1310.795    -0.32   0.753    -2991.198    2164.455
a11 |  -1220.982   1430.616    -0.85   0.394     -4034.45    1592.486
a12 |  -1076.411   1450.708    -0.74   0.459    -3929.392    1776.569
b2 |   .0197206   .0474133     0.42   0.678    -.0735229    .1129641
b3 |   .2183407   .1415785     1.54   0.124    -.0600893    .4967707
b4 |   .3398839   .1306741     2.60   0.010     .0828985    .5968693
b5 |     .30743    .128895     2.39   0.018     .0539436    .5609165
b6 |    .338236   .1254585     2.70   0.007     .0915077    .5849643
b7 |   .4292664   .1225126     3.50   0.001     .1883316    .6702011
b8 |   .3855295   .1262504     3.05   0.002     .1372438    .6338152
b9 |   .4743625   .1264804     3.75   0.000     .2256246    .7231004
b10 |   .5337515   .1278549     4.17   0.000     .2823104    .7851925
b11 |   .6487194   .1271098     5.10   0.000     .3987437    .8986951
b12 |   .6188886   .1311527     4.72   0.000      .360962    .8768152
c1 |   .4598504   .0886104     5.19   0.000      .285588    .6341127
c2 |   .1780578   .0341819     5.21   0.000     .1108352    .2452803
c3 |  -.3850267   .0481769    -7.99   0.000     -.479772   -.2902814
c4 |   .2680433   .0393044     6.82   0.000     .1907467    .3453399
c5 |   .0679777   .0194236     3.50   0.001      .029779    .1061764
c6 |   .0932556    .021253     4.39   0.000     .0514591     .135052
c7 |   .1534133    .021252     7.22   0.000     .1116189    .1952077
c8 |   8.572183   .0409525   209.32   0.000     8.491646    8.652721
d1 |   1.024883   .2198255     4.66   0.000     .5925712    1.457194
d2 |   .5861134   .7924544     0.74   0.460    -.9723374    2.144564
d3 |   -.149842   .2155522    -0.70   0.487    -.5737496    .2740656
d4 |   1.150419    .347234     3.31   0.001     .4675444    1.833294
d5 |   .3132963   .1385789     2.26   0.024     .0407654    .5858273
d6 |   .0163816   .1407749     0.12   0.907     -.260468    .2932313
d7 |   226.2423   4.491789    50.37   0.000     217.4087    235.0759
d8 |  -223.6678          .        .       .            .           .
e2 |  -9.321882          .        .       .            .           .
e3 |   225.8105   4.416818    51.13   0.000     217.1244    234.4967
e4 |    225.818   4.410297    51.20   0.000     217.1447    234.4914
e5 |   225.3464   4.385453    51.38   0.000      216.722    233.9709
e6 |   225.4068   4.379008    51.47   0.000      216.795    234.0186
e7 |   225.3594   4.388533    51.35   0.000     216.7288    233.9899
e8 |   225.6487   4.417134    51.08   0.000     216.9619    234.3355
e9 |   225.5402   4.407819    51.17   0.000     216.8718    234.2087
e10 |   226.0232   4.412848    51.22   0.000     217.3448    234.7015
e11 |   226.0775   4.462525    50.66   0.000     217.3015    234.8536
e12 |    226.117   4.421014    51.15   0.000     217.4226    234.8115
------------------------------------------------------------------------------
* 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, 18 to missing)

.
. gen nprice=price*winv
(18 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 = 386)

Iteration 0:   residual SS =  3793.244
...
Iteration 51:  residual SS =  11.84729
Iteration 52:  residual SS =  11.84729

Source |       SS       df       MS            Number of obs =       386
-------------+------------------------------         F( 46,   339) =   1272.95
Model |  2046.39509    46  44.4868497         Prob > F      =    0.0000
Residual |  11.8472856   339  .034947745         R-squared     =    0.9942
Total |  2058.24237   385  5.34608409         Root MSE      =  .1869432
Res. dev.     = -249.3025
(ct_2)
------------------------------------------------------------------------------
nprice |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
a3 |   129.1587   649.8698     0.20   0.843    -1149.127    1407.444
a4 |  -332.9346   853.5884    -0.39   0.697    -2011.931    1346.062
a5 |  -39.00799   816.3593    -0.05   0.962    -1644.776     1566.76
a6 |  -49.30975   887.7125    -0.06   0.956    -1795.428    1696.809
a7 |  -251.5048   1055.969    -0.24   0.812    -2328.582    1825.572
a8 |   475.7476   1105.123     0.43   0.667    -1698.014    2649.509
a9 |   1305.252   1054.533     1.24   0.217    -769.0007    3379.505
a10 |   1500.556   1475.416     1.02   0.310    -1401.567    4402.679
a11 |   275.3094    1918.11     0.14   0.886    -3497.586    4048.205
a12 |  -369.0768   2256.641    -0.16   0.870    -4807.859    4069.706
b2 |  -.0382212   .0476313    -0.80   0.423    -.1319113    .0554688
b3 |   .1007584   .1545476     0.65   0.515    -.2032345    .4047514
b4 |   .3643006   .1549828     2.35   0.019     .0594514    .6691497
b5 |   .3350521   .1522164     2.20   0.028     .0356445    .6344596
b6 |    .448507   .1460037     3.07   0.002     .1613198    .7356943
b7 |   .5008198   .1605704     3.12   0.002     .1849799    .8166597
b8 |   .5229822   .1722726     3.04   0.003     .1841243      .86184
b9 |   .3892165   .1802644     2.16   0.032     .0346389    .7437941
b10 |   .5894213   .2041663     2.89   0.004     .1878289    .9910137
b11 |   .8546564    .199235     4.29   0.000      .462764    1.246549
b12 |   1.000789   .1993482     5.02   0.000     .6086733    1.392904
c1 |   1.059199   .3598464     2.94   0.003     .3513857    1.767012
c2 |   .1615223   .1132255     1.43   0.155    -.0611907    .3842354
c3 |  -.6524711   .2018085    -3.23   0.001    -1.049426   -.2555164
c4 |   .2672449   .1289842     2.07   0.039     .0135347    .5209551
c5 |   .2030882   .0598443     3.39   0.001     .0853753    .3208011
c6 |   .2113444   .0828113     2.55   0.011     .0484557    .3742331
c7 |   .1550375   .0660018     2.35   0.019     .0252129    .2848621
c8 |   8.340437    .114401    72.91   0.000     8.115411    8.565462
d1 |   1.111578   .2140655     5.19   0.000     .6905136    1.532641
d2 |  -.4348055    .168151    -2.59   0.010    -.7655563   -.1040547
d3 |   .0754878   .0932058     0.81   0.419    -.1078468    .2588223
d4 |    .559007    .181628     3.08   0.002     .2017472    .9162667
d5 |   .1239908   .0427616     2.90   0.004     .0398792    .2081023
d6 |  -.0185416   .0827602    -0.22   0.823    -.1813297    .1442466
d7 |  -.5680364   .7627409    -0.74   0.457    -2.068338    .9322647
d8 |   7.995761   .1883688    42.45   0.000     7.625242     8.36628
e2 |         .1          .        .       .            .           .
e3 |  -.3274455   .7327471    -0.45   0.655    -1.768749    1.113858
e4 |  -.3156744   .7324389    -0.43   0.667    -1.756372    1.125023
e5 |  -.5595854   .7452185    -0.75   0.453     -2.02542    .9062493
e6 |  -.5473584   .7454929    -0.73   0.463    -2.013733    .9190161
e7 |  -.6603122   .7473731    -0.88   0.378    -2.130385    .8097605
e8 |  -.4940828   .7382813    -0.67   0.504    -1.946272    .9581066
e9 |  -.4857295   .7335855    -0.66   0.508    -1.928682    .9572232
e10 |  -.2352299   .7308603    -0.32   0.748    -1.672822    1.202362
e11 |  -.1529434   .7360252    -0.21   0.836    -1.600695    1.294808
e12 |  -.0194963   .7348633    -0.03   0.979    -1.464963     1.42597
------------------------------------------------------------------------------
* 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>
> er-8\system_nocon.log
log type:  text
closed on:  24 Jun 2024, 00:34:17
----------------------------------------------------------------------------------

. 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 = case_when(
type == "JT" ~ 0,
TRUE ~ yd2
),
tyd2 = case_when(
type != "JT" ~ 0,
TRUE ~ yd2
),
cyd3 = case_when(
type == "JT" ~ 0,
TRUE ~ yd3
),
tyd3 = case_when(
type != "JT" ~ 0,
TRUE ~ yd3
),
cyd4 = case_when(
type == "JT" ~ 0,
TRUE ~ yd4
),
tyd4 = case_when(
type != "JT" ~ 0,
TRUE ~ yd4
),
cyd5 = case_when(
type == "JT" ~ 0,
TRUE ~ yd5
),
tyd5 = case_when(
type != "JT" ~ 0,
TRUE ~ yd5
),
cyd6 = case_when(
type == "JT" ~ 0,
TRUE ~ yd6
),
tyd6 = case_when(
type != "JT" ~ 0,
TRUE ~ yd6
),
cyd7 = case_when(
type == "JT" ~ 0,
TRUE ~ yd7
),
tyd7 = case_when(
type != "JT" ~ 0,
TRUE ~ yd7
),
cyd8 = case_when(
type == "JT" ~ 0,
TRUE ~ yd8
),
tyd8 = case_when(
type != "JT" ~ 0,
TRUE ~ yd8
),
cyd9 = case_when(
type == "JT" ~ 0,
TRUE ~ yd9
),
tyd9 = case_when(
type != "JT" ~ 0,
TRUE ~ yd9
),
cyd10 = case_when(
type == "JT" ~ 0,
TRUE ~ yd10
),
tyd10 = case_when(
type != "JT" ~ 0,
TRUE ~ yd10
),
cyd11 = case_when(
type == "JT" ~ 0,
TRUE ~ yd11
),
tyd11 = case_when(
type != "JT" ~ 0,
TRUE ~ yd11
),
cyd12 = case_when(
type == "JT" ~ 0,
TRUE ~ yd12
),
tyd12 = case_when(
type != "JT" ~ 0,
TRUE ~ 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  -232.06287  893.96625  -0.260 0.795332
a4  -632.87850  914.45900  -0.692 0.489338
a5  -367.97647  876.38040  -0.420 0.674825
a6   -48.30074  863.33409  -0.056 0.955416
a7  -749.19564  928.78692  -0.807 0.420413
a8   187.75024  939.25918   0.200 0.841679
a9   333.01836 1009.60725   0.330 0.741708
a10  243.82112 1132.40125   0.215 0.829646
a11 -211.61067 1212.77220  -0.174 0.861583
a12   11.61149 1219.90438   0.010 0.992411
b2     0.01556    0.04777   0.326 0.744779
b3     0.16598    0.13240   1.254 0.210804
b4     0.27475    0.12216   2.249 0.025124 *
b5     0.24519    0.11990   2.045 0.041592 *
b6     0.26695    0.11691   2.283 0.022994 *
b7     0.36389    0.11358   3.204 0.001478 **
b8     0.33385    0.11688   2.856 0.004537 **
b9     0.40608    0.11768   3.451 0.000626 ***
b10    0.46226    0.11912   3.881 0.000124 ***
b11    0.55678    0.11831   4.706 3.62e-06 ***
b12    0.51644    0.12254   4.214 3.18e-05 ***
c1     0.47758    0.08994   5.310 1.93e-07 ***
c2     0.21440    0.04811   4.457 1.12e-05 ***
c3    -0.39686    0.04746  -8.362 1.41e-15 ***
c4     0.28379    0.03979   7.132 5.56e-12 ***
c5     0.06936    0.02017   3.439 0.000653 ***
c6     0.09921    0.02178   4.555 7.20e-06 ***
c7     0.16038    0.02163   7.415 8.94e-13 ***
c8     8.38888    0.20671  40.583  < 2e-16 ***
d1     0.88011    0.21681   4.059 6.05e-05 ***
d2     0.47431    0.54808   0.865 0.387397
d3    -0.15194    0.16103  -0.944 0.346051
d4     0.89964    0.34067   2.641 0.008636 **
d5     0.25704    0.11574   2.221 0.026994 *
d6     0.02296    0.10258   0.224 0.822986
d7     0.64556    0.26226   2.462 0.014309 *
d8     3.42341    3.80931   0.899 0.369421
e2    -0.75842    1.13213  -0.670 0.503352
e3     0.38740    0.33106   1.170 0.242720
e4     0.35839    0.31518   1.137 0.256252
e5     0.10602    0.32166   0.330 0.741881
e6     0.13491    0.32069   0.421 0.674237
e7     0.04074    0.30751   0.132 0.894680
e8     0.20083    0.31805   0.631 0.528160
e9     0.09907    0.31555   0.314 0.753733
e10    0.40385    0.36546   1.105 0.269884
e11    0.43926    0.34766   1.263 0.207252
e12    0.48634    0.35567   1.367 0.172359
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 971.1 on 356 degrees of freedom

Number of iterations to convergence: 46
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   6.538e+02  2.721e+02   2.403 0.016762 *
a4   1.301e+03  2.979e+02   4.365 1.67e-05 ***
a5   1.423e+03  2.842e+02   5.007 8.72e-07 ***
a6   1.938e+03  2.927e+02   6.620 1.32e-10 ***
a7   1.971e+03  2.748e+02   7.172 4.31e-12 ***
a8   2.478e+03  3.573e+02   6.936 1.91e-11 ***
a9   2.834e+03  2.997e+02   9.459  < 2e-16 ***
a10  3.732e+03  5.234e+02   7.130 5.63e-12 ***
a11  4.650e+03  5.096e+02   9.125  < 2e-16 ***
a12  5.719e+03  5.764e+02   9.922  < 2e-16 ***
b2  -5.319e-03  6.580e-02  -0.081 0.935618
b3  -1.247e-02  9.428e-02  -0.132 0.894871
b4  -9.225e-02  1.060e-01  -0.870 0.384779
b5  -1.341e-01  1.021e-01  -1.314 0.189793
b6  -1.575e-01  1.045e-01  -1.507 0.132733
b7  -9.892e-02  9.792e-02  -1.010 0.313095
b8  -6.148e-02  1.125e-01  -0.546 0.585133
b9   6.130e-02  1.017e-01   0.603 0.546933
b10 -1.094e-02  1.215e-01  -0.090 0.928310
b11 -1.361e-01  1.382e-01  -0.985 0.325535
b12 -3.865e-01  1.695e-01  -2.281 0.023133 *
c1   4.558e-01  2.257e-01   2.019 0.044207 *
c2   5.981e-01  2.570e-01   2.327 0.020522 *
c3  -2.753e-01  1.126e-01  -2.444 0.015028 *
c4   6.650e-01  1.146e-01   5.804 1.43e-08 ***
c5   1.549e-01  5.320e-02   2.911 0.003831 **
c6   1.789e-01  5.407e-02   3.309 0.001031 **
c7   1.813e-01  5.576e-02   3.252 0.001257 **
c8   5.149e+00  1.476e+00   3.489 0.000547 ***
d1   4.063e-01  6.114e-02   6.645 1.14e-10 ***
d2   1.191e-02  4.744e-02   0.251 0.801843
d3   6.573e-02  3.308e-02   1.987 0.047646 *
d4   2.526e-01  4.697e-02   5.378 1.37e-07 ***
d5   3.851e-02  1.365e-02   2.820 0.005068 **
d6  -2.048e-02  3.637e-02  -0.563 0.573782
d7   2.448e-01  3.168e-02   7.726 1.14e-13 ***
d8   7.326e+00  3.043e-01  24.072  < 2e-16 ***
e2   2.753e-02  1.555e-02   1.771 0.077445 .
e3   2.568e-01  2.406e-02  10.670  < 2e-16 ***
e4   2.587e-01  2.458e-02  10.524  < 2e-16 ***
e5   1.887e-01  2.371e-02   7.960 2.32e-14 ***
e6   1.903e-01  2.388e-02   7.968 2.21e-14 ***
e7   1.387e-01  2.473e-02   5.606 4.16e-08 ***
e8   1.847e-01  2.676e-02   6.901 2.37e-11 ***
e9   1.956e-01  2.944e-02   6.643 1.15e-10 ***
e10  2.804e-01  3.795e-02   7.390 1.06e-12 ***
e11  3.098e-01  3.738e-02   8.289 2.36e-15 ***
e12  3.654e-01  3.774e-02   9.682  < 2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1613 on 356 degrees of freedom

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