Announcement

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

  • Matrix with specific output

    I am trying to write a code that shows me the results of 1000 replications (I want to do a placebo test). I would like to end up with a matrix that shows the coefficient, the standard error and the t-value of the variable of interest (thus 3 columns, 1000 rows).

    Assume that I am using the auto dataset, and I am running a regression and I am interested in the variable foreign. For the placebo test, I am assigning random values to foreign, re-running the regression, and storing the coefficients of foreign in a matrix (this is what I managed to do, see the code below). How can I expand this matrix so that it also includes the s.e. and t-value?

    Code:
    sysuse auto, clear
    
    regress price weight mpg foreign
    
    count if foreign == 1
    local numberforeign = r(N)
    
    tempname resmat
    
    forv i=1(1)1000 {
    
    gen randomorder = runiform()
    sort randomorder
    
    drop foreign
    
    gen foreign = 0
    replace foreign = 1 in 1/`numberforeign'
    
    regress price weight mpg foreign
    
    matrix `resmat'=nullmat(`resmat')\ e(b)
    
    drop randomorder
    }
    
    matlist `resmat', format(%9.3f)
    
    matrix V = `resmat'
    matrix a = V[1..1000 ,3..3]
    
    mat list a

  • #2
    You can pick all these up from -r(table)- after each regression. See

    Code:
    sysuse auto, clear
    regress price weight mpg foreign
    mat list r(table)

    Comment


    • #3
      Thank you Andrew. Since I will be doing 1000 replications, I want to put only the necessary information into my matrix (that is, only b, se and t; the first 3 rows of r(table), and only for the variable foreign) with 1000 rows. Any idea how I can adjust my loop above to do this?

      Comment


      • #4
        Another point is that, at least in the example you show, you are going through a lot of effort to do the job of the built-in command -permute-. See -help permute-, and see how you like this:
        Code:
        sysuse auto, clear
        tempfile results
        permute foreign ///
           b = el(r(table), 1,3) ///
           se = el(r(table), 2,3) ///
           t = el(r(table), 3,3), ///
           reps(1000) saving(`results'): regress price weight mpg foreign
        use `results', clear
        // If you really want the results in matrices.
        mkmat b, matrix(B)
        mkmat se, matrix(SE)
        mkmat t, matrix(T)

        Comment


        • #5
          Alternatively, for your example, this works

          Code:
          sysuse auto, clear
          regress price weight mpg foreign
          count if foreign == 1
          local numberforeign = r(N)
          tempname resmat
          forv i=1(1)5 {
          gen randomorder = runiform()
          sort randomorder
          drop foreign
          gen foreign = 0
          replace foreign = 1 in 1/`numberforeign'
          regress price weight mpg foreign
          matrix `resmat'=nullmat(`resmat')\  r(table)[rownumb(r(table), "b"),colnumb(r(table), "foreign") ], r(table)[rownumb(r(table), "se"),colnumb(r(table), "foreign") ], r(table)[rownumb(r(table), "t"),colnumb(r(table), "foreign") ]
          
          drop randomorder
          }
          
          mat colnames `resmat'= b se t
          mat l `resmat'
          I reduce the iterations to 5

          Res.:

          Code:
           mat l `resmat'
          
          __000000[5,3]
                       b          se           t
          r1   -273.9315   643.59864  -.42562474
          r2  -292.11112   647.96885  -.45081043
          r3   533.20503   642.26189   .83019876
          r4   1130.2824   636.85117   1.7747984
          r5  -130.48738   644.36575  -.20250515

          Comment


          • #6
            Thank you both for these suggestions. Permute works in the example I used here, but in my actual dataset, I have a difference in difference design so not only my T but also the interaction with T would have to change.

            Andrew, your code did not work when I tried it, unfortunately (did you use it exactly like this? I copy-pasted, but I get the error "invalid syntax" after the first command line that you added). Maybe I should add that I'm using STATA 15

            Comment


            • #7
              Yes, it runs. Can you copy and paste exactly what you type and the error message from Stata. The invalid syntax error can result from spaces at the beginning of commands.

              Comment


              • #8
                -permute- is a very general purpose command, and I'd be mildly surprised if it can't work in your situation. It might be necessary to write a wrapper program to calculate and returns the statistics of interest, but that's a very standard way to use -permute-. I'd encourage you to show the code for your difference in difference analysis.

                There are times when it's hard or impossible to make an analysis fit with -permute-, but that's not very common in my experience. There also is at least one community-contributed program, -shufflevar- at ssc, that might help you. Sorting the whole data set at each repetition, as you are doing, is definitely not a desirable thing.

                Comment


                • #9
                  Andrew, I used this code (I tried both working from a do file and from the command window. This is the output when I use a do-file). I don't see any excess spaces

                  Code:
                  . do "C:\Users\lucp7667\AppData\Local\Temp\STD5840_000000.tmp"
                  
                  . sysuse auto, clear
                  (1978 Automobile Data)
                  
                  . regress price weight mpg foreign
                  
                        Source |       SS           df       MS      Number of obs   =        74
                  -------------+----------------------------------   F(3, 70)        =     23.29
                         Model |   317252881         3   105750960   Prob > F        =    0.0000
                      Residual |   317812515        70  4540178.78   R-squared       =    0.4996
                  -------------+----------------------------------   Adj R-squared   =    0.4781
                         Total |   635065396        73  8699525.97   Root MSE        =    2130.8
                  
                  ------------------------------------------------------------------------------
                         price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                        weight |   3.464706    .630749     5.49   0.000     2.206717    4.722695
                           mpg |    21.8536   74.22114     0.29   0.769    -126.1758     169.883
                       foreign |    3673.06   683.9783     5.37   0.000     2308.909    5037.212
                         _cons |  -5853.696   3376.987    -1.73   0.087    -12588.88    881.4934
                  ------------------------------------------------------------------------------
                  
                  . count if foreign == 1
                    22
                  
                  . local numberforeign = r(N)
                  
                  . tempname resmat
                  
                  . forv i=1(1)5 {
                    2. gen randomorder = runiform()
                    3. sort randomorder
                    4. drop foreign
                    5. gen foreign = 0
                    6. replace foreign = 1 in 1/`numberforeign'
                    7. regress price weight mpg foreign
                    8. matrix `resmat'=nullmat(`resmat')\  r(table)[rownumb(r(table), "b"),colnumb(r(table), "foreign") ], r(table)[rownu
                  > mb(r(table), "se"),colnumb(r(table), "foreign") ], r(table)[rownumb(r(table), "t"),colnumb(r(table), "foreign") ]
                    9. 
                  . drop randomorder
                   10. }
                  (22 real changes made)
                  
                        Source |       SS           df       MS      Number of obs   =        74
                  -------------+----------------------------------   F(3, 70)        =      9.72
                         Model |   186797793         3  62265931.1   Prob > F        =    0.0000
                      Residual |   448267603        70   6403822.9   R-squared       =    0.2941
                  -------------+----------------------------------   Adj R-squared   =    0.2639
                         Total |   635065396        73  8699525.97   Root MSE        =    2530.6
                  
                  ------------------------------------------------------------------------------
                         price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                  -------------+----------------------------------------------------------------
                        weight |   1.724651   .6505523     2.65   0.010     .4271652    3.022136
                           mpg |  -53.15707   87.74648    -0.61   0.547    -228.1619    121.8478
                       foreign |   177.6377   651.2039     0.27   0.786    -1121.148    1476.423
                         _cons |   2037.034   3636.052     0.56   0.577    -5214.842    9288.911
                  ------------------------------------------------------------------------------
                  invalid syntax
                  r(198);
                  
                  end of do-file
                  
                  r(198);

                  Comment


                  • #10
                    Thank you for these suggestions, Mike. Permute seems extremely useful so it would be great if I can make it work in my application. About my code: I generated a new variable that is the interaction term (I didn't use the # notation simply because that output doesn't show in output of outreg2, which I like to use). So that means that in the code above, I generated a placebo treatment and a placebo interaction, like this:

                    Code:
                    sysuse auto, clear
                    gen interaction = mpg*foreign 
                    regress price weight mpg foreign interaction
                    
                    count if foreign == 1
                    local numberforeign = r(N)
                    
                    tempname resmat
                    
                    forv i=1(1)1000 {
                    
                    gen randomorder = runiform()
                    sort randomorder
                    
                    drop foreign interaction
                    
                    gen foreign = 0
                    replace foreign = 1 in 1/`numberforeign'
                    gen interaction = mpg*foreign
                    
                    regress price weight mpg foreign interaction 
                    
                    matrix `resmat'=nullmat(`resmat')\ e(b)
                    
                    drop randomorder
                    }
                    
                    matlist `resmat', format(%9.3f)
                    
                    matrix V = `resmat'
                    matrix a = V[1..1000 ,4..4]
                    
                    mat list a

                    Comment


                    • #11
                      Andrew, I used this code (I tried both working from a do file and from the command window. This is the output when I use a do-file). I don't see any excess spaces
                      You are absolutely correct that this is a version issue. The code in #5 will run without errors in Stata 16 but not Stata 15. The issue is that you cannot directly reference an r() matrix in the latter but you can do this in the former. The fix is to copy the r() matrix to a Stata matrix, a one-liner!

                      Code:
                      sysuse auto, clear
                      regress price weight mpg foreign
                      count if foreign == 1
                      local numberforeign = r(N)
                      tempname resmat
                      forv i=1(1)5 {
                      gen randomorder = runiform()
                      sort randomorder
                      drop foreign
                      gen foreign = 0
                      replace foreign = 1 in 1/`numberforeign'
                      regress price weight mpg foreign
                      mat R= r(table)
                      matrix `resmat'=nullmat(`resmat')\  R[rownumb(r(table), "b"),colnumb(R, "foreign") ], R[rownumb(r(table), "se"),colnumb(R, "foreign") ], R[rownumb(R, "t"),colnumb(R, "foreign") ]
                      drop randomorder
                      }
                      mat colnames `resmat'= b se t
                      mat l `resmat'

                      Comment

                      Working...
                      X