-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCKD_Classification.R
215 lines (165 loc) · 7.13 KB
/
CKD_Classification.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Read original dataset
originalData <- read.csv("Data/chronic_kidney_disease_full.csv", header = TRUE)
colnames(originalData)
head(originalData, n = 5)
# Replace extraneous characters, whitespace and blankspace with NA values
replacedData <- read.csv("Data/chronic_kidney_disease_full.csv",
header = TRUE, na.strings = c("", " ", "?"))
head(replacedData, n = 1)
any(is.na(replacedData))
# Omit observations with NA entries
cleanedData <- na.omit(replacedData)
any(is.na(cleanedData))
str(cleanedData)
# Correct the variable types
formattedData <- cleanedData
formattedData$id <- as.integer(formattedData$id)
formattedData$X.sg. <- as.factor(formattedData$X.sg.)
formattedData$X.al. <- as.factor(formattedData$X.al.)
formattedData$X.su. <- as.factor(formattedData$X.su.)
formattedData$X.rbc. <- as.factor(formattedData$X.rbc.)
formattedData$X.pc. <- as.factor(formattedData$X.pc.)
formattedData$X.pcc. <- as.factor(formattedData$X.pcc.)
formattedData$X.ba. <- as.factor(formattedData$X.ba.)
formattedData$X.htn. <- as.factor(formattedData$X.htn.)
formattedData$X.dm. <- as.factor(formattedData$X.dm.)
formattedData$X.cad. <- as.factor(formattedData$X.cad.)
formattedData$X.appet. <- as.factor(formattedData$X.appet.)
formattedData$X.pe. <- as.factor(formattedData$X.pe.)
formattedData$X.ane. <- as.factor(formattedData$X.ane.)
formattedData$X.class. <- as.factor(formattedData$X.class.)
str(formattedData)
summary(formattedData)
# Extract continuous numerical variables
correlationMatrix <- data.frame(formattedData$X.age., formattedData$X.bp.,
formattedData$X.bgr., formattedData$X.bu., formattedData$X.sc.,
formattedData$X.sod., formattedData$X.pot., formattedData$X.hemo.,
formattedData$X.pcv., formattedData$X.wbcc., formattedData$X.rbcc.)
# Abbreviate names
colnames(correlationMatrix) <- c("X.age.", "X.bp.", "X.bgr.", "X.bu.", "X.sc.",
"X.sod.", "X.pot.", "X.hemo.", "X.pcv.", "X.wbcc.", "X.rbcc")
# Correlation plot
plot(correlationMatrix, main = "Scatterplot of Correlation Matrix")
# Compute Pearson correlation coefficients and round r-values
round(cor(correlationMatrix, method = "pearson"), digits = 2)
abs(cor(correlationMatrix, method = "pearson")) > 0.7
table(formattedData$X.class.)
# Generate a row-randomized dataset
set.seed(42)
randomizedData <- formattedData[sample(nrow(formattedData)), ]
# Construct testing dataset
testingData <- randomizedData # Dataset is constructed by Complement Rule
# Construct training dataset
trainingData <- testingData[-c(1:157), ] # Copy column names & preserve type
for (i in 1:length(testingData$id)) # Extract first 22 CKD observations
{
if ((testingData[i, ]$X.class. == "ckd") &
(sum(trainingData$X.class. == "ckd") < 22))
{
trainingData[nrow(trainingData) + 1, ] <- testingData[i, ]
testingData <- testingData[-c(i), ]
}
}
for (i in 1:length(testingData$id)) # Extract first 57 non-CKD observations
{
if ((testingData[i, ]$X.class. == "notckd") &
(sum(trainingData$X.class. == "notckd") < 57))
{
trainingData[nrow(trainingData) + 1, ] <- testingData[i, ]
testingData <- testingData[-c(i), ]
}
}
rm(i) # Clears the counter variable from the environment
# Reorder datasets
trainingData <- trainingData[order(trainingData$id), ]
testingData <- testingData[order(testingData$id), ]
# Generate the Null Model
nullModel <- glm(X.class. ~ 1, data = trainingData, family = "binomial")
# Generate the Full Model
fullModel <- glm(X.class. ~ X.age. + X.bp. + X.bgr. + X.bu. + X.sod. + X.pot. +
X.hemo. + X.pcv. + X.wbcc. + X.rbcc., data = trainingData,
family = "binomial")
# Run Forward Selection algorithm
forwardModel <- step(nullModel, direction = "forward",
scope = list(upper = fullModel, lower = ~1), trace = 0)
summary(forwardModel)
# Run the Backward Elimination algorithm
backwardModel <- step(fullModel, direction = "backward", trace = 0)
summary(backwardModel)
# Run Sequential Selection algorithm
sequentialModel <- step(nullModel, direction = "both",
scope = formula(fullModel), trace = 0)
summary(sequentialModel)
# Load the glmnet package
library(glmnet)
# k-fold cross-validation
lassoY <- trainingData$X.class.
lassoX <- data.matrix(trainingData[, colnames(trainingData)[2:25]])
lassoCVModel <- cv.glmnet(lassoX, lassoY, alpha = 1, family = "binomial")
lassoBestLambda <- lassoCVModel$lambda.min
lassoBestLambda
# Run LASSO Regression algorithm
lassoModel <- glmnet(lassoX, lassoY, alpha = 1, lambda = lassoBestLambda,
family = "binomial")
coef(lassoModel)
# k-fold cross-validation
ridgeY <- trainingData$X.class.
ridgeX <- data.matrix(trainingData[, colnames(trainingData)[2:25]])
ridgeCVModel <- cv.glmnet(ridgeX, ridgeY, alpha = 0, family = "binomial")
ridgeBestLambda <- ridgeCVModel$lambda.min
ridgeBestLambda
# Run Ridge Regression algorithm
ridgeModel <- glmnet(ridgeX, ridgeY, alpha = 0, lambda = ridgeBestLambda,
family = "binomial")
coef(ridgeModel)
table(formattedData$X.class.)
# Load car package
library(car)
# Multicollinearity Detection
vif(forwardModel)
vif(backwardModel)
# Cook's Distance Plot Backward Model
plot(cooks.distance(backwardModel), main = "Cook's Distance Values in the
Backward Model (Cutoff = 1", xlab = "Observation #", ylab = "Di Value")
# Linearity Check for the Backward Model
backwardModelLogOdds <- backwardModel$linear.predictors
cor(trainingData$X.bgr., backwardModelLogOdds)
cor(trainingData$X.bu., backwardModelLogOdds)
cor(trainingData$X.wbcc., backwardModelLogOdds)
plot(trainingData$X.bgr., backwardModelLogOdds,
main = "Backward Model Log Odds vs. Blood Glucose (X.bgr.) Scatterplot",
xlab = "Blood Glucose (mg/dL)", ylab = "Log Odds")
plot(trainingData$X.bu., backwardModelLogOdds,
main = "Backward Model Log Odds vs. Blood Urea (X.bu.) Scatterplot",
xlab = "Blood Urea (mg/dL)", ylab = "Log Odds")
plot(trainingData$X.wbcc., backwardModelLogOdds,
main = "Backward Model Log Odds vs. White Blood Cell Count (X.wbcc.)
Scatterplot", xlab = "White Blood Cell Count (cells/mm^3)",
ylab = "Log Odds")
# Sample Size Check
table(trainingData$X.class.)
(10 * 4) / (22 / 79) # 4 regressors, 22 / 79 CKD-classified observations
# Load the dplyr package
library(dplyr)
# Run the Backward Model on the test dataset
backwardModelProbabilities <- backwardModel %>% predict(testingData,
type = "response")
backwardModelPredictedClasses <- ifelse(backwardModelProbabilities < 0.5,
"ckd", "notckd")
# Testing data matrix
testDataMatrix <- data.matrix(testingData[, 2:25])
# Run the LASSO Model on the test dataset
lassoModelPredictedProbabilities <- predict(lassoModel, s = lassoBestLambda,
newx = testDataMatrix, type = "response")
lassoModelPredictedClasses <- ifelse(lassoModelPredictedProbabilities < 0.5,
"ckd", "notckd")
ridgeModelPredictedProbabilities <- predict(ridgeModel, s = ridgeBestLambda,
newx = testDataMatrix, type = "response")
ridgeModelPredictedClasses <- ifelse(ridgeModelPredictedProbabilities < 0.5,
"ckd", "notckd")
# Load the caret package
library(caret)
confusionMatrix(table(backwardModelPredictedClasses, testingData$X.class.))
confusionMatrix(table(lassoModelPredictedClasses, testingData$X.class.))
confusionMatrix(table(ridgeModelPredictedClasses, testingData$X.class.))
knitr::purl("CKD_Classification_Paper.Rmd", documentation = 0)