Announcement

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

  • saving regression output by strata

    Hi there

    Code:
    use http://www.stata-press.com/data/r13/stan3, clear
    stset t1, failure(died) id(id)
    bysort surg posttran: stcox age year
    matrix results = r(table)
    matlist results
    In the above command, the matrix only stores the last regression, in this case where surgery==1 & posttran==1, whereas my aim is to store all of them in separate matrices (which I can then put into Excel).

    I could run a long series of conditional commands but this is not a very satisfactory option (especially when the "by" variables could take a higher/changeable number of values).

    I'd be grateful if anyone could suggest a more elegant solution (perhaps as a loop?).

  • #2
    Code:
    clear*
    use http://www.stata-press.com/data/r13/stan3, clear
    stset, clear
    
    capture program drop one_regression
    program define one_regression
        stset t1, failure(died) id(id)
        local surg = surg[1]
        local posttran = posttran[1]
        stcox age year
        matrix results_s`surg'_pt`posttran' = r(table)
        exit
    end
    
    runby one_regression, by(surg posttran)
    will give you a separate matrix for each regression, with names indicating the values of surg and posttran that correspond. You can then write a loop exporting those to your spreadsheet. (Or, you can place an appropriate -putexcel- command inside program one_regression to do it as soon as the results have been obtained. I don't show code for that because I don't know how you want to place these different matrices inside the spreadsheet, and there would need to be some programming to tell -putexcel- where to put which matrix based on the current values of surg and poststran..)

    To use this code, you must install the -runby- command, by Robert Picard and me, available from SSC.

    Comment


    • #3
      This is great, Clyde, thanks so much.

      Out of curiosity, why does the stset command need to be inside the program for it to run successfully?

      (Not to worry if the answer is complicated!)

      Thanks again

      Comment


      • #4
        Actually, it's pretty simple. Behind the scenes, -runby- starts by taking the data set and putting it into a Mata matrix and clearing the data from active memory. Then each time it calls program one_regression, it seeds active memory with the observations from a single by-group, allows one_regression to run, saves the results, clears active memory again, and moves on to the next by-group. So meta-data such as -stset- and -xtset- settings are lost when the active memory is cleared, and they are not storable in a Mata matrix. So in order to provide that information about the data in active memory, it has to be reset inside one_regression.

        Comment


        • #5
          Thanks - very helpful.

          Is it possible, therefore, to add a -stsplit- command (either within the -runby- programme or outside it) and stratify by a time-varying covariate?

          The following doesn't seem to work:

          Code:
          clear*
          use http://www.stata-press.com/data/r13/stan3, clear
          stset, clear
          capture program drop one_regression
          program define one_regression
              stset t1, failure(died) id(id)
              stsplit followup, at(0,500,1799)
              local followup = followup[1]
              stcox age year
              matrix results_followup`followup' = r(table)
              exit
          end
          
          runby one_regression, by(followup)

          Comment


          • #6
            So what does "The following doesn't seem to work" mean?

            When I run it, it fails because the variable is not found. And, indeed, it does not exist in the data set that you start with. So -runby- doesn't know about it: you are creating it inside program one_regression, but -runby- doesn't know about that. Even if -runby- did know about that, since the variable doesn't exist at the time -runby- is called, you can't use it in your -runby- command.

            I'm not sure what you are actually trying to accomplish here. If you want stratified Cox models, -stcox- has a -strata()- option. Or perhaps you want separate Cox models for each value of follow-up. But either way, you have to create the variable followup before you can use it.

            Comment

            Working...
            X