Announcement

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

  • Output results from looped cox regression

    Hello,
    I am fairly new to Stata.
    I need to run a cox regression over 248 variables and I want to then output HR and p-value. I have done a simple loop and got the results.
    I got a table shown in the pic below for 248 variables and I tried to put results (table) in the excel but it didn't work.
    Click image for larger version

Name:	Untitled.png
Views:	1
Size:	17.2 KB
ID:	1501323




    stset Day_death, failure(Con_OS)
    forvalue i = 1/248 {
    stcox v`i'
    putexcel (A`i') = matrix (r(table)), names
    }

    Any help would be appreciated.
    Last edited by Marie Webbs; 03 Jun 2019, 08:09.

  • #2
    You'll want something like this:

    Code:
    stset Day_death, failure(Con_OS)
    putexcel set "YourFile.xls"  // establish output file
    forvalue i = 1/248 {
       // extract desired values from r(table); you apparently don't want the whole r(table) matrix
       local hr = el(r(table), 1, 2)
       local p = el(r(table), 4, 2)  
       // output values
       putexcel A`i' = `hr'  // don't want the whole r(table) matrix
       putexcel B`i' =`p'
    }

    Comment


    • #3
      Thank for your help Mike.
      I tried your command but my excel was blank like it didn't store any results.
      Last edited by Marie Webbs; 04 Jun 2019, 02:47.

      Comment


      • #4
        .

        Originally posted by Mike Lacy View Post
        Code:
        stset Day_death, failure(Con_OS)
        putexcel set "YourFile.xls" // establish output file
        forvalue i = 1/248 {
        stcox v`i'
        // extract desired values from r(table); you apparently don't want the whole r(table) matrix
        local hr = el(r(table), 1, 1)
        local p = el(r(table), 4, 1)
        // output values
        putexcel A`i' = `hr' // don't want the whole r(table) matrix
        putexcel B`i' =`p'
        }
        putexcel close

        Comment


        • #5
          Originally posted by Andrea Discacciati View Post
          .
          That worked! Thank you Andrea!

          Comment


          • #6
            I am running a similar model as the original poster with 394 outcomes but have covariates and am not sure how to alter the code to get the hazard ratios and p values. Running the code below gives me the error, "A: invalid cell name". I am using Stata v16.1.
            Code:
            stset time, failure(EFS_EVENT) id(sampleid)
            putexcel set "PKresults.xls" // establish output file
            foreach var of varlist rs10141763 - rs997777 {
                 stcox `var' age histology group lymph_node measurement pri_site
             } 
            // extract desired values from r(table); 
            local hr = el(r(table), 1, 1)
            local p = el(r(table), 4, 1)
            // output values
            putexcel A `var' = `hr' 
            putexcel B `var' = `p'
            }
            putexcel close
            I have read the help file for putexcel but cannot seem to fix the error.

            Comment

            Working...
            X