-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine_Learning_Tidymodel_Linear.Rmd
More file actions
294 lines (169 loc) · 3.69 KB
/
Machine_Learning_Tidymodel_Linear.Rmd
File metadata and controls
294 lines (169 loc) · 3.69 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
---
title: "Machine_Learning_Tidymodel_Linear"
author: "Bernard Asante"
date: "2025-02-16"
output:
html_document:
toc: true
toc_float: true
toc_depth: 3
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidymodels)
library(themis)
library(parsnip)
library(gtsummary)
library(psych)
```
```{r}
data <- read_csv("Heart Attack.csv")
data %>%
glimpse()
```
```{r}
data <- data %>%
rename(pulse = impluse, sys = pressurehight, dias = pressurelow) %>%
mutate(glu_mmol = glucose/18)
data %>%
glimpse()
```
## Recoding variables
```{r}
data <- data %>%
mutate(gender = case_when(
gender == 1 ~ "Male",
gender == 0 ~ "Female"
)) %>%
mutate(gender = as.factor(gender)) %>%
mutate(class = as.factor(class))
data %>%
glimpse()
```
## Exploring missing data
```{r}
missing_data <- data %>%
summarise_all(~sum(is.na(.)))
missing_data
```
# **Predictor Analysis**
## EDA of the predictor
```{r}
ggplot(data, aes (troponin ))+
geom_histogram()
```
```{r}
univ_table <- data %>%
tbl_uvregression(
method = lm,
y = troponin
)
univ_table %>% as_kable()
```
## Correlation
### Correlation_matrix
```{r}
library(reshape2)
corr_matrix <- data %>%
select(age, pulse, glu_mmol,kcm,troponin) %>%
cor()
corr_matrix
melted_matrix <- melt(corr_matrix)
melted_matrix
```
```{r}
ggplot(melted_matrix, aes(x = Var1, y = Var2, fill = value))+
geom_tile()+
scale_fill_gradient2(low = "red", mid = "white", high = "blue", midpoint = 0)
```
```{r}
pairs.panels( data,
scale = FALSE,
method = "spearman",
lm = TRUE,
cor = TRUE,
jiggle = FALSE,
stars = FALSE)
```
# **Machine Leaning**
## Selecting variable
```{r}
lin_data <- data %>%
select(age,gender, pulse,sys, dias,kcm,glu_mmol,troponin, class)
lin_data %>%
glimpse()
lin_data %>%
summary()
```
## Splitting Data
```{r}
set.seed(123)
data_split <- initial_split(lin_data, prop = 0.70)
train_data <- training(data_split)
test_data <- testing(data_split)
```
## Building Model
```{r}
lin_ridge <- linear_reg(penalty= tune(), mixture = tune()) %>%
set_engine("glmnet")
```
## Building Recipe
```{r}
lin_recipe <- recipe(troponin ~ ., data = train_data) %>%
step_normalize(all_numeric_predictors()) %>%
step_dummy(all_nominal_predictors()) %>%
step_zv(all_predictors())
```
## Building workflow
```{r}
lin_workflow <- workflow() %>%
add_model(lin_ridge) %>%
add_recipe(lin_recipe)
```
## Hyperparameter Tunning
```{r}
fold = vfold_cv(train_data, v=5)
grid <- grid_regular(
penalty(range = c(0.0001, 1)),
mixture(range = c(0, 1)), # Mixture is between 0 (ridge) and 1 (lasso)
levels = c(5, 5) # Adjust the number of grid points
)
lin_tune_wrkflow <- tune_grid(lin_workflow,
resamples = fold,
grid = grid
)
lin_tune_wrkflow
```
## Selecting and Finalizing the best model
```{r}
best_params <- select_best(lin_tune_wrkflow, metric = "rmse") # Change to "accuracy" for classification
final_wrkflow <- finalize_workflow(lin_workflow, best_params)
```
## Training the model
```{r}
final_wrkflow_fit <- final_wrkflow %>%
fit(train_data)
```
```{r}
final_wrkflow_fit %>%
extract_fit_parsnip() %>%
tidy()
```
## Predictions
```{r}
predict <- predict(final_wrkflow_fit, new_data = test_data)
predict %>%
head()
```
## Model Evaluation
```{r}
model_eval <- bind_cols(test_data, predict)
model_eval %>%
glimpse()
```
## Accuracy
```{r}
rmse <- rmse(model_eval, truth = troponin, estimate = .pred)
rmse
```