Announcement

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

  • colnumb() after gsem

    Hi,

    I am struggling to get the column number of a coefficient after estimating a model with multiple equations via gsem. For this, I am using colnumb():

    gsem .. omitted

    matrix b=e(b)

    scalar pos = colnumb(b, "_b["equation_name:covariate_name]")
    display pos

    where equation_name and covariate_name are the name of a specific equation and covariate of my model.

    Unfortunately, nothing is displayed.

    From my understanding, written in this way, colnumb() seems not support the equation name. For example, in my model most covariates appear in multiple equations and when I modify the line above with only the covariate name:

    scalar pos = colnumb(b, "_b["covariate_name]")
    display pos

    the position of the first appearance of that covariate in b is displayed.

    I need to access the position of each coefficient by equation name and covariate name.

    Any help would be greatly appreciated,

    Simone
    Last edited by Simone Angioloni; 21 Aug 2025, 02:21.

  • #2
    Originally posted by Simone Angioloni View Post
    scalar pos = colnumb(b, "_b["equation_name:covariate_name]")
    The syntax should be:

    Code:
    scalar pos = colnumb(b, "equation_name:covariate_name")




    Comment


    • #3
      Hi Andrew,

      My thanks for spotting my syntax error and apologies for this.

      However, my problem in somehow still remains.

      In my model, I have different equations with the same covariates per equation. My goal is to loop over equation-covariate name combinations to get the coefficients. My problem is that I do know how to separate the two "headers"/ column names of the element of b=e(b).

      Below a fake example of how my b row vector looks like:

      matrix list b

      equation1_name equation1_name ............ equation2_name
      first covariate name second covariate name ............ first_covariate name
      numerical coefficient numerical coefficient ........... numerical coefficient

      Any help would be greatly appreciated.

      Simone

      Comment


      • #4
        Perhaps if you stated your ultimate goal, you'd get better solutions. Stata has improved ways to reference matrices in recent versions, so I don't see people using the function -colnumb()- much anymore. Furthermore, you can reference the coefficients matrix directly in recent versions without needing to save it as a separate matrix.

        To your question: what appears before the colon are the column equation names, whereas what appears after are the column names. You can extract these as follows:

        Code:
        use https://www.stata-press.com/data/r18/sem_2fmm, clear
        qui sem (Affective -> a1 a2 a3 a4 a5) (Cognitive -> c1 c2 c3 c4 c5)
        
        mat list e(b)
        
        local coleqnames `:coleq e(b)'
        local coleqnames: list uniq coleqnames
        
        local colnames `:colnames e(b)'
        local colnames: list uniq colnames
        
        di "`colnames'"
        di "`coleqnames'"
        Res.:

        Code:
        . mat list e(b)
        
        e(b)[1,23]
                       a1:            a2:            a3:            a4:            a5:            c1:            c2:            c3:            c4:            c5:             /:             /:             /:             /:
                                                                                                                                                                                                                             
                Affective      Affective      Affective      Affective      Affective      Cognitive      Cognitive      Cognitive      Cognitive      Cognitive       var(e.a1)      var(e.a2)      var(e.a3)      var(e.a4)
        y1              1      .97580977      .83725991      .96404607      1.0637008              1      1.1147024      1.3298818       1.172792      1.1263562       384.1359      357.35245      154.95073      496.45944
        
                        /:             /:             /:             /:             /:             /:             /:             /:             /:
                                                                                                                                     cov(Affect~e,
                 var(e.a5)      var(e.c1)      var(e.c2)      var(e.c3)      var(e.c4)      var(e.c5)  var(Affect~e)  var(Cognit~e)     Cognitive)
        y1      191.68568      171.66381      171.80551      276.01442      224.19942      146.86551      1644.4625      455.93486      702.07357
        
        . 
        . 
        . 
        . local coleqnames `:coleq e(b)'
        
        . 
        . local coleqnames: list uniq coleqnames
        
        . 
        . 
        . 
        . local colnames `:colnames e(b)'
        
        . 
        . local colnames: list uniq colnames
        
        . 
        . 
        . 
        . di "`colnames'"
        Affective Cognitive var(e.a1) var(e.a2) var(e.a3) var(e.a4) var(e.a5) var(e.c1) var(e.c2) var(e.c3) var(e.c4) var(e.c5) var(Affective) var(Cognitive) cov(Affective,Cognitive)
        
        . 
        . di "`coleqnames'"
        a1 a2 a3 a4 a5 c1 c2 c3 c4 c5 /

        Comment


        • #5
          Thanks
          Last edited by Simone Angioloni; 22 Aug 2025, 01:41.

          Comment


          • #6
            Thanks, Andrew

            Comment

            Working...
            X