-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_debug.R
More file actions
152 lines (119 loc) · 4.81 KB
/
model_debug.R
File metadata and controls
152 lines (119 loc) · 4.81 KB
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
#| label: package-install
#| echo: false
library(easypackages)
dep_packages = c("tidyverse",
"ggthemes",
"ggrepel",
"dslabs",
"ggplot2",
"dplyr",
"data.table",
"extrafont",
"dataMaid",
"gt",
"gtsummary",
"gtExtras",
"mlr",
"e1071",
"caret",
"caTools",
"MLmetrics",
"pROC"
)
easypackages::libraries(dep_packages)
mimic_data <- fread("./MIMIC_ICU_Data/mimic_icu_data.csv")
#| label: main-model
# a random number based on keyboard bashing the number row to ensure that our random data set is repeatable
# while we are reporting on it
set.seed(01923904)
# Create a subset of the entire dataset to just include the rows we have determined are of interest
# convert the gender field to an `is_male` flag. Also discretize the heart rate and age
model_data <- mimic_data %>%
select(
age,
gender,
hospital_expire_flag,
heartrate_mean,
sysbp_mean,
resprate_mean,
tempc_mean,
wbc_mean,
platelet_min,
creatinine_max,
lactate_mean
) %>%
mutate(
surviving = factor(case_when(hospital_expire_flag == 0 ~ "YES", TRUE ~ "NO")),
is_male = factor(case_when(gender == "M" ~ 1, TRUE ~ 0)),
age_range = factor(case_when(
age <= 18 ~ "<=18",
age > 18 & age <= 40 ~ "19 to 40",
age > 40 & age <= 60 ~ "41 to 60",
age > 60 ~ ">=61"
)),
heart_rate = factor(case_when(
heartrate_mean < 60 ~ "<60",
heartrate_mean >= 61 & heartrate_mean <= 80 ~ "60 to 80",
heartrate_mean >= 81 & heartrate_mean <= 100 ~ "81 to 100",
heartrate_mean > 100 ~ "above 100"
)),
) %>%
select(-gender, -heartrate_mean, -age, -hospital_expire_flag)
model_data$id <- 1:nrow(model_data)
model_data <- na.omit(model_data)
# use z score standardization on our features to see what we get
scaled_model_data <- model_data %>%
select(-id) %>%
mutate_if(is.numeric, scale) %>%
cbind(id = model_data$id)
# our data are skewed such that the patients tend to survive (hospital expire flag = 0), to resolve this issue we will downsample the surviving pateints and construct a training set based on an 80% selection of all of the hospital_expire_flag = 1, then randomly select and equal number of observations where hosptial expre flag=1
amount_to_sample = floor(sum(scaled_model_data$surviving == "NO") * 0.95)
train <- scaled_model_data %>% group_by(surviving) %>% sample_n(size = amount_to_sample) %>% ungroup()
test <- scaled_model_data %>% anti_join(train, by="id") %>% select(-id)
train <- train %>% select(-id)
## Things to do
## possibliy do some weights
## Down or
## bootstrapping with multiple data sets
# build the model
# nu and gamas will need to be set for tuning, they have a single value for now
# so we have something to show
kernels <- c("radial")
costs <- c(250)
gammas <- c(0.1) #seq(0.05, 1, 0.05)
nus <- c(0.55) #seq(0.05, 1, 0.02)
ranges <- list(cost=costs, gamma = gammas, kernel = kernels, nu=nus)
modelTuning <- tune(svm,
surviving ~ .,
type="nu-classification",
data= train,
probability=TRUE,
ranges = ranges,
scale = TRUE)
model <- modelTuning$best.model
print('---------- Primary Model --------------')
model_predict <- predict(model, test, probability = TRUE)
confusionMatrix(model_predict, test$surviving)
model_probabilities <- attr(model_predict, "probabilities")[,1]
model_predictions <- ROCR::prediction(model_probabilities, test$surviving, label.ordering = c("YES","NO"))
model_perf <- ROCR::performance(model_predictions, "tpr", "fpr")
plot(model_perf, colorize = TRUE)
abline(a=0, b=1)
summary(model)
print('---------- CHALLENGER --------------')
challengerTuning <- tune(svm,
hospital_expire_flag ~ age_range + heart_rate + sysbp_mean + resprate_mean + tempc_mean + platelet_min + is_male,
data= train,
type = "nu-classification",
probability=TRUE,
ranges = ranges,
scale = TRUE)
challenger <- challengerTuning$best.model
summary(challenger)
challenger_predict <- predict(challenger, test, probability = TRUE)
confusionMatrix(challenger_predict, test$hospital_expire_flag)
challenger_probabilities <- attr(challenger_predict, "probabilities")[, 1]
challenger_predictions <- ROCR::prediction(challenger_probabilities, test$hospital_expire_flag, label.ordering = c(1,0))
challenger_perf <- ROCR::performance(challenger_predictions, "tpr", "fpr")
plot(challenger_perf, colorize = TRUE)
abline(a=0,b=1)