-
Notifications
You must be signed in to change notification settings - Fork 4
/
boottest.mata
2947 lines (2590 loc) · 126 KB
/
boottest.mata
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*! boottest 4.4.11 12 April 2024
*! Copyright (C) 2015-23 David Roodman
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
mata
mata clear
mata set matastrict on
mata set mataoptimize on
mata set matalnum off
struct smatrix {
real matrix M
}
struct ssmatrix {
struct smatrix matrix M
}
struct structboottestClust {
real scalar N, multiplier, even
real colvector order
real matrix info
}
struct structFE {
real colvector is, wt, sqrtwt
}
// return pointer to chosen columns of a matrix, but don't duplicate data if return value is whole matrix
pointer (real matrix) pcol(real matrix A, real vector p)
return(length(p)==cols(A)? &A : &A[,p])
// right-multiply a data matrix by a matrix, with efficient handling of special cases like latter is identity
pointer (real matrix) scalar pXB(real matrix X, real matrix M) {
real scalar r
r = rows(M)
return (all(colsum(M) :== 1) & all(colsum(!M) :== r-1)? // is M 0's but for one 1 in each col, so it's just copying/reordering cols?
(all(diagonal(M)) & rows(M)==cols(M)? // is M just the identity matrix?
&X :
&X[,colsum(M:*(1::r))]) : // reorder X
&(X * M))
}
// return [X1 X2]B where X1 or X2 may have 0 cols
pointer(real matrix) scalar pX12B(real matrix X1, real matrix X2, real matrix B)
return(cols(B)? (cols(X1)? (cols(X2)? &(*pXB(X1, B[|.,.\cols(X1),.|]) + *pXB(X2, B[|cols(X1)+1,.\.,.|])) : pXB(X1, B)) : pXB(X2, B)) : &J(rows(X1),0,0))
// return &X[|S|] with appropiate behavior if cols(X)=0 or S requests no rows (S[2,1]=0), presuming that in degenerate cases S does not specify columns
// if retval = X, doesn't duplicate data
// S should be 2x1 because the function is only for selecting rows
pointer(real matrix) scalar pXS(real matrix X, real colvector S)
return(cols(X)? (S[2,1]? ((S[1,1]==. | S[1,1]==1) & (S[2,1]==. | S[2,1]==rows(X))? &X : &X[|S,(.\.)|]) : &J(0,cols(X),0)) : &J(editmissing(S[2,1],rows(X))-editmissing(S[1,1],1)+1,0,0))
// do X[|S|] = Y while allowing X to have no cols and S to be a colvector
void setXS(real matrix X, real colvector S, real matrix Y) if (cols(X)) X[|S,(.\.)|] = Y;;
matrix fold(matrix X) return(uppertriangle(X) + lowertriangle(X,0)') // fold matrix diagonally; returns same values as a quad form, but runs faster because of all the 0's
class boottestOLS { // class for performing OLS
real scalar y1y1, LIML, Fuller, kappa, isDGP, kZ, kX1, kX, y1bary1bar, restricted
real colvector invXXXy1par, X1y1, dbetadr, beta0, y1bar, Zperpy1, t1, t1Y, deltadddot, X2y1, Xy1bar, deltaX, deltaY, u1dddot
real rowvector y1Y2, Yendog, y1barU2ddot
real matrix Z, ZZ, XZ, XX, PXZ, R1invR1R1, R1perp, Rpar, RperpX, RRpar, RparX, RparY, RR1invR1R1, YY, AR, XAR, XinvXX, Rt1, invXX, invH, Deltadddot, Y2bar, perpRperpX, XY2, XU2ddot, U2ddotU2ddot, Y2Y2, Piddot, ZY2, V, U2ddot
pointer(real colvector) scalar py1par, pXy1par, py1
pointer(real matrix) scalar pA, pZperp, pX1, pX1par, pR1AR1, pZR1, pZperpZperp, pinvZperpZperp, pX2, pY2
pointer (class boottest scalar) scalar parent
struct smatrix rowvector WXAR, CT_XAR, beta, u1ddot, XinvHg, invMg, Xg, XXg
private void new(), InitTestDenoms()
private virtual void InitVars(), SetR(), Estimate(), MakeResiduals()
real matrix _select(), perp()
}
class boottestARubin extends boottestOLS {
private virtual void InitVars(), Estimate()
}
class boottestIVGMM extends boottestOLS {
real matrix H_2SLS, ZR1ZR1, X2ZR1, ZR1Y2, X1ZR1, ZZR1, H_2SLSmZZ, X2jk, Y2jk, X1jk, Zjk, ZR1jk
real colvector Zy1, y1jk
real rowvector twoy1ZR1
real scalar y1pary1par
pointer(real rowvector) scalar pZy1par
pointer(real rowvector) scalar py1parY2
pointer(real matrix) scalar pRperp
pointer(struct smatrix rowvector) scalar py1parY2g, pZy1parg, pXy1parg // jk stuff
struct smatrix rowvector XY2g, ZY2g, XXg, XZg, YYg, Zy1g, X1y1g, X2y1g, y1Y2g, ZZg, invXXg, H_2SLSg, H_2SLSmZZg, ZR1Y2g, ZR1ZR1g, twoy1ZR1g, ZZR1g, X1ZR1g, X2ZR1g, invHg
real colvector _Nobsg, y1pary1parg, y1y1g
pointer(real colvector) scalar py1pary1parg, py1parjk
private void new()
private virtual void InitVars(), Estimate(), MakeResiduals(), MakeH()
}
class boottest {
real scalar scoreBS, B, small, auxwttype, null, dirty, initialized, ML, Nobs, _Nobs, kZ, kY2, kX1, sumwt, NClustVar, haswt, REst, multiplier, smallsample, quietly, FEboot, NErrClustCombs, ///
sqrt, LIML, Fuller, kappa, WRE, WREnonARubin, ptype, twotailed, df, df_r, ARubin, willplot, notplotted, NumH0s, p, NBootClustVar, NErrClustVar, BootClust, FEdfadj, jk, ///
NFE, granular, granularjk, purerobust, subcluster, Nstar, BFeas, v_sd, level, ptol, MaxMatSize, Nw, enumerate, bootstrapt, q, interpolable, interpolating, interpolate_u, robust, kX2, kX, Nall, bootneqcap
real matrix AR, v, CI, CT_WE, infoBootData, infoErrAll, JNcapNstar, statDenom, SuwtXA, numer0, deltadenom_b, _Jcap, YYstar_b, YPXYstar_b, numerw, ustar0, YbarYbar, XYbar, invXXXZbar, PXZbar, Zbar, invXXXX1par, PiddotRparY
real colvector DistCDR, plotX, plotY, beta, ClustShare, WeightGrpStart, WeightGrpStop, confpeak, gridmin, gridmax, gridpoints, numersum, anchor, poles, invFEwt, sqrtwt, Sstary1y1, CTstarFEy1
real rowvector peak, betas, As
string scalar obswttype, madjtype, seed
pointer (real matrix) scalar pX2, pR1, pR, pID, pFEID, pY2, pX1, pinfoAllData, pinfoCapData, pIDAll, pnumer, pustar, pU2parddot
pointer (real colvector) scalar pr1, pr, py1, pSc, pwt, pA, pDist, pIDBootData, pIDBootAll
class boottestOLS scalar DGP, Repl
pointer(class boottestOLS scalar) scalar pM
struct structboottestClust rowvector Clust
struct smatrix matrix denom, Kcd, denom0, Jcd0, CTUX, FillingT0, SstarUU, negSstarUMZperpX, ScapXX1par
struct smatrix rowvector Kd, dudr, dnumerdr, IDCTCapstar, infoCTCapstar, deltadenom, Zyi, SstarUPX, YYstar, YPXYstar, ScapXYbar, ScapPXYbarZperp, CT_FEcapYbar, SstarUX, SstarUXinvXX,
SstarinvZperpZperpZperpU, SstarZperpU, CTstarFEU, SstarUYbar, SCTcapUXinvXX, SstarUMZperp, ScapXX, ScapYX, SstarY2X, SstarXY2par, ScapXZperp, SstarZR1X, SstarXy1,
SstarZX, SstarZZ, SstarZR1Zperp, SstarZperpy1, SstarZZperp, SstarZperpY2par, SstarY2Zperp, SstarX1parY2par, SstarXX1par, SstarX1pary1, SstarY2X1par, SstarZR1X1par, SstarZX1par,
SstarY2parY2par, Sstary1Y2par, Sstary1Y2, SstarZR1Y2par, SstarZR1Y2, SstarZY2par, SstarZY2, SstarY2Y2par, SstarZR1y1, SstarZy1, SstarZR1ZR1, SstarZR1Z, SstarY2Y2, SallXU2RparY,
SallXu1ddot, SallU2X, CTstarFEY2, CTstarFEX, CTcapFEX, CTstarFEZR1, CTstarFEZ
pointer(struct smatrix rowvector) pSstarXX, pSstarXZperp, pSallXy1, pSallZX, pSallZR1X, pSallY2X, pSallXX
struct ssmatrix rowvector ddenomdr, dJcddr
struct ssmatrix matrix ddenomdr2
pointer(struct smatrix matrix) scalar pJcd
struct structFE rowvector FEs
void new(), setsqrt(), setX1(), setptype(), setdirty(), setY2(), setY(), setX2(), setobswt(), setsc(), setML(), setLIML(), setARubin(), setauxwttype(),
setFuller(), setkappa(), setquietly(), setbeta(), setA(), setsmall(), sethascons(), setjk(), setscoreBS(), setB(), setnull(), setID(), setFEID(), setlevel(), setptol(),
setrobust(), setR1(), setR(), setwillplot(), setgrid(), setmadjust(), setMaxMatSize(), setstattype(), close()
private void MakeNumerAndJ(), _clustAccum(), MakeWREStats(), MakeInterpolables(), _MakeInterpolables(), MakeNonWREStats(), UpdateBootstrapcDenom(), Init(), plot(), MakeWildWeights(), boottest(), crosstabCapstarMinus(), InitWRE(), PrepWRE(), storeWtGrpResults(), NoNullUpdate()
real matrix getplot(), getCI(), getV(), getv()
real scalar getp(), getpadj(), getstat(), getdf(), getdf_r(), getreps(), getrepsFeas(), getNBootClust()
real rowvector getpeak()
real colvector getdist(), getb()
private real scalar r_to_p(), search()
private real matrix count_binary(), crosstabFE(), HessianFixedkappa(), threebyone()
private real rowvector _HessianFixedkappa()
private pointer(real matrix) scalar Filling(), partialFE()
private static real matrix combs()
private static real colvector stableorder()
private real vector _selectindex()
static void _st_view()
}
void boottestOLS::new() {
LIML = Fuller = kappa = 0
Rpar = isDGP = 1
beta = smatrix()
}
void boottestIVGMM::new() {
Fuller = 0
kappa = isDGP = 1
u1ddot = beta = smatrix()
}
// do select() but handle case that both args are scalar and second is 0 by interpreting second arg as rowvector and returning J(1,0,0)
// if v = 0 (so can't tell if row or col vector), returns J(1, 0, 0)
real matrix boottestOLS::_select(real matrix X, real rowvector v)
return (rows(X)==1 & cols(X)==1 & v==0? J(1,0,0) : select(X,v))
real matrix boottestOLS::perp(real matrix A) {
real matrix vec; real rowvector val; pragma unset vec; pragma unset val
symeigensystem(A*invsym(A'A)*A', vec, val); _edittozero(val, 1000)
return (_select(vec, !val))
}
// R1 is constraints. R is attack surface for null; only needed when using FWL for WRE
// for DGP regression, R1 is maintained constraints + null if imposed while R should have 0 rows
// for replication regressions R1 is maintained constraints, R is null
void boottestOLS::SetR(real matrix R1, | real matrix R) {
real matrix _R, vec; real rowvector val
pragma unset vec; pragma unset val
restricted = rows(R1) > 0
if (restricted) {
R1invR1R1 = invsym(R1 * R1')
if (all(diagonal(R1invR1R1))==0)
_error(111, "Null hypothesis or model constraints are inconsistent or redundant.")
R1invR1R1 = R1 ' R1invR1R1
symeigensystem(R1invR1R1 * R1, vec, val); _edittozero(val, 1000)
R1perp = _select(vec, !val) // eigenvectors orthogonal to span of R1; foundation for parameterizing subspace compatible with constraints
} else
R1invR1R1 = J(parent->kZ,0,0) // and R1perp = I
if (kappa) {
// prepare to reduce regression via FWL
_R = R \ J(parent->kY2, parent->kX1, 0), I(parent->kY2) // rows to prevent partialling out of endogenous regressors
if (restricted)
_R = _R * R1perp
symeigensystem(_R ' invsym(_R * _R') * _R, vec, val); _edittozero(val, 1000)
Rpar = _select(vec, val); if (restricted) Rpar = R1perp * Rpar // par of RR₁perp
if (isDGP | !parent->WREnonARubin) { // in WRE, Rperp is same DGP and Repl; might not be in WUE, but is arranged so by call to SetR()
RperpX = _select(vec, !val)
if (restricted) RperpX = R1perp * RperpX
RperpX = *pXS(RperpX, .\parent->kX1)
}
RparX = *pXS(Rpar, .\parent->kX1) // part of Rpar that refers to X1
RparY = *pXS(Rpar, parent->kX1+1\.) // part of Rpar that refers to Y2
if (isDGP==0) {
RRpar = R * Rpar
RR1invR1R1 = R * R1invR1R1
}
}
}
// stuff that can be done before r set, and depends only on exogenous variables, which are fixed throughout all bootstrap methods
void boottestOLS::InitVars(pointer(real matrix) scalar pRperp) { // pRperp is for replication regression--no null imposed
real matrix H, negR1AR1; real scalar g; real colvector S
u1ddot = smatrix(1 + parent->jk) // for jackknife, need jk'd residuals but also non-jk'd residuals for original test stat
py1par = parent->py1
H = cross(*parent->pX1, *parent->pX1)
invH = invsym(H)
pR1AR1 = rows(R1perp)? &( R1perp * invsym( R1perp ' H * R1perp) * R1perp') : &invH // for DGP regression
X1y1 = cross(*parent->pX1, *py1par)
beta0 = *pR1AR1 * X1y1
dbetadr = *pR1AR1 * H * R1invR1R1 - R1invR1R1
if (parent->jk)
if (parent->purerobust) // set up for optimized "robust" jk
(invMg = smatrix()).M = 1 :/ (1 :- rowsum(*parent->pX1 * fold(*pR1AR1) :* *parent->pX1)) // standard hc3 multipliers
else {
u1ddot[2].M = J(parent->Nobs, 1, 0)
Xg = smatrix(parent->Nstar)
if (parent->granularjk)
invMg = Xg
else
XXg = XinvHg = Xg
negR1AR1 = - *pR1AR1
for (g=parent->Nstar; g; g--) {
S = parent->NClustVar? parent->infoBootData[g,1] \ parent->infoBootData[g,2] : g\g
Xg[g].M = *pXS(*parent->pX1,S)
if (parent->granularjk) { // for many small clusters, faster to compute jackknife errors vaia hc3-like formula
invMg[g].M = Xg[g].M * negR1AR1 * Xg[g].M'
_diag(invMg[g].M, diagonal(invMg[g].M) :+ 1)
invMg[g].M = invsym(invMg[g].M)
} else {
XXg[g].M = cross(Xg[g].M, Xg[g].M)
XinvHg[g].M = Xg[g].M * (rows(R1perp)? R1perp * invsym(R1perp ' (H - XXg[g].M) * R1perp) * R1perp' : invsym(H - XXg[g].M))
}
}
}
pA = rows(*pRperp)? &(*pRperp * invsym(*pRperp ' H * *pRperp) * *pRperp') : &invH // for replication regression
AR = *pA * *parent->pR'
if (parent->scoreBS | parent->robust)
XAR = *parent->pX1 * AR
}
void boottestARubin::InitVars(| pointer(real matrix) pRperp) {
pragma unused pRperp
real matrix H, X2X1, _X2X1, _X1, _X2, tmp; real colvector S, X1y1, X2y1; real scalar g
u1ddot = smatrix(1 + parent->jk) // for jackknife, need jk'd residuals but also non-jk'd residuals for original test stat
X2X1 = cross(*parent->pX2, *parent->pX1)
H = cross(*parent->pX1, *parent->pX1), X2X1' \ X2X1, cross(*parent->pX2, *parent->pX2)
pA = &invsym(H)
pR1AR1 = rows(R1perp)? &(R1perp * invsym(R1perp ' H * R1perp) * R1perp') : pA
X1y1 = cross(*parent->pX1, *parent->py1)
X2y1 = cross(*parent->pX2, *parent->py1)
beta0 = *pR1AR1 * (X1y1 \ X2y1)
dbetadr = *pR1AR1 * (cross(*parent->pX1, *parent->pY2) \ cross(*parent->pX2, *parent->pY2))
if (parent->jk)
if (parent->purerobust) { // set up for optimized "robust" jk
tmp = fold(*pR1AR1)
(invMg=smatrix()).M = parent->kX1? rowsum(*parent->pX1 * tmp[|.,.\parent->kX1,parent->kX1|] :* *parent->pX1) +
rowsum(*parent->pX1 * tmp[|.,parent->kX1+1\parent->kX1,.|] :* *parent->pX2) +
rowsum(*parent->pX2 * tmp[|parent->kX1+1,parent->kX1+1\.,.|] :* *parent->pX2) :
rowsum(*parent->pX2 * tmp :* *parent->pX2)
invMg.M = 1 :/ (1 :- invMg.M) // standard hc3 multipliers
} else {
u1ddot[2].M = J(parent->Nobs, 1, 0)
Xg = smatrix(parent->Nstar)
if (parent->granularjk)
invMg = Xg
else
XXg = XinvHg = smatrix(parent->Nstar)
for (g=parent->Nstar; g; g--) {
S = parent->NClustVar? parent->infoBootData[g,1] \ parent->infoBootData[g,2] : g\g
_X1 = *pXS(*parent->pX1, S)
_X2 = *pXS(*parent->pX2, S)
Xg[g].M = _X1, _X2
if (parent->granularjk) { // for many small clusters, faster to compute jackknife errors vaia hc3-like formula
invMg[g].M = -(Xg[g].M * *pR1AR1 * Xg[g].M')
_diag(invMg[g].M, diagonal(invMg[g].M) :+ 1)
invMg[g].M = invsym(invMg[g].M)
} else {
_X2X1 = cross(_X2, _X1)
XXg[g].M = cross(_X1, _X1), _X2X1' \ _X2X1, cross(_X2, _X2)
XinvHg[g].M = Xg[g].M * (rows(R1perp)? R1perp * invsym(R1perp ' (H - XXg[g].M) * R1perp) * R1perp' : invsym(H - XXg[g].M))
}
}
}
AR = *pA * *parent->pR' // for replication regression
if (parent->scoreBS | parent->robust)
XAR = *pX12B(*parent->pX1, *parent->pX2, AR)
}
void boottestIVGMM::InitVars(|pointer(real matrix) scalar pRperp) {
real matrix ZperpZ, ZperpZR1, _invZperpZperp, X1g, X2g, Zg, Zperpg, ZR1g, Y2g, _X2X1, _X1X1, _X2X2, _X1Y2, _X2Y2, X2X1, _invZperpZperpZperpX1, _invZperpZperpZperpX2, _invZperpZperpZperpZ, _invZperpZperpZperpZR1, _invZperpZperpZperpY2, ZperpX1, ZperpX2, ZperpY2, X1Y2, X2X2, X1X1, X2Y2, X1Z, X2Z, tX1, tX2, tY2, tZ, tZR1
real colvector S, y1g, _invZperpZperpZperpy1, Zperpy1, ty1
real rowvector y1ZR1
real scalar g
this.pRperp = pRperp
if (!isDGP & parent->WREnonARubin) {
pZperp = parent->DGP.pZperp
pinvZperpZperp = parent->DGP.pinvZperpZperp
} else {
perpRperpX = perp(RperpX)
pZperp = pXB(*parent->pX1, RperpX)
pinvZperpZperp = &invsym(*(pZperpZperp = &cross(*pZperp, *pZperp)))
pX1 = pXB(*parent->pX1, perpRperpX) // retained exogenous regressors in instrument set
ZperpX1 = cross(*pZperp, *pX1)
ZperpX2 = cross(*pZperp, *parent->pX2)
ZperpY2 = cross(*pZperp, *parent->pY2)
Zperpy1 = cross(*pZperp, *parent->py1)
kX = (kX1 = cols(*pX1)) + parent->kX2
u1ddot = smatrix()
}
kZ = cols(Rpar)
Z = *(pX1par = pXB(*parent->pX1, RparX)) + *parent->pY2 * RparY // Zpar, X1par (retained exogenous regressors in regressor set)
pZR1 = pX12B(*parent->pX1, *parent->pY2, R1invR1R1) // Z*R1
ZperpZ = cross(*pZperp, Z )
ZperpZR1 = cross(*pZperp, *pZR1)
if (parent->jk & isDGP & parent->WREnonARubin) {
XY2g = ZY2g = XXg = XZg = YYg = Zy1g = X1y1g = X2y1g = y1Y2g = invHg = ZZg = invXXg = H_2SLSg = H_2SLSmZZg = ZR1Y2g = ZR1ZR1g = twoy1ZR1g = ZZR1g = X1ZR1g = X2ZR1g = smatrix(parent->Nstar)
y1y1g = J(parent->Nstar, 1, 0)
beta = smatrix(parent->Nstar + 1)
u1ddot.M = u1dddot = J(parent->Nobs, 1, 0); U2ddot = J(parent->Nobs, parent->kY2, 0)
if (restricted) {
py1pary1parg = &J(parent->Nstar,1,0)
py1parY2g = &smatrix(parent->Nstar)
pZy1parg = &smatrix(parent->Nstar)
pXy1parg = &smatrix(parent->Nstar)
}
Y2jk = J(parent->Nobs, parent->kY2, 0)
X2jk = J(parent->Nobs, parent->kX2, 0)
X1jk = J(parent->Nobs, kX1, 0)
Zjk = J(parent->Nobs, kZ, 0)
ZR1jk = J(parent->Nobs, cols(*pZR1), 0)
y1jk = J(parent->Nobs, 1, 0)
X2X1 = cross(*parent->pX2, *pX1)
X1X1 = cross(*pX1, *pX1)
X2X2 = cross(*parent->pX2, *parent->pX2)
X2Y2 = cross(*parent->pX2, *parent->pY2)
X2y1 = cross(*parent->pX2, *parent->py1)
X1y1 = cross(*pX1, *parent->py1)
Zy1 = cross(Z, *parent->py1)
X1Y2 = cross(*pX1, *parent->pY2)
X1Z = cross(*pX1, Z)
X2Z = cross(*parent->pX2, Z)
ZZ = cross(Z, Z)
y1Y2 = cross(*parent->py1, *parent->pY2)
ZY2 = cross(Z, *parent->pY2)
y1y1 = cross(*parent->py1, *parent->py1)
X1ZR1 = cross(*pX1, *pZR1)
X2ZR1 = cross(*parent->pX2, *pZR1)
ZZR1 = cross(Z, *pZR1)
y1ZR1 = cross(*parent->py1, *pZR1)
ZR1ZR1 = cross(*pZR1, *pZR1)
ZR1Y2 = cross(*pZR1, *parent->pY2)
for (g=parent->Nstar; g; g--) {
S = parent->NClustVar? parent->infoBootData[g,]' : g\g
X2g = *pXS(*parent->pX2, S)
Y2g = *pXS(*parent->pY2, S)
y1g = *pXS(*parent->py1, S)
Zperpg = *pXS(*pZperp, S)
X1g = *pXS(*pX1, S)
Zg = *pXS(Z , S)
ZR1g = *pXS(*pZR1, S)
_invZperpZperp = invsym(*pZperpZperp - cross(Zperpg, Zperpg))
_invZperpZperpZperpX1 = _invZperpZperp * (ZperpX1 - cross(Zperpg, X1g))
_invZperpZperpZperpX2 = _invZperpZperp * (ZperpX2 - cross(Zperpg, X2g))
_invZperpZperpZperpZ = _invZperpZperp * (ZperpZ - cross(Zperpg, Zg))
_invZperpZperpZperpZR1 = _invZperpZperp * (ZperpZR1 - cross(Zperpg, ZR1g))
_invZperpZperpZperpY2 = _invZperpZperp * (ZperpY2 - cross(Zperpg, Y2g))
_invZperpZperpZperpy1 = _invZperpZperp * (Zperpy1 - cross(Zperpg, y1g))
X1g = X1g - Zperpg * _invZperpZperpZperpX1 // FWL-process
X2g = X2g - Zperpg * _invZperpZperpZperpX2
Zg = Zg - Zperpg * _invZperpZperpZperpZ
ZR1g = ZR1g - Zperpg * _invZperpZperpZperpZR1
Y2g = Y2g - Zperpg * _invZperpZperpZperpY2
y1g = y1g - Zperpg * _invZperpZperpZperpy1
setXS(X1jk , S, X1g ) // save partialled-out vars from each jk iteration, to compute residuals later
setXS(X2jk , S, X2g )
setXS(Y2jk , S, Y2g )
setXS(y1jk , S, y1g )
setXS(Zjk , S, Zg )
setXS(ZR1jk, S, ZR1g)
tX1 = ZperpX1 - (*pZperpZperp) * _invZperpZperpZperpX1
tX2 = ZperpX2 - (*pZperpZperp) * _invZperpZperpZperpX2
tZ = ZperpZ - (*pZperpZperp) * _invZperpZperpZperpZ
tY2 = ZperpY2 - (*pZperpZperp) * _invZperpZperpZperpY2
ty1 = Zperpy1 - (*pZperpZperp) * _invZperpZperpZperpy1
_X2X1 = X2X1 - ZperpX2 ' _invZperpZperpZperpX1 - _invZperpZperpZperpX2 ' tX1 - cross(X2g, X1g)
_X1X1 = X1X1 - ZperpX1 ' _invZperpZperpZperpX1 - _invZperpZperpZperpX1 ' tX1 - cross(X1g, X1g)
_X2X2 = X2X2 - ZperpX2 ' _invZperpZperpZperpX2 - _invZperpZperpZperpX2 ' tX2 - cross(X2g, X2g)
_X1Y2 = X1Y2 - ZperpX1 ' _invZperpZperpZperpY2 - _invZperpZperpZperpX1 ' tY2 - cross(X1g, Y2g)
_X2Y2 = X2Y2 - ZperpX2 ' _invZperpZperpZperpY2 - _invZperpZperpZperpX2 ' tY2 - cross(X2g, Y2g)
y1Y2g[g].M = y1Y2 - Zperpy1 ' _invZperpZperpZperpY2 - _invZperpZperpZperpy1 ' tY2 - cross(y1g, Y2g)
X2y1g[g].M = X2y1 - ZperpX2 ' _invZperpZperpZperpy1 - _invZperpZperpZperpX2 ' ty1 - cross(X2g, y1g)
X1y1g[g].M = X1y1 - ZperpX1 ' _invZperpZperpZperpy1 - _invZperpZperpZperpX1 ' ty1 - cross(X1g, y1g)
y1y1g[g] = y1y1 - 2 * (Zperpy1 ' _invZperpZperpZperpy1) + _invZperpZperpZperpy1 ' (*pZperpZperp) * _invZperpZperpZperpy1 - cross(y1g, y1g)
Zy1g [g].M = Zy1 - ZperpZ ' _invZperpZperpZperpy1 - _invZperpZperpZperpZ ' ty1 - cross(Zg , y1g)
XZg [g].M = X1Z - ZperpX1 ' _invZperpZperpZperpZ - _invZperpZperpZperpX1 ' tZ - cross(X1g, Zg) \
X2Z - ZperpX2 ' _invZperpZperpZperpZ - _invZperpZperpZperpX2 ' tZ - cross(X2g, Zg)
ZZg[g].M = ZZ - ZperpZ ' _invZperpZperpZperpZ - _invZperpZperpZperpZ ' tZ - cross(Zg , Zg)
ZY2g[g].M = ZY2 - ZperpZ ' _invZperpZperpZperpY2 - _invZperpZperpZperpZ ' tY2 - cross(Zg, Y2g)
XY2g[g].M = _X1Y2 \ _X2Y2
invXXg[g].M = invsym((XXg[g].M = _X1X1, _X2X1' \ _X2X1, _X2X2))
H_2SLSg[g].M = XZg[g].M ' invXXg[g].M * XZg[g].M
if (kappa!=1 | LIML) H_2SLSmZZg[g].M = H_2SLSg[g].M - ZZg[g].M
if (restricted) {
tZR1 = ZperpZR1 - (*pZperpZperp) * _invZperpZperpZperpZR1
X1ZR1g[g].M = X1ZR1 - ZperpX1 ' _invZperpZperpZperpZR1 - _invZperpZperpZperpX1 ' tZR1 - cross(X1g , ZR1g)
X2ZR1g[g].M = X2ZR1 - ZperpX2 ' _invZperpZperpZperpZR1 - _invZperpZperpZperpX2 ' tZR1 - cross(X2g , ZR1g)
ZZR1g[g].M = ZZR1 - ZperpZ ' _invZperpZperpZperpZR1 - _invZperpZperpZperpZ ' tZR1 - cross(Zg , ZR1g)
twoy1ZR1g[g].M = y1ZR1 - Zperpy1 ' _invZperpZperpZperpZR1 - _invZperpZperpZperpy1 ' tZR1 - cross(y1g , ZR1g)
twoy1ZR1g[g].M = twoy1ZR1g[g].M + twoy1ZR1g[g].M
ZR1ZR1g[g].M = ZR1ZR1 - ZperpZR1 ' _invZperpZperpZperpZR1 - _invZperpZperpZperpZR1 ' tZR1 - cross(ZR1g, ZR1g)
ZR1Y2g[g].M = ZR1Y2 - ZperpZR1 ' _invZperpZperpZperpY2 - _invZperpZperpZperpZR1 ' tY2 - cross(ZR1g, Y2g )
}
}
if (!restricted) {
py1parY2g = &y1Y2g
pZy1parg = &Zy1g
py1pary1parg = &y1y1g
pXy1parg = &smatrix(parent->Nstar); for (g=parent->Nstar;g;g--) (*pXy1parg)[g].M = X1y1g[g].M \ X2y1g[g].M
py1parjk = &y1jk
}
if (Fuller) // number of observations outside each bootstrapping cluster
_Nobsg = parent->_Nobs :- (parent->obswttype=="fweight"? panelsum(*parent->pwt, parent->infoBootData) : (parent->infoBootData[,2] - parent->infoBootData[,1]) :+ 1)
}
if (!isDGP & parent->WREnonARubin) {
pX1 = parent->DGP.pX1
pX2 = parent->DGP.pX2
invXX = parent->DGP.invXX
pY2 = parent->DGP.pY2
py1 = parent->DGP.py1
y1Y2 = parent->DGP.y1Y2
X2y1 = parent->DGP.X2y1
X1y1 = parent->DGP.X1y1
y1y1 = parent->DGP.y1y1
} else {
pX1 = &(*pX1 - *pZperp * (*pinvZperpZperp * ZperpX1)) // FWL-process X1
pX2 = &(*parent->pX2 - *pZperp * (*pinvZperpZperp * ZperpX2))
pY2 = &(*parent->pY2 - *pZperp * (*pinvZperpZperp * ZperpY2))
py1 = &(*parent->py1 - *pZperp * (*pinvZperpZperp * Zperpy1))
X2X1 = cross(*pX2, *pX1)
invXX = invsym(XX = (cross(*pX1, *pX1), X2X1' \ X2X1, cross(*pX2, *pX2)))
if (parent->scoreBS==0) Y2Y2 = cross(*pY2, *pY2)
XY2 = cross(*pX1, *pY2) \ cross(*pX2, *pY2)
y1Y2 = cross(*py1, *pY2)
X2y1 = cross(*pX2, *py1)
X1y1 = cross(*pX1, *py1)
y1y1 = cross(*py1, *py1)
}
Z = Z - *pZperp * (*pinvZperpZperp * ZperpZ) // XXX when is this avoidable, like in jk?
Zy1 = cross(Z, *py1)
XZ = cross(*pX1, Z) \ cross(*pX2, Z)
V = invXX * XZ
ZZ = cross(Z,Z)
if (parent->WREnonARubin) ZY2 = cross(Z, *pY2)
if (restricted) {
pZR1 = &(*pZR1 - *pZperp * (*pinvZperpZperp * ZperpZR1)) // XXX when is this avoidable, like in jk?
X2ZR1 = cross(*pX2 , *pZR1)
X1ZR1 = cross(*pX1 , *pZR1)
ZZR1 = cross(Z , *pZR1)
twoy1ZR1 = cross( *py1 , *pZR1); twoy1ZR1 = twoy1ZR1 + twoy1ZR1
ZR1ZR1 = cross(*pZR1, *pZR1)
ZR1Y2 = cross(*pZR1, *pY2 )
} else {
py1parY2 = &y1Y2
pZy1par = &Zy1
y1pary1par = y1y1
pXy1par = &(X1y1 \ X2y1)
py1par = py1
t1 = J(parent->kZ,1,0)
t1Y = J(parent->kY2,1,0)
}
if (isDGP) {
if (parent->jk & LIML==0 & parent->WREnonARubin)
for (g=parent->Nstar; g; g--)
invHg[g].M = invsym(kappa==1? H_2SLSg[g].M : ZZg[g].M + kappa * H_2SLSmZZg[g].M)
H_2SLS = V ' XZ // Hessian
if (kappa!=1 | LIML) H_2SLSmZZ = H_2SLS - ZZ
if (LIML==0) // DGP is LIML except possibly when getting confidence peak for A-R plot; but LIML=0 when exactly id'd, for then kappa=1 always and Hessian doesn't depend on r1 and can be computed now
MakeH()
} else {
pX1par = &(*pX1par - *pZperp * (*pinvZperpZperp * cross(*pZperp, *pX1par)))
Yendog = 1, colsum(RparY :!= 0) // columns of Y = [y1par Zpar] that are endogenous (normally all)
if (parent->robust & parent->bootstrapt & (parent->granular | parent->jk)) { // for WRE replication regression, prepare for CRVE
XinvXX = *pX12B(*pX1, *pX2, invXX)
if ((parent->granular | parent->jk) & parent->WREnonARubin)
PXZ = *pX12B(*pX1, *pX2, V)
}
}
}
// do most of estimation; for LIML r1 must be passed now in order to solve eigenvalue problem involving it
// inconsistency: for replication regression of Anderson-Rubin, r1 refers to the *null*, not the maintained constraints, because that's what affects the endogenous variables
// For WRE, should only be called once for the replication regressions, since for them r1 is the unchanging model constraint
void boottestOLS::Estimate(real scalar _jk, real colvector r1) {
beta.M = beta0 - dbetadr * r1
if (_jk & rows(R1perp))
t1 = R1invR1R1 * r1
}
void boottestARubin::Estimate(real scalar _jk, real colvector r1) {
beta.M = beta0 - dbetadr * r1
py1par = &(*parent->py1 - *parent->pY2 * r1)
if (_jk & rows(R1perp))
t1 = R1invR1R1 * r1
}
void boottestIVGMM::MakeH() {
pointer(real matrix) scalar pH
pH = kappa==1? &H_2SLS : &(ZZ + kappa * H_2SLSmZZ)
invH = invsym(*pH)
if (pRperp) { // for score bootstrap
pA = cols(*pRperp)? &(*pRperp * invsym(*pRperp ' (*pH) * *pRperp) * *pRperp') : &invH
AR = *pA * (Rpar ' (*parent->pR'))
XAR = *pX12B(*pX1, *pX2, V * AR)
}
}
void boottestIVGMM::Estimate(real scalar _jk, real colvector r1) {
real rowvector val; real matrix vec; real scalar g, kappag; real colvector ZXinvXXXy1par, invXXXy1parg; real matrix YPXY
pragma unset vec; pragma unset val
if (restricted) {
t1 = R1invR1R1 * r1
if (isDGP)
py1par = &(*py1 - *pZR1 * r1)
y1pary1par = y1y1 - twoy1ZR1 * r1 + r1 ' ZR1ZR1 * r1
py1parY2 = &(y1Y2 - r1 ' ZR1Y2)
pZy1par = &( Zy1 - ZZR1 * r1)
pXy1par = &(X1y1 - X1ZR1 * r1 \ X2y1 - X2ZR1 * r1)
if (_jk) {
t1Y = t1[|parent->kX1+1,.\.,.|]
for (g=parent->Nstar; g; g--) {
py1parjk = &(y1jk - ZR1jk * r1)
(*py1pary1parg)[g] = y1y1g[g] - twoy1ZR1g[g].M * r1 + r1 ' ZR1ZR1g[g].M * r1
(*py1parY2g) [g].M = y1Y2g[g].M - r1 ' ZR1Y2g[g].M
(*pZy1parg) [g].M = Zy1g[g].M - ZZR1g[g].M * r1
(*pXy1parg) [g].M = X1y1g[g].M - X1ZR1g[g].M * r1 \ X2y1g[g].M - X2ZR1g[g].M * r1
}
}
}
invXXXy1par = invXX * *pXy1par
YY = y1pary1par, *pZy1par' \ *pZy1par, ZZ
if (isDGP) {
ZXinvXXXy1par = XZ ' invXXXy1par
if (LIML) {
YPXY = invXXXy1par ' (*pXy1par) , ZXinvXXXy1par' \ ZXinvXXXy1par , H_2SLS
eigensystemselecti(invsym(YY) * YPXY, rows(YY)\rows(YY), vec, val)
kappa = 1/(1 - Re(val)) // sometimes a tiny imaginary component sneaks into val
if (Fuller) kappa = kappa - Fuller / (parent->_Nobs - parent->kX)
MakeH()
}
beta.M = invH * (kappa==1? ZXinvXXXy1par : kappa * (ZXinvXXXy1par - *pZy1par) + *pZy1par)
if (_jk)
for (g=parent->Nstar; g; g--) {
ZXinvXXXy1par = XZg[g].M ' (invXXXy1parg = invXXg[g].M * (*pXy1parg)[g].M)
YYg[g].M = (*py1pary1parg)[g], (*pZy1parg)[g].M' \ (*pZy1parg)[g].M, ZZg[g].M
if (LIML) {
YPXY = invXXXy1parg ' (*pXy1parg)[g].M , ZXinvXXXy1par' \ ZXinvXXXy1par , H_2SLSg[g].M
eigensystemselecti(invsym(YYg[g].M) * YPXY, rows(YYg[g].M)\rows(YYg[g].M), vec, val)
kappag = 1/(1 - Re(val))
if (Fuller) kappag = kappag - Fuller / (_Nobsg[g] - parent->kX)
invHg[g].M = invsym(ZZg[g].M + kappag * H_2SLSmZZg[g].M)
} else
kappag = kappa
beta[g+1].M = invHg[g].M * (kappag==1? ZXinvXXXy1par : kappag * (ZXinvXXXy1par - (*pZy1parg)[g].M) + (*pZy1parg)[g].M)
}
} else if (parent->WREnonARubin) // if not score bootstrap of IV/GMM...
Rt1 = RR1invR1R1 * r1
}
void boottestOLS::MakeResiduals(real scalar _jk) {
real scalar g, m; real colvector S, u1ddotg, Xt1
u1ddot.M = *py1par - *pX12B(*parent->pX1, *parent->pX2, beta.M)
if (_jk) {
m = parent->small? sqrt((parent->Nstar - 1) / parent->Nstar) : 1
if (parent->purerobust) // somewhat faster handling classic hc3
if (rows(R1perp)) {
Xt1 = *pX12B(*parent->pX1, *parent->pX2, t1)
u1ddot[2].M = m * ((invMg.M :* (u1ddot.M + Xt1)) - Xt1)
} else
u1ddot[2].M = m * invMg.M :* u1ddot.M
else if (parent->granularjk) {
for (g=parent->Nstar; g; g--) {
S = parent->NClustVar? parent->infoBootData[g,]' : g\g
if (rows(R1perp)) {
Xt1 = Xg[g].M * t1
u1ddot[2].M[|S|] = m * (cross(invMg[g].M, u1ddot.M[|S|] + Xt1) - Xt1)
} else
u1ddot[2].M[|S|] = m * cross(invMg[g].M, u1ddot.M[|S|])
}
} else
for (g=parent->Nstar; g; g--) {
S = parent->infoBootData[g,]'
u1ddotg = u1ddot.M[|S|]
u1ddot[2].M[|S|] = m * (u1ddotg + XinvHg[g].M * (rows(R1perp)? cross(Xg[g].M, u1ddotg) + XXg[g].M * t1 : cross(Xg[g].M, u1ddotg)))
}
}
}
void boottestIVGMM::MakeResiduals(real scalar _jk) {
pragma unused _jk
real matrix tmp, Piddotjk; real colvector Xu, negXuinvuu, _beta, delta, S, _t1Y; real scalar uu; real scalar g
if (parent->jk)
for (g=parent->Nstar; g; g--) {
S = parent->NClustVar? parent->infoBootData[g,1] \ parent->infoBootData[g,2] : g\g
u1ddot.M[|S|] = *pXS(*py1parjk,S) - *pXS(Zjk,S) * beta[1+g].M
}
else if (parent->granular | parent->scoreBS)
u1ddot.M = *py1par - Z * beta.M
if (parent->scoreBS==0) {
_beta = 1 \ -beta.M
uu = _beta ' YY * _beta
Xu = *pXy1par - XZ * beta.M // after DGP regression, compute Y2 residuals by regressing Y2 on X while controlling for y1 residuals, done through FWL
negXuinvuu = Xu / -uu
Piddot = invsym(XX + negXuinvuu * Xu') * (negXuinvuu * (*py1parY2 - beta.M ' ZY2) + XY2)
U2ddotU2ddot = Y2Y2 - Piddot'XY2 - XY2'Piddot + Piddot'XX*Piddot
delta = Rpar * beta.M + t1 - parent->Repl.t1
deltaX = *pXS(delta, .\parent->kX1)
deltaY = *pXS(delta, parent->kX1+1\.)
tmp = perpRperpX'deltaX
deltadddot = Piddot * deltaY; if (kX1) deltadddot[|.\kX1|] = deltadddot[|.\kX1|] + tmp // (X_∥'X_∥)^-1 * X_∥'y1bar
Xy1bar = XX * deltadddot
y1bary1bar = deltadddot'Xy1bar
XU2ddot = XY2 - XX * Piddot
y1barU2ddot = deltadddot'XU2ddot
if (parent->granular | parent->jk) {
Y2bar = *pX12B(*pX1, *pX2, Piddot)
y1bar = *pX1 * tmp + Y2bar * deltaY
}
if (parent->jk) {
_t1Y = t1Y - parent->Repl.t1Y
for (g=parent->Nstar; g; g--) {
S = parent->NClustVar? parent->infoBootData[g,1] \ parent->infoBootData[g,2] : g\g
_beta = 1 \ -beta[1+g].M
uu = _beta ' YYg[g].M * _beta
Xu = (*pXy1parg)[g].M - XZg[g].M * beta[1+g].M
negXuinvuu = Xu / -uu
Piddotjk = invsym(XXg[g].M + negXuinvuu * Xu') * (negXuinvuu * ((*py1parY2g)[g].M - beta[1+g].M ' ZY2g[g].M) + XY2g[g].M)
U2ddot [|S,(.\.)|] = tmp = *pXS(Y2jk,S) - *pX12B(*pXS(X1jk,S), *pXS(X2jk,S), Piddotjk)
u1dddot[|S |] = u1ddot.M[|S|] + tmp * (RparY * beta[1+g].M + _t1Y)
}
} else if (parent->granular) {
U2ddot = *pY2 - Y2bar
u1dddot = u1ddot.M + U2ddot * deltaY
}
}
}
// non-WRE stuff that only depends on r in A-R case, for test stat denominators in replication regressions
// since the non-AR OLS code never creates an object for replication regresssions, in that case this is called on the DGP regression object
// depends on results of Estimate() only when doing OLS-style bootstrap on an overidentified IV/GMM regression--score bootstrap or A-R. Then kappa from DGP LIML affects Hessian, pH.
void boottestOLS::InitTestDenoms() {
real scalar d
if (parent->bootstrapt & (parent->scoreBS | parent->robust)) {
if (parent->granular | parent->purerobust) {
WXAR = smatrix(parent->df)
for (d=parent->df;d;d--)
WXAR[d].M = XAR[,d]
}
if (parent->NFE & parent->robust & (parent->FEboot | parent->scoreBS)==0 & parent->granular < parent->NErrClustCombs) { // make first factor of second term of (64) for c=∩ (c=1)
CT_XAR = smatrix(parent->df)
for (d=parent->df;d;d--)
CT_XAR[d].M = parent->crosstabFE(XAR[,d], *parent->pinfoCapData)
}
}
}
// partial fixed effects out of a data matrix
pointer(real matrix) scalar boottest::partialFE(pointer(real matrix) scalar pIn) {
real matrix Out, tmp; real scalar i
if (NFE & pIn) {
Out = *pIn
if (haswt)
for (i=NFE;i;i--) {
tmp = Out[FEs[i].is,]
Out[FEs[i].is,] = tmp :- FEs[i].sqrtwt * cross(FEs[i].wt, tmp)
}
else
for (i=NFE;i;i--) {
tmp = Out[FEs[i].is,]
Out[FEs[i].is,] = tmp :- cross(FEs[i].wt, tmp)
}
return(&Out)
}
return (pIn)
}
void boottest::new() {
ARubin = LIML = Fuller = WRE = small = scoreBS = auxwttype = ML = initialized = quietly = sqrt = ptype = robust = NFE = FEboot = granular = NErrClustCombs = subcluster = B = BFeas = interpolating = jk = 0
twotailed = null = dirty = willplot = v_sd = notplotted = FEdfadj = bootstrapt = 1
level = 95
ptol = 1e-6
confpeak = MaxMatSize = .
pY2 = pX1 = pX2 = py1 = pSc = pID = pFEID = pR1 = pR = pwt = &J(0,0,0)
pr1 = pr = &J(0,1,0)
pIDBootData = pIDBootAll = &.
}
// break loops in data structure topology to enable garbage collection
void boottest::close() {
DGP.parent = Repl.parent = NULL
}
void boottest::setdirty(real scalar _dirty, | real scalar noinitialize) {
dirty = _dirty
if (_dirty & noinitialize!=1)
initialized = 0
}
void boottest::setsqrt(real scalar _sqrt) {
if (_sqrt < sqrt) {
if (dirty==0) {
pDist = &(*pDist :* *pDist)
multiplier = multiplier * multiplier
}
} else
setdirty(1)
sqrt = _sqrt
}
void boottest::setptype(string scalar ptype) {
real scalar p
p = cross( (strtrim(strlower(ptype)) :== ("symmetric"\"equaltail"\"lower"\"upper")), 1::4 ) - 1
if (p<0)
_error(198, `"p-value type must be "symmetric", "equaltail", "lower", or "upper"."')
this.ptype = p
this.twotailed = p<=1
}
void boottest::setstattype(string scalar stattype) {
real scalar p
p = cross( (strtrim(strlower(stattype)) :== ("c"\"t")), 1::2 ) - 1
if (p<0)
_error(198, `"statistic type must be "t" or "c"."')
this.bootstrapt = p
setdirty(1)
}
void boottest::setX1(real matrix X1) {
this.pX1 = &X1; setdirty(1)
}
void boottest::setX2(real matrix X2) {
this.pX2 = &X2; setdirty(1)
}
void boottest::setY(real matrix y1) {
this.py1 = &y1; setdirty(1)
}
void boottest::setY2(real matrix Y2) {
this.pY2 = &Y2; setdirty(1)
}
void boottest::setobswt(real matrix wt, string scalar obswttype) {
this.pwt = &wt; this.obswttype = obswttype; setdirty(1)
}
void boottest::setsc(real matrix Sc) {
this.pSc = &Sc
setdirty(1)
}
void boottest::setML(real scalar ML) {
this.ML = ML; setdirty(1)
if (ML) setscoreBS(1)
}
void boottest::setLIML(real scalar LIML) {
this.LIML = LIML; setdirty(1)
}
void boottest::setARubin(real scalar ARubin) {
this.ARubin = ARubin; setdirty(1)
}
void boottest::setFuller(real scalar Fuller) {
this.Fuller = Fuller; setdirty(1)
}
void boottest::setkappa(real scalar kappa) { // kappa as in k-class
this.kappa = kappa; setdirty(1)
}
void boottest::setquietly(real scalar quietly )
this.quietly = quietly
void boottest::setbeta(real colvector beta) {
this.beta = beta; setdirty(1)
}
void boottest::setA(real matrix A) {
this.pA = &A; setdirty(1)
}
void boottest::setsmall(real scalar small) {
this.small = small; setdirty(1)
}
void boottest::setscoreBS (real scalar scoreBS) {
this.scoreBS = scoreBS; setdirty(1)
}
void boottest::setjk (real scalar jk) {
this.jk = jk; setdirty(1)
}
void boottest::setB(real scalar B) {
this.B = B
if (B==0)
setscoreBS(1)
setdirty(1)
}
void boottest::setnull (real scalar null) {
this.null = null; setdirty(1)
}
void boottest::setID (real matrix ID, | real scalar NBootClustVar, real scalar NErrClustVar) {
this.pID = &ID; this.NBootClustVar = editmissing(NBootClustVar,1); this.NErrClustVar=editmissing(NErrClustVar,editmissing(NBootClustVar,1)); setdirty(1)
if (cols(ID)) this.robust = 1
}
void boottest::setFEID(real matrix ID, real scalar NFE, | real scalar FEdfadj) {
this.pFEID = &ID; this.NFE = NFE; this.FEdfadj = editmissing(FEdfadj,1); setdirty(1)
}
void boottest::setlevel(real scalar level)
this.level = level
void boottest::setptol(real scalar ptol)
this.ptol = ptol
void boottest::setrobust(real scalar robust) {
this.robust = robust
if (robust==0) setID(J(0,0,0), 1, 1)
setdirty(1)
}
void boottest::setR1(real matrix R1, real matrix r1) {
this.pR1 = &R1; this.pr1 = &r1; setdirty(1)
}
void boottest::setR(real matrix R, real colvector r) {
this.pR = &R; this.pr = &r; q = rows(R); setdirty(1) // q can differ from df in ARubin test
}
void boottest::setwillplot(real scalar willplot) {
this.willplot = willplot
}
void boottest::setgrid(real rowvector gridmin, real rowvector gridmax, real rowvector gridpoints) {
this.gridmin = gridmin; this.gridmax = gridmax; this.gridpoints = gridpoints
}
void boottest::setmadjust(string scalar madjtype, real scalar NumH0s) {
this.madjtype = strlower(madjtype)
this.NumH0s = NumH0s
if (this.madjtype != "bonferroni" & this.madjtype != "sidak" & this.madjtype != "")
_error(198, `"Multiple-hypothesis adjustment type must be "Bonferroni" or "Sidak"."')
}
void boottest::setauxwttype(string scalar auxwttype) {
auxwttype = strlower(auxwttype)
if (.==(this.auxwttype = auxwttype=="rademacher" ? 0 : (auxwttype=="mammen" ? 1 : (auxwttype=="webb" ? 2 : (auxwttype=="normal" ? 3 : (auxwttype=="gamma" ? 4 : .))))))
_error(198, `"Wild type must be "Rademacher", "Mammen", "Webb", "Normal", or "Gamma"."')
setdirty(1)
}
void boottest::setMaxMatSize(real scalar MaxMatSize) {
this.MaxMatSize = MaxMatSize; setdirty(1)
}
real colvector boottest::getdist(| string scalar diststat) {
pointer (real rowvector) scalar _pnumer
if (dirty) boottest()
if (diststat == "numer") {
_pnumer = v_sd==1? pnumer : &(*pnumer / v_sd)
_sort( DistCDR = (*_pnumer)[|2\.|]' /*:+ *pr*/ , 1)
} else if (rows(DistCDR)==0)
if (cols(*pDist) > 1)
_sort( DistCDR = multiplier * (*pDist)[|2\.|]' , 1)
else
DistCDR = J(0,1,0)
return(DistCDR)
}
// get p value. Robust to missing bootstrapped values interpreted as +infinity.
real scalar boottest::getp(|real scalar classical) {
real scalar tmp; real scalar _p
if (dirty) boottest()
tmp = (*pDist)[1]
if (tmp == .) return (.)
if (B & classical==.)
if (sqrt & ptype != 3) {
if (ptype==0)
p = rowsum(-abs(tmp) :> -abs(*pDist)) / BFeas // symmetric p value; do so as not to count missing entries in *pDist
else if (ptype==1) // equal-tail p value
p = 2 * min((rowsum(tmp :> *pDist) , rowsum(-tmp:>- *pDist))) / BFeas
else
p = rowsum( tmp :> *pDist) / BFeas // lower-tailed p value
} else
p = rowsum(-tmp :> - *pDist) / BFeas // upper-tailed p value or p value based on squared stats
else {
tmp = tmp * multiplier
_p = small? Ftail(df, df_r, sqrt? tmp*tmp : tmp) : chi2tail(df, sqrt? tmp*tmp : tmp)
if (sqrt & twotailed==0) {
_p = _p / 2
if ((ptype==3) == (tmp<0))
_p = 1 - _p
}
if (classical != .)
return(_p)
p = _p // only save as the official p if this was not requested by plot() for AR case
}
return(p)
}
// numerator for full-sample test stat
real colvector boottest::getb() {
if (dirty) boottest()
return(v_sd == 1? (*pnumer)[,1] : (*pnumer)[,1] / v_sd)
}
// denominator for full-sample test stat
real matrix boottest::getV() {
if (dirty) boottest()
return (statDenom / ((v_sd == 1? smallsample : v_sd * v_sd * smallsample) * (sqrt? multiplier*multiplier : multiplier) * df))
}
// wild weights
real matrix boottest::getv()
return(v_sd==1? v[|.,2\.,.|] : v[|.,2\.,.|] / v_sd)
// Return number of bootstrap replications with feasible results
// Returns 0 if getp() not yet accessed, or doing non-bootstrapping tests
real scalar boottest::getrepsFeas()
return (BFeas)
real scalar boottest::getNBootClust()
return (Nstar)
// return number of replications, possibly reduced to 2^G
real scalar boottest::getreps()
return (B)
real scalar boottest::getpadj(|real scalar classical) {
real scalar _p
_p = dirty | classical != . ? getp(classical) : p
if (madjtype=="bonferroni") return(min((1, NumH0s*_p)))