Announcement

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

  • Merge Mata matrix with Stata data file

    Hi!


    I know that this question has already been published but the solution is not actually what I am looking for.
    I am a beginner user of Stata and I have a data set with patients information and Mata matrix with population survival probabilities. The mata matrix is stratified by sex, age and calendar year:

    | sex _year _age survprob | |-------------------------------| | 1 2000 50 .99376 | | 1 2000 51 .99318 | | 2 2000 50 .99751 | | 2 2000 51 .99744 |

    I am looking for either:

    1. Save the mata matrix of population mortality rates as .dta file which I can merge m1 with patients data file aftrewords OR

    2. Merge mata matrix with .dta file by sex,age and calendar year.

    I am grateful for any advice.

    Thank you very mych in advance.

    Regards,
    Yuliya



  • #2
    Perhaps this sample technique will start you in a useful direction to save your Mata matrix as a Stata dataset that you can then merge with the patients dataset.
    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) ----------------------
    : sp
                     1             2             3             4
        +---------------------------------------------------------+
      1 |            1          2000            50   .9937599897  |
      2 |            1          2000            51   .9931799769  |
      3 |            2          2000            50    .997510016  |
      4 |            2          2000            51   .9974399805  |
        +---------------------------------------------------------+
    
    : st_dropvar(.)
    
    : st_addvar("int",("sex" , "year" , "age"))
           1   2   3
        +-------------+
      1 |  1   2   3  |
        +-------------+
    
    : st_addvar("float", "survprob")
      4
    
    : st_addobs(rows(sp))
    
    : st_store(.,.,sp)
    
    : end
    ------------------------------------------------------------------------------------------------
    
    . list
    
         +-----------------------------+
         | sex   year   age   survprob |
         |-----------------------------|
      1. |   1   2000    50     .99376 |
      2. |   1   2000    51     .99318 |
      3. |   2   2000    50     .99751 |
      4. |   2   2000    51     .99744 |
         +-----------------------------+
    
    . save survprob, replace
    file survprob.dta saved
    
    .

    Comment


    • #3
      Dear Yuliya LeontyevaL
      A common (and often good) strategy when asking for help is to isolate the problem at hand, to make it simpler to ask a question and get an answer.
      However, this approach ignores the path you choose at first to solve your real problem.
      And sometimes it is good to go back to the original problem.

      I'm guessing now, but it seems to me like you want to move some predicted survival probabilties dependent on sex, year and age to another dataset.
      If that is the case a simpler way is to save your regression estimates from the estimation dataset by -estimates save-, open the dataset where you need to predict/merge, retrieve the regression by -estimates use- and finally get the predicted survival probabilities by using -predict-.

      If my solution isn't what you need, I still think you will benefit from looking back at question that really lead you to ask the current question.
      Kind regards

      nhb

      Comment


      • #4
        Niels Henrik Bruun in post #3 has cleared up confusion I had in addressing post #1. If indeed your goal is to apply the results of a model built on on a "training dataset" to the observations in a different dataset, his solution is how Stata intends for this process to be done. The approach begun in post #1 is doing it the hard way.

        Comment

        Working...
        X