Announcement

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

  • Question On Extracting Value Label and assign them as the column name of coefficient matrix

    Hi, My question is about row names in matrix. Below are my code and its output.

    Code:
        version 15.1
        sysuse auto.dta, clear
        reg price i.foreign, robust
        mat b = r(table)
        mat b = b[1..2, 2..3]'
        mat colnames b = "Car type" "Car type se"
        mat list b
    
                  Car type  Car type se
    1.foreign    312.25874    701.78135
        _cons    6072.4231    431.20837

    As we know that, there are value labels for foreign, "Domestic" and "Foreign"

    Code:
                tabulation:  Freq.   Numeric  Label
                                52         0  Domestic
                                22         1  Foreign


    So, my question is that how can I extract the value label of "foreign" and assign that values labels to the matrix I made above as the row name of this matrix?

    Thanks a lot!




  • #2
    Code:
    local lbl1 : label (foreign) 1
    mat rownames b = "`lbl1'" "Constant"
    Code:
    . mat list b
    
    b[2,2]
                 Car type  Car type se
     Foreign    312.25874    701.78135
    Constant    6072.4231    431.20837

    Comment


    • #3
      Originally posted by William Lisowski View Post
      Code:
      local lbl1 : label (foreign) 1
      mat rownames b = "`lbl1'" "Constant"
      Code:
      . mat list b
      
      b[2,2]
      Car type Car type se
      Foreign 312.25874 701.78135
      Constant 6072.4231 431.20837
      Thank you, William! Just one more question

      Just like below, what if there are more than one value label for this variable? Is there any way to write a loop to add the label into row name?

      Code:
          drop if rep78 ==. 
          label define rep78_lab 1 "One" 2 "Two" 3 "Three" 4 "Four" 5 "Five"
          label val rep78 rep78_lab
      
      
                  tabulation:  Freq.   Numeric  Label
                                   2         1  One
                                   8         2  Two
                                  30         3  Three
                                  18         4  Four
                                  11         5  Five

      Comment


      • #4
        Code:
        version 15.1
        sysuse auto.dta, clear
        drop if rep78 ==. 
        label define rep78_lab 1 "One" 2 "Two" 3 "Three" 4 "Four" 5 "Five"
        label val rep78 rep78_lab
        reg price i.rep78, robust
        mat b = r(table)
        mat b = b[1..2, 2..6]'
        mat colnames b = "Car type" "Car type se"
        local lbls
        forvalues i=2/5 {
            local lbl : label (rep78) `i'
            local lbls `" `lbls' "`lbl'" "'
        }
        local lbls `" `lbls' "The Constant" "'
        macro list _lbls
        mat rownames b = `lbls' 
        mat list b
        Code:
        . macro list _lbls
        _lbls:           "Two" "Three" "Four" "Five" "The Constant"
        
        . mat rownames b = `lbls' 
        
        . mat list b
        
        b[5,2]
                         Car type  Car type se
                 Two     1403.125    1258.7153
               Three    1864.7333    710.84008
                Four         1507    488.80865
                Five       1348.5    826.58826
        The Constant       4564.5    271.29014
        
        .

        Comment


        • #5
          Originally posted by William Lisowski View Post
          Code:
          version 15.1
          sysuse auto.dta, clear
          drop if rep78 ==.
          label define rep78_lab 1 "One" 2 "Two" 3 "Three" 4 "Four" 5 "Five"
          label val rep78 rep78_lab
          reg price i.rep78, robust
          mat b = r(table)
          mat b = b[1..2, 2..6]'
          mat colnames b = "Car type" "Car type se"
          local lbls
          forvalues i=2/5 {
          local lbl : label (rep78) `i'
          local lbls `" `lbls' "`lbl'" "'
          }
          local lbls `" `lbls' "The Constant" "'
          macro list _lbls
          mat rownames b = `lbls'
          mat list b
          Code:
          . macro list _lbls
          _lbls: "Two" "Three" "Four" "Five" "The Constant"
          
          . mat rownames b = `lbls'
          
          . mat list b
          
          b[5,2]
          Car type Car type se
          Two 1403.125 1258.7153
          Three 1864.7333 710.84008
          Four 1507 488.80865
          Five 1348.5 826.58826
          The Constant 4564.5 271.29014
          
          .
          Thank you so much, William! This is really helpful!

          But, if you have time, could you please clarify that why do we need macro list _lbls ? The below is my code. I only added one line at the third line from the bottom to test the value of `lbls' after macro.
          Code:
                  version 15.1
                  sysuse auto.dta, clear
                  drop if rep78 ==. 
                  label define rep78_lab 1 "One" 2 "Two" 3 "Three" 4 "Four" 5 "Five"
                  label val rep78 rep78_lab
                  reg price i.rep78, robust
                  mat b = r(table)
                  mat b = b[1..2, 2..6]'
                  mat colnames b = "Car type" "Car type se"
                  local lbls
                  forvalues i=2/5 {
                      local lbl : label (rep78) `i'
                      // local lbls "`lbls' `lbl'"
                      // di "`lbls'"
                      local lbls `" `lbls' "`lbl'" "'
                  }
                  local lbls `" `lbls' "The Constant" "'
                  macro list _lbls
                  di `lbls' // Xinghuan added it here
                  mat rownames b = `lbls'
                  mat list b
          The output of the third line from the bottom, "di `lbls'", is still "TwoThreeFourFiveThe Constant". Then, I wonder how could we get the final correct result? macro list _lbls does not change the value of `lbls'....

          Thank you so much!

          Comment


          • #6
            The macro list command presents exactly the contents of the macro without attempting any interpretation. The display command works to show what it thinks you want to see. When I am developing code, I use only macro list - never display - to determine if my macro holds what I am expecting it to.
            Code:
            . local x 5+5
            
            . macro list _x
            _x:             5+5
            
            . display `x'
            10
            
            . display `"`x'"'
            5+5
            
            . display "`x'"
            5+5
            
            .
            When the macro contains elements with quotation marks around them, the display command becomes even more problematic. What it has done, in your case, is concatenate five string values together.
            Code:
            . macro list _lbls
            _lbls:           "Two" "Three" "Four" "Five" "The Constant"
            
            . display "Two" "Three" "Four" "Five" "The Constant"
            TwoThreeFourFiveThe Constant
            
            . di `lbls'
            TwoThreeFourFiveThe Constant
            
            . di `"`lbls'"'
                  "Two"  "Three"  "Four"  "Five"  "The Constant"
            
            . di "`lbls'"
                Two"  "Three"  "Four"  "Five"  "The invalid name
            r(198);
            Last edited by William Lisowski; 27 Sep 2020, 18:38.

            Comment

            Working...
            X