Announcement

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

  • Converting Mata matrix to Stata variables without losing variable names

    Dear Statalist, I am long time reader, first time poster. I thank everyone for their previous contributions that have solved many a problem of mine.

    I am new to Mata and am currently using it to store the results of a simulation model. I am using Mata because it is fast, and only to store data, not for any matrix algebra.

    I finish with a Mata matrix mSum which I combine with a matrix of variable names cmSum using the _matrix_list command (another tip from someone on Statalist - thanks!)

    Code:
    mata: _matrix_list(mSum, rmSum, cmSum)
    I can convert this matrix mSum into Stata variables by first converting to a Stata matrix and then using svmat.

    Code:
    mata: st_matrix("stSum", mSum)
    My problem is I lose all of the variable names that I have stored in Mata matrix cmSum.

    Does anyone know if there is a way for me to convert mSum to Stata variables and use cmSum as the variable names?

    I realise the solution may be don't use Mata in the first place and maybe at some point I'll figure out a more efficient method of storing and reading lots of large different sets of data without saving and loading datasets.

    Thanks,
    Adam

  • #2
    Originally posted by Adam Irving View Post
    Does anyone know if there is a way for me to convert mSum to Stata variables and use cmSum as the variable names?
    Couldn't you just use the Mata functions st_addvar() in conjunction with st_store() and do both in one fell swoop?

    Perhaps something like the following (all called from within Mata).
    Code:
    st_addobs(rows(mSum))
    st_store(., st_addvar("double", cmSum), mSum)
    That first line of code is required only if you don't have sufficient observations already in the Stata dataset to accommodate the rows of the Mata numerical matrix.

    I am new to Mata and am currently using it to store the results of a simulation model. I am using Mata because it is fast . . . I realise the solution may be don't use Mata in the first place and maybe at some point I'll figure out a more efficient method . . .
    I'm not sure what your needs are with a "simulation model", but I've never needed to resort to Mata in order to store simulation results.

    Comment


    • #3
      My first question is: Why not save in a Stata matrix?
      You can append Stata matrices with the \-operator and you might want to look into the function nullmat when starting adding up information.
      And this way row names are preserved.

      If you insist on using Mata, I'll refer to:https://www.statalist.org/forums/for...66#post1656966

      Btw: Do you have a reference to _matrix_list? Or do you mean st_matrix_list()?
      Kind regards

      nhb

      Comment


      • #4
        Thanks for your suggestions Joseph and Niels. Unfortunately I wasn't able to get st_store to work.

        I chose Mata because I have Stata SE and thought the 11,000 Stata matrix row limit could be a problem - I normally run the model with 10,000 patients but need the option to expand to check Monte Carlo error.

        Niels I found _matrix_list from this Statalist thread of yours. It was very useful for display purposes as I was building the model.

        Cheers,
        Adam

        Comment


        • #5
          See

          Code:
          help st_sstore
          Code:
          clear
          set obs 5
          gen str29 rownames=""
          mata
          b1= (("first","second","third","fourth","fifth")',strofreal(J(5,6,1),"%2.1f"))
          b1
          rownames=b1[1...,1]
          st_sstore(., "rownames", rownames)
          end
          
          l
          Res.:

          Code:
          .
          . mata
          ------------------------------------------------- mata (type end to exit) --------------------------------------------------------------
          :
          : b1= (("first","second","third","fourth","fifth")',strofreal(J(5,6,1),"%2.1f"))
          
          :
          : b1
                      1        2        3        4        5        6        7
              +----------------------------------------------------------------+
            1 |   first      1.0      1.0      1.0      1.0      1.0      1.0  |
            2 |  second      1.0      1.0      1.0      1.0      1.0      1.0  |
            3 |   third      1.0      1.0      1.0      1.0      1.0      1.0  |
            4 |  fourth      1.0      1.0      1.0      1.0      1.0      1.0  |
            5 |   fifth      1.0      1.0      1.0      1.0      1.0      1.0  |
              +----------------------------------------------------------------+
          
          :
          : rownames=b1[1...,1]
          
          :
          : st_sstore(., "rownames", rownames)
          
          :
          : end
          ----------------------------------------------------------------------------------------------------------------------------------------
          
          .
          .
          .
          . l
          
               +----------+
               | rownames |
               |----------|
            1. |    first |
            2. |   second |
            3. |    third |
            4. |   fourth |
            5. |    fifth |
               +----------+

          Comment

          Working...
          X