Announcement

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

  • Diagonal

    I am modelling a huge database and I have to create a new variable that contains the value of the diagonal of the current database.
    Basically on the x-axis and y-axis I have the same variables and I want to select the interaction term - you can think about it as the variance in the variance-covariance matrix. I have tried with MATA and the function diagonal() but I get errors.
    Any help with the code? I would really appreciate it!

  • #2
    You're question is unclear. If you want the diagonal of a matrix, the vecdiag() and diag() functions can be useful. Here's an example of those functions:

    Code:
    mat X = (1,2,3 \ 4,5,6 \ 7,8,9)
    mat list X
    mat dv = vecdiag(X)
    mat list dv
    mat D = diag(vecdiag(X))
    mat list D
    Result

    Code:
    . mat X = (1,2,3 \ 4,5,6 \ 7,8,9)
    
    . mat list X
    
    X[3,3]
        c1  c2  c3
    r1   1   2   3
    r2   4   5   6
    r3   7   8   9
    
    . mat dv = vecdiag(X)
    
    . mat list dv
    
    dv[1,3]
        c1  c2  c3
    r1   1   5   9
    
    . mat D = diag(vecdiag(X))
    
    . mat list D
    
    symmetric D[3,3]
        c1  c2  c3
    c1   1
    c2   0   5
    c3   0   0   9
    If this does not accomplish what you want, then please spend some time reading the FAQ, in particular section 12 on how to ask effective questions. You are encouraged to post a reproducible data example using -dataex- which is included with fully updated versions of Stata 14.2, 15.1, 16 and 17.

    Comment


    • #3
      Typically, when using such "tabular" data in a program like Stata, you will want that data in long or vectorized form, e.g.:

      Code:
      . use "http://fmwww.bc.edu/repec/bocode/m/mob.dta", clear
      (mobility table from the USA collected in 1973)
      
      . list, sepby(row)
      
           +------------------------------------------+
           |             row               col    pop |
           |------------------------------------------|
        1. | upper nonmanual   upper nonmanual   1414 |
        2. | upper nonmanual   lower nonmanual    521 |
        3. | upper nonmanual      upper manual    302 |
        4. | upper nonmanual      lower manual    643 |
        5. | upper nonmanual              farm     40 |
           |------------------------------------------|
        6. | lower nonmanual   upper nonmanual    724 |
        7. | lower nonmanual   lower nonmanual    524 |
        8. | lower nonmanual      upper manual    254 |
        9. | lower nonmanual      lower manual    703 |
       10. | lower nonmanual              farm     48 |
           |------------------------------------------|
       11. |    upper manual   upper nonmanual    798 |
       12. |    upper manual   lower nonmanual    648 |
       13. |    upper manual      upper manual    856 |
       14. |    upper manual      lower manual   1676 |
       15. |    upper manual              farm    108 |
           |------------------------------------------|
       16. |    lower manual   upper nonmanual    756 |
       17. |    lower manual   lower nonmanual    914 |
       18. |    lower manual      upper manual    771 |
       19. |    lower manual      lower manual   3325 |
       20. |    lower manual              farm    237 |
           |------------------------------------------|
       21. |            farm   upper nonmanual    409 |
       22. |            farm   lower nonmanual    357 |
       23. |            farm      upper manual    441 |
       24. |            farm      lower manual   1611 |
       25. |            farm              farm   1832 |
           +------------------------------------------+
      In my example each observation is a cell in the table and the variables indicating what row and column a cell is on are called row and col. The content of the cell is in my example in the variable pop. A cell is on the main diagonal (from top left to bottom right) when row == col. You can make a variable indicating when that is the case as follows: gen diag = (row==col) . This will make a variable called diag, which takes the value 1 when it is on the diagonal an 0 otherwise. There are several tips and tricks on dealing with this type of data in this talk I gave at the 2015 Stata Users' Group meeting in London: http://maartenbuis.nl/presentations/london15b.pdf
      Last edited by Maarten Buis; 24 Oct 2021, 06:06.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment

      Working...
      X