Docsity

Predicting Cardiovascular Disease Risks with Neural Networks, Summaries of Computer Science

Toggle sidebar
bg1
Neural Networks
Table of Contents
Objective: Predicting Cardiovascular Diseases Risks using Neural
Network
Cardiovascular diseases is one the leading cause of death in in the United States and other
developed countries. The early prognosis of cardiovascular diseases can aid in making decisions
on lifestyle changes in high risk patients and in turn reduce the complications. Framingham
study is an dataset with the follow-up 10-year risk of future coronary heart disease (CHD). It
includes over 4,000 records and 15 attributes of patients’ information.
The objective of this study is to familiar with simple Neural Network machine learning method
to predict the risk of cardiovascular diseases.
The neural network is constructed with an interconnected group of nodes, which involves the
input, connected weights, processing element, and output. No underlying assumptions are
required to create and evaluate the model and it can be used with qualitative and quantitative
responses.
Data Specification
• Age: Age in years
• Sex: Sex (1 = male; 0 = female)
• Chest_Pain_Type: chest pain type
– Value 1: typical angina – Value 2: atypical angina – Value 3: non-anginal pain – Value 4:
asymptomatic
• Exercise_Induced_Angina: (1 = Yes; 2 = No)
• BP: Resting Blood Pressure (in mm Hg on admission to the hospital)
• thal: Maximum Heart Rate achieved
• Cholesterol: Serum Cholesterol in mg/dl
• Fasting_Blood_MoreThan_120: Yes or No
• Resting_Electrocardiographic_Reading
• Maximum_Heart_Rate
• Old_Peak: ST depression induced by exercise relative to rest
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Predicting Cardiovascular Disease Risks with Neural Networks and more Summaries Computer Science in PDF only on Docsity!

Neural Networks

Table of Contents

Objective: Predicting Cardiovascular Diseases Risks using Neural

Network

Cardiovascular diseases is one the leading cause of death in in the United States and other

developed countries. The early prognosis of cardiovascular diseases can aid in making decisions

on lifestyle changes in high risk patients and in turn reduce the complications. Framingham

study is an dataset with the follow-up 10-year risk of future coronary heart disease (CHD). It

includes over 4,000 records and 15 attributes of patients’ information.

The objective of this study is to familiar with simple Neural Network machine learning method

to predict the risk of cardiovascular diseases.

The neural network is constructed with an interconnected group of nodes, which involves the

input, connected weights, processing element, and output. No underlying assumptions are

required to create and evaluate the model and it can be used with qualitative and quantitative

responses.

Data Specification

  • Age: Age in years
  • Sex: Sex (1 = male; 0 = female)
  • Chest_Pain_Type: chest pain type
  • Value 1: typical angina – Value 2: atypical angina – Value 3: non-anginal pain – Value 4:

asymptomatic

  • Exercise_Induced_Angina: (1 = Yes; 2 = No)
  • BP: Resting Blood Pressure (in mm Hg on admission to the hospital)
  • thal: Maximum Heart Rate achieved
  • Cholesterol: Serum Cholesterol in mg/dl
  • Fasting_Blood_MoreThan_120: Yes or No
  • Resting_Electrocardiographic_Reading
  • Maximum_Heart_Rate
  • Old_Peak: ST depression induced by exercise relative to rest
  • Number_Blood_Vessels_Calcified (1,2,3,4)
  • Risk: (1 = Yes; 2 = No) knitr::opts_chunk$set(message = FALSE,warning = FALSE) STEP 1: Load the Libraries library(ISLR) library(readxl) library(pROC) library(lattice) library(ggplot2) library(dplyr) library(e1071) library(corrplot) library(kknn) library(ggplot2) library(multiROC) library(MLeval) library(AppliedPredictiveModeling) library(corrplot) library(Hmisc) library(dplyr) library(quantmod) library(nnet) library(caret) library(NeuralNetTools) STEP 2: Load the Data and prepare it. CHDdata<-read.csv("CHD_preprocessed.csv") str(CHDdata)

'data.frame': 4133 obs. of 16 variables:

$ male : int 1 0 1 0 0 0 0 0 1 1 ...

$ age : int 39 46 48 61 46 43 63 45 52 43 ...

$ education : int 1 0 0 1 1 0 0 0 0 0 ...

$ currentSmoker : int 0 0 1 1 1 0 0 1 0 1 ...

$ cigsPerDay : num 0 0 20 30 23 0 0 20 0 30 ...

$ BPMeds : num 0 0 0 0 0 0 0 0 0 0 ...

$ prevalentStroke: int 0 0 0 0 0 0 0 0 0 0 ...

$ prevalentHyp : int 0 0 0 1 0 1 0 0 1 1 ...

$ diabetes : int 0 0 0 0 0 0 0 0 0 0 ...

$ totChol : num 195 250 245 225 285 228 205 313 260

225 ...

$ sysBP : num 106 121 128 150 130 ...

$ diaBP : num 70 81 80 95 84 110 71 71 89 107 ...

$ BMI : num 27 28.7 25.3 28.6 23. ...

$ heartRate : num 80 95 75 65 85 77 60 79 76 93 ...

sysBP diaBP BMI heartRate

glucose

0 0 0 0

0

TenYearCHD

0

  • Convert Categorical variables CHDdata$male <- as.character(CHDdata$male) CHDdata$education <- as.character(CHDdata$education) CHDdata$currentSmoker <- as.character(CHDdata$currentSmoker) CHDdata$BPMeds <- as.character(CHDdata$BPMeds) CHDdata$prevalentStroke <- as.character(CHDdata$prevalentStroke) CHDdata$prevalentHyp <- as.character(CHDdata$prevalentHyp) CHDdata$diabetes <- as.character(CHDdata$diabetes) CHDdata$TenYearCHD <- as.character(CHDdata$TenYearCHD) Implement Neural Network Algorithm
  • Using Caret package to train a model using neural network # Splitted the data set.seed( 212 ) start <- proc.time()[ 3 ] trainIndex <- createDataPartition(CHDdata$TenYearCHD, p = 0.8, list=FALSE, times= 1 ) subTrain <- CHDdata[trainIndex,] subTest <- CHDdata[-trainIndex,] # set up cross validation TrainingParameters <- trainControl(method = "repeatedcv", number = 10 , repeats= 10 ) # CARET 'train` function nn_model <- train(TenYearCHD ~ ., subTrain, method = "nnet", # nnnet package by default uses the Logistic trControl= TrainingParameters, preProcess=c("scale","center","BoxCox"), na.action = na.omit, trace=FALSE, verbose=FALSE) print(nn_model)

Neural Network

3307 samples

15 predictor

2 classes: '0', '1'

Pre-processing: scaled (15), centered (15), Box-Cox transformation

(7)

Resampling: Cross-Validated (10 fold, repeated 10 times)

Summary of sample sizes: 2976, 2976, 2977, 2976, 2977, 2977, ...

Resampling results across tuning parameters:

size decay Accuracy Kappa

1 0e+00 0.8478686 0.

1 1e-04 0.8481404 0.

1 1e-01 0.8484428 0.

3 0e+00 0.8442707 0.

3 1e-04 0.8435762 0.

3 1e-01 0.8463266 0.

5 0e+00 0.8368913 0.

5 1e-04 0.8383449 0.

5 1e-01 0.8433646 0.

Accuracy was used to select the optimal model using the largest

value.

The final values used for the model were size = 1 and decay = 0.1.

Size: Number of Hidden Layers.

Decay: Is the regularization factor that offsets overfitting.

Kappa: Evaluates the match is significant or by chance.

  • Prediction in the test dataset from the optimal results prediction <- predict(nn_model, subTest[,- 16 ]) # predict table(prediction, subTest$TenYearCHD)

prediction 0 1

0 697 119

1 4 6

typeof(subTest$TenYearCHD)

[1] "character"

subTest$TenYearCHD<-as.integer(subTest$TenYearCHD) typeof(subTest$TenYearCHD)

[1] "integer"

typeof(prediction)

[1] "integer"

end <- proc.time()[ 3 ] print(paste("This took ", round(end-start, digits = 1 ), " seconds", sep = ""))

[1] "This took 580 seconds"

Using the full data, this took quite a long time to finish. The accuracy is 85% for the test data

Improve Neural Network Performance

  • Ranking Feature by Importance set.seed( 1414 ) varImp_nn<-varImp(nn_model) varImp_nn

nnet variable importance

Overall

age 100.

sysBP 58.

cigsPerDay 47.

male1 35.

diabetes1 33.

prevalentStroke1 17.

prevalentHyp1 17.

totChol 14.

BPMeds1 9.

diaBP 5.

currentSmoker1 3.

BMI 2.

heartRate 1.

glucose 0.

education1 0.

ggplot(varImp_nn)

  • The top 10 conventional variables # keeping only 10 predictors CHDdata_modified <- CHDdata %>% dplyr:: select(- c("currentSmoker","education","BMI", "glucose","heartRate")) trainIndex <- createDataPartition(CHDdata$TenYearCHD, p = 0.8, list=FALSE, times= 1 ) subTrain <- CHDdata_modified[trainIndex,] subTest <- CHDdata_modified[-trainIndex,] set.seed( 123 ) start <-proc.time()[ 3 ] nn_model <- train(TenYearCHD ~ ., subTrain, method='nnet', trace = FALSE) print(nn_model)

Neural Network

3307 samples

10 predictor

2 classes: '0', '1'

plotnet(nn_model$finalModel, y_names = "CHD Risk") title("Graphical Representation of our Neural Network")

The

width of the connecting lines represent the relative weights between nodes and the colors

represent direction, black for positive and grey for negative. B1 and B2 represent bias nodes

that function similarly to the constant term in your standard OLS regression. We see here

totChol had the highest positive weight on the second node, while the bias term for the hidden

layer has a large positive weight with the 2nd node in the hidden layer and a large negative

weight with the 1st node in the hidden layer

Creating and Comparing Algorithms control <- trainControl(method="repeatedcv", number= 10 , repeats= 3 ) # NNET set.seed( 100 ) fit.nnet <- train(TenYearCHD~., data=subTrain, method="nnet", trControl=control,trace=FALSE) # LDA set.seed( 100 ) fit.lda <- train(TenYearCHD~., data=subTrain, method="lda", trControl=control) # SVM set.seed( 100 ) fit.svm <- train(TenYearCHD~., data=subTrain, method="svmRadial", trControl=control) # kNN set.seed( 100 ) fit.knn <- train(TenYearCHD~., data=subTrain, method="knn", trControl=control) # Random Forest set.seed( 100 ) fit.rf <- train(TenYearCHD~., data=subTrain, method="rf", trControl=control)

algo_results <- resamples(list(NNET=fit.nnet, LDA=fit.lda, SVM=fit.svm, KNN=fit.knn, RF=fit.rf)) summary(algo_results)

Call:

summary.resamples(object = algo_results)

Models: NNET, LDA, SVM, KNN, RF

Number of resamples: 30

Accuracy

Min. 1st Qu. Median Mean 3rd Qu. Max.

NA's

NNET 0.8363636 0.8463855 0.8502289 0.8501147 0.8545455 0.

0

LDA 0.8277946 0.8399997 0.8469697 0.8462882 0.8549849 0.

0

SVM 0.8393939 0.8489426 0.8515152 0.8513247 0.8540115 0.

0

KNN 0.8181818 0.8333333 0.8429003 0.8411467 0.8488282 0.

0

RF 0.8393939 0.8465623 0.8493976 0.8501168 0.8548750 0.

0

Kappa

Min. 1st Qu. Median Mean 3rd Qu.

Max. NA's

NNET -0.01745201 0.03618218 0.08340083 0.07866070 0.

0.1724100 0

LDA 0.01173328 0.07279551 0.11241200 0.11183442 0.

0.2332561 0

SVM -0.01745201 0.03347280 0.05933682 0.06149411 0.

0.1478812 0

KNN -0.01351494 0.02044659 0.04291801 0.05627364 0.

0.1723010 0

RF -0.01179245 0.02852867 0.05877237 0.06138990 0.

0.1510928 0 scales <- list(x=list(relation="free"), y=list(relation="free")) bwplot(algo_results, scales=scales)

Call:

summary.diff.resamples(object = diffs)

p-value adjustment: bonferroni

Upper diagonal: estimates of the difference

Lower diagonal: p-value for H0: difference = 0

Accuracy

NNET LDA SVM KNN RF

NNET 3.827e-03 -1.210e-03 8.968e-03 -2.131e-

LDA 0.0866384 -5.036e-03 5.141e-03 -3.829e-

SVM 1.0000000 0.0324997 1.018e-02 1.208e-

KNN 0.0002942 0.0915369 4.384e-05 -8.970e-

RF 1.0000000 0.4197377 1.0000000 0.

Kappa

NNET LDA SVM KNN RF

NNET -0.0331737 0.0171666 0.0223871 0.

LDA 0.0200328 0.0503403 0.0555608 0.

SVM 1.0000000 0.0001020 0.0052205 0.

KNN 0.3851641 0.0004658 1.0000000 -0.

RF 1.0000000 0.0002133 1.0000000 1.

Conclusion:

  • Here we used caret package illustrated how the neural network model performed in the

classification analysis. Figure of Graphical Representation of our Neural Network

presented the three major Architecture: input layer, Hidden layer and output layer

  • In the CHDdata set, we improve the performance via reducing the variables to top 10

conventional features, the accuracy is the same 85% but 10 times faster.

  • Among 5 different algorithms, the best performance accuracy is SVM model(0.8513),

and the next is neural network model(0.8501)