Sunday, May 31, 2020

Matrix not found error after just listing the matrix

Dear All,
Sorry for a very lengthy post, but I am trying to explain the problem comprehensively. I am attempting to estimate an event-study to evaluate the effect of a set of policy changes. I want to see whether the policies had a differential effect in rural areas and thus include event-time dummies with interaction for an indicator for rural.

My data looks something like:

Code:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double dex byte(rural ds_FirstConfirmedCase_1 ds_FirstConfirmedCase_2) long county_d float date long state_d
121.1581 0 1 0 1558 21965 43
 74.4307 1 1 0  714 21937 18
 52.4785 1 0 0 1829 22009 47
 31.8169 1 0 0 1846 22019 47
 35.5682 0 0 0 1169 22029 33
 53.1551 0 0 0 1624 21998 43
 34.6536 1 0 0  576 22029 16
 56.2691 1 0 0 1024 21990 26
213.1753 0 0 0  241 21970 10
 91.7799 1 0 0  633 21976 17
 63.2084 0 0 0  669 22021 18
 72.0173 0 1 0 1538 21957 41
  47.946 0 0 0 1600 21996 43
  66.612 0 0 0 1686 22007 44
 42.2578 0 0 0 1431 22033 38
102.8714 0 1 0  883 21944 24
275.4706 0 0 0  316 21987 11
 71.4822 0 0 0  250 21999 10
 39.3476 0 0 0 1916 22022 48
 44.3592 1 1 0 1333 21964 36
 89.0314 0 1 0 1862 21977 47
 38.3352 0 0 0 1560 22007 43
 29.2048 0 0 0 1107 21996 31
 24.7918 1 0 0 1168 22000 33
106.3698 0 1 0  508 21969 15
 86.6527 1 1 0  297 21970 11
 83.5633 1 0 0  239 22033 10
 96.2455 0 1 0  538 21962 15
 38.4943 1 0 0 1296 22020 36
301.6847 1 1 0 1262 21953 34
 68.3879 0 0 0 1863 22029 47
 30.8103 0 0 0  205 22036  7
 23.5787 1 0 0  771 21984 20
 24.5118 1 0 0  498 22004 15
 26.4736 1 1 0 1974 21950 50
 70.9563 0 0 0 1116 22008 32
  44.122 0 0 0 1069 22035 28
 51.8799 1 1 0  709 21965 18
 59.7964 1 0 0    9 21983  1
 42.5701 1 0 0 1578 21997 43
 56.6789 1 0 0 1146 21989 33
  41.445 0 0 0 1314 22010 36
 52.1805 1 0 0 1664 22015 44
 94.2434 1 1 0 1461 21968 39
 45.5013 1 1 0  625 21942 17
 97.7381 1 0 0 1370 22020 37
108.7252 0 1 0  806 21956 22
182.1346 1 1 0  503 21939 15
 65.0327 0 1 0  760 21965 20
 236.405 0 0 0 1660 21984 44
157.5645 1 1 0  493 21960 15
 80.0207 1 1 0  547 21954 15
 64.1631 0 0 0  841 22035 23
 15.2419 1 1 0  767 21955 20
 93.2231 1 0 0  569 21973 16
 38.5618 0 0 0 1744 22004 44
  95.841 0 1 0 1670 21941 44
 75.7985 1 1 0  112 21955  4
 28.3126 0 0 0  387 22035 12
107.8034 1 0 0 1265 21972 34
  36.874 0 0 0  586 22008 16
 32.3622 0 0 0  442 22017 14
 99.8781 1 0 0 1077 21982 29
 100.907 0 1 0 1548 21944 42
 68.8354 1 0 0 1949 22036 49
 52.2867 0 0 0  356 22019 11
 272.789 0 1 0  642 21946 17
 42.6058 1 0 0 1578 22006 43
100.9722 1 1 0   42 21961  1
 16.5106 1 0 0  400 22016 13
 23.5787 0 0 0  143 21995  5
 66.8417 0 0 0 1620 21994 43
 22.4997 1 1 0 1485 21969 39
 26.1053 0 0 0 1152 21995 33
 93.6394 0 1 0 1096 21955 31
 34.3344 0 0 0 1094 22031 31
 80.7337 1 1 0   67 21953  3
 44.9029 1 1 0 1943 21944 49
 87.6698 1 1 0 1987 21936 50
 66.7398 1 0 0 1593 22025 43
121.8709 0 0 0 1372 21992 37
 23.8923 1 0 0  881 22009 24
 33.3761 0 0 0  907 22005 24
 59.8646 1 1 0  889 21970 24
 82.9484 1 1 0 1639 21983 44
 52.2777 1 1 0 1477 21951 39
 60.7247 0 0 0  668 21995 18
140.2874 0 1 0  216 21949 10
 71.7848 1 1 0 1872 21942 47
159.0968 0 0 0  891 21976 24
 68.3546 1 1 0  312 21942 11
172.3867 1 1 0 1567 21959 43
 44.7498 1 0 0  641 22001 17
121.0611 0 0 0 1402 22015 37
 74.2227 0 1 0 1787 21943 45
 34.9993 1 0 0   85 21997  4
 214.749 0 1 0  782 21959 21
645.7959 0 1 0 1876 21938 47
410.3959 0 0 1 1687 21959 44
 89.4759 1 1 0 1077 21970 29
end
format %d date
label values county_d county_d
label def county_d 9 "01019", modify
label def county_d 42 "01093", modify
label def county_d 67 "04007", modify
label def county_d 85 "05021", modify
label def county_d 112 "05113", modify
label def county_d 143 "06047", modify
label def county_d 205 "09007", modify
label def county_d 216 "12005", modify
label def county_d 239 "12063", modify
label def county_d 241 "12071", modify
label def county_d 250 "12089", modify
label def county_d 297 "13069", modify
label def county_d 312 "13107", modify
label def county_d 316 "13117", modify
label def county_d 356 "13225", modify
label def county_d 387 "15009", modify
label def county_d 400 "16057", modify
label def county_d 442 "17097", modify
label def county_d 493 "18031", modify
label def county_d 498 "18041", modify
label def county_d 503 "18051", modify
label def county_d 508 "18061", modify
label def county_d 538 "18133", modify
label def county_d 547 "18151", modify
label def county_d 569 "19029", modify
label def county_d 576 "19055", modify
label def county_d 586 "19085", modify
label def county_d 625 "20021", modify
label def county_d 633 "20059", modify
label def county_d 641 "20113", modify
label def county_d 642 "20121", modify
label def county_d 668 "21049", modify
label def county_d 669 "21059", modify
label def county_d 709 "21207", modify
label def county_d 714 "21217", modify
label def county_d 760 "23001", modify
label def county_d 767 "23015", modify
label def county_d 771 "23025", modify
label def county_d 782 "24017", modify
label def county_d 806 "25017", modify
label def county_d 841 "26081", modify
label def county_d 881 "27021", modify
label def county_d 883 "27027", modify
label def county_d 889 "27047", modify
label def county_d 891 "27053", modify
label def county_d 907 "27109", modify
label def county_d 1024 "29157", modify
label def county_d 1069 "31155", modify
label def county_d 1077 "32007", modify
label def county_d 1094 "34005", modify
label def county_d 1096 "34009", modify
label def county_d 1107 "34031", modify
label def county_d 1116 "35013", modify
label def county_d 1146 "36037", modify
label def county_d 1152 "36053", modify
label def county_d 1168 "36089", modify
label def county_d 1169 "36091", modify
label def county_d 1262 "37189", modify
label def county_d 1265 "37195", modify
label def county_d 1296 "39043", modify
label def county_d 1314 "39081", modify
label def county_d 1333 "39125", modify
label def county_d 1370 "40047", modify
label def county_d 1372 "40051", modify
label def county_d 1402 "40143", modify
label def county_d 1431 "41067", modify
label def county_d 1461 "42063", modify
label def county_d 1477 "42097", modify
label def county_d 1485 "42117", modify
label def county_d 1538 "45087", modify
label def county_d 1548 "46099", modify
label def county_d 1558 "47015", modify
label def county_d 1560 "47019", modify
label def county_d 1567 "47035", modify
label def county_d 1578 "47059", modify
label def county_d 1593 "47099", modify
label def county_d 1600 "47115", modify
label def county_d 1620 "47163", modify
label def county_d 1624 "47173", modify
label def county_d 1639 "48025", modify
label def county_d 1660 "48099", modify
label def county_d 1664 "48123", modify
label def county_d 1670 "48145", modify
label def county_d 1686 "48199", modify
label def county_d 1687 "48201", modify
label def county_d 1744 "48395", modify
label def county_d 1787 "49045", modify
label def county_d 1829 "51083", modify
label def county_d 1846 "51143", modify
label def county_d 1862 "51187", modify
label def county_d 1863 "51191", modify
label def county_d 1872 "51590", modify
label def county_d 1876 "51660", modify
label def county_d 1916 "53067", modify
label def county_d 1943 "54067", modify
label def county_d 1949 "54097", modify
label def county_d 1974 "55065", modify
label def county_d 1987 "55097", modify
label values state_d state_d
label def state_d 1 "01", modify
label def state_d 3 "04", modify
label def state_d 4 "05", modify
label def state_d 5 "06", modify
label def state_d 7 "09", modify
label def state_d 10 "12", modify
label def state_d 11 "13", modify
label def state_d 12 "15", modify
label def state_d 13 "16", modify
label def state_d 14 "17", modify
label def state_d 15 "18", modify
label def state_d 16 "19", modify
label def state_d 17 "20", modify
label def state_d 18 "21", modify
label def state_d 20 "23", modify
label def state_d 21 "24", modify
label def state_d 22 "25", modify
label def state_d 23 "26", modify
label def state_d 24 "27", modify
label def state_d 26 "29", modify
label def state_d 28 "31", modify
label def state_d 29 "32", modify
label def state_d 31 "34", modify
label def state_d 32 "35", modify
label def state_d 33 "36", modify
label def state_d 34 "37", modify
label def state_d 36 "39", modify
label def state_d 37 "40", modify
label def state_d 38 "41", modify
label def state_d 39 "42", modify
label def state_d 41 "45", modify
label def state_d 42 "46", modify
label def state_d 43 "47", modify
label def state_d 44 "48", modify
label def state_d 45 "49", modify
label def state_d 47 "51", modify
label def state_d 48 "53", modify
label def state_d 49 "54", modify
label def state_d 50 "55", modify
------------------ copy up to and including the previous line ------------------
Then I estimate the regression, estimate the total effect in rural areas using margins and try to save the output in a matrix to graph. Its lengthy (so I am showing a subset of the code and attaching the rest) but seems to be going well until I get an error that the estimates matrix is not found right after its listed.:

Code:
*Fit the event study regressions
foreach T in FirstConfirmedCase SchoolClose StayAtHome FirstDeath{
    gen b_`T' = .
 gen upper_`T' = .
 gen lower_`T' = .
 gen br_`T' = .
 gen upperr_`T' = .
 gen lowerr_`T' = .
 gen bi_`T' = .
 gen upperi_`T' = .
 gen loweri_`T' = .

 reghdfe dex rural##(ds_`T'_1-ds_`T'_20 ds_`T'_22-ds_`T'_43), ///
 absorb(county_d date state_d##date, save) cluster(county_d)
 
 estimates store c`T'
 
 local row = 0
   
    forvalues t = 2(1)20 {
        local ++row
        qui replace b_`T' = _b[1.ds_`T'_`t'] in `row'
        qui replace upper_`T' = _b[1.ds_`T'_`t'] + 1.96*_se[1.ds_`T'_`t'] in `row'
        qui replace lower_`T' = _b[1.ds_`T'_`t']-  1.96*_se[1.ds_`T'_`t'] in `row'
  qui replace br_`T' = _b[1.rural#1.ds_`T'_`t'] in `row'
        qui replace upperr_`T' = _b[1.rural#1.ds_`T'_`t'] + 1.96*_se[1.rural#1.ds_`T'_`t'] in `row'
        qui replace lowerr_`T' = _b[1.rural#1.ds_`T'_`t']-  1.96*_se[1.rural#1.ds_`T'_`t'] in `row'
    }
    local ++row
    qui replace b_`T'= 0 in `row'
 qui replace br_`T'= 0 in `row'
    forvalues t = 22/42 {
        local ++row
        qui replace b_`T'  = _b[1.ds_`T'_`t'] in `row'
        qui replace upper_`T' = _b[1.ds_`T'_`t'] + 1.96*_se[1.ds_`T'_`t'] in `row'
        qui replace lower_`T' = _b[1.ds_`T'_`t'] - 1.96*_se[1.ds_`T'_`t'] in `row'
  qui replace br_`T'  = _b[1.rural#1.ds_`T'_`t'] in `row'
        qui replace upperr_`T' = _b[1.rural#1.ds_`T'_`t'] + 1.96*_se[1.rural#1.ds_`T'_`t'] in `row'
        qui replace lowerr_`T' = _b[1.rural#1.ds_`T'_`t'] - 1.96*_se[1.rural#1.ds_`T'_`t'] in `row'
    }

 local ys 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 foreach y of local ys{
 qui reghdfe dex rural##(ds_`T'_1-ds_`T'_20 ds_`T'_22-ds_`T'_43), ///
 absorb(county_d date state_d##date, save) cluster(county_d)
 margins, expression(_b[1.ds_`T'_`y']+_b[1.rural#1.ds_`T'_`y']) post
  /*qui*/ esttab, ci
  mat ci`T'`y'= r(coefs)
}
mat ci`T'21=(0,0,0,0)
mat ci`T' = ci`T'1\ci`T'2\ci`T'3\ci`T'4\ci`T'5\ci`T'6\ci`T'7\ci`T'8\ci`T'9\ci`T'10\ci`T'11\ci`T'12\ci`T'13\ci`T'14\ci`T'15\ci`T'16\ci`T'17\ci`T'18\ci`T'19\ci`T'20\ci`T'21\ci`T'22\ci`T'23\ci`T'24\ci`T'25\ci`T'26\ci`T'27\ci`T'28\ci`T'29\ci`T'30\ci`T'31\ci`T'32\ci`T'33\ci`T'34\ci`T'35\ci`T'36\ci`T'37\ci`T'38\ci`T'39\ci`T'40\ci`T'41\ci`T'42\ci`T'43
mat list ci`T'
mat colnames ci`T' ="tot_rural`T'" "tot_rural_uci`T'" "tot_rural_lci`T'" "tot_rural_p`T'"
matrix rownames ci`T'= "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"
//mat ci`T'p= ci`T''

forvalues ti = 2(1)20 {
        local ++row
        qui replace bi_`T' = ci`T'[`ti',1] in `row'
        qui replace upperi_`T' = ci`T'[`ti',2] in `row'
        qui replace loweri_`T' = ci`T'[`ti',3] in `row'
    }
    local ++row
    qui replace bi_`T'= 0 in `row'
    forvalues t = 22/42 {
        local ++row
        qui replace bi_`T' = ci`T'[`ti',1] in `row'
        qui replace upperi_`T' = ci`T'[`ti',2] in `row'
        qui replace loweri_`T' = ci`T'[`ti',3] in `row'
    }

 
   # delimit ;
    twoway
        (rarea upper_`T' lower_`T' timeG if inrange(timeG, -20,20), color(gs12%35))
        (connected b_`T' timeG if inrange(timeG, -20,-2),
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small))
  (connected b_`T' timeG if inrange(timeG, 0,20),
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small))
        (function y = 0, range(-20 20) lcolor(gs10)),
        xline(-.5 , lwidth(2.2) lcolor(gs10) )
        xsize(4) ysize(2)
        xtitle("") ytitle("Mixing Index", size(vsmall))
  graphregion(color(white))
        xlabel(-20(5)20, labsize(small))
  ylabel(/*-400(150)200*/, labsize(small) )  
        legend(off)
        graphregion(margin(r+5))
        title("`Title`T''", pos(11) size(3.5))
        name(`T', replace)
     ;
    # delimit cr
  graph export "$plotdir/ES_`var'.png",  replace  width(4000) 
 
 # delimit ;
    twoway
        (rarea upperr_`T' lowerr_`T' timeG if inrange(timeG, -20,20), color(gs12%35))
        (connected br_`T' timeG if inrange(timeG, -20,-2),
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small))
  (connected br_`T' timeG if inrange(timeG, 0,20),
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small))
        (function y = 0, range(-20 20) lcolor(gs10)),
        xline(-.5 , lwidth(2.2) lcolor(gs10) )
        xsize(4) ysize(2)
        xtitle("") ytitle("Mixing Index", size(vsmall))
  graphregion(color(white))
        xlabel(-20(5)20, labsize(small))
  ylabel(/*-400(150)200*/, labsize(small) )  
        legend(off)
        graphregion(margin(r+5))
        title("`Title`T''", pos(11) size(3.5))
        name(r`T', replace)
     ;
    # delimit cr
  graph export "$plotdir/ESr_`var'.png",  replace  width(4000) 
 
 # delimit ;
    twoway
        (rarea upperi_`T' loweri_`T' timeG if inrange(timeG, -20,20), color(gs12%35))
        (connected bi_`T' timeG if inrange(timeG, -20,-2),
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small))
  (connected bi_`T' timeG if inrange(timeG, 0,20),
            mcolor(cranberry) lwidth(medium) lcolor(cranberry) msize(small))
        (function y = 0, range(-20 20) lcolor(gs10)),
        xline(-.5 , lwidth(2.2) lcolor(gs10) )
        xsize(4) ysize(2)
        xtitle("") ytitle("Mixing Index", size(vsmall))
  graphregion(color(white))
        xlabel(-20(5)20, labsize(small))
  ylabel(/*-400(150)200*/, labsize(small) )  
        legend(off)
        graphregion(margin(r+5))
        title("`Title`T''", pos(11) size(3.5))
        name(i`T', replace)
     ;
    # delimit cr
  graph export "$plotdir/ESi_`var'.png",  replace  width(4000) 
 
 rename __hdfe2__ datefe`T'
 
}
The event-study runs, the margins all run to estimate the full effect in rural areas (see attached) and the matrix ciFirstConfirmedCase is generated and then I get the following error (confusing since the matrix was just generated and listed):
Code:
ciFirstConfirmedCase[43,4]
           active:     active:     active:     active:
                b        ci_l        ci_u           p
_cons  -33.992753  -46.021658  -21.963848   3.047e-08
_cons  -19.201088  -32.343054  -6.0591215   .00418846
_cons  -24.223434  -42.416965  -6.0299033   .00906595
_cons  -19.428991  -32.896787  -5.9611955   .00469138
_cons  -17.670705  -33.584708  -1.7567012   .02953143
_cons   -10.69152  -26.332381   4.9493411   .18032351
_cons  -21.091456  -34.776707  -7.4062037   .00252224
_cons  -15.791958  -29.443427  -2.1404892   .02337327
_cons   .78847254  -14.131996   15.708941   .91750715
_cons  -.21585848  -15.417481   14.985764   .97779702
_cons  -6.0163747  -19.340196    7.307447   .37614467
_cons   10.879587   -4.749868   26.509043   .17246646
_cons  -.82162958  -14.251595   12.608335   .90455578
_cons  -7.8447947  -20.425515    4.735926   .22165129
_cons  -2.0624241  -13.437858   9.3130093    .7223263
_cons   4.5547978   -9.334093   18.443689   .52037908
_cons   3.5513728   -8.942559   16.045305   .57744853
_cons  -1.1591633  -16.744876   14.426549   .88410352
_cons  -3.3511476  -16.348078   9.6457827   .61330602
_cons   3.2054066  -23.388244   29.799057   .81324652
   r1           0           0           0           0
_cons  -5.0311321  -22.144137   12.081873    .5644672
_cons  -3.3002542  -15.772785   9.1722769   .60403274
_cons  -19.825333  -31.621159  -8.0295082   .00098728
_cons  -8.1466876  -22.243875   5.9504996   .25736021
_cons  -9.6166914  -23.845398   4.6120148   .18527996
_cons  -7.3184411  -19.616744   4.9798619     .243481
_cons  -8.9264227  -22.261508    4.408663   .18952432
_cons  -13.739964  -26.698042  -.78188667    .0376884
_cons  -10.077364  -25.529255   5.3745276    .2011638
_cons  -21.923598  -36.939063  -6.9081342   .00421401
_cons  -17.711232  -32.069384  -3.3530799   .01561993
_cons  -10.102648  -24.816717   4.6114217   .17839704
_cons  -19.011671  -33.147993  -4.8753489   .00839099
_cons  -20.020774  -31.996916  -8.0446331   .00105097
_cons  -5.5561248  -20.200059   9.0878092   .45709473
_cons  -20.287334  -35.070798  -5.5038709   .00715255
_cons  -27.383867  -43.912183  -10.855552    .0011653
_cons   -23.66688  -37.357931  -9.9758301   .00070386
_cons   -24.01003  -41.201916  -6.8181439   .00619519
_cons  -24.553526  -40.959911  -8.1471416   .00335434
_cons  -31.183961  -45.877887  -16.490035   .00003189
_cons  -38.747589  -51.936516  -25.558662   8.504e-09
ciFirstConfirmedCase not found
r(111);

end of do-file

r(111);

.
But, i see ciFirstConfirmedCase listed just above the error...so not sure what happens. Will be very grateful for any help offered and apologies again for the very lengthy post.
Sincerely,
Sumedha.

No comments:

Post a Comment