# Relationship between systolic blood pressure, total/HDL cholesterol and BMI

A project log for Medical tricorder

Using artificial intelligence to identify a disease by its symptoms

M. Bindhammer 05/02/2015 at 03:000 Comments

The relationship between total/HDL cholesterol and BMI can be expressed by simple linear regression models:

The relationship between the systolic blood pressure and BMI can be expressed by a simple multiple regression model:

Now diabetes type 2 and Framingham risk score calculator can be combined as mentioned in my previous project log. With the data we get from the diabetes type 2 questionnaire, we can compute total/HDL cholesterol and systolic blood pressure which are needed for the Framingham risk score algorithm. Video after the code snippet...

/*
diabetes type 2 & heart attack risk calculator
total/HDL cholesterol and systolic blood pressure
estimated by linear regression models
*/

char commandbuffer[10];
int commandbuffer_index;

void setup (){
Serial.begin(9600);
Serial.flush();
}

void loop () {
char* a_c[] = {"a) ", "b) ", "c) "};
char* questionary[8][4] =
{
{"1. GENDER", "Female", "Male"},
{"2. HIGH BLOOD PRESSURE MEDICATION", "Yes", "No"},
{"3. STEROIDS MEDICATION", "Yes", "No"},
{"4. AGE [years]"},
{"5. BODY MASS [kg]"},
{"6. BODY HEIGHT [m]"},
{"7. FAMILY MEDICAL HISTORY", "No 1st degree family members with diabetes",
"Parent OR siblings with diabetes mellitus",
"Parent AND siblings with diabetes mellitus"},
{"8. SMOKER", "Non smoker", "Used to smoke", "Smoker"}
};
int column_index[] = {2, 2, 2, 0, 0, 0, 3, 3};
boolean smoker, gender, medication;
float score[6][4] =
{
{-0.879, 0.0},
{1.222, 0.0},
{2.191, 0.0},
{0.0, 0.699, 1.97, 2.518},
{0.0, 0.728, 0.753},
{0.0, -0.218, 0.855}
};
float terms[7];
float mass, height, age, BMI, TC, HDLC, SBP;
for(int i = 0; i < 8; i++) {
for(int j = 0; j < column_index[i] + 1; j++) {
if(j > 0) Serial.print(a_c[j-1]);
Serial.println(questionary[i][j]);
}
while(1) {
user_input();
if(i < 3 && ((commandbuffer_index > 0 && strcmp("a", commandbuffer) == 0) ||
(commandbuffer_index > 0 && strcmp("b", commandbuffer) == 0))) {
if(strcmp("a", commandbuffer) == 0) {
if(i == 0) gender = false;
if(i == 1) medication = true;
terms[i] = score[i][0];
break;
}
if(strcmp ("b", commandbuffer) == 0) {
if(i == 0) gender = true;
if(i == 1) medication = false;
terms[i] = score[i][1];
break;
}
}
if(i == 3 && user_input_to_int() > 0 && user_input_to_int() < 121) {
Serial.print("Age: ");
Serial.print(user_input_to_int());
Serial.println(" year(s)");
terms[i] = user_input_to_float();
age = user_input_to_float();
clear_buffer();
break;
}
if(i == 4 && user_input_to_float() > 5.0 && user_input_to_float() < 250.0) {
Serial.print("Body mass: ");
Serial.print(user_input_to_float());
Serial.println(" kg");
mass = user_input_to_float();
clear_buffer();
break;
}
if(i == 5 && user_input_to_float() > 0.4 && user_input_to_float() < 2.5) {
Serial.print("Body height: ");
Serial.print(user_input_to_float());
Serial.println(" m");
height = user_input_to_float();
BMI = mass / pow(height, 2.0);
Serial.print("BMI: ");
Serial.print(BMI);
Serial.println(" kg/m^2");
if(BMI < 25.0) terms[i-1] = score[i-2][0];
if(BMI >= 25.0 && BMI < 27.5) terms[i-1] = score[i - 2][1];
if(BMI >= 27.5 && BMI < 30) terms[i-1] = score[i - 2][2];
if(BMI >= 30.0) terms[i-1] = score[i-2][3];
clear_buffer();
break;
}
if(i > 5 && ((commandbuffer_index > 0 && strcmp("a", commandbuffer) == 0) ||
(commandbuffer_index > 0 && strcmp("b", commandbuffer) == 0) ||
(commandbuffer_index > 0 && strcmp("c", commandbuffer) == 0))) {
if(strcmp("a", commandbuffer) == 0) {
if(i == 7) smoker = false;
terms[i-1] = score[i-2][0];
break;
}
if(strcmp("b", commandbuffer) == 0) {
if(i == 7) smoker = true;
terms[i-1] = score[i-2][1];
break;
}
if(strcmp("c", commandbuffer) == 0) {
if(i == 7) smoker = true;
terms[i-1] = score[i-2][2];
break;
}
}
}
Serial.println("");
}
// compute diabetes type 2 risk
float exponent = 6.322-terms[0]-terms[1]-terms[2]-0.063*terms[3]-terms[4]-terms[5]-terms[6];
float risk = 100.0/(1+exp(exponent));
Serial.print("RISK TO SUFFER FROM TYPE 2 DIABETES: ");
Serial.print(risk, 3);
Serial.println(" %");
Serial.println("");
// compute total cholesterol
TC = 28.07+6.49*BMI;
if(TC < 130.0) TC = 130.0; // set limits
if(TC > 320.0) TC = 320.0;
// compute HDL cholesterol
HDLC = 111.79-2.35*BMI;
if(HDLC < 20.0) HDLC = 20.0; // set limits
if(HDLC > 60.0) HDLC = 60.0;
char* cholesterol_text[] ={"ESTIMATED ", "TOTAL ", "HDL ", "CHOLESTEROL: ", " mg/dl"};
Serial.print(cholesterol_text[0]);
Serial.print(cholesterol_text[1]);
Serial.print(cholesterol_text[3]);
Serial.print(TC);
Serial.println(cholesterol_text[4]);
Serial.print(cholesterol_text[0]);
Serial.print(cholesterol_text[2]);
Serial.print(cholesterol_text[3]);
Serial.print(HDLC);
Serial.println(cholesterol_text[4]);
// compute systolic blood pressure
SBP = 68.15+0.58*BMI+0.65*age+0.94*float(gender)+6.44*float(medication);
if(SBP < 90.0) SBP = 90.0; // set limits
if(SBP > 200.0) SBP = 200.0;
Serial.print(cholesterol_text[0]);
Serial.print("SYSTOLIC BLOOD PRESSURE: ");
Serial.print(SBP);
Serial.println(" mm Hg");
// Framingham risk score only valid for age between 20 and 79
if(int(age) >= 20 && int(age) <= 79) {
Serial.println("");
int CHD_score;
// look-up tables obtained from http://en.wikipedia.org/wiki/Framingham_Risk_Score
int score_age_women_men[10][4] =
{
{20, 34, -7, -9},
{35, 39, -3, -4},
{40, 44, 0, 0},
{45, 49, 3, 3},
{50, 54, 6, 6},
{55, 59, 8, 8},
{60, 64, 10, 10},
{65, 69, 12, 11},
{70, 74, 14, 12},
{75, 79, 16, 13}
};
int tot_cholesterol_women_men[5][12] =
{
{130, 159,  0, 0, 0, 0, 0,  0, 0, 0, 0, 0},
{160, 199,  4, 3, 2, 1, 1,  4, 3, 2, 1, 0},
{200, 239,  8, 6, 4, 2, 1,  7, 5, 3, 1, 0},
{240, 279,  11, 8, 5, 3, 2,  9, 6, 4, 2, 1},
{280, 320,  13, 10, 7, 4, 2,  11, 8, 5, 3, 1}
};
int smoker_women_men[5][2] =
{
{9, 8},
{7, 5},
{4, 3},
{2, 1},
{1, 1}
};
int HDL_cholesterol_women_men[4][3] =
{
{60, 100, -1},
{50, 59, 0},
{40, 49, 1},
{20, 39, 2}
};
int blood_pressure_women_men[5][6] =
{
{90, 119,  0, 0,  0, 0},
{120, 129,  1, 3,  0, 1},
{130, 139,  2, 4,  1, 2},
{140, 159,  3, 5,  1, 2},
{160, 200,  4, 6,  2, 3}
};
int CHD_risk_score[14][6] =
{
{-10, 8,  -10, 0,  1, 1},
{9, 12,  1, 4,  1, 1},
{13, 14,  5, 6,  2, 2},
{15, 15,  7, 7,  3, 3},
{16, 16,  8, 8,  4, 4},
{17, 17,  9, 9,  5, 5},
{18, 18,  10, 10,  6, 6},
{19, 19,  11, 11,  8, 8},
{20, 20,  12, 12,  11, 10},
{21, 21,  13, 13,  14, 12},
{22, 22,  14, 14,  17, 16},
{23, 23,  15, 15,  22, 20},
{24, 24,  16, 16,  27, 25},
{25, 46,  17, 46,  30, 30}
};
// compute age score
for(int i = 0; i < 10; i++) {
if(gender == false && int(age) >= score_age_women_men[i][0] && int(age) <= score_age_women_men[i][1]) {
CHD_score = score_age_women_men[i][2];
break;
}
if(gender == true && int(age) >= score_age_women_men[i][0] && int(age) <= score_age_women_men[i][1]) {
CHD_score = score_age_women_men[i][3];
break;
}
}
// get age index for cholesterol and smoker score
int j;
for(j = 0; j < 9; j += 2) {
if(int(age) >= score_age_women_men[j][0] && int(age) <= score_age_women_men[j+1][1]) {
j = j/2;
break;
}
}
// compute cholesterol and smoker score
for(int i = 0; i < 5; i++) {
if(int(TC) >= tot_cholesterol_women_men[i][0] && int(TC) <= tot_cholesterol_women_men[i][1]) {
if(gender == false) {
CHD_score += tot_cholesterol_women_men[i][j+2];
if(smoker == true) CHD_score += smoker_women_men[j][0];
}
if(gender == true) {
CHD_score += tot_cholesterol_women_men[i][j+7];
if(smoker == true) CHD_score += smoker_women_men[j][1];
}
break;
}
}
// compute HDL cholesterol score
for(int i = 0; i < 4; i++) {
if(int(HDLC) >= HDL_cholesterol_women_men[i][0] && int(HDLC) <= HDL_cholesterol_women_men[i][1]) {
CHD_score += HDL_cholesterol_women_men[i][2];
break;
}
}
// compute blood pressure score
for(int i = 0; i < 5; i++) {
if(int(SBP) >= blood_pressure_women_men[i][0] && int(SBP) <= blood_pressure_women_men[i][1]) {
if(gender == false) {
if(medication == false) CHD_score += blood_pressure_women_men[i][2];
if(medication == true) CHD_score += blood_pressure_women_men[i][3];
}
if(gender == true) {
if(medication == false) CHD_score += blood_pressure_women_men[i][4];
if(medication == true) CHD_score += blood_pressure_women_men[i][5];
}
}
}
// compute 10 year CHD risk
for(int i = 0; i < 14; i++) {
if(gender == false) {
if(CHD_score >= CHD_risk_score[i][0] && CHD_score <= CHD_risk_score[i][1]) {
if(i == 0) Serial.print("< ");
if(i == 13) Serial.print("> ");
Serial.print(CHD_risk_score[i][4]);
break;
}
}
if(gender == true) {
if(CHD_score >= CHD_risk_score[i][2] && CHD_score <= CHD_risk_score[i][3]) {
if(i == 0) Serial.print("< ");
if(i == 13) Serial.print("> ");
Serial.print(CHD_risk_score[i][5]);
break;
}
}
}
Serial.println(" of 100 PEOPLE WITH THIS LEVEL OF RISK WILL HAVE A HEART ATTACK IN THE NEXT 10 YEARS");
Serial.println("");
}
}

void user_input() {
commandbuffer_index = 0;
if(Serial.available()){
delay(100);
while(Serial.available() && commandbuffer_index < 9) {
}
commandbuffer[commandbuffer_index++] = '\0';
}
if(commandbuffer_index > 0) {
Serial.println((char*)commandbuffer);
}
}
int user_input_to_int() {
return atoi(commandbuffer);
}
float user_input_to_float() {
return atof(commandbuffer);
}
void clear_buffer() {
memset(commandbuffer, 0, strlen(commandbuffer));
}