Announcement

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

  • Create colnames for sem-like coefficient matrix

    Dear all,

    I'm looking for a way to automatically create column names for a coefficient matrix (and colnames and rownames for a vce matrix) so that the output of ereturn display (or _coef_table) looks like the output of the sem or gsem command (mainly so that the dependent variable is indicated).
    As I understand from the documentation I have to use two-part names (see section 14.2.2 in the User's Guide).

    Since I'm categorical variables and factor notation my first idea was to use the fvexpand command on my variable list and use the return as colnames but that doesn't give me two part names.
    I also looked at the code of the gsem and sem commands (and the ados that belong to it) but I wasn't able to figure out which (exact) part of the code is relevant.

    Is there a command/code part or just general advice that might help me with this problem?

    Thanks in advance,

    Max

  • #2
    It might be helpful if you could provide an illustration of what you hope the end result would resemble and/or the use cases for it. Maybe creating a quick ASCII mock up or even an excel file to show things a bit more clearly would help the Stata community members offer you suggestions and/or solutions.

    Comment


    • #3
      You are right. So the end result should look like the output of the gsem command when using factor variables. Say I have 3 categorical variables with 2 dimensions each and I run the command:

      Code:
      gsem (i.a <- i.b) (i.c <- i.b##i.a)
      Then I get an output that structures the coefficients inside the table after the separate equations and after the dimensions of the variables like this:

      Click image for larger version

Name:	output.jpg
Views:	1
Size:	61.2 KB
ID:	1296556


      That is due to the column names of the coefficient matrix. To see which form the column names have one can display the matrix or retrieve and display the column names as a local macro with the commands:

      Code:
      matrix list e(b)
      local col_names : colfullnames e(b)
      di "`col_names'"
      And this is what the column names look like:

      Code:
      1b.a:1b.b 1b.a:2o.b 1b.a:o._cons 2.a:1b.b 2.a:2.b 2.a:_cons 1b.c:1b.b 1b.c:2o.b 1b.c:1b.a 1b.c:2o.a 1b.c:1b.b#1b.a 1b.c:1b.b#2o.a 1b.c
      > :2o.b#1b.a 1b.c:2o.b#2o.a 1b.c:o._cons 2.c:1b.b 2.c:2.b 2.c:1b.a 2.c:2.a 2.c:1b.b#1b.a 2.c:1b.b#2o.a 2.c:2o.b#1b.a 2.c:2.b#2.a 2.c:_
      > cons
      One can see that two-part names are used with the factor variables in the form where each blank-separated element of the string consists of the dimension of the dependent variable and (separated by a colon) the dimension of the independent variable.

      I want to use this kind of displaying for an ado that I'm working on that has input syntax similar to gsem and retrieves the coefficient matrix (which has been calculated externally). But for that I need to automatically create column names like this from the input syntax.

      I hope that helps and I'm looking forward to any suggestions!


      Comment


      • #4
        Since the problem I described above came up again I looked through the gsem ado files. It seems the operation that creates the column name is located in the command
        Code:
        mata: st_gsem_parse("`GSEM'", "`touse'")
        (line 271 in the gsem_parse.ado). However, since I'm not familiar with the use of mata I wasn't able to view the code of the mentioned command.
        Is there a way to do that?
        Maybe then I can figure out how the column name is created.

        Best regards,
        Max

        Comment


        • #5
          It turned out that the solution wasn't as complicated as I thought. Therefore I didn't want to keep it from those who might be interested:

          Using the model
          (i.var0 <- i.var1 i.var2 i.var1#i.var2)
          as an example the
          Code:
          //Input:
          local dependent_variable = "i.var0"
          local independent_variables    = "i.var1 i.var2 i.var1#i.var2"
              
          fvexpand `dependent_variable'
          local dv = "`r(varlist)'"
              
          fvexpand `independent_variables'
          local ivs = "`r(varlist)'"
              
          local colname = ""
          forvalues i=1/`: word count `dv'' {
              forvalues j=1/`: word count `ivs'' {
                  local colname = "`colname' `: word `i' of `dv'':`: word `j' of `ivs''"
              }
              local colname = "`colname' `: word `i' of `dv'':_cons"
          }
          
          di "colname: `colname'"
          gave me the columnnames:
          Code:
          1b.var0:1b.var1 1b.var0:2.var1 1b.var0:1b.var2 1b.var0:2.var2 1b.var0:1b.var1#1b.var2 1b.var0:1b.var1#2o.var2 1b.var0:2
          > o.var1#1b.var2 1b.var0:2.var1#2.var2 1b.var0:_cons 2.var0:1b.var1 2.var0:2.var1 2.var0:1b.var2 2.var0:2.var2 2.var0:1b.var1#1b.
          > var2 2.var0:1b.var1#2o.var2 2.var0:2o.var1#1b.var2 2.var0:2.var1#2.var2 2.var0:_cons
          If I have more than one equation I just have to repeat the process for the other equations (with different input of course) and cocatenate the strings.
          Then I can use
          Code:
          matrix colnames b = `colname'
          //and
          matrix colnames V = `colname'
          matrix rownames V = `colname'
          to set these as columnnames and rownames of my coefficient and covariance matrices. After that ereturn post and ereturn display will give me a table that looks like the one produced by gsem.

          (The only difference is that my method does not input "o"'s for the omitted categories as in "1b.a:2o.b" but that doesn't make a difference when you display it.)

          Comment


          • #6
            Just to share: it can be useful to keep "equations" and "names" separate:

            Code:
            //Input:
            local dependent_variable = "i.q1"
            local independent_variables    = "i.q2 i.q3 i.q2#i.q3"
                
            fvexpand `dependent_variable'
            local dv = "`r(varlist)'"
            local n_dv "`: word count `dv''"
                
            fvexpand `independent_variables'
            local ivs = "`r(varlist)'"
            local n_ivs "`: word count `ivs''"
                
            local coleq = ""
            local colname = ""
            forvalues i=1/`n_dv' {
                forvalues j=1/`n_ivs' {
                    local coleq = "`coleq' `: word `i' of `dv''"
                    local colname = "`colname' `: word `j' of `ivs''"
                }
                local coleq = "`coleq' `: word `i' of `dv''"
                local colname = "`colname' _cons"
            }
            //Output:
            
            matrix A = J(1,`=`n_dv'*(`n_ivs'+1)',0)
            matrix coleq A = `coleq'
            matrix colnames A = `colname'
            matrix list A
            Doug Hemken
            SSCC, Univ. of Wisc.-Madison

            Comment

            Working...
            X