Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Code went wrong!

    Dear All, I was asked the following question. (cluster2 can be downloaded here: https://www.kellogg.northwestern.edu...rogramming.htm)
    Code:
    matrix coef = J(100, 2, 0)
    forv i=1/100{
            gen GT6_`i'=runiformint(0,1)  //
            qui xi: cluster2 NCSKEW GT6_`i' lagsigma lagret lagdturn lagleverage lagsize lagbm lagroa lagDAnewC  i.year i.ind,fcluster(stkcd) tcluster(year) 
            matrix coef[`i',1] = _b[GT6_`i'] 
            matrix coef[`i',2] = _b[GT6_`i']/_se[GT6_`i'] 
            }
    matrix list coef
    The error message is
    Click image for larger version

Name:	WeChat 圖片_20181115152852.png
Views:	1
Size:	11.6 KB
ID:	1470500
    Any idea? Thanks.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2

    matrix define coef = J(100, 2, 0)

    Comment


    • #3
      Dear Eric, Is this what you meant? However, it didn't work.
      Click image for larger version

Name:	WeChat 圖片_20181115171943.png
Views:	1
Size:	10.3 KB
ID:	1470506


      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        I would try something dopey like

        Code:
        mat coef = J(100, 2, 0)
        
         forval j = 1/100 {
               mat coef[`j', 1] = 42
               mat coef[`j', 2] = _pi
         }
        If that works, the implication is that cluster2 is zapping the matrix so that it no longer exists when you try to write to it.

        Comment


        • #5
          Dear Nick, I think you are right. I tried the following example, and it worked (ssc install reghdfe).
          Code:
          webuse grunfeld, clear
          
          matrix coef = J(100, 2, 0)
          forvalue i=1/100{
            gen GT6_`i' = runiformint(0,1) 
            reghdfe invest mvalue kstock GT6_`i', a(company year) 
            matrix coef[`i',1] = _b[GT6_`i'] 
            matrix coef[`i',2] = _b[GT6_`i']/_se[GT6_`i'] 
          }
          matrix list coef
          Code:
          . matrix list coef 
          
          coef[100,2]
                        c1          c2
            r1   2.1599327   .27305193
            r2  -6.0922036  -.76393218
            r3  -9.3331528  -1.1716116
            r4   12.677839   1.6207238
            r5  -10.220933  -1.3043066
            r6  -2.4261394  -.30816608
            r7  -12.309045  -1.5834124
            r8   -6.201976  -.75959585
            r9   .01998259   .00255861
           r10    14.17779   1.8460891
           r11  -.57267177    -.071111
           r12  -12.273537  -1.5729727
           r13   5.1018514   .63031945
           r14  -2.2010058  -.27357395
           r15  -4.8632984  -.60993961
           r16  -7.0465437  -.88387518
           r17   9.8736509   1.2667642
           r18  -5.0633924  -.62695259
           r19  -3.6337209  -.45956548
           r20  -4.4606186  -.54010791
           r21   7.0025994   .89083894
           r22   10.062411   1.2939403
           r23   9.5635554   1.2262075
           r24    1.620994   .19913811
           r25   -1.875987  -.23640423
           r26  -5.9919603  -.75860783
           r27  -3.2119056  -.40947015
           r28   1.2786428   .16832019
           r29  -3.8005771  -.46621549
           r30   3.6677399   .45605978
           r31   4.8917069   .60265623
           r32   2.7310132   .34191917
           r33  -.51421832  -.06480622
           r34   8.5314832   1.0497433
           r35   8.1494169   1.0138305
           r36   11.936048   1.4624583
           r37  -6.8952559  -.87446903
           r38  -5.7026133  -.69200066
           r39   7.4251766   .96055409
           r40  -1.9419617  -.23804129
           r41  -20.356635  -2.5479516
           r42  -10.170731  -1.2408979
           r43   5.3146498   .67120635
           r44  -16.123378  -2.0751691
           r45   3.9289888   .50888697
           r46   1.8791899   .24092916
           r47  -7.0277887  -.93040191
           r48   7.8866576   .94862256
           r49   .59199665    .0754688
           r50  -6.3095736  -.78835663
           r51  -3.0232275  -.38934176
           r52   3.2098451   .40124226
           r53  -13.607678    -1.65409
           r54  -8.8851131  -1.1282977
           r55    11.46697   1.4415115
           r56   5.9662576    .7774772
           r57   2.6907005   .33500321
           r58   6.0640176   .78232602
           r59  -4.1667037  -.51158281
           r60   .73161134   .09209914
           r61   -15.06803  -1.9380743
           r62   -6.222779  -.78053461
           r63   1.0031092   .12794236
           r64  -2.4203911  -.31031699
           r65  -5.0195543  -.63067292
           r66  -.72104514  -.08944257
           r67  -3.7915133  -.47964887
           r68   8.5639528   1.1097097
           r69  -1.9950039  -.25936242
           r70   .86429485   .10881545
           r71  -5.4577239   -.6965662
           r72   4.1412829   .52702723
           r73  -.72531403  -.08932575
           r74   3.4995145   .44246033
           r75   .69129446   .08755558
           r76   .54769486   .07016951
           r77   2.9089155    .3559859
           r78   9.6471107   1.1971879
           r79  -3.1741957  -.39726275
           r80  -8.9496539  -1.1359116
           r81  -6.9301766  -.87977171
           r82  -3.9027019  -.46696514
           r83   -15.06352  -1.8492547
           r84  -9.1607414   -1.131136
           r85    6.641792   .80418887
           r86  -2.4420786  -.30563394
           r87  -2.5632151  -.32428123
           r88   10.639612   1.3599792
           r89     5.49265   .70566662
           r90  -.00660216  -.00083412
           r91   3.8571585   .47272299
           r92  -14.759942  -1.9023655
           r93  -5.1432376  -.64869893
           r94  -3.9282047  -.48013024
           r95   12.870125   1.5832077
           r96   6.4792459   .82781548
           r97   5.5176001   .71475589
           r98   3.4589618   .42736524
           r99   5.2232054   .65268802
          r100  -4.9918762  -.64525837
          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment


          • #6
            Nick is correct. The code for cluster2.ado ends in

            Code:
            matrix drop coef vc vcf vct;
            This is bad programming style. The author should have used temporary objects inside the ado-file.

            A simple fix is to use another name for the matrix, e.g. Coef (note capitalization).

            Best
            Daniel

            Comment


            • #7
              Dear Daniel, Thanks for this additional information.
              Ho-Chuan (River) Huang
              Stata 17.0, MP(4)

              Comment

              Working...
              X