Announcement

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

  • Overlaying graph produced by roccomp and twoway commands

    Hello, can we overlay a graph produced by roccomp and twoway? I have the following graphs, and I would like the points on the two-way scatter plot to appear on the ROC curve graph. Here are the codes and the respective graphs produced: Please also note the numbering on the x and y axes of both graphs and the size of the graphs, which I think need to be the same for the overlaying to be correct and accurate.


    Code:
    roccomp xpert_result1 cad4tb7_2 qxr_v4 infv_2, summary graph
    Click image for larger version

Name:	ROC curves.png
Views:	1
Size:	94.3 KB
ID:	1776531




    Code:
    twoway (scatter tpr fpr, ///
            msymbol(circle triangle square diamond x +) ///
            mlabel(reader_id) mlabpos(12) ///
            legend(off) ///
            xtitle("False Positive Rate (1 – Specificity)") ///
            ytitle("True Positive Rate (Sensitivity)") ///
            title("Expert Reader Overlay") ///
            xlabel(0(.1)1) ylabel(0(.1)1)), ///
            name(expert_overlay, replace)
    Click image for larger version

Name:	Two-way scatter plot.png
Views:	1
Size:	71.1 KB
ID:	1776532


  • #2
    It doesn't look like you can modify the roccomp graph. Your best bet is to recreate this graph using twoway and add your edits. Here is a way using sersets.

    Code:
    webuse ct2, clear
    preserve
    cap frame drop data
    frame create data
    tempfile holding
    roccomp status mod1 mod3, graph summary saving(gr1, replace)
    qui serset dir
    forval i= 0/`=`r(N_g)'-1'{
        serset `i'
        serset use, clear
        save `holding', replace
        local cmd= cond(!`i', "append", "merge 1:1 _n") 
        frame data: `cmd' using `holding'
    }
    restore
    frame data{
        qui ds
        tokenize `r(varlist)'
        tw line sens* `2'|| scatter sens* `2', mc(stc1 stc2)|| ///
        scatteri 0 0 1 1, ms(none) connect(line) lc(black) ||, ///
        ytitle(Sensitivity) xtitle(1-specificity) saving(gr2, replace)
    }
    
    gr combine gr1.gph gr2.gph
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	73.1 KB
ID:	1776539

    Comment


    • #3
      Another way is to save the GPH created by roccomp and add your edits using the addplot command.

      Code:
      ssc describe addplot

      Comment


      • #4
        Instead of

        tw line sens* `2' || scatter sens* `2', mc(stc1 stc2) || ///
        in #2 that compels you to specify colors for the markers matching the lines, it's better to use

        Code:
        tw connected sens* `2' || ///
        This should combine the line and marker in the legend too.
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.3 KB
ID:	1776559



        Comment


        • #5
          Hi Andrew, thank you for your feedback. When I try to run the code above, an error is flagged indicating that the variable sens* is not found. Are you able to help resolve this error? Additionally, when I replaced the ct2 dataset variables with my own (i.e., xpert_result1 cad4tb7_2 qxr_v4 and infv_2) and run the first block of code before the restore command, the roccomp graph is generated but an error is flagged indicating that the variable _merge is already defined. What could be the reason for this error? Lastly, I tried using addplot, to plot the the 6 readers data points onto the roccomp generated graph but that did not work too. My understanding is that addplot adds twoway plot objects to an existing twoway graph and the graph generated by roccomp is not a twoway graph (I stand to be corrected). See the code below that executes but no changes are effected to the roc_base graph.

          Code:
          addplot roc_base.gph: twoway (scatter tpr fpr, msymbol(circle triangle square diamond x +) ///
                  legend(off) ///
                  xtitle("False Positive Rate (1 – Specificity)") ///
                  ytitle("True Positive Rate (Sensitivity)") ///
                  title("Expert Reader Overlay") ///
          Thank you for valuable feedback as usual.

          Best regards,
          Paul

          Comment


          • #6
            Can you add a data example? Also, state what version of Stata you are using if it is not the latest (StataNow 19.5). For the merge error, it is apparent that if you have more than one series to plot, it will emerge. Add:

            Code:
            webuse ct2, clear
            preserve
            cap frame drop data
            frame create data
            tempfile holding
            roccomp status mod1 mod3, graph summary saving(gr1, replace)
            qui serset dir
            forval i= 0/`=`r(N_g)'-1'{
                serset `i'
                serset use, clear
                save `holding', replace
                local cmd= cond(!`i', "append", "merge 1:1 _n")
                frame data: cap drop _merge
                frame data: `cmd' using `holding'
            }
            restore
            frame data{
                qui ds
                tokenize `r(varlist)'
                tw connected sens* `2'||  ///
                scatteri 0 0 1 1, ms(none) connect(line) lc(black) ||, ///
                ytitle(Sensitivity) xtitle(1-specificity) saving(gr2, replace)
            }
            
            gr combine gr1.gph gr2.gph
            Last edited by Andrew Musau; 27 Apr 2025, 08:30.

            Comment


            • #7
              Hi Andrew,

              Thank you for your feedback, as always. Apologies for the delayed response; I was in transit. I have been able to fix the issues raised in the previous post, and I have one issue pending to get the result that I want. Just adding the reader data points to the gr2.gph that has been created. The code below runs, however I am not seeing the readers data points on the gr2.gph. What is it that I am not doing right? I am using STATA18

              Code:
              use readers.dta, clear
              
              keep if reference_test=="MRS"
              
              graph use gr2.gph
              
              addplot gr2.gph: (scatter tpr fpr, msymbol(circle triangle square diamond x +) ///
               mlabel(reader_id) mlabpos(12)), ///
              xlabel(0(0.2)1) ylabel(0(0.2)1) ///
              xtitle("False Positive Rate (1 – Specificity)") ///
              ytitle("True Positive Rate (Sensitivity)") ///
              title("ROC Curve with Expert Reader Points")
              Best regards,
              Paul
              Last edited by Paul Somwe; 29 Apr 2025, 04:32.

              Comment


              • #8
                As I hinted in #6, I cannot help much without a reproducible example. Using the dataset from #2, I cannot replicate your issue.

                Code:
                webuse ct2, clear
                roccomp status mod1 mod3, graph summary saving(gr1, replace)
                gr use gr1.gph
                set seed 04292025
                gen y= rnormal()
                gen x= rnormal()
                addplot: (scatter y x if inrange(y, -.5, 1.5) & inrange(x, -.5, 1.5), ms(oh) mc(black) leg(off))
                Res.:

                Click image for larger version

Name:	Graph.png
Views:	1
Size:	53.3 KB
ID:	1776708

                Comment


                • #9
                  Hello Andrew,

                  Thank you for your feedback. With your help, I was able to find the solution code below which works perfectly fine.

                  Code:
                  use readers.dta, clear
                  
                  keep if reference_test=="Unconfirmed"
                  
                  graph use gr2.gph
                  
                  addplot gr2:(scatter tpr fpr if reader_id==1, msymbol(circle) mlabpos(12)) ///
                              (scatter tpr fpr if reader_id==2, msymbol(triangle) mlabpos(12)) ///
                              (scatter tpr fpr if reader_id==3, msymbol(square) mlabpos(12)) ///
                              (scatter tpr fpr if reader_id==4, msymbol(diamond) mlabpos(12)) ///
                              (scatter tpr fpr if reader_id==5, msymbol(x) mlabpos(12)) ///
                              (scatter tpr fpr if reader_id==6, msymbol(+) mlabpos(12)), ///
                  ytitle(True Positive Rate (Sensitivity)) xtitle(False Positive Rate (1 – Specificity)) title(ROC Curves with Expert Reader True and False Positive Rates)
                  Best regards,
                  Paul

                  Comment

                  Working...
                  X