Announcement

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

  • Creating a txt file of coefficients without names and headings AND deleting a baseline from a covariance matrix

    Hello,

    I hope all of you are staying safe and healthy.

    I am trying to figure out two things:
    1. How do I create a text file of coefficients from a regression without variable names and column headings
    2. How do I create a text file of a covariance matrix without the `omitted' dummy variable when we use -i.var-

    To illustrate my situation, I ran the following:

    eststo regression1: reg yvar v1 v2 v3 i.E
    estout regression1using "$output/regressionBeta.txt", c(b) keep(v1 v2 v3 2.E 3.E) replace

    The second line gives me a text file with the following result:
    regression1
    b
    _cons 13.69642
    v1 .3184874
    v2 .12065
    v3 -0.03345
    2.E .3685693
    3.E .7410206
    To make the file more readable for another programming language, I want to get rid of both the variable names and the headings ("regression1" and "b" ) so that the result would only contain the numbers.

    Also, in order to store the covariance matrix, I ran the following:
    matrix V1=e(V)
    mat2txt, matrix(V1) saving("$output/regressionVCov.txt") replace

    The second line produces a text file with unnecessary row and column for '1.v3':
    v1 v2 v3 1b.E 2.E 3.E _cons
    v1 .00008257 -4.338e-06 5.486e-08 0 -4.418e-06 -7.580e-06 .00001818
    v2 -4.338e-06 .00003073 -4.590e-07 0 -6.885e-06 -9.601e-06 -.00048082
    v3 5.486e-08 -4.590e-07 6.920e-09 0 1.266e-07 1.836e-07 7.085e-06
    1b.E 0 0 0 0 0 0 0
    2.E -4.418e-06 -6.885e-06 1.266e-07 0 .00022758 .00019557 -.00010479
    3.E -7.580e-06 -9.601e-06 1.836e-07 0 .00019557 .00022442 -.00008609
    _cons .00001818 -.00048082 7.085e-06 0 -.00010479 -.00008609 .00789561

    What command should I use instead to get rid of the column and row for 1b.E and the headings here?

    Thank you very much for your help.
    Last edited by Jaepil Lee; 10 Jul 2020, 09:31. Reason: Added tags

  • #2
    The following example is inelegant, in that I cannot figure out a good way of eliminating rows and columns from Stata matrixes, so I had Mata pull in the Stata matrix, keep only the wanted rows and columns, and then stow the output into e(out) where estout could get at it. Very inelegant. In particular, I expect there are better ways of dropping rows and columns in Mata than my brute force approach. But for what it's worth, it may start you in a useful direction.
    Code:
    sysuse auto, clear
    quietly regress price length mpg i.foreign
    matrix list e(b)
    matrix list e(V), nohalf
    matrix out = e(b)',e(V)
    matrix list out
    mata: out = st_matrix("e(out)", st_matrix("out")[(1..2,4),(1..3,5)])
    matrix list e(out)
    estout e(out) using "~/Downloads/matrix.txt", replace mlabels(none) collabels(none) varlabels(none)
    type "~/Downloads/matrix.txt"
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . quietly regress price length mpg i.foreign
    
    . matrix list e(b)
    
    e(b)[1,5]
                                        0b.          1.            
            length         mpg     foreign     foreign       _cons
    y1   59.611932  -139.08138           0   2644.7712  -2861.9844
    
    . matrix list e(V), nohalf
    
    symmetric e(V)[5,5]
                                                0b.          1.            
                    length         mpg     foreign     foreign       _cons
        length   571.46082   1486.9784           0   8411.8202  -141565.45
           mpg   1486.9784   6758.4288           0   7598.6217  -425646.79
    0b.foreign           0           0           0           0           0
     1.foreign   8411.8202   7598.6217           0   580478.16  -1915258.5
         _cons  -141565.45  -425646.79           0  -1915258.5    36319904
    
    . matrix out = e(b)',e(V)
    
    . matrix list out
    
    out[5,6]
                                                            0b.          1.            
                        y1      length         mpg     foreign     foreign       _cons
        length   59.611932   571.46082   1486.9784           0   8411.8202  -141565.45
           mpg  -139.08138   1486.9784   6758.4288           0   7598.6217  -425646.79
    0b.foreign           0           0           0           0           0           0
     1.foreign   2644.7712   8411.8202   7598.6217           0   580478.16  -1915258.5
         _cons  -2861.9844  -141565.45  -425646.79           0  -1915258.5    36319904
    
    . mata: out = st_matrix("e(out)", st_matrix("out")[(1..2,4),(1..3,5)])
    
    . matrix list e(out)
    
    e(out)[3,4]
                c1          c2          c3          c4
    r1   59.611932   571.46082   1486.9784   8411.8202
    r2  -139.08138   1486.9784   6758.4288   7598.6217
    r3   2644.7712   8411.8202   7598.6217   580478.16
    
    . estout e(out) using "~/Downloads/matrix.txt", replace mlabels(none) collabels(none) varlabels(none)
    (output written to ~/Downloads/matrix.txt)
    
    . type "~/Downloads/matrix.txt"
            59.61193        571.4608        1486.978        8411.82
            -139.0814       1486.978        6758.429        7598.622
            2644.771        8411.82 7598.622        580478.2

    Comment


    • #3
      Thank you so much, William. This helps a lot.

      Comment

      Working...
      X