Login | Register

Info | Home

BioPHP - Sensibility and Specificity of diagnostic tests (original)

Original code submitted by joseba
Code bellow is covered by GNU GPL v2 license.

Description

Last change: 2016/10/28 21:41 | Recent Changes
Computes Sensitivity, Specificity, Positive Predictive Value, Negative
Predictive Value,Positive Likelihood Ratio and Negative Likelihood Ratio .

Code

Last change: 2016/10/28 21:41 | Download original | Recent Changes | Original code and
<html>
<head>
<title>Sensitivity and Specificity of diagnostic tests</title>
</head>
<body bgcolor=FFFFFF>
<center>
<h1>Sensitivity and Specificity of diagnostic tests</h1>

<?php

// author    Joseba Bikandi
// license   GNU GPL v2
// biophp.org

error_reporting(0);

// if formula is requested, show it
if ($_GET["show"]=="formula"){print_formula();die;}

if (!$_POST){
        // when nothing is posted, this data is used as an example
        $TP=88;
        $FP=7;
        $TN=12;
        $FN=15;
}else{
        // when data is posted, get the data
        $TP=$_POST["TP"];
        $FP=$_POST["FP"];
        $TN=$_POST["TN"];
        $FN=$_POST["FN"];

}
$sensitivity =$TP/($TP+$FN);
$specificity=$TN/($TN+$FP);
$PPV=$TP/($TP+$FP);
$NPV=$TN/($TN+$FN);
$LRp = $sensitivity / (1-$specificity);
$LRn = (1-$sensitivity) / $specificity;

print_form($TP,$FP,$TN,$FN);
print_results($sensitivity,$specificity,$PPV,$NPV,$LRp,$LRn);


//########print form
function print_form($TP,$FP,$TN,$FN){
?>
    <form action="<?php print $_SERVER["PHP_SELF"]; ?>" method=post>
    <table border="1" cellpadding="0" cellspacing="2" bgcolor=EEEEFF>
    <tbody>
        <tr>
                <td rowspan=2 colspan=2></td>
                <td colspan=2 align=center bgcolor=AAAAAA>Disease</td>
                <td rowspan=2></td>
        </tr>
        <tr>
                <td align=center bgcolor=AAAAFF>Positive</td>
                <td align=center bgcolor=AAAAFF>Negative</td>
        </tr>
        <tr>
                <td rowspan=2 align=center bgcolor=AAAAAA>Test</td>
                <td align=center bgcolor=AAAAFF>Positive</td>
                <td align=center bgcolor=DDDDFF>True Positive (TP)<br><input type=text name=TP value="<?php print $TP; ?>" size=5></td>
                <td align=center bgcolor=DDDDFF>False Positive (FP)<br><input type=text name=FP value="<?php print $FP; ?>" size=5></td>
                <td align=center>TP + FP<br><b><?php print $TP+$FP; ?></b></td>
        </tr>
        <tr>
                <td align=center bgcolor=AAAAFF>Negative</td>
                <td align=center bgcolor=DDDDFF>False Negative (FN)<br><input type=text name=FN value="<?php print $FN; ?>" size=5></td>
                <td align=center bgcolor=DDDDFF>True Negative (TN)<br><input type=text name=TN value="<?php print $TN; ?>" size=5></td>
                <td align=center>FN + TN<br><b><?php print $FN+$TN; ?></b></td>
        </tr>
        <tr>
                <td colspan=2></td>
                <td align=center>TP + FN<br><b><?php print $TP+$FN; ?></b></td>
                <td align=center>FP + TN<br><b><?php print $FP+$TN; ?></b></td>
                <td align=center><?php print $TP+$FN+$FP+$TN; ?></td>
        </tr>
    </tbody>
    </table>
    <input type=submit value=Submit>
    </form>
<?php
}
//######## print results
function print_results($sensitivity,$specificity,$PPV,$NPV,$LRp,$LRn){
?>

        <table>

        <tr><td><a href=?show=formula>Sensitivity:</a></td><td><?php print $sensitivity; ?></td></tr>
        <tr><td><a href=?show=formula>Specificity:</a></td><td><?php print $specificity; ?></td></tr>
        <tr><td><a href=?show=formula>Positive Predictive Value (PPV):</a></td><td><?php print $PPV; ?></td></tr>
        <tr><td><a href=?show=formula>Negative Predictive Value (NPV):</a></td><td><?php print $NPV; ?></td></tr>
        <tr><td><a href=?show=formula>Likelihood Ratio Positive (LR+):</a></td><td><?php print $LRp; ?> &nbsp; &nbsp; </td></tr>
        <tr><td><a href=?show=formula>Likelihood Ratio Negative (LR-):</a></td><td><?php print $LRn; ?></td></tr>

        </table>
<?php
}

// #################### print example
function print_formula(){
?>
        <table width=600>
        <tr><td>
                <b>Sensitivity</b>
                <p>The probability of the test finding disease among those who have the disease
                or the proportion of people with disease who have a positive test result.
                <p> Sensitivity = true positives / (true positives + false negatives)
                <hr>

                <b>Specificity</b>
                <p>The probability of the test finding NO disease among those who do NOT have
                the disease or the proportion of people free of a disease who have a negative test.
                <p> Specificity = true negatives / (true negatives + false positives)
                <hr>

                <b>Positive Predictive Value (PPV)</b>
                <p>The percentage of people with a positive test result who actually have the disease.
                <p> Positive predictive value = true positives / (true positives + false positives)
                <hr>

                <b>Negative Predictive Value (NPV)</b>
                <p>The percentage of people with a negative test who do NOT have the disease.
                <p> Negative predictive value = true negatives / (true negatives + false negatives)
                <hr>

                <b>Likelihood Ratio Positive (LR+)</b>
                <p>The odds that a positive test result would be found in a patient with,
                versus without, a disease.
                <p> Likelihood Ratio Positive (LR+) = Sensitivity / (1 - Specificity).
                <hr>

                <b>Likelihood Ratio Negative (LR-)</b>
                <p>The odds that a negative test result would be found in a patient without,
                versus with, a disease.
                <p> Likelihood Ratio Negative (LR-) = (1- Sensitivity) / Specificity.
                <hr>
               <div align=right><a href="<? print $_SERVER["PHP_SELF"]; ?>">Go back to this tool</a></div>
        </td></tr>
        </table>
<?php
}

function sum ($nums) {
        $temp = 0;
        foreach ($nums as $key => $val) {
                $temp += $val;
        }
        return $temp;
}

function sum2 ($nums) {
        $temp = 0;
        foreach ($nums as $key => $val) {
                $temp += pow($val,2);
        }
        return $temp;
}


function mean ($nums) {
        $temp = 0;
        foreach ($nums as $key => $val) {
                $temp += $val;
        }
        return  $temp/sizeof($nums);;
}

function median ($nums) {
        $n = count($nums);
        sort($nums);
        if ($n & 1) {
                return $nums [($n-1)/2];
        } else {
                return ($nums [($n-1)/2] + $nums [$n/2])/2;
        }
}

function mode ($nums) {
        foreach ($nums as $key => $val) {
                $counts[$val]++;
        }
        arsort($counts);
        if (count($nums)==count($counts)){
                return "frequency for each data is 1";
        }else{
                return key($counts);
        }

}


function variance ($nums) {
        $n = count($nums);
        $mean = mean($nums);
        foreach ($nums as $key => $val) {
                $temp += pow($val - $mean, 2);
        }
        return $temp/$n;
}

function sd ($nums) {
        return sqrt(variance($nums));
}


function skewness ($nums) {
        $n = count($nums);
        $mean = mean($nums);
        $sd = sd($nums);
        foreach ($nums as $key => $val) {
                $temp += pow(($val - $mean), 3);
        }
        $s = $temp/(($n - 1)*pow($sd,3));
        return $s;
}

function kurtosis ($nums) {
        $n = count($nums);
        $mean = mean($nums);
        $sd = sd($nums);
        foreach ($nums as $key => $val) {
                $temp += pow(($val - $mean), 4);
        }
        $s = ($temp/(($n - 1)*pow($sd,4)))-3;
        return $s;
}

?>
<hr>
Source code is available at <a href=http://www.biophp.org/stats/sensitivity_specificity/>biophp.org</a>
</center>
</body>
</html>