Announcement

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

  • Sensitivity, Specificity, PPV, NPV.+likelihood,- likelihood

    Hi,
    I want to calculate sensitivity,specificity,PPV,NPV.+likelihood,- likelihhod ratio for my data set. I have transferred the data sheet from SPSS to STATA. My disease staus was coded in SPSS file into 1 (disease positive) and 2 (disease negative) and test variable- 1 (positive) and 2 (negative). After transferring into STATA, it is categorizing positive and negative automatically for disease and normal and abnormal under test variable thus giving me a wrong statistics. Kindly advise

  • #2
    Hi, Ankit.

    There are a few options. Have you tried -diagt-?

    Code:
    findit diagt
    Last edited by Tiago Pereira; 16 Apr 2025, 06:54.

    Comment


    • #3
      While coding dichotomous variables as 1 = positive 2 = negative is useful in some other statistical packages, the native commands in Stata expect the coding to be negative = 0 and positive = 1 (or some other non-zero value). I have no idea what you mean when you say "After transferring into STATA, it is categorizing positive and negative automatically for disease and normal and abnormal under test variable..." Whether you imported to Stata by using SPSS's Save As command or Stata's -import spss-, Stata is not going to change the numerical values of the variables. It seems to me that what you need is to -recode disease test (2 = 0)-. If the variables have value labels, you will need to modify those to reflect the change from 2 to 0 to represent negative/normal.

      Once you do that, the usual Stata commands for diagnostic tests, such as -lsens- and -lroc- or -roctab- will function properly. Or you can follow Tiago Pereira's suggestion in #2--it requires installing a user-written command, but that one will work with variable that contain any two distinct values.

      Comment


      • #4
        Thank you, sir, for your feedback. However, after categorizing my positive values as 1 and negative values as 0 in the STATA file, it is not generating the desired results. After putting the tab command between the gold standard and the test variable , my TP=78,FP=98,TN=123,FN=26, yielding a sensitivity of 44.3% and specificity of 82.6% as per diagt command. According to the formula, sensitivity is calculated as TP/(TP+FN) = 78/(78+26) = 75%. There is a discordance in the results. please help

        Comment


        • #5
          @Clyde Schechter
          Sir, one more thing I would like to bring to your kind notice is that when I give the diagtest command, sensitivity and specificity are displayed correctly as per the formula. However, with the diagt command, they are displayed incorrectly. I need PPV,NPV, likelihood +/- ratio, and odds ratio, which are covered under the diagt command, not in the diagtest command.

          Comment


          • #6
            Hello Ankit Bhardwaj. It would help others a lot if you used -dataex- to share a small dataset followed by the commands you are executing. See items 12.1 through 12.3 in the FAQ.
            --
            Bruce Weaver
            Email: [email protected]
            Version: Stata/MP 18.5 (Windows)

            Comment


            • #7
              You probably just need to get the orientation correct:
              Code:
              . diagti  78 26 98 123
              
                    True |
                 disease |      Test result
                  status |      Neg.       Pos. |     Total
              -----------+----------------------+----------
                  Normal |       123         98 |       221 
                Abnormal |        26         78 |       104 
              -----------+----------------------+----------
                   Total |       149        176 |       325 
              
                                                                [95% Confidence Interval]
              ---------------------------------------------------------------------------
              Prevalence                         Pr(A)     32.0%     27.0%      37.4%
              ---------------------------------------------------------------------------
              Sensitivity                      Pr(+|A)     75.0%     65.6%     83.0%
              Specificity                      Pr(-|N)     55.7%     48.8%     62.3%
              ROC area               (Sens. + Spec.)/2      0.65      0.60      0.71 
              ---------------------------------------------------------------------------
              Likelihood ratio (+)     Pr(+|A)/Pr(+|N)      1.69      1.41      2.03 
              Likelihood ratio (-)     Pr(-|A)/Pr(-|N)      0.45      0.32      0.64 
              Odds ratio                   LR(+)/LR(-)      3.77      2.25      6.30 
              Positive predictive value        Pr(A|+)     44.3%     36.8%     52.0% 
              Negative predictive value        Pr(N|-)     82.6%     75.5%     88.3% 
              ---------------------------------------------------------------------------
              Works for me.

              Comment


              • #8
                You may be on to something there, daniel klein. Most of the resources I am familiar with lay out the 2x2 table with test results in in the rows (T+ in row 1, T- in row 2) and disease status in the columns (D+ in column 1, D- in column 2). For your example in #7, the table would look like this:


                Code:
                           |          col
                       row |        D+         D- |     Total
                -----------+----------------------+----------
                        T+ |      a=78       b=98 |       176
                        T- |      c=26      d=123 |       149
                -----------+----------------------+----------
                     Total |       104        221 |       325

                Because that layout is so common (at least in the resources I know), I wish -diagt- had an option to use that layout.

                Paul T Seed, what do you reckon? Is there any chance of adding that option? Thanks for considering.
                --
                Bruce Weaver
                Email: [email protected]
                Version: Stata/MP 18.5 (Windows)

                Comment


                • #9
                  diagtest (from STB or SCC) expects the first variable to be the test results, the second to be the true disease status. diagt (from SJ or SSC) reverses this order, aligning more closely with other Stata commands, such as rcotab and regression commands, where the outcome precedes the predictor(s). Both commands use tabulate to produce the 2-by-2 contingency table. However, they differ in layout and internal coding. diagtest places test results in rows and true states in columns. Because lower values indicate negative results and non-disease status, true positives appear in the bottom-right cell [2,2]. diagt internally flips the coding so that true positives are shown in the top-left cell [1,1]. However, it keeps the variable order and, thus, test results appear in columns and true states in rows. There's (at least) one more command, classtab (from SSC), with the same layout as diagt.

                  I've had my version of these commands for a while, and only now got around to polishing and uploading it. Anyone interested can download tab2diag from my GitHub repository.

                  Here's an example of the four commands:
                  Code:
                  . webuse hanley
                  (Tomographic images)
                  
                  . generate byte high_rating = rating == 5
                  
                  . diagtest high_rating disease
                  
                             |  True disease status
                  high_ratin |      of subject
                           g |         0          1 |     Total
                  -----------+----------------------+----------
                           0 |        56         18 |        74
                           1 |         2         33 |        35
                  -----------+----------------------+----------
                       Total |        58         51 |       109
                  
                  True D defined as disease ~= 0                        [95% Conf. Inter.]
                  -------------------------------------------------------------------------
                  Sensitivity                     Pr( +| D)  64.71%      55.73%   73.68%
                  Specificity                     Pr( -|~D)  96.55%      93.13%   99.98%
                  Positive predictive value       Pr( D| +)  94.29%      89.93%   98.64%
                  Negative predictive value       Pr(~D| -)  75.68%      67.62%   83.73%
                  -------------------------------------------------------------------------
                  Prevalence                      Pr(D)      46.79%      37.42%   56.16%
                  -------------------------------------------------------------------------
                  
                  . diagt disease high_rating
                  
                        True |
                     disease |
                   status of |      high_rating
                     subject |      Pos.       Neg. |     Total
                  -----------+----------------------+----------
                    Abnormal |        33         18 |        51
                      Normal |         2         56 |        58
                  -----------+----------------------+----------
                       Total |        35         74 |       109
                  True abnormal diagnosis defined as disease = 1
                  
                  
                                                                    [95% Confidence Interval]
                  ---------------------------------------------------------------------------
                  Prevalence                         Pr(A)     46.8%     37.2%      56.6%
                  ---------------------------------------------------------------------------
                  Sensitivity                      Pr(+|A)     64.7%     50.1%     77.6%
                  Specificity                      Pr(-|N)     96.6%     88.1%     99.6%
                  ROC area               (Sens. + Spec.)/2      0.81      0.74      0.88
                  ---------------------------------------------------------------------------
                  Likelihood ratio (+)     Pr(+|A)/Pr(+|N)     18.76      4.74     74.35
                  Likelihood ratio (-)     Pr(-|A)/Pr(-|N)      0.37      0.25      0.53
                  Odds ratio                   LR(+)/LR(-)     51.33     12.22         .
                  Positive predictive value        Pr(A|+)     94.3%     80.8%     99.3%
                  Negative predictive value        Pr(N|-)     75.7%     64.3%     84.9%
                  ---------------------------------------------------------------------------
                  
                  . classtab disease high_rating
                  
                               -----   Classified  -----
                     True    |         +            -   |      Total
                  -----------+--------------------------+-----------
                       +     |        33            18  |         51
                       -     |         2            56  |         58
                  -----------+--------------------------+-----------
                     Total   |        35            74  |        109
                  
                  
                  
                  Measure                                  Estimate  [95% Conf. Interval]
                  ------------------------------------------------------------------------
                  Sensitivity                     A/(A+B)   64.71%     52.08%    77.57%
                  Specificity                     D/(C+D)   96.55%     90.76%    99.58%
                  Positive predictive value       A/(A+C)   94.29%     85.08%    99.30%
                  Negative predictive value       D/(B+D)   75.68%     65.79%    84.90%
                  ------------------------------------------------------------------------
                  False positive rate             C/(C+D)    3.45%      1.08%    11.91%
                  False negative rate             B/(A+B)   35.29%     24.13%    49.93%
                  ------------------------------------------------------------------------
                  Correctly classified      A+D/(A+B+C+D)   81.65%     74.13%    88.42%
                  ------------------------------------------------------------------------
                  ROC area                                  80.63%     73.60%    87.66%
                  ------------------------------------------------------------------------
                  
                  . tab2diag disease high_rating
                  
                             |  True disease status
                             |      of subject
                  Classified |  Pos. (D)  Neg. (~D) |     Total
                  -----------+----------------------+----------
                    Pos. (+) |        33          2 |        35
                    Neg. (-) |        18         56 |        74
                  -----------+----------------------+----------
                       Total |        51         58 |       109
                  
                     Pos. (D):  disease == 1          
                    Neg. (~D):  disease == 0          
                  
                     Pos. (+):  high_rating == 1      
                     Neg. (-):  high_rating == 0      
                  
                                                                             [95% conf. interval]
                  -------------------------------------------------------------------------------
                  Sensitivity                         Pr( +| D)    .6470588    .500682   .7756938
                  Specificity                         Pr( -|~D)    .9655172   .8809227   .9957964
                  Positive predictive value           Pr( D| +)    .9428571   .8084286   .9930032
                  Negative predictive value           Pr(~D| -)    .7567568   .6430857   .8490182
                  -------------------------------------------------------------------------------
                  Accuracy                          Pr(correct)    .8165138   .7309446   .8841692
                  Prevalence                              Pr(D)    .4678899    .371676   .5658923
                  -------------------------------------------------------------------------------
                  Likelihood ratio (LR+)     Pr(+|D) / Pr(+|~D)    18.76471   4.735921   74.34967
                  Likelihood ratio (LR-)     Pr(-|D) / Pr(-|~D)    .3655462   .2512923   .5317475
                  Odds ratio                      LR(+) / LR(-)    51.33333   10.85526    462.478
                  -------------------------------------------------------------------------------
                  ROC area                 [Pr(+|D)+Pr(-|~D)]/2     .806288   .7359504   .8766257
                  -------------------------------------------------------------------------------

                  Comment

                  Working...
                  X