Announcement

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

  • Graphing Sensitivity vs a Covariate

    Hi, I'm looking for help with the commands to generate a graph to illustrate the effect of Variable X on the sensitivities at 95% specificity of Variable Y.

    Attached is an example

    Thanks!
    Attached Files
    Last edited by Kelvin Wan; 02 Apr 2017, 12:30.

  • #2

    Comment


    • #3
      Code is like a set of directions to get you from point A to point B. You've told us what point B is. But you have left it to the imagination what point A is. What does the data set that you're starting from look like. Please use -dataex- to post an example of it. (Install -dataex- by running -ssc install dataex-, then run -help dataex- to read the simple directions for using it.)

      Comment


      • #4
        My data contains 3 variables only: I have done a ROC analysis to test the sensitivity of VarY to identity a case, now I want to to test how the sensitivity changes across VarX. Thanks.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte case float(VarY VarX)
        0 67.782104         0
        0  66.60757 1.3376954
        0  55.62016 1.1250879
        0  62.93091         0
        0  57.56018         0
        1  52.43447         0
        1  56.04311 1.6595508
        1  52.87027 1.1951367
        1  65.13115         0
        1  55.72673 1.6695703
        end

        Comment


        • #5
          Thanks. Now I have a sense of the variables you have in hand and where we need to start from. The example data set itself is too small to calculate values of sensitivity and specificity in a meaningful way, so I'm going to use a data set with some random data to illustrate the approach.

          There are some ambiguities in the way your question is posed. Since you are working from a data set with actual values of x, y, and case, your estimates of sensitivity and specificity will have gaps in them. That is, there will, in general, be no particular value of Y that gives exactly 95% specificity for any given X. (If there is one, it's just a lucky coincidence.) For example, in my data set below, as you move the cutpoint on Y up, the specificity will jump up each time a new non-case is diagnosed as such. So specificity may skip from, say .85 to 1. So what the code below does is it reports the first (highest possible) sensitivity that is consistent with a specificity >= 95%. If that isn't how you want to handle this situation, then modify the code accordingly.

          Code:
          // GENERATE TOY DATA
          set more off
          clear
          set obs 10
          set seed 1234
          gen x = runiform()
          expand 50
          gen y = rnormal(50, 5)
          
          gen p_case = invlogit(1 + 0.01*y - 0.004*x*y)
          summ p_case
          
          gen byte case = runiform() < p_case
          drop p_case
          
          //    CALCULATE SENS & SPEC AT VALUES OF Y WITHIN X
          by x, sort: egen n_cases = total(case)
          by x, sort: egen n_non_cases = total(!case)
          
          by x (y), sort: gen specificity = sum(!case)/n_non_cases
          gsort x -y
          by x: gen sensitivity = sum(case)/n_cases
          
          //    NOW FIND THE HIGHEST SENSITIVITY AT WHICH SPECIFICTY >= 95%
          by x: egen sens_at_spec_95 = max(cond(specificity >= 0.95, sensitivity, .))
          
          egen flag = tag(x)
          graph twoway line sens_at_spec_95 x if flag

          Comment

          Working...
          X