-
Notifications
You must be signed in to change notification settings - Fork 3
/
_ivcrc_estimator.ado
637 lines (563 loc) · 26.4 KB
/
_ivcrc_estimator.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
program _ivcrc_estimator, eclass
syntax [anything] [if] [in] ///
[,NOConstant ///
AVErage(string) ///
Ranks(integer 50) ///
Kernel(string) ///
GENerate(string) ///
BANDwidth(string) ///
BOOTstrap(string) ///
DENDOG(varlist fv ts) ///
USERANK(varlist) ///
SAVECOEF(string) ///
VARCOEF(string) *]
** Preliminaries
if trim("`varcoef'")=="" {
_iv_parse `0'
local lhs `s(lhs)'
local endog `s(endog)'
local exog `s(exog)'
local inst `s(inst)'
local 0 `s(zero)'
local vnames = "`endog' `dendog' `exog' _cons"
local kendog : word count `endog'
tempvar rankstat
tempname beta subbeta smtb bsub bbw
marksample touse
markout `touse' `lhs' `exog' `inst' `endog' `dendog'
if trim("`bandwidth'")=="" & trim("`bootstrap'")!="" & trim("`varcoef'")=="" local only_bw "1"
if trim("`average'")=="" local average "0(0)1"
mata: aveparse("`average'")
if `kendog'==1 & `ascendflag'>0 {
dis as err "Integral subsets must not overlap"
error 198
}
}
else if trim("`varcoef'")!="" {
if trim("`bandwidth'") == "" {
dis as err "Option {bf: bandwidth()} must be specified with {bf: varcoef()}"
error 198
}
if trim("`average'")=="" {
dis as err "Option {bf: average()} must be specified with {bf: varcoef()}"
error 198
}
gettoken varclist 0 : 0, parse(",")
tokenize "`varclist'"
local lhs "`1'"
macro shift
local exog "`*'"
fvexpand `exog'
local exog `r(varlist)'
local vnames = "`exog' _cons"
local kendog : word count `varcoef'
tempname beta subbeta smtb bsub bbw
marksample touse
markout `touse' `lhs' `exog' `varcoef'
mata: aveparse("`average'")
}
** IF BOOTSTRAP & NO BANDWIDTH - 1ST ESTIMATE ROT BANDWIDTH
if "`only_bw'"=="1" {
if "`userank'"=="" {
qui rank_estimate , rankvar(`rankstat') endogvar(`endog') instvar(`inst') exogvar(`exog') tousevar(`touse') ranks(`ranks') generate(`generate') bootstrap(`bootstrap')
}
else qui gen `rankstat' = `userank' if `touse'
mata: ivcrc_rotbw("`lhs'", "`endog'", "`dendog' `exog'", "`rankstat'","`touse'")
ereturn scalar rotbw = `bandwidth'
}
** SAMPLE AVERAGE METHOD
if "`sampleave'"=="1" & `kendog'==1 & trim("`varcoef'")=="" & "`only_bw'"=="" {
if "`userank'"=="" {
display in gr "(estimating the conditional rank of `endog')"
qui rank_estimate , rankvar(`rankstat') endogvar(`endog') instvar(`inst') exogvar(`exog') tousevar(`touse') ranks(`ranks') generate(`generate') bootstrap(`bootstrap')
}
else qui gen `rankstat' = `userank' if `touse'
if trim("`bandwidth'") == "" {
dis in gr "(estimating rule-of-thumb bandwidth)"
mata: ivcrc_rotbw("`lhs'", "`endog'", "`dendog' `exog'", "`rankstat'","`touse'")
}
dis in gr "(estimating beta(r) at each r[i] rank in the sample)"
mata: ivcrc_beta_onevar("`lhs'","`endog' `dendog' `exog'","`rankstat'","`touse'")
qui count if `touse'==1
ereturn post beta, esample(`touse') obs(`r(N)') depname("`lhs'")
ereturn local cmd "ivcrc"
}
** SAMPLE AVERAGE METHOD -- multiple basic endogenous variables
if "`sampleave'"=="1" & `kendog'>1 & trim("`varcoef'")=="" & "`only_bw'"=="" {
if "`userank'"=="" {
foreach v of varlist `endog' {
display in gr "(estimating the conditional rank of `v')"
tempvar rankstat`v'
qui rank_estimate , rankvar(`rankstat`v'') endogvar(`v') instvar(`inst') exogvar(`exog') tousevar(`touse') ranks(`ranks') generate(`generate') bootstrap(`bootstrap')
local ranklist = "`ranklist' `rankstat`v''"
if "`savecoef'"!="" local ranknames = "`ranknames' rank_`v'"
}
}
else {
tokenize "`userank'"
local r 1
foreach v of varlist `endog' {
qui gen `rankstat`v'' = `1'
local ranklist = "`ranklist' `rankstat`v''"
if "`savecoef'"!="" local ranknames = "`ranknames' rank_`v'"
}
}
if trim("`bandwidth'") == "" {
dis in gr "(estimating rule-of-thumb bandwidth)"
mata: ivcrc_rotbw("`lhs'", "`endog'", "`dendog' `exog'", "`ranklist'","`touse'")
}
dis in gr "(estimating beta(r) at each r[i] rank vector in the sample)"
mata: ivcrc_beta_multivar("`lhs'","`endog' `dendog' `exog'","`ranklist'","`touse'")
qui count if `touse'==1
ereturn post beta, esample(`touse') obs(`r(N)') depname("`lhs'")
ereturn local cmd "ivcrc"
}
** GRID METHOD
if "`sampleave'"=="" & `kendog'==1 & trim("`varcoef'")=="" & "`only_bw'"=="" {
if "`userank'"=="" {
display in gr "(estimating the conditional rank of `endog')"
qui rank_estimate , rankvar(`rankstat') endogvar(`endog') instvar(`inst') exogvar(`exog') tousevar(`touse') ranks(`ranks') generate(`generate') bootstrap(`bootstrap')
}
else qui gen `rankstat' = `userank' if `touse'
if trim("`bandwidth'") == "" {
dis in gr "(estimating rule-of-thumb bandwidth)"
mata: ivcrc_rotbw("`lhs'", "`endog'", "`dendog' `exog'", "`rankstat'","`touse'")
}
dis in gr "(estimating beta(r) at specified grid points r)"
mata: ivcrc_beta_grid("`lhs'","`endog' `dendog' `exog'","`rankstat'","`touse'")
qui count if `touse'==1
ereturn post beta, esample(`touse') obs(`r(N)') depname("`lhs'")
ereturn local cmd "ivcrc"
}
** VARYING COEFFICIENT MODEL
if trim("`varcoef'")!="" & "`sampleave'"=="1" & `kendog'==1 {
display in gr "(estimating beta(`=abbrev("`varcoef'",7)') at each `varcoef'[i] in the sample)"
mata: ivcrc_beta_onevar("`lhs'","`exog'","`varcoef'","`touse'")
qui count if `touse'==1
ereturn post beta, esample(`touse') obs(`r(N)') depname("`lhs'")
ereturn local cmd "ivcrc - varying coefficient estimator"
}
if trim("`varcoef'")!="" & "`sampleave'"=="1" & `kendog'>1 {
display in gr "(estimating beta(v) at each vector v = (`varcoef') in the sample)"
mata: ivcrc_beta_multivar("`lhs'","`exog'","`varcoef'","`touse'")
qui count if `touse'==1
ereturn post beta, esample(`touse') obs(`r(N)') depname("`lhs'")
ereturn local cmd "ivcrc - varying coefficient estimator"
}
if trim("`varcoef'")!="" & "`sampleave'"=="" & `kendog'==1 {
display in gr "(estimating beta(`=abbrev("`varcoef'",7)') at each `varcoef'[i] in the sample)"
mata: ivcrc_beta_grid("`lhs'","`exog'","`varcoef'","`touse'")
qui count if `touse'==1
ereturn post beta, esample(`touse') obs(`r(N)') depname("`lhs'")
ereturn local cmd "ivcrc - varying coefficient estimator"
}
end
program rank_estimate
syntax [anything] ///
[, RANKVAR(string) ///
ENDOGVAR(varlist fv ts) ///
INSTVAR(varlist fv ts) ///
EXOGVAR(varlist fv ts) ///
TOUSEVAR(varlist) ///
RANKS(integer 50) ///
GENERATE(string) ///
BOOTSTRAP(string) * ]
forval s = 1(1)`= `ranks' - 1' {
local ss = `s'/`ranks'
_qreg `endogvar' `instvar' `exogvar' if `tousevar', quantile(`ss')
tempvar q_`s' ind_`s'
predict `q_`s'' if `tousevar'
gen `ind_`s'' = `q_`s''<=`endogvar' if `tousevar'
local ind_list `ind_list' `ind_`s''
}
egen `rankvar' = rowmean(`ind_list') if `tousevar'
tokenize "`generate'", parse(",")
if "`generate'" != "" & trim("`bootstrap'") == "" {
if "`2'" == "" | ("`2'" == "," & "`3'" == "") {
gen `1'`endogvar' = `rankvar' if `tousevar'
display in gr "(conditional rank of `endogvar' saved to `1'`endogvar')"
}
else if "`2'" == "," & ("`3'" == "replace" | "`3'" == "rep" | "`3'" == "repl" | "`3'" == "re" | "`3'" == "r") {
replace `1'`endogvar' = `rankvar' if `tousevar'
display in gr "(conditional rank of `endogvar' saved to `1'`endogvar')"
}
else {
display as err "Syntax error with rank generate()"
error 198
}
}
end
mata:
function aveparse(avestr)
{
vect = select(tokens(strtrim(avestr),","),strmatch(tokens(strtrim(avestr),","),","):==0)
if (vect[1,cols(vect)]=="report") {
st_local("subsetcoef",strofreal(1))
vect = vect[1,1::(cols(vect)-1)]
}
else {
st_local("subsetcoef",strofreal(0))
}
subsL = substr(vect,J(1,cols(vect),1),strpos(vect,"(")-J(1,cols(vect),1))
subsU = substr(vect,strpos(vect,")")+J(1,cols(vect),1),strlen(vect)-strpos(vect,")"))
gstep = substr(vect,strpos(vect,"(")+J(1,cols(vect),1),strpos(vect,")")-strpos(vect,"(")-J(1,cols(vect),1))
st_local("nsubsets",strofreal(cols(vect)))
for(i=1;i<=cols(vect);i++) {
st_local("aveL" + strofreal(i),subsL[1,i])
st_local("aveU" + strofreal(i),subsU[1,i])
}
if (sum(strtoreal(substr(vect,strpos(vect,"(")+J(1,cols(vect),1),strpos(vect,")")-strpos(vect,"(")-J(1,cols(vect),1))))==0) st_local("sampleave","1")
ascendflag = J(1,cols(vect)-1,0)
for(i=1;i<=cols(vect);i++) {
if (i<cols(vect)) ascendflag[1,i] = (strtoreal(subsU[1,i])>=strtoreal(subsL[1,i+1]))
if (strtoreal(gstep)!=J(1,cols(gstep),0)) st_local("int" + strofreal(i),vect[1,i])
}
st_local("ascendflag",strofreal(sum(ascendflag)))
}
end
mata:
function ivcrc_beta_onevar(yvar, xvars, rankvars, tousevar)
{
lhs = st_data( . , (yvar) , tousevar )
rhs = st_data( . , (xvars) , tousevar )
R = st_data( . , (rankvars) , tousevar )
bw = strtoreal(select(tokens(strtrim(st_local("bandwidth")),","),strmatch(tokens(strtrim(st_local("bandwidth")),","),","):==0))
for(h=1;h<=cols(bw);h++) {
for(i=1;i<=rows(lhs);i++) {
if ( (st_local("kernel")=="uniform") | (st_local("kernel")=="rectangle") | (st_local("kernel")=="unif") | (st_local("kernel")=="rec")) {
w = (1/2)*( abs( (R :- R[i]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="triangle") | (st_local("kernel")=="triangular") | (st_local("kernel")=="tri")) {
w = ( 1 :- abs( (R :- R[i]):/bw[h] ) ):*( abs( (R :- R[i]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="biweight") | (st_local("kernel")=="biweigh") | (st_local("kernel")=="bi")) {
w = (15/16)*( ( 1 :- ( (R :- R[i]):/bw[h] ):^2 ):^2 ):*( abs( (R :- R[i]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="triweight") | (st_local("kernel")=="triweigh") | (st_local("kernel")=="tri")) {
w = (35/32)*( ( 1 :- ( (R :- R[i]):/bw[h] ):^2 ):^3 ):*( abs( (R :- R[i]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="cosine") | (st_local("kernel")=="cos")) {
w = (pi()/4)*cos( (pi()/2)*(R :- R[i]):/bw[h] ):*( abs( (R :- R[i]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="") | (st_local("kernel")=="epanechnikov") | (st_local("kernel")=="epanech") | (st_local("kernel")=="epan") | (st_local("kernel")=="ep")) {
w = (3/4)*( 1 :- ( (R :- R[i]):/bw[h] ):^2 ):*( abs( (R :- R[i]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="gaussian") | (st_local("kernel")=="gauss") | (st_local("kernel")=="gaus") | (st_local("kernel")=="gau")) {
w = (1/sqrt(2*pi()))*exp( -(1/2)*( (R :- R[i]):/bw[h] ):^2 )
}
else {
stata(`"display "Error specifying kernel function""')
exit(error(198))
}
if (st_local("noconstant")=="noconstant") wx = sqrt(w):*rhs
else wx = sqrt(w):*(rhs , J(rows(rhs),1,1))
if (i==1) betar = ( R[i] , (invsym(cross(wx,wx))*cross(wx,sqrt(w):*lhs))' )
else betar = betar \ ( R[i] , (invsym(cross(wx,wx))*cross(wx,sqrt(w):*lhs))' )
if (st_local("savecoef")!="") {
if (i==1) {
stata("tempname ivcrc_coef")
stata(sprintf("file open %s using %s.csv, replace write text",st_local("ivcrc_coef"),st_local("savecoef")))
stata(sprintf(`"file write %s "bandwidth , rank , %s " _n "',st_local("ivcrc_coef"),invtokens(tokens(st_local("vnames")),",")))
}
stata(sprintf(`"file write %s "%s , %s" _n"',st_local("ivcrc_coef"),strofreal(bw[h]),invtokens(strofreal(betar[i,.]),",")))
}
}
if ( (strtoreal(st_local("nsubsets"))==1) & (strtoreal(st_local("subsetcoef"))==0) ) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local("aveL1")))
tb = select(tb, tb[.,1]:<=strtoreal(st_local("aveU1")))
mtb = mean(tb)
if (h==1) {
st_matrix("beta", mtb[. , 2 .. cols(mtb)])
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = sa_%s_%s_%s:",st_local("aveL1"),st_local("aveU1"),strofreal(bw[h])) )
}
else if (h>1) {
st_matrix("mtb", mtb[. , 2 .. cols(mtb)])
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = sa_%s_%s_%s:",st_local("aveL1"),st_local("aveU1"),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb"')
}
}
else if ( (strtoreal(st_local("nsubsets"))>1) & (strtoreal(st_local("subsetcoef"))==0) ) {
for(s=1;s<=strtoreal(st_local("nsubsets"));s++) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local( sprintf("aveL%s",strofreal(s)))))
tb = select(tb, tb[.,1]:<=strtoreal(st_local( sprintf("aveU%s",strofreal(s)))))
if (s==1) subbetar = tb[. , 2 .. cols(betar)]
else subbetar = subbetar \ tb[. , 2 .. cols(betar)]
}
if (h==1) {
st_matrix("beta", mean(subbetar))
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
}
else if (h>1) {
st_matrix("mtb", mean(subbetar))
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb"')
}
}
else if ( (strtoreal(st_local("nsubsets"))>1) & (strtoreal(st_local("subsetcoef"))==1) ){
for(s=1;s<=strtoreal(st_local("nsubsets"));s++) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local( sprintf("aveL%s",strofreal(s)))))
tb = select(tb, tb[.,1]:<=strtoreal(st_local( sprintf("aveU%s",strofreal(s)))))
mtb = mean(tb)
st_matrix("smtb", mtb[. , 2 .. cols(mtb)])
stata( sprintf("matrix colnames smtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames smtb = sa_%s_%s_%s:",st_local(sprintf("aveL%s",strofreal(s))),st_local(sprintf("aveU%s",strofreal(s))),strofreal(bw[h])) )
if (s==1) {
subbetar = tb[. , 2 .. cols(betar)]
stata(`"matrix subbeta = smtb"')
}
else if (s>1) {
subbetar = subbetar \ tb[. , 2 .. cols(betar)]
stata(`"matrix subbeta = subbeta , smtb"')
}
}
if (h==1) {
st_matrix("beta", mean(subbetar))
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
stata(`"matrix beta = beta , subbeta"')
}
else if (h>1) {
st_matrix("mtb", mean(subbetar))
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb , subbeta"')
}
}
}
if (st_local("savecoef")!="") stata(sprintf("file close %s",st_local("ivcrc_coef")))
}
end
mata:
function ivcrc_beta_multivar(yvar, xvars, rankvars, tousevar)
{
lhs = st_data( . , (yvar) , tousevar )
rhs = st_data( . , (xvars) , tousevar )
R = st_data( . , (rankvars) , tousevar )
bw = strtoreal(select(tokens(strtrim(st_local("bandwidth")),","),strmatch(tokens(strtrim(st_local("bandwidth")),","),","):==0))
for(h=1;h<=cols(bw);h++) {
for(i=1;i<=rows(lhs);i++) {
if ( (st_local("kernel")=="uniform") | (st_local("kernel")=="rectangle") | (st_local("kernel")=="unif") | (st_local("kernel")=="rec")) {
w = (1/2)*( abs( (R :- R[i,.]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="triangle") | (st_local("kernel")=="triangular") | (st_local("kernel")=="tri")) {
w = ( 1 :- abs( (R :- R[i,.]):/bw[h] ) ):*( abs( (R :- R[i,.]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="biweight") | (st_local("kernel")=="biweigh") | (st_local("kernel")=="bi")) {
w = (15/16)*( ( 1 :- ( (R :- R[i,.]):/bw[h] ):^2 ):^2 ):*( abs( (R :- R[i,.]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="triweight") | (st_local("kernel")=="triweigh") | (st_local("kernel")=="tri")) {
w = (35/32)*( ( 1 :- ( (R :- R[i,.]):/bw[h] ):^2 ):^3 ):*( abs( (R :- R[i,.]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="cosine") | (st_local("kernel")=="cos")) {
w = (pi()/4)*cos( (pi()/2)*(R :- R[i,.]):/bw[h] ):*( abs( (R :- R[i,.]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="") | (st_local("kernel")=="epanechnikov") | (st_local("kernel")=="epanech") | (st_local("kernel")=="epan") | (st_local("kernel")=="ep")) {
w = (3/4)*( 1 :- ( (R :- R[i,.]):/bw[h] ):^2 ):*( abs( (R :- R[i,.]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="gaussian") | (st_local("kernel")=="gauss") | (st_local("kernel")=="gaus") | (st_local("kernel")=="gau")) {
w = (1/sqrt(2*pi()))*exp( -(1/2)*( (R :- R[i,.]):/bw[h] ):^2 )
}
else {
stata(`"display "Error specifying kernel function""')
exit(error(198))
}
w = exp(rowsum(log(w)))
if (st_local("noconstant")=="noconstant") wx = sqrt(w):*rhs
else wx = sqrt(w):*(rhs , J(rows(rhs),1,1))
if (i==1) betar = ( R[i,.] , (invsym(cross(wx,wx))*cross(wx,sqrt(w):*lhs))' )
else betar = betar \ ( R[i,.] , (invsym(cross(wx,wx))*cross(wx,sqrt(w):*lhs))' )
if (st_local("savecoef")!="") {
if (i==1) {
stata("tempname ivcrc_coef")
stata(sprintf("file open %s using %s.csv, replace write text",st_local("ivcrc_coef"),st_local("savecoef")))
stata(sprintf(`"file write %s "bandwidth , %s , %s " _n "',st_local("ivcrc_coef"),invtokens(tokens(st_local("ranknames")),","),invtokens(tokens(st_local("vnames")),",")))
}
stata(sprintf(`"file write %s "%s , %s" _n"',st_local("ivcrc_coef"),strofreal(bw[h]),invtokens(strofreal(betar[i,.]),",")))
}
}
if (strtoreal(st_local("nsubsets"))==1) {
tb = select(betar , rowsum(betar[.,1 .. cols(R)]:>=strtoreal(st_local("aveL1"))):==cols(R))
tb = select(tb, rowsum(tb[.,1 .. cols(R)]:<=strtoreal(st_local("aveU1"))):==cols(R))
mtb = mean(tb)
if (h==1) {
st_matrix("beta", mtb[. , 1 + cols(R) .. cols(mtb)])
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = samv_%s_%s_%s:",st_local("aveL1"),st_local("aveU1"),strofreal(bw[h])) )
}
else if (h>1) {
st_matrix("mtb", mtb[. , 1 + cols(R) .. cols(mtb)])
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = samv_%s_%s_%s:",st_local("aveL1"),st_local("aveU1"),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb"')
}
}
else if (strtoreal(st_local("nsubsets"))>1) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local("aveL1")))
tb = select(tb, tb[.,1]:<=strtoreal(st_local("aveU1")))
for(v=2;v<=strtoreal(st_local("nsubsets"));v++) {
tb = select(tb , tb[.,v]:>=strtoreal(st_local( sprintf("aveL%s",strofreal(v)))))
tb = select(tb, tb[.,v]:<=strtoreal(st_local( sprintf("aveU%s",strofreal(v)))))
}
if (h==1) {
st_matrix("beta", mtb[. , 1 + cols(R) .. cols(mtb)])
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = samv_%s",strofreal(bw[h])) )
}
else if (h>1) {
st_matrix("mtb", mtb[. , 1 + cols(R) .. cols(mtb)])
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = samv_%s",strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb"')
}
}
}
if (st_local("savecoef")!="") stata(sprintf("file close %s",st_local("ivcrc_coef")))
}
end
mata:
function ivcrc_beta_grid(yvar, xvars, rankvars, tousevar)
{
lhs = st_data( . , (yvar) , tousevar )
rhs = st_data( . , (xvars) , tousevar )
R = st_data( . , (rankvars) , tousevar )
bw = strtoreal(select(tokens(strtrim(st_local("bandwidth")),","),strmatch(tokens(strtrim(st_local("bandwidth")),","),","):==0))
for(s=1;s<=strtoreal(st_local("nsubsets"));s++) {
stata(sprintf(`"numlist "%s""',st_local(sprintf("int%s",strofreal(s)))))
if (s==1) grid = strtoreal(tokens(st_global("r(numlist)")))
else if (s>1) grid = ( grid , strtoreal(tokens(st_global("r(numlist)"))) )
}
for(h=1;h<=cols(bw);h++) {
for(r=1;r<=cols(grid);r++) {
if ( (st_local("kernel")=="uniform") | (st_local("kernel")=="rectangle") | (st_local("kernel")=="unif") | (st_local("kernel")=="rec")) {
w = (1/2)*( abs( (R :- grid[r]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="triangle") | (st_local("kernel")=="triangular") | (st_local("kernel")=="tri")) {
w = ( 1 :- abs( (R :- grid[r]):/bw[h] ) ):*( abs( (R :- grid[r]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="biweight") | (st_local("kernel")=="biweigh") | (st_local("kernel")=="bi")) {
w = (15/16)*( ( 1 :- ( (R :- grid[r]):/bw[h] ):^2 ):^2 ):*( abs( (R :- grid[r]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="triweight") | (st_local("kernel")=="triweigh") | (st_local("kernel")=="tri")) {
w = (35/32)*( ( 1 :- ( (R :- grid[r]):/bw[h] ):^2 ):^3 ):*( abs( (R :- grid[r]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="cosine") | (st_local("kernel")=="cos")) {
w = (pi()/4)*cos( (pi()/2)*(R :- grid[r]):/bw[h] ):*( abs( (R :- grid[r]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="") | (st_local("kernel")=="epanechnikov") | (st_local("kernel")=="epanech") | (st_local("kernel")=="epan") | (st_local("kernel")=="ep")) {
w = (3/4)*( 1 :- ( (R :- grid[r]):/bw[h] ):^2 ):*( abs( (R :- grid[r]):/bw[h] ) :<= 1 )
}
else if ((st_local("kernel")=="gaussian") | (st_local("kernel")=="gauss") | (st_local("kernel")=="gaus") | (st_local("kernel")=="gau")) {
w = (1/sqrt(2*pi()))*exp( -(1/2)*( (R :- grid[r]):/bw[h] ):^2 )
}
else {
stata(`"display "Error specifying kernel function""')
exit(error(198))
}
if (st_local("noconstant")=="noconstant") wx = sqrt(w):*rhs
else wx = sqrt(w):*(rhs , J(rows(rhs),1,1))
if (r==1) betar = ( grid[r] , (invsym(cross(wx,wx))*cross(wx,sqrt(w):*lhs))' )
else betar = betar \ ( grid[r] , (invsym(cross(wx,wx))*cross(wx,sqrt(w):*lhs))' )
if (st_local("savecoef")!="") {
if (r==1) {
stata("tempname ivcrc_coef")
stata(sprintf("file open %s using %s.csv, replace write text",st_local("ivcrc_coef"),st_local("savecoef")))
stata(sprintf(`"file write %s "bandwidth , rank , %s " _n "',st_local("ivcrc_coef"),invtokens(tokens(st_local("vnames")),",")))
}
stata(sprintf(`"file write %s "%s , %s" _n"',st_local("ivcrc_coef"),strofreal(bw[h]),invtokens(strofreal(betar[r,.]),",")))
}
}
if ( (strtoreal(st_local("nsubsets"))==1) & (strtoreal(st_local("subsetcoef"))==0) ) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local("aveL1")))
tb = select(tb, tb[.,1]:<=strtoreal(st_local("aveU1")))
mtb = mean(tb)
if (h==1) {
st_matrix("beta", mtb[. , 2 .. cols(mtb)])
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = sa_%s_%s_%s:",st_local("aveL1"),st_local("aveU1"),strofreal(bw[h])) )
}
else if (h>1) {
st_matrix("mtb", mtb[. , 2 .. cols(mtb)])
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = sa_%s_%s_%s:",st_local("aveL1"),st_local("aveU1"),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb"')
}
}
else if ( (strtoreal(st_local("nsubsets"))>1) & (strtoreal(st_local("subsetcoef"))==0) ) {
for(s=1;s<=strtoreal(st_local("nsubsets"));s++) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local( sprintf("aveL%s",strofreal(s)))))
tb = select(tb, tb[.,1]:<=strtoreal(st_local( sprintf("aveU%s",strofreal(s)))))
if (s==1) subbetar = mean(tb[. , 2 .. cols(betar)]):*( strtoreal(st_local( sprintf("aveU%s",strofreal(s)))) - strtoreal(st_local( sprintf("aveL%s",strofreal(s)))) ):/( strtoreal(st_local( sprintf("aveU%s",st_local("nsubsets")))) - strtoreal(st_local("aveL1")) )
else subbetar = subbetar \ mean(tb[. , 2 .. cols(betar)]):*( strtoreal(st_local( sprintf("aveU%s",strofreal(s)))) - strtoreal(st_local( sprintf("aveL%s",strofreal(s)))) ):/( strtoreal(st_local( sprintf("aveU%s",st_local("nsubsets")))) - strtoreal(st_local("aveL1")) )
}
if (h==1) {
st_matrix("beta", colsum(subbetar))
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
}
else if (h>1) {
st_matrix("mtb", colsum(subbetar))
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb"')
}
}
else if ( (strtoreal(st_local("nsubsets"))>1) & (strtoreal(st_local("subsetcoef"))==1) ){
for(s=1;s<=strtoreal(st_local("nsubsets"));s++) {
tb = select(betar , betar[.,1]:>=strtoreal(st_local( sprintf("aveL%s",strofreal(s)))))
tb = select(tb, tb[.,1]:<=strtoreal(st_local( sprintf("aveU%s",strofreal(s)))))
st_matrix("smtb", mean(tb[. , 2 .. cols(betar)]))
stata( sprintf("matrix colnames smtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames smtb = sa_%s_%s_%s:",st_local(sprintf("aveL%s",strofreal(s))),st_local(sprintf("aveU%s",strofreal(s))),strofreal(bw[h])) )
if (s==1) {
subbetar = mean(tb[. , 2 .. cols(betar)]):*( strtoreal(st_local( sprintf("aveU%s",strofreal(s)))) - strtoreal(st_local( sprintf("aveL%s",strofreal(s)))) ):/( strtoreal(st_local( sprintf("aveU%s",st_local("nsubsets")))) - strtoreal(st_local("aveL1")) )
stata(`"matrix subbeta = smtb"')
}
else if (s>1) {
subbetar = subbetar \ mean(tb[. , 2 .. cols(betar)]):*( strtoreal(st_local( sprintf("aveU%s",strofreal(s)))) - strtoreal(st_local( sprintf("aveL%s",strofreal(s)))) ):/( strtoreal(st_local( sprintf("aveU%s",st_local("nsubsets")))) - strtoreal(st_local("aveL1")) )
stata(`"matrix subbeta = subbeta , smtb"')
}
}
if (h==1) {
st_matrix("beta", colsum(subbetar))
stata( sprintf("matrix colnames beta = %s",st_local("vnames")) )
stata( sprintf("matrix colnames beta = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
stata(`"matrix beta = beta , subbeta"')
}
else if (h>1) {
st_matrix("mtb", colsum(subbetar))
stata( sprintf("matrix colnames mtb = %s",st_local("vnames")))
stata( sprintf("matrix colnames mtb = sa_%s_%s_%s:",st_local("aveL1"),st_local(sprintf("aveU%s",st_local("nsubsets"))),strofreal(bw[h])) )
stata(`"matrix beta = beta , mtb , subbeta"')
}
}
}
if (st_local("savecoef")!="") stata(sprintf("file close %s",st_local("ivcrc_coef")))
}
end
mata:
function ivcrc_rotbw(yvar, endovars, othvars, rankvars, tousevar)
{
Y = st_data( . , (yvar) , tousevar )
beX = st_data( . , (endovars) , tousevar )
R = st_data( . , (rankvars) , tousevar )
if (strtrim(othvars)!="") othX = ( st_data( . , (othvars) , tousevar ) , J(rows(R),1,1) )
else if (strtrim(othvars)=="") othX = J(rows(R),1,1)
nlRX = ( (R:^2):*beX , (R:^3):*beX , (R:^4):*beX , R:*beX , beX , othX )
for(r=1;r<=cols(R);r++) {
nlRX = (nlRX , R[.,r]:*othX , (R[.,r]:^2):*othX , (R[.,r]:^3):*othX , (R[.,r]:^4):*othX )
}
glob_coef = invsym(cross(nlRX,nlRX))*cross(nlRX,Y)
for(r=1;r<=cols(R);r++) {
if (r==1) glob_2dr = cross( ( ( beX[.,r] , 3:*R[.,r]:*beX[.,r] , 6:*(R[.,r]:^2):*beX[.,r] ) * (glob_coef[(r, r + cols(R), r + 2*cols(R))]) ):^2 , J(rows(Y),1,1) ) / rows(Y)
if (r>1) glob_2dr = ( glob_2dr , cross( ( ( beX[.,r] , 3:*R[.,r]:*beX[.,r] , 6:*(R[.,r]:^2):*beX[.,r] ) * (glob_coef[(r, r + cols(R), r + 2*cols(R))]) ):^2 , J(rows(Y),1,1) ) / rows(Y) )
}
glob_sigsq = cross( (Y - nlRX*invsym(cross(nlRX,nlRX))*cross(nlRX,Y)):^2 , J(rows(Y),1,1) ) / rows(Y)
rot_bw = 0.58 :* ( glob_sigsq :/ (rows(Y):*glob_2dr) ):^(1/5)
st_local("bandwidth", strofreal(rowmin(rot_bw)))
if (cols(R)>1) st_matrix("rotbw",rot_bw)
}
end