411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Diagnosing Heart Disease
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Diagnosing Heart Disease Electrical Circuits SIR Model
Loading Libraries
First, you need to install and use the following libraries:
using CSV
using DataFrames
using GLM
using StatsBase:mean
Part 1: Data
The "heart.csv" dataset, downloadable from Kaggle, contains 1,025 observations, each containing 14 heart-related variables. These variables are as follows:
age: the person's age in years
sex: the person's gender (1 = male, 0 = female)
cp: chest pain experienced (1: typical angina, 2: atypical angina, 3: non-anginal pain, 4: asymptomatic)
trestbps: the person's resting blood pressure (mm/Hg at the time of hospital admission)
chol: cholesterol measurement in mg/dL
fbs: the person's fasting blood glucose (> 120 mg/dL, 1 = true; 0 = false)
restecg: resting electrocardiogram (0 = normal, 1 = with ST-T wave abnormality, 2 = probable or definite left ventricular hypertrophy according to Estes criteria)
thalach: the person's maximum heart rate
exang: exercise-induced angina (1 = yes; 0 = (no)
oldpeak: exercise-induced ST depression ('ST' refers to the positions on the ECG diagram)
slop: ST segment slope (value 1: rising, value 2: flat, value 3: falling)
ca: number of major vessels (0-3)
tal: blood disorder called thalassemia (3 = normal; 6 = fixed defect; 7 = reversible defect)
target: heart disease (0 = no, 1 = yes)
Loading data:
data = CSV.read("heart.csv",DataFrame)
Part 2: Research Question
We want to predict the possible presence of heart disease in a patient by predicting the target variable based on the 13 aforementioned variables.
Part 3: Modeling and Prediction
The dataset is divided into two parts: train_data, consisting of 80% of the observations on which the logistic regression model is trained, and test_data, consisting of the remaining 20% on which the model is tested and its accuracy is calculated:
train_ratio = 0.8
n = nrow(data)
n_train = Int(floor(train_ratio * n));
train_data = data[1:n_train, :]
test_data = data[(n_train + 1):end, :]
The logistic regression model is created on the training set, predicting the target variable based on the remaining 13 variables:
fm = @formula(target ~ age + sex + cp + trestbps + chol + fbs + restecg + thalach + exang + oldpeak + slope + ca + thal)
logit = glm(fm, train_data, Binomial(), ProbitLink())
The prediction vector is constructed with the predictions made by the model on the testing set:
prediction = GLM.predict(logit, test_data)
205-element Vector{Union{Missing, Float64}}:
0.017732763641414958
0.06652842473470792
0.04289852866536204
0.5470639026550284
0.006095278913460305
0.9955768152608755
0.9703221524362567
0.5142923144983738
0.9053830876384388
0.0023961543663923637
0.6965675386130677
0.04394901761879894
0.5745052037713632
⋮
0.005816355358123194
0.9984324380351746
0.0003433323966801158
0.7506524853801526
0.000691630070259472
0.6261769123744736
0.7526861512703369
0.7105401597352763
0.0033310752534602382
0.05287226311321894
0.949991107720036
0.06892902969480652
A vector of 1s and 0s is constructed based on the probabilities contained in the prediction vector. If the probability is greater than 50%, it takes the value 1, otherwise it is 0. Then the accuracy is calculated, which is 80.48%:
prediction_class = [if x < 0.5 0 else 1 end for x in prediction];
accuracy = mean(test_data.target.==prediction_class)
0.8048780487804879
Therefore, logistic regression is a good predictive model for assessing the presence of heart disease.