src/Services/Health/CvdRiskPredictionLipids.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Services\Health;
  3. class CvdRiskPredictionLipids
  4. {
  5.     private $M_AGE = 3.06117;
  6.     private $M_SBP_NO_TRT = 1.93303;
  7.     private $M_SBP_TRT = 1.99881;
  8.     private $M_TCL = 1.1237;
  9.     private $M_HDL = -0.93263;
  10.     private $M_SMOKE = 0.65451;
  11.     private $M_DIAB = 0.57367;
  12.     private $F_AGE = 2.32888;
  13.     private $F_SBP_NO_TRT = 2.76157;
  14.     private $F_SBP_TRT = 2.82263;
  15.     private $F_TCL = 1.20904;
  16.     private $F_HDL = -0.70833;
  17.     private $F_SMOKE = 0.52873;
  18.     private $F_DIAB = 0.69154;
  19.     public function calcGenCvdRiskPredictionLipids(
  20.         int $iSex,
  21.         int $iAge,
  22.         int $iSysBloodPress,
  23.         bool $bTreatmentForHypertension,
  24.         bool $bSmoking,
  25.         bool $bDiabetes,
  26.         int $iHDL,
  27.         int $iTotalCholesterol,
  28.         bool $debug = false
  29.     ) {
  30.         if ($debug) {
  31.             echo 'iSex: ' . $iSex . '<br/>';
  32.             echo 'iAge: ' . $iAge . '<br/>';
  33.             echo 'iSysBloodPress: ' . $iSysBloodPress . '<br/>';
  34.             echo 'bTreatmentForHypertension: ' . (int)$bTreatmentForHypertension . '<br/>';
  35.             echo 'bSmoking: ' . (int)$bSmoking . '<br/>';
  36.             echo 'bDiabetes: ' . (int)$bDiabetes . '<br/>';
  37.             echo 'iHDL: ' . $iHDL . '<br/>';
  38.             echo 'iTotalCholesterol: ' . $iTotalCholesterol . '<br/><br/>';
  39.         }
  40.         $risk = new PredictedRisk($iSex, $bTreatmentForHypertension);
  41.         $sex = $iSex;
  42.         $age = log($iAge);
  43.         $sbp = log($iSysBloodPress);
  44.         $tcl = log($iTotalCholesterol);
  45.         $hdl = log($iHDL);
  46.         $trtbp = $bTreatmentForHypertension ? 1 : 0;
  47.         $smoke = $bSmoking ? 1 : 0;
  48.         $diab = $bDiabetes ? 1 : 0;
  49.         if ($debug) {
  50.             echo 'sex: ' . $sex . '<br />';
  51.             echo 'age: ' . $age . '<br />';
  52.             echo 'sbp: ' . $sbp . '<br />';
  53.             echo 'tcl: ' . $tcl . '<br />';
  54.             echo 'hdl: ' . $hdl . '<br />';
  55.             echo 'trtbp: ' . $trtbp . '<br />';
  56.             echo 'smoke: ' . $smoke . '<br />';
  57.             echo 'diab: ' . $diab . '<br /><br />';
  58.         }
  59.         if ($iSex == GenderType::FEMALE) {
  60.             $sumBetaX_No_SBP_Trt = $age * $this->F_AGE + $sbp * $this->F_SBP_NO_TRT + $tcl * $this->F_TCL + $hdl * $this->F_HDL + $smoke * $this->F_SMOKE + $diab * $this->F_DIAB;
  61.             $sumBetaX_SBP_With_Trt = $age * $this->F_AGE + $sbp * $this->F_SBP_TRT + $tcl * $this->F_TCL + $hdl * $this->F_HDL + $smoke * $this->F_SMOKE + $diab * $this->F_DIAB;
  62.             $riskScore_No_SBP_Trt = 1 - pow(0.95012, exp($sumBetaX_No_SBP_Trt - 26.1931));
  63.             $riskScore_With_SBP_Trt = 1 - pow(0.95012, exp($sumBetaX_SBP_With_Trt - 26.1931));
  64.         } elseif ($iSex == GenderType::MALE) {
  65.             $sumBetaX_No_SBP_Trt = $age * $this->M_AGE + $sbp * $this->M_SBP_NO_TRT + $tcl * $this->M_TCL + $hdl * $this->M_HDL + $smoke * $this->M_SMOKE + $diab * $this->M_DIAB;
  66.             $sumBetaX_SBP_With_Trt = $age * $this->M_AGE + $sbp * $this->M_SBP_TRT + $tcl * $this->M_TCL + $hdl * $this->M_HDL + $smoke * $this->M_SMOKE + $diab * $this->M_DIAB;
  67.             $riskScore_No_SBP_Trt = 1 - pow(0.88936, exp($sumBetaX_No_SBP_Trt - 23.9802));
  68.             $riskScore_With_SBP_Trt = 1 - pow(0.88936, exp($sumBetaX_SBP_With_Trt - 23.9802));
  69.         } else {
  70.             return null;  // ERROR invalid gender
  71.         }
  72.         if ($debug) {
  73.             echo 'sumBetaX_No_SBP_Trt: ' . $sumBetaX_No_SBP_Trt . '<br/>';
  74.             echo 'sumBetaX_SBP_With_Trt: ' . $sumBetaX_SBP_With_Trt . '<br/>';
  75.             echo 'riskScore_No_SBP_Trt: ' . $riskScore_No_SBP_Trt . '<br/>';
  76.             echo 'riskScore_With_SBP_Trt: ' . $riskScore_With_SBP_Trt . '<br/><br />';
  77.         }
  78.         $risk->riskScore_No_SBP_Trt = $riskScore_No_SBP_Trt;
  79.         $risk->riskScore_With_SBP_Trt = $riskScore_With_SBP_Trt;
  80.         if ($iSex == GenderType::FEMALE) {
  81.             $sumBetaX_No_SBP_Trt_OPTIMAL = $this->F_AGE * $age + $this->F_SBP_NO_TRT * log(110) + $this->F_TCL * log(160) + $this->F_HDL * log(60);
  82.             $sumBetaX_No_SBP_Trt_NORMAL = $this->F_AGE * $age + $this->F_SBP_NO_TRT * log(125) + $this->F_TCL * log(180) + $this->F_HDL * log(45);
  83.         } elseif ($iSex == GenderType::MALE) {
  84.             $sumBetaX_No_SBP_Trt_OPTIMAL = $this->M_AGE * $age + $this->M_SBP_NO_TRT * log(110) + $this->M_TCL * log(160) + $this->M_HDL * log(60);
  85.             $sumBetaX_No_SBP_Trt_NORMAL = $this->M_AGE * $age + $this->M_SBP_NO_TRT * log(125) + $this->M_TCL * log(180) + $this->M_HDL * log(45);
  86.         } else {
  87.             return null;  // ERROR invalid gender
  88.         }
  89.         if ($debug) {
  90.             echo 'sumBetaX_No_SBP_Trt_OPTIMAL: ' . $sumBetaX_No_SBP_Trt_OPTIMAL * 100 . '<br/>';
  91.             echo 'sumBetaX_No_SBP_Trt_NORMAL: ' . $sumBetaX_No_SBP_Trt_NORMAL * 100 . '<br/><br/>';
  92.         }
  93.         if ($iSex == GenderType::FEMALE) {
  94.             $optimalRisk = 1 - pow(0.95012, exp($sumBetaX_No_SBP_Trt_OPTIMAL - 26.1931));
  95.             $normalRisk = 1 - pow(0.95012, exp($sumBetaX_No_SBP_Trt_NORMAL - 26.1931));
  96.         } elseif ($iSex == GenderType::MALE) {
  97.             $optimalRisk = 1 - pow(0.88936, exp($sumBetaX_No_SBP_Trt_OPTIMAL - 23.9802));
  98.             $normalRisk = 1 - pow(0.88936, exp($sumBetaX_No_SBP_Trt_NORMAL - 23.9802));
  99.         } else {
  100.             return null;  // ERROR invalid gender
  101.         }
  102.         $risk->optimalRisk = $optimalRisk;
  103.         $risk->normalRisk = $normalRisk;
  104.         if ($iSex == GenderType::FEMALE) {
  105.             if ($bTreatmentForHypertension) {
  106.                 $consti_num = exp(-($this->F_SBP_TRT * $sbp + $this->F_TCL * $tcl + $this->F_HDL * $hdl + $this->F_SMOKE * $smoke + $this->F_DIAB * $diab - 26.1931) / 2.32888);
  107.                 $consti_denom = pow((-log(0.95012)), (1 / 2.32888));
  108.                 $consti = $consti_num * 1 / $consti_denom;
  109.                 $expo = 1 * (1 / 2.32888);
  110.                 $term = pow(-log(1 - $riskScore_With_SBP_Trt), $expo);
  111.             } else {
  112.                 $consti_num = exp(-($this->F_SBP_NO_TRT * $sbp + $this->F_TCL * $tcl + $this->F_HDL * $hdl + $this->F_SMOKE * $smoke + $this->F_DIAB * $diab - 26.1931) / 2.32888);
  113.                 $consti_denom = pow((-log(0.95012)), (1 / 2.32888));
  114.                 $consti = $consti_num * 1 / $consti_denom;
  115.                 $expo = 1 * (1 / 2.32888);
  116.                 $term = pow(-log(1 - $riskScore_No_SBP_Trt), $expo);
  117.             }
  118.             $heart_age = $consti * $term;
  119.         } elseif ($iSex == GenderType::MALE) {
  120.             if ($bTreatmentForHypertension) {
  121.                 $consti_num = exp(-($this->M_SBP_NO_TRT * $sbp + $this->M_TCL * $tcl + $this->M_HDL * $hdl + $this->M_SMOKE * $smoke + $this->M_DIAB * $diab - 23.9802) / 3.06117);
  122.                 $consti_denom = pow((-log(0.88936)), (1 / 3.06117));
  123.                 $consti = $consti_num * 1 / $consti_denom;
  124.                 $expo = 1 * (1 / 3.06117);
  125.                 $term = pow(-log(1 - $riskScore_With_SBP_Trt), $expo);
  126.             } else {
  127.                 $consti_num = exp(-($this->M_SBP_NO_TRT * $sbp + $this->M_TCL * $tcl + $this->M_HDL * $hdl + $this->M_SMOKE * $smoke + $this->M_DIAB * $diab - 23.9802) / 3.06117);
  128.                 $consti_denom = pow((-log(0.88936)), (1 / 3.06117));
  129.                 $consti = $consti_num * 1 / $consti_denom;
  130.                 $expo = 1 * (1 / 3.06117);
  131.                 $term = pow(-log(1 - $riskScore_No_SBP_Trt), $expo);
  132.             }
  133.             $heart_age = $consti * $term;
  134.         } else {
  135.             return null;  // ERROR invalid gender
  136.         }
  137.         if ($debug) {
  138.             echo 'consti_num: ' . $consti_num . '<br/>';
  139.             echo 'consti_denom: ' . $consti_denom . '<br/>';
  140.             echo 'consti: ' . $consti . '<br/>';
  141.             echo 'expo: ' . $expo . '<br/>';
  142.             echo 'term: ' . $term . '<br/>';
  143.         }
  144.         $risk->heart_age = round($heart_age);
  145.         return $risk->toArray();
  146.     }
  147. }