This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
csdid_stats.mata
660 lines (582 loc) · 15.4 KB
/
csdid_stats.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
mata
class select_range {
real matrix selgvar
real matrix seltvar
real matrix selevent
real matrix selbal
}
// mata drop estat
// mata drop csdid_estat()
class csdid_estat {
// functions to create tables
void bvcv_asym()
void bvcv_clus()
// Aggregatprs
// void attgt()
void atts_asym()
void atts_wboot()
void group_att()
void calendar_att()
void simple_att()
void cevent_att()
void event_att()
void pretrend()
// makes WB and the table for WB
void mboot_any()
void make_table()
// void init only for testing
void init()
string matrix attgt_names()
real matrix aggte()
real matrix rtokens()
real matrix select_data()
real matrix wmult()
real matrix iqrse()
real scalar qtc()
real matrix erif, table
real matrix bb, vv, sderr, bsmean
string matrix onames
// to be initialized
real scalar t_stat
// Required Info
real scalar cilevel, bwtype, reps, max_mem, test_type
// info created
real scalar nclust, nobs, ggroups, ccalendar, eevent, error
// to transfer Range Info.
class select_range scalar range
}
// Creates ASYM VCV
void csdid_estat::init() {
cilevel = 0.95
bwtype = 1
reps = 999
max_mem = 1
test_type = 1
}
void csdid_estat::bvcv_asym(real matrix rif) {
bb = mean(rif)
nobs= rows(rif)
vv = quadcrossdev(rif,bb, rif,bb) :/ (nobs^2)
}
// Creates ASYM Cluster VCV
// Need to think how to Compress data when clustered
void csdid_estat::bvcv_clus(real matrix rif,
real matrix cvar) {
real matrix ord, info
bb = mean(rif)
nobs= rows(rif)
// sort //ord = order(cvar,1) //rif = rif[ord,] //cvar= cvar[ord,]
// Standard Errors
info = panelsetup(cvar,1)
nclust= rows(info)
real matrix sumrif
sumrif= panelsum(rif:-bb,info)
vv = quadcross(sumrif,sumrif):/(nobs^2)
// unsort //rif = rif[invorder(ord),] //cvar= cvar[invorder(ord),]
}
real matrix csdid_estat::aggte(real matrix rif,| real matrix wgt ) {
real matrix mn_all, mn_rif, mn_wgt
if (args()==1) {
wgt = J(1,cols(rif),1)
}
// Avg Effect
mn_rif = mean(rif)
mn_wgt = mean(wgt)
mn_all = sum(mn_rif:*mn_wgt):/sum(mn_wgt)
// gets agg rif
real matrix wgtw, attw
wgtw = (mn_wgt ) :/sum(mn_wgt)
attw = (mn_rif ) :/sum(mn_wgt)
// r1 r2 r3
real matrix r1 , r2 , r3
r1 = (wgtw:*(rif :-mn_rif))
r2 = (attw:*(wgt :-mn_wgt ))
r3 = (wgt :- mn_wgt) :* (mn_all :/ sum(mn_wgt) )
// Aggregates into 1
return(rowsum(r1):+rowsum(r2):-rowsum(r3):+mn_all)
}
// Will use Separate function for WB bc it process data differently
void csdid_estat::atts_asym(class csdid scalar csdid){
// Estimate effects
error = 0
if (test_type==1) {
// ATTGT
erif=select(csdid.frif,select_data(csdid)')
if (length(csdid.cvar)==0) {
bvcv_asym(erif)
}
else {
bvcv_clus(erif,csdid.cvar)
}
// names
onames=attgt_names(csdid)'
}
else if (test_type==2) {
//simple att
simple_att(csdid)
if (length(csdid.cvar)==0) bvcv_asym(erif)
else bvcv_clus(erif,csdid.cvar)
onames = J(rows(onames),1,""),onames
}
else if (test_type==3) {
//group att
group_att(csdid)
if (length(csdid.cvar)==0) bvcv_asym(erif)
else bvcv_clus(erif,csdid.cvar)
onames = J(rows(onames),1,""),onames
}
else if (test_type==4) {
//calendar att
calendar_att(csdid)
if (length(csdid.cvar)==0) bvcv_asym(erif)
else bvcv_clus(erif,csdid.cvar)
onames = J(rows(onames),1,""),onames
}
else if (test_type==5) {
//event att
event_att(csdid)
if (length(csdid.cvar)==0) bvcv_asym(erif)
else bvcv_clus(erif,csdid.cvar)
onames = J(rows(onames),1,""),onames
}
else if (test_type==6) {
//cevent att
cevent_att(csdid)
if (length(csdid.cvar)==0) bvcv_asym(erif)
else bvcv_clus(erif,csdid.cvar)
onames = J(rows(onames),1,""),onames
}
if (error == 0) {
st_matrix("_bb",bb)
st_matrix("_vv",vv)
st_matrixcolstripe("_bb", onames)
st_matrixrowstripe("_vv", onames)
st_matrixcolstripe("_vv", onames)
}
else {
stata(`"display in red "There was an error estimating aggregation" "')
}
// drops vv, the largest matrix
vv=0
erif = 0
}
void csdid_estat::make_table(){
real matrix serr
real matrix ci
// Standard error
serr = iqrse(bsmean)
// Critical value
t_stat =qtc(bsmean:/serr, cilevel )
// bb are point estimates
ci = (bb:-serr:*t_stat) \ (bb:+serr:*t_stat)
table = bb \ serr \ (bb:/serr) \ ci \ J(1,cols(bb),t_stat)
bsmean=J(0,0,.)
}
void csdid_estat::atts_wboot(class csdid scalar csdid){
// Estimate effects
if (test_type==1) {
// ATTGT
error=0
erif=select(csdid.frif,select_data(csdid)')
onames=attgt_names(csdid)'
mboot_any(csdid)
make_table()
// names
}
else if (test_type==2) {
//simple att
simple_att(csdid)
onames = J(rows(onames),1,""),onames
mboot_any(csdid)
make_table()
}
else if (test_type==3) {
//group att
group_att(csdid)
onames = J(rows(onames),1,""),onames
mboot_any(csdid)
make_table()
}
else if (test_type==4) {
//calendar att
calendar_att(csdid)
onames = J(rows(onames),1,""),onames
mboot_any(csdid)
make_table()
}
else if (test_type==5) {
//event att
event_att(csdid)
onames = J(rows(onames),1,""),onames
mboot_any(csdid)
make_table()
}
else if (test_type==6) {
//cevent att
cevent_att(csdid)
onames = J(rows(onames),1,""),onames
mboot_any(csdid)
make_table()
}
if (error == 0) {
string matrix xnames
xnames =J(6,1,""),("b"\"se"\"t"\"ll"\"ul"\"crit")
st_matrix("_table",table)
st_matrixrowstripe("_table", xnames)
st_matrixcolstripe("_table", onames)
}
else {
stata(`"display in red "There was an error estimating aggregation" "')
}
// drops vv, the largest matrix
}
string csdid_estat::attgt_names(class csdid scalar csdid){
real scalar i
string matrix toreturn
real matrix sfgtvar
sfgtvar=select(csdid.fgtvar,select_data(csdid))
toreturn=J(2,rows(sfgtvar),"")
for(i=1;i<=cols(toreturn);i++){
toreturn[1,i]=sprintf("g%f",sfgtvar[i,1])
toreturn[2,i]=sprintf("t%f_%f",sfgtvar[i,3],sfgtvar[i,4])
}
return(toreturn)
}
real matrix csdid_estat::rtokens(string scalar totok){
return(uniqrows(strtoreal(tokens(totok))' )')
}
////////////////////////////////////////////////////////////////////////////////
/// Group Aggregations
////////////////////////////////////////////////////////////////////////////////
void csdid_estat::group_att(class csdid scalar csdid ){
// Counter i
// kgroups (max)
real scalar i , iic
real matrix toselect0,toselect, aux_rif, sumwgt
real matrix aux_wgt, aux
toselect0 =select_data(csdid)'
//:*csdid.convar'
error=0
if (sum(toselect0)>0) {
ggroups = rows(csdid.sgvar)
nobs = rows(csdid.frif)
onames=J(ggroups+1,1,"")
onames[1,]="GAverage"
aux =J(nobs,ggroups,.)
sumwgt =J(nobs,ggroups,.)
iic=0
for(i=1;i<=ggroups;i++){
// select
toselect=toselect0:*(csdid.fgtvar[,1]:==csdid.sgvar[i]:& csdid.eventvar:>=0)'
if (sum(toselect)>0) {
iic++
// if any selected -> Estimate
onames[iic+1,] = sprintf("g%f", csdid.sgvar[i])
aux_wgt = select(csdid.frwt,toselect)
aux [,iic] = aggte(select(csdid.frif,toselect),aux_wgt )
sumwgt[,iic] = rowsum(aux_wgt):/cols(aux_wgt)
}
}
// Drop Zeroes
sumwgt = sumwgt[,1..iic]
aux = aux[,1..iic]
onames = onames[1..iic+1,]
sumwgt = colsum(sumwgt)
erif= aggte(aux,sumwgt ), aux
}
else {
error=1
}
}
////////////////////////////////////////////////////////////////////////////////
/// Calendaar Aggregations
////////////////////////////////////////////////////////////////////////////////
void csdid_estat::calendar_att(class csdid scalar csdid ){
// Counter i
// kgroups (max)
real scalar i , iic
real matrix toselect0,toselect, aux_rif, sumwgt
real matrix aux_wgt, aux
toselect0 =select_data(csdid)'
//:*csdid.convar'
error=0
if (sum(toselect0)>0) {
ccalendar = rows(csdid.stvar)
nobs = rows(csdid.frif)
onames=J(ccalendar+1,1,"")
onames[1,]="TAverage"
aux =J(nobs,ccalendar,.)
//sumwgt =J(nobs,ccalendar,.)
iic=0
for(i=1;i<=ccalendar;i++){
// select
toselect=toselect0:*(csdid.fgtvar[,2]:==csdid.stvar[i] :& csdid.eventvar:>=0)'
if (sum(toselect)>0) {
iic++
// if any selected -> Estimate
onames[iic+1,] = sprintf("t%f", csdid.stvar[i])
aux_wgt = select(csdid.frwt,toselect)
aux [,iic] = aggte(select(csdid.frif,toselect),aux_wgt )
//sumwgt[i,] = rowsum(aux_wgt):/cols(aux_wgt)
}
}
//sumwgt = sumwgt[,1..iic]
aux = aux[,1..iic]
onames = onames[1..iic+1,]
erif = aggte(aux, J(1,cols(aux),1) ), aux
}
else {
error=1
}
}
void csdid_estat::pretrend(class csdid scalar csdid ){
// should be always drop v?
real scalar df
real matrix toselect,toselect0
toselect0=select_data(csdid)'
//:*csdid.convar'
toselect=toselect0:*(csdid.eventvar :< 0)'
error=0
if (sum(toselect)>0) {
if (length(csdid.cvar)==0) bvcv_asym(select(csdid.frif,toselect))
else bvcv_clus(select(csdid.frif,toselect),csdid.cvar)
real scalar chi2
chi2=bb*invsym(vv)*bb'
df = cols(bb)
// Drops V matrix
vv=0
st_numscalar("chi2_",chi2)
st_numscalar("df_",df)
st_numscalar("pchi2_",chi2tail(df,chi2))
}
else {
error = 1
}
}
void csdid_estat::simple_att(class csdid scalar csdid ){
real matrix toselect,toselect0
// nobs = rows(csdid.frif)
// Select based on some criteria
toselect0 = select_data(csdid)'
//:*csdid.convar'
toselect = toselect0:*(csdid.eventvar:>=0)'
error = 0
if (sum(toselect)>0) {
onames = "SimpleATT"
erif = aggte(select(csdid.frif,toselect),select(csdid.frwt,toselect) )
}
else {
error = 1
}
}
real matrix csdid_estat::select_data(class csdid scalar csdid){
real matrix toselect1,toselect2,toselect3,toselect4
real scalar i, i1, i2, i3, i4
real scalar rws
// Can we adapt this to other?. Yes we should be able to!
rws = rows(csdid.eventvar)
toselect1 =toselect2=toselect3=toselect4= J(rws,1,1)
i1=length(range.selgvar)
i2=length(range.seltvar)
i3=length(range.selevent)
i4=length(range.selbal)
if (i1>0) {
toselect1 =J(rws,1,0)
for(i=1;i<=i1;i++){
toselect1=toselect1:+(csdid.fgtvar[,1]:==range.selgvar[i])
}
}
if (i2>0){
toselect2 =J(rws,1,0)
for(i=1;i<=i2;i++){
toselect2=toselect2:+(csdid.fgtvar[,2]:==range.seltvar[i])
}
}
if (i3>0){
toselect3 =J(rws,1,0)
for(i=1;i<=i3;i++){
toselect3=toselect3:+(csdid.eventvar[,1]:==range.selevent[i])
}
}
if (i4>0){
toselect4 =J(rws,1,0)
for(i=1;i<=i4;i++){
toselect4=toselect4:+(csdid.eventvar[,1]:==range.selbal[i])
}
real matrix gg
// select Ggroups
gg = select(csdid.fgtvar[,1],toselect4:>0)
gg = uniqrows(gg,1) ; gg=select(gg[,1],gg[,2]:==max(gg[,2]))
// do gvar again
toselect1 =J(rws,1,0)
for(i=1;i<=length(gg);i++){
toselect1=toselect1:+(csdid.fgtvar[,1]:==gg[i])
}
if (i3>0) {
toselect4=toselect3
}
}
// tosel1 gvar
// tosel2 tvar
// tosel3 evar
// tosel4 evar
// if rbalance ---> tsel4 event Balance & tsel1 group balance
// \-> But if tsel3 exist It trumps tsel4
return( csdid.convar:* (toselect1:*toselect2:*toselect3:*toselect4):>0 )
}
void csdid_estat::cevent_att(class csdid scalar csdid ){
real matrix toselect
toselect = select_data(csdid)'
error = 0
if (sum(toselect)>0) {
onames = "ATTC"
erif = aggte(select(csdid.frif,toselect),select(csdid.frwt,toselect) )
}
else {
error = 1
}
}
////////////////////////////////////////////////////////////////////////////////
/// event Aggregations
////////////////////////////////////////////////////////////////////////////////
void csdid_estat::event_att(class csdid scalar csdid){
// Counter i
// kgroups (max)
real scalar i , iic , ievent
real matrix toselect,toselect0, aux_rif, sumwgt
real matrix aux_wgt, iim
real matrix aux_event, aux
//
toselect0=select_data(csdid)'
error = 0
if (sum(toselect0)>0) {
ievent = 0
aux_event=select(csdid.eventvar,toselect0')
eevent = rows(csdid.sevent)
nobs = rows(csdid.frif)
onames=J(eevent+sum(csdid.sevent:>=0)+sum(csdid.sevent:< 0),1,"")
// Is there a Pre or post
iic = 0
if (sum(aux_event:<0 )) {
iic++
ievent++
onames[iic,]="Pre_avg"
}
if (sum(aux_event:>=0)) {
iic++
ievent++
onames[iic,]="Post_avg"
}
aux =J(nobs,eevent,.)
iim =J(1,0,.)
for(i=1;i<=eevent;i++){
// select
toselect=toselect0:*(csdid.eventvar:==csdid.sevent[i])'
if (sum(toselect)>0) {
iic++
// if any selected -> Estimate
if (csdid.sevent[i]<0) {
onames[iic,] = sprintf("tm%f", abs(csdid.sevent[i]))
iim = iim , 0
}
else {
onames[iic,] = sprintf("tp%f", abs(csdid.sevent[i]))
iim = iim , 1
}
aux_wgt = select(csdid.frwt,toselect)
aux[,iic-ievent] = aggte(select(csdid.frif,toselect),aux_wgt )
//sumwgt[i,] = rowsum(aux_wgt):/cols(aux_wgt)
}
}
// drop zeroes
aux = aux[,1..iic-ievent]
onames = onames[1..iic,]
// iim ids pre and post effects
erif =J(nobs,0,.)
if (sum(iim:==0)) {
erif =erif, aggte(select(aux,iim:==0) )
}
if (sum(iim:==1)) {
erif =erif, aggte(select(aux,iim:==1) )
}
// erif
erif = erif, aux
}
else {
error=1
}
}
/// Auxiliary programs
// Gets the pth value of the rowmas matrix sent
// used to get t-critical for uniform matrix
real scalar csdid_estat::qtc(real matrix y, real scalar p){
// idea. maximizar
y =rowmax(y)
_sort(y,1)
if ( p>0 & p<1) return(y[ ceil( (rows(y)+1)*p ) ])
else if (p==0) return(y[ 1 ])
else if (p==1) return(y[ rows(y) ])
}
real matrix csdid_estat::iqrse(real matrix y) {
real scalar q25,q75
// saves q25 and q75
q25=ceil(rows(y)*.25);q75=ceil(rows(y)*.75)
real scalar j
real matrix iqrs, sy
iqrs=J(1,cols(y),0)
for(j=1;j<=cols(y);j++){
sy=sort(y[,j],1)
iqrs[,j]=(sy[q75,]-sy[q25,]):/(invnormal(.75)-invnormal(.25) )
}
return(iqrs)
}
real matrix csdid_estat::wmult(real scalar mdsize_eff) {
real scalar k1, k2
k1=((1+sqrt(5))/(2*sqrt(5)))
k2=0.5*(1+sqrt(5))
if (bwtype==1) return( k2:-sqrt(5)*rbinomial(nobs,mdsize_eff,1, k1) )
else if (bwtype==2) return( 1 :-2* rbinomial(nobs,mdsize_eff,1,0.5) )
}
void csdid_estat::mboot_any(class csdid scalar csdid ) {
// RIF is FED from out.
real matrix mean_rif
real scalar i, ncols, xnobs
real scalar coord1
real matrix ccrd
//real matrix bsmean
// First Means
bb=mean_rif=mean(erif)
// Re-estimate RIF
erif = erif:-mean_rif
// contains all iterations, for reps parameters
ncols=cols(erif)
xnobs=rows(erif)
//
bsmean=J(reps,ncols,0)
// Options for cluster.
real matrix info
if (length(csdid.cvar)>0) {
info=panelsetup(csdid.cvar,1)
erif= panelsum(erif,info)
}
nobs =rows(erif)
// check Repetitions and parameters
// This is to use BLOCKS of Stuff. But not sure about how large it can be
real scalar mdsize_eff, mdsize, mmax_mem
// 134217728 <-- Total number of observations in 1gb of memory. We can select More
// Need to initialize max men<- Global. MMaxmem local
if (max_mem==0) mmax_mem=134217728
else mmax_mem=max_mem*134217728
mdsize = min( (reps, max( ( 1 , floor(mmax_mem/nobs/ncols) ) ) ) )
coord1=1
mdsize_eff = mdsize
for(i=1;i<=reps;i=i+mdsize){
ccrd = (coord1,1) \ ( coord1+mdsize_eff-1 ,ncols)
coord1 = coord1+mdsize_eff
bsmean[|ccrd|]= cross(erif, wmult(mdsize_eff))':/xnobs
mdsize_eff = min( (mdsize, reps-(coord1-1)) )
}
erif = J(0,0,.)
//return(bsmean)
}
end