#1.Data Exploration:
dataSheet=read.csv(file.choose(),header = TRUE)
View(dataSheet)
str(dataSheet)
## 'data.frame': 299 obs. of 11 variables:
## $ age : num 75 55 65 50 65 90 75 60 65 80 ...
## $ anaemia : int 0 0 0 1 1 1 1 1 0 1 ...
## $ creatinine_phosphokinase: int 582 7861 146 111 160 47 246 315 157 123 ...
## $ diabetes : int 0 0 0 0 1 0 0 1 0 0 ...
## $ ejection_fraction : int 20 38 20 20 20 40 15 60 65 35 ...
## $ high_blood_pressure : int 1 0 0 0 0 1 0 0 0 1 ...
## $ platelets : num 265000 263358 162000 210000 327000 ...
## $ serum_creatinine : num 1.9 1.1 1.3 1.9 2.7 2.1 1.2 1.1 1.5 9.4 ...
## $ serum_sodium : int 130 136 129 137 116 132 137 131 138 133 ...
## $ sex : int 1 1 1 1 0 1 1 1 0 1 ...
## $ smoking : int 0 0 1 0 0 1 0 1 0 1 ...
# Scatter plot 1 for Platelets and Serum Sodium
plot(dataSheet$platelets, dataSheet$serum_sodium, main = "Platelets and Serum Sodium",
xlab = "Platelets", ylab = "Serum Sodium", col = "purple",pch = 20)
# Scatter plot 2 for Creatinine Phosphokinase and Serum Sodium
plot(dataSheet$creatinine_phosphokinase, dataSheet$serum_sodium, main = "Creatinine Phosphokinase and Serum Sodium",
xlab = "Creatinine Phosphokinase", ylab = "Serum Sodium", col = "red", pch = 20)
# Scatter plot 3 for Ejection Fraction and Serum Sodium
plot(dataSheet$ejection_fraction, dataSheet$serum_sodium, main = "Ejection Fraction and Serum Sodium",
xlab = "Ejection Fraction", ylab = "Serum Sodium", col = "blue", pch = 20)
# Scatter plot 4 for Serum Creatinine and Serum Sodium
plot(dataSheet$serum_creatinine, dataSheet$serum_sodium, main = "Serum Creatinine and Serum Sodium",
xlab = "Serum Creatinine", ylab = "Serum Sodium", col = "green", pch = 20)
#2.Data Pre processing:
#searching for missing values
missing_values <- is.na(dataSheet)#checking in whole dataset
column_missing <- colSums(is.na(dataSheet))#check in each column
column_missing
## age anaemia creatinine_phosphokinase
## 0 0 0
## diabetes ejection_fraction high_blood_pressure
## 0 0 0
## platelets serum_creatinine serum_sodium
## 0 0 0
## sex smoking
## 0 0
summary(missing_values)
## age anaemia creatinine_phosphokinase diabetes
## Mode :logical Mode :logical Mode :logical Mode :logical
## FALSE:299 FALSE:299 FALSE:299 FALSE:299
## ejection_fraction high_blood_pressure platelets serum_creatinine
## Mode :logical Mode :logical Mode :logical Mode :logical
## FALSE:299 FALSE:299 FALSE:299 FALSE:299
## serum_sodium sex smoking
## Mode :logical Mode :logical Mode :logical
## FALSE:299 FALSE:299 FALSE:299
# Setting seed
set.seed(123)
#Splitting data set (80% trainng model)
train_indices <- sample(1:nrow(dataSheet), 0.8 * nrow(dataSheet))
#training set
train_dataset <- dataSheet[train_indices, ]
# testing set
test_dataset <- dataSheet[-train_indices, ]
# printing
print(train_dataset)
## age anaemia creatinine_phosphokinase diabetes ejection_fraction
## 179 63.000 1 122 1 60
## 14 50.000 1 168 0 38
## 195 45.000 0 582 0 20
## 118 85.000 1 102 0 60
## 229 65.000 0 56 0 25
## 244 73.000 1 1185 0 40
## 298 45.000 0 2413 0 38
## 153 50.000 0 115 0 45
## 90 57.000 1 115 0 25
## 91 70.000 0 66 1 45
## 256 52.000 1 191 1 30
## 197 45.000 0 582 1 38
## 290 90.000 1 337 0 38
## 137 65.000 1 59 1 60
## 26 80.000 0 148 1 38
## 7 75.000 1 246 0 15
## 286 55.000 1 170 1 40
## 254 70.000 0 88 1 35
## 211 70.000 0 212 1 17
## 78 42.000 0 102 1 40
## 81 70.000 0 69 0 40
## 43 70.000 0 571 1 45
## 143 50.000 0 482 1 30
## 32 85.000 0 23 0 45
## 109 63.000 0 936 0 38
## 263 65.000 1 258 1 25
## 23 68.000 1 220 0 35
## 135 81.000 0 4540 0 35
## 224 47.000 0 582 0 25
## 166 80.000 0 776 1 38
## 217 70.000 0 1202 0 50
## 69 70.000 0 161 0 25
## 72 58.000 0 582 1 35
## 76 60.000 1 47 0 20
## 63 55.000 0 109 0 35
## 141 80.000 0 805 0 38
## 210 49.000 0 972 1 35
## 292 60.000 0 320 0 35
## 41 70.000 0 582 0 20
## 291 45.000 0 615 1 55
## 223 42.000 1 86 0 35
## 16 82.000 1 379 0 50
## 116 58.000 1 400 0 40
## 94 60.000 1 154 0 25
## 6 90.000 1 47 0 40
## 235 53.000 1 582 0 45
## 200 60.000 0 1211 1 35
## 86 51.000 0 78 0 50
## 252 55.000 0 572 1 35
## 39 60.000 0 2656 1 30
## 159 85.000 1 910 0 50
## 240 55.000 1 180 0 45
## 209 60.000 1 2281 1 40
## 296 55.000 0 1820 0 38
## 50 57.000 1 129 0 30
## 241 70.000 0 81 1 35
## 34 50.000 1 159 1 30
## 4 50.000 1 111 0 20
## 13 45.000 1 981 0 30
## 268 56.000 1 135 1 38
## 127 46.000 0 168 1 17
## 262 62.000 1 655 0 40
## 52 53.000 1 91 0 20
## 22 65.000 1 128 1 30
## 89 44.000 0 84 1 40
## 160 59.000 1 129 0 45
## 25 75.000 0 582 1 30
## 215 65.000 1 135 0 35
## 35 65.000 0 94 1 50
## 168 59.000 0 66 1 20
## 112 55.000 0 60 0 35
## 30 82.000 0 70 1 30
## 140 50.000 0 1548 0 30
## 249 40.000 0 624 0 35
## 121 60.000 1 737 0 60
## 110 45.000 0 292 1 35
## 158 50.000 0 250 0 25
## 64 45.000 0 582 0 35
## 142 46.000 1 291 0 35
## 199 50.000 1 582 1 20
## 67 42.000 1 250 1 15
## 151 72.000 0 233 0 45
## 122 66.000 1 68 1 38
## 79 75.000 1 203 1 38
## 85 59.000 1 280 1 25
## 165 45.000 0 2442 1 30
## 136 75.000 0 582 0 40
## 51 68.000 1 577 0 25
## 74 65.000 0 224 1 50
## 178 49.000 1 69 0 50
## 106 72.000 1 328 0 30
## 98 70.000 1 59 0 60
## 281 70.000 0 2695 1 40
## 239 65.000 1 720 1 40
## 204 60.000 0 59 0 25
## 174 50.000 1 115 0 20
## 17 87.000 1 149 0 38
## 190 40.000 0 244 0 45
## 46 50.000 0 582 1 38
## 54 70.000 1 69 1 50
## 163 63.000 1 582 0 40
## 271 44.000 0 582 1 30
## 289 65.000 0 892 1 35
## 216 73.000 0 582 0 35
## 24 53.000 0 63 1 60
## 113 50.000 0 369 1 25
## 107 55.000 0 748 0 45
## 272 51.000 0 582 1 40
## 218 54.000 1 427 0 70
## 154 50.000 0 1846 1 35
## 102 75.000 0 582 0 45
## 234 53.000 1 707 0 38
## 192 64.000 1 62 0 60
## 155 65.000 1 335 0 35
## 170 70.000 0 835 0 35
## 5 65.000 1 160 1 20
## 70 65.000 0 113 1 25
## 258 58.000 0 132 1 38
## 297 45.000 0 2060 1 60
## 276 45.000 0 582 0 38
## 21 65.000 1 52 0 25
## 55 60.000 1 260 1 38
## 75 69.000 0 582 0 20
## 164 50.000 1 2334 1 35
## 83 60.000 1 76 1 25
## 250 53.000 0 207 1 40
## 253 50.000 0 245 0 45
## 283 42.000 0 64 0 30
## 48 60.000 0 582 1 38
## 77 70.000 0 92 0 60
## 175 65.000 0 198 1 35
## 111 85.000 0 129 0 60
## 205 78.000 1 64 0 40
## 1 75.000 0 582 0 20
## 228 55.000 1 2794 0 35
## 288 45.000 0 582 1 55
## 182 59.000 1 176 1 25
## 88 65.000 1 68 1 60
## 173 70.000 1 171 0 60
## 20 48.000 1 582 1 55
## 104 42.000 0 5209 0 30
## 93 42.000 0 582 0 60
## 237 75.000 0 119 0 50
## 108 45.000 1 1876 1 35
## 236 77.000 1 109 0 50
## 42 50.000 0 124 1 30
## 59 49.000 0 789 0 20
## 84 79.000 1 55 0 50
## 11 75.000 1 81 0 38
## 225 58.000 0 582 1 25
## 213 78.000 0 224 0 50
## 201 63.000 1 1767 0 45
## 232 70.000 0 93 0 35
## 275 60.000 1 257 1 30
## 193 50.000 1 121 1 40
## 185 58.000 1 145 0 25
## 36 69.000 0 582 1 35
## 221 73.000 0 582 0 20
## 220 55.000 0 582 1 35
## 125 60.000 0 582 0 40
## 33 50.000 1 249 1 35
## 40 60.000 0 235 1 38
## 10 80.000 1 123 0 35
## 227 58.000 1 57 0 25
## 9 65.000 0 157 0 65
## 284 65.000 0 1688 0 38
## 61 45.000 0 7702 1 25
## 265 61.000 0 582 1 38
## 161 66.000 1 72 0 40
## 285 50.000 1 54 0 40
## 139 62.000 0 281 1 35
## 57 70.000 1 75 0 35
## 149 75.000 1 582 0 30
## 29 58.000 1 60 0 38
## 115 60.000 1 754 1 40
## 172 52.000 0 3966 0 40
## 53 60.000 0 3964 1 62
## 131 53.000 1 1808 0 60
## 144 61.000 1 84 0 40
## 145 72.000 1 943 0 25
## 191 80.000 0 582 1 35
## 233 40.000 1 129 0 35
## 157 52.000 1 58 0 35
## 130 53.000 1 270 1 35
## 134 63.000 0 193 0 60
## 246 61.000 1 80 1 38
## 87 55.000 0 47 0 35
## 260 53.000 0 56 0 50
## 180 55.000 0 835 0 40
## 152 62.000 0 30 1 60
## 181 40.000 0 478 1 30
## 128 58.000 1 200 1 60
## 73 85.000 0 5882 0 35
## 105 60.000 0 53 0 50
## 101 65.000 1 305 0 25
## 273 67.000 0 213 0 38
## 293 52.000 0 190 1 38
## 255 53.000 1 446 0 60
## 287 60.000 0 253 0 35
## 270 40.000 0 582 1 35
## 295 62.000 0 61 1 38
## 58 60.000 1 607 0 40
## 279 50.000 1 1051 1 30
## 126 43.000 1 358 0 50
## 257 65.000 0 326 0 38
## 27 95.000 1 112 0 40
## 147 52.000 0 132 0 30
## 206 50.000 1 167 1 45
## 60 72.000 0 364 1 20
## 95 58.000 0 144 1 38
## 261 55.000 0 66 0 40
## 198 65.000 0 167 0 30
## 189 60.667 1 151 1 40
## 31 94.000 0 582 1 38
## 203 70.000 0 97 0 60
## 222 65.000 0 118 0 50
## 37 90.000 1 60 1 50
## 156 60.000 1 231 1 25
## 188 60.000 0 1896 1 25
## 231 60.000 0 166 0 30
## 129 61.000 0 248 0 30
## 66 60.000 0 68 0 20
## 242 65.000 0 582 1 30
## 207 40.000 1 101 0 40
## 184 75.000 0 99 0 38
## 114 70.000 1 143 0 60
## 8 60.000 1 315 1 60
## 274 42.000 0 64 0 40
## 45 60.000 1 588 1 60
## 18 45.000 0 582 0 14
## 202 45.000 0 308 1 60
## 251 50.000 0 2522 0 30
## 194 73.000 1 231 1 30
## 133 46.000 0 719 0 40
## 171 51.000 1 582 1 35
## 259 45.000 1 66 1 25
## 123 60.000 0 96 1 38
## 71 41.000 0 148 0 40
## 28 70.000 0 122 1 45
## high_blood_pressure platelets serum_creatinine serum_sodium sex smoking
## 179 0 267000 1.20 145 1 0
## 14 1 276000 1.10 137 1 0
## 195 1 126000 1.60 135 1 0
## 118 0 507000 3.20 138 0 0
## 229 0 237000 5.00 130 0 0
## 244 1 220000 0.90 141 0 0
## 298 0 140000 1.40 140 1 1
## 153 1 184000 0.90 134 1 1
## 90 1 181000 1.10 144 1 0
## 91 0 249000 0.80 136 1 1
## 256 1 334000 1.00 142 1 1
## 197 1 263358 1.18 137 0 0
## 290 0 390000 0.90 144 0 0
## 137 0 172000 0.90 137 0 0
## 26 0 149000 1.90 144 1 1
## 7 0 127000 1.20 137 1 0
## 286 0 336000 1.20 135 1 0
## 254 1 236000 1.20 132 0 0
## 211 1 389000 1.00 136 1 1
## 78 0 237000 1.20 140 1 0
## 81 0 293000 1.70 136 0 0
## 43 1 185000 1.20 139 1 1
## 143 0 329000 0.90 132 0 0
## 32 0 360000 3.00 132 1 0
## 109 0 304000 1.10 133 1 1
## 263 0 198000 1.40 129 1 0
## 23 1 289000 0.90 140 1 1
## 135 0 231000 1.18 137 1 1
## 224 0 130000 0.80 134 1 0
## 166 1 192000 1.30 135 0 0
## 217 1 358000 0.90 141 0 0
## 69 0 244000 1.20 142 0 0
## 72 0 122000 0.90 139 1 1
## 76 0 204000 0.70 139 1 1
## 63 0 254000 1.10 139 1 1
## 141 0 263358 1.10 134 1 0
## 210 1 268000 0.80 130 0 0
## 292 0 133000 1.40 139 1 0
## 41 1 263358 1.83 134 1 1
## 291 0 222000 0.80 141 0 0
## 223 0 365000 1.10 139 1 1
## 16 0 47000 1.30 136 1 0
## 116 0 164000 1.00 139 0 0
## 94 0 210000 1.70 135 1 0
## 6 1 204000 2.10 132 1 1
## 235 0 305000 1.10 137 1 1
## 200 0 263358 1.80 113 1 1
## 86 0 406000 0.70 140 1 0
## 252 0 231000 0.80 143 0 0
## 39 0 305000 2.30 137 1 0
## 159 0 235000 1.30 134 1 0
## 240 0 263358 1.18 137 1 1
## 209 0 283000 1.00 141 0 0
## 296 0 270000 1.20 139 0 0
## 50 0 395000 1.00 140 0 0
## 241 1 533000 1.30 139 0 0
## 34 0 302000 1.20 138 0 0
## 4 0 210000 1.90 137 1 0
## 13 0 136000 1.10 137 1 0
## 268 0 133000 1.70 140 1 0
## 127 1 271000 2.10 124 0 0
## 262 0 283000 0.70 133 0 0
## 52 1 418000 1.40 139 0 0
## 22 1 297000 1.60 136 0 0
## 89 1 235000 0.70 139 1 0
## 160 1 362000 1.10 139 1 1
## 25 1 263358 1.83 134 0 0
## 215 1 290000 0.80 134 1 0
## 35 1 188000 1.00 140 1 0
## 168 0 70000 2.40 134 1 0
## 112 0 228000 1.20 135 1 1
## 30 0 200000 1.20 132 1 1
## 140 1 211000 0.80 138 1 0
## 249 0 301000 1.00 142 1 1
## 121 1 210000 1.50 135 1 1
## 110 0 850000 1.30 142 1 1
## 158 0 262000 1.00 136 1 1
## 64 0 385000 1.00 145 1 0
## 142 0 348000 0.90 140 0 0
## 199 1 279000 1.00 134 0 0
## 67 0 213000 1.30 136 0 0
## 151 1 235000 2.50 135 0 0
## 122 1 162000 1.00 136 0 0
## 79 1 283000 0.60 131 1 1
## 85 1 302000 1.00 141 0 0
## 165 0 334000 1.10 139 1 0
## 136 0 263358 1.18 137 1 0
## 51 1 166000 1.00 138 1 0
## 74 0 149000 1.30 137 1 1
## 178 0 132000 1.00 140 0 0
## 106 1 621000 1.70 138 0 1
## 98 0 255000 1.10 136 0 0
## 281 0 241000 1.00 137 1 0
## 239 0 257000 1.00 136 0 0
## 204 1 212000 3.50 136 1 1
## 174 0 189000 0.80 139 1 0
## 17 0 262000 0.90 140 1 0
## 190 1 275000 0.90 140 0 0
## 46 0 310000 1.90 135 1 1
## 54 1 351000 1.00 134 0 0
## 163 0 448000 0.90 137 1 1
## 271 1 263358 1.60 130 1 1
## 289 0 263358 1.10 142 0 0
## 216 1 203000 1.30 134 1 0
## 24 0 368000 0.80 135 1 0
## 113 0 252000 1.60 136 1 0
## 107 0 263000 1.30 137 1 0
## 272 0 221000 0.90 134 0 0
## 218 1 151000 9.00 137 0 0
## 154 0 263358 1.18 137 1 1
## 102 1 263358 1.18 137 1 0
## 234 0 330000 1.40 137 1 1
## 192 0 309000 1.50 135 0 0
## 155 1 235000 0.80 136 0 0
## 170 1 305000 0.80 133 0 0
## 5 0 327000 2.70 116 0 0
## 70 0 497000 1.83 135 1 0
## 258 1 253000 1.00 139 1 0
## 297 0 742000 0.80 138 0 0
## 276 1 422000 0.80 137 0 0
## 21 1 276000 1.30 137 0 0
## 55 0 255000 2.20 132 0 1
## 75 0 266000 1.20 134 1 1
## 164 0 75000 0.90 142 0 0
## 83 0 196000 2.50 132 0 0
## 250 0 223000 1.20 130 0 0
## 253 1 274000 1.00 133 1 0
## 283 0 215000 3.80 128 1 1
## 48 1 451000 0.60 138 1 1
## 77 1 317000 0.80 140 0 1
## 175 1 281000 0.90 137 1 1
## 111 0 306000 1.20 132 1 1
## 205 0 277000 0.70 137 1 1
## 1 1 265000 1.90 130 1 0
## 228 1 141000 1.00 140 1 0
## 288 0 543000 1.00 132 0 0
## 182 0 221000 1.00 136 1 1
## 88 1 304000 0.80 140 1 0
## 173 1 176000 1.10 145 1 1
## 20 0 87000 1.90 121 0 0
## 104 0 226000 1.00 140 1 1
## 93 0 263358 1.18 137 0 0
## 237 1 248000 1.10 148 1 0
## 108 0 226000 0.90 138 1 0
## 236 1 406000 1.10 137 1 0
## 42 1 153000 1.20 136 0 1
## 59 1 319000 1.10 136 1 1
## 84 1 172000 1.80 133 1 0
## 11 1 368000 4.00 131 1 1
## 225 0 504000 1.00 138 1 0
## 213 0 481000 1.40 138 1 1
## 201 0 73000 0.70 137 1 0
## 232 0 185000 1.10 134 1 1
## 275 0 150000 1.00 137 1 1
## 193 0 260000 0.70 130 1 0
## 185 0 219000 1.20 137 1 1
## 36 0 228000 3.50 134 1 0
## 221 0 263358 1.83 134 1 0
## 220 1 371000 0.70 140 0 0
## 125 0 217000 3.70 134 1 0
## 33 1 319000 1.00 128 0 0
## 40 0 329000 3.00 142 0 0
## 10 1 388000 9.40 133 1 1
## 227 0 189000 1.30 132 1 1
## 9 0 263358 1.50 138 0 0
## 284 0 263358 1.10 138 1 1
## 61 1 390000 1.00 139 1 0
## 265 0 147000 1.20 141 1 0
## 161 1 242000 1.20 134 1 0
## 285 0 279000 0.80 141 1 0
## 139 0 221000 1.00 136 0 0
## 57 0 223000 2.70 138 1 1
## 149 0 225000 1.83 134 1 0
## 29 0 153000 5.80 134 1 0
## 115 1 328000 1.20 126 1 0
## 172 0 325000 0.90 140 1 1
## 53 0 263358 6.80 146 0 0
## 131 1 249000 0.70 138 1 1
## 144 1 229000 0.90 141 0 0
## 145 1 338000 1.70 139 1 1
## 191 0 350000 2.10 134 1 0
## 233 0 255000 0.90 137 1 0
## 157 0 277000 1.40 136 0 0
## 130 0 227000 3.40 145 1 0
## 134 1 295000 1.30 145 1 1
## 246 0 282000 1.40 137 1 0
## 87 1 173000 1.10 137 1 0
## 260 0 308000 0.70 135 1 1
## 180 0 279000 0.70 140 1 1
## 152 1 244000 0.90 139 1 0
## 181 0 303000 0.90 136 1 0
## 128 0 300000 0.80 137 0 0
## 73 0 243000 1.00 132 1 1
## 105 1 286000 2.30 143 0 0
## 101 0 298000 1.10 141 1 0
## 273 0 215000 1.20 133 0 0
## 293 0 382000 1.00 140 1 1
## 255 1 263358 1.00 139 1 0
## 287 0 279000 1.70 140 1 0
## 270 0 222000 1.00 132 1 0
## 295 1 155000 1.10 143 1 1
## 58 0 216000 0.60 138 1 1
## 279 0 232000 0.70 136 0 0
## 126 0 237000 1.30 135 0 0
## 257 0 294000 1.70 139 0 0
## 27 1 196000 1.00 138 0 0
## 147 0 218000 0.70 136 1 1
## 206 0 362000 1.00 136 0 0
## 60 1 254000 1.30 136 1 1
## 95 1 327000 0.70 142 0 0
## 261 0 203000 1.00 138 1 0
## 198 0 259000 0.80 138 0 0
## 189 1 201000 1.00 136 0 0
## 31 1 263358 1.83 134 1 0
## 203 1 220000 0.90 138 1 0
## 222 0 194000 1.10 145 1 1
## 37 0 226000 1.00 134 1 0
## 156 0 194000 1.70 140 1 0
## 188 0 365000 2.10 144 0 0
## 231 0 62000 1.70 127 0 0
## 129 1 267000 0.70 136 1 1
## 66 0 119000 2.90 127 1 1
## 242 0 249000 1.30 136 1 1
## 207 0 226000 0.80 141 0 0
## 184 1 224000 2.50 134 1 0
## 114 0 351000 1.30 137 0 0
## 8 0 454000 1.10 131 1 1
## 274 0 189000 0.70 140 1 0
## 45 0 194000 1.10 142 0 0
## 18 0 166000 0.80 127 1 0
## 202 1 377000 1.00 136 1 0
## 251 1 404000 0.50 139 0 0
## 194 0 160000 1.18 142 1 1
## 133 1 263358 1.18 137 0 0
## 171 0 263358 1.50 136 1 1
## 259 0 233000 0.80 135 1 0
## 123 0 228000 0.75 140 0 0
## 71 0 374000 0.80 140 1 1
## 28 1 284000 1.30 136 1 1
print(test_dataset)
## age anaemia creatinine_phosphokinase diabetes ejection_fraction
## 2 55.000 0 7861 0 38
## 3 65.000 0 146 0 20
## 12 62.000 0 231 0 25
## 15 49.000 1 80 0 30
## 19 70.000 1 125 0 25
## 38 82.000 1 855 1 50
## 44 72.000 0 127 1 50
## 47 51.000 0 1380 0 25
## 49 80.000 1 553 0 20
## 56 95.000 1 371 0 30
## 62 50.000 0 318 0 40
## 65 45.000 0 582 0 80
## 68 72.000 1 110 0 25
## 80 55.000 0 336 0 45
## 82 67.000 0 582 0 50
## 92 60.000 0 897 1 45
## 96 58.000 1 133 0 60
## 97 63.000 1 514 1 25
## 99 60.000 1 156 1 25
## 100 63.000 1 61 1 40
## 103 80.000 0 898 0 25
## 117 60.000 1 96 1 60
## 119 65.000 1 113 1 60
## 120 86.000 0 582 0 38
## 124 60.000 1 582 0 30
## 132 60.000 1 1082 1 45
## 138 68.000 1 646 0 25
## 146 50.000 0 185 0 30
## 148 64.000 0 1610 0 60
## 150 60.000 0 2261 0 35
## 162 45.000 1 130 0 35
## 167 53.000 0 196 0 60
## 169 65.000 0 582 1 40
## 176 60.000 1 95 0 60
## 177 69.000 0 1419 0 40
## 183 65.000 0 395 1 25
## 186 60.667 1 104 1 30
## 187 50.000 0 582 0 50
## 196 77.000 1 418 0 45
## 208 85.000 0 212 0 38
## 212 50.000 0 582 0 62
## 214 48.000 1 131 1 30
## 219 68.000 1 1021 1 35
## 226 75.000 0 675 1 60
## 230 72.000 0 211 0 25
## 238 70.000 0 232 0 30
## 243 40.000 0 90 0 35
## 245 54.000 0 582 1 38
## 247 55.000 0 2017 0 25
## 248 64.000 0 143 0 25
## 264 68.000 1 157 1 60
## 266 50.000 1 298 0 35
## 267 55.000 0 1199 0 20
## 269 45.000 0 582 1 38
## 277 70.000 0 618 0 35
## 278 70.000 0 582 1 38
## 280 55.000 0 84 1 38
## 282 70.000 0 582 0 40
## 294 63.000 1 103 1 35
## 299 50.000 0 196 0 45
## high_blood_pressure platelets serum_creatinine serum_sodium sex smoking
## 2 0 263358 1.10 136 1 0
## 3 0 162000 1.30 129 1 1
## 12 1 253000 0.90 140 1 1
## 15 1 427000 1.00 138 0 0
## 19 1 237000 1.00 140 0 0
## 38 1 321000 1.00 145 0 0
## 44 1 218000 1.00 134 1 0
## 47 1 271000 0.90 130 1 0
## 49 1 140000 4.40 133 1 0
## 56 0 461000 2.00 132 1 0
## 62 1 216000 2.30 131 0 0
## 65 0 263358 1.18 137 0 0
## 68 0 274000 1.00 140 1 1
## 80 1 324000 0.90 140 0 0
## 82 0 263358 1.18 137 1 1
## 92 0 297000 1.00 133 1 0
## 96 1 219000 1.00 141 1 0
## 97 1 254000 1.30 134 1 0
## 99 1 318000 1.20 137 0 0
## 100 0 221000 1.10 140 0 0
## 103 0 149000 1.10 144 1 1
## 117 1 271000 0.70 136 0 0
## 119 1 203000 0.90 140 0 0
## 120 0 263358 1.83 134 0 0
## 124 1 127000 0.90 145 0 0
## 132 0 250000 6.10 131 1 0
## 138 0 305000 2.10 130 1 0
## 146 0 266000 0.70 141 1 1
## 148 0 242000 1.00 137 1 0
## 150 1 228000 0.90 136 1 0
## 162 0 174000 0.80 139 1 1
## 167 0 220000 0.70 133 1 1
## 169 0 270000 1.00 138 0 0
## 176 0 337000 1.00 138 1 1
## 177 0 105000 1.00 135 1 1
## 183 0 265000 1.20 136 1 1
## 186 0 389000 1.50 136 1 0
## 187 0 153000 0.60 134 0 0
## 196 0 223000 1.80 145 1 0
## 208 0 186000 0.90 136 1 0
## 212 1 147000 0.80 140 1 1
## 214 1 244000 1.60 130 0 0
## 219 0 271000 1.10 134 1 0
## 226 0 265000 1.40 125 0 0
## 230 0 274000 1.20 134 0 0
## 238 0 173000 1.20 132 1 0
## 243 0 255000 1.10 136 1 1
## 245 0 264000 1.80 134 1 0
## 247 0 314000 1.10 138 1 0
## 248 0 246000 2.40 135 1 0
## 264 0 208000 1.00 140 0 0
## 266 0 362000 0.90 140 1 1
## 267 0 263358 1.83 134 1 1
## 269 0 302000 0.90 140 0 0
## 277 0 327000 1.10 142 0 0
## 278 0 25100 1.10 140 1 0
## 280 0 451000 1.30 136 0 0
## 282 0 51000 2.70 136 1 1
## 294 0 179000 0.90 136 1 1
## 299 0 395000 1.60 136 1 1
View(train_dataset)
View(test_dataset)
#3.Training on Linear Regression Model
#linear_mod <- lm(serum_creatinine ~ age + serum_sodium + platelets + creatinine_phosphokinase + ejection_fraction, data = train_dataset)
linear_mod <- lm(serum_sodium ~ serum_creatinine + platelets + creatinine_phosphokinase + ejection_fraction, data = train_dataset)
print(linear_mod)
##
## Call:
## lm(formula = serum_sodium ~ serum_creatinine + platelets + creatinine_phosphokinase +
## ejection_fraction, data = train_dataset)
##
## Coefficients:
## (Intercept) serum_creatinine platelets
## 1.338e+02 -6.708e-01 2.333e-06
## creatinine_phosphokinase ejection_fraction
## 4.458e-04 7.793e-02
summary(linear_mod)
##
## Call:
## lm(formula = serum_sodium ~ serum_creatinine + platelets + creatinine_phosphokinase +
## ejection_fraction, data = train_dataset)
##
## Residuals:
## Min 1Q Median 3Q Max
## -23.4494 -1.7543 0.1813 2.5663 10.4348
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.338e+02 1.245e+00 107.441 < 2e-16 ***
## serum_creatinine -6.708e-01 2.629e-01 -2.552 0.01135 *
## platelets 2.333e-06 2.819e-06 0.828 0.40872
## creatinine_phosphokinase 4.458e-04 2.982e-04 1.495 0.13625
## ejection_fraction 7.793e-02 2.482e-02 3.140 0.00191 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.348 on 234 degrees of freedom
## Multiple R-squared: 0.07734, Adjusted R-squared: 0.06157
## F-statistic: 4.904 on 4 and 234 DF, p-value: 0.0008167
#4.Model Evaluation
# Calculations (MSE)
predictions <- predict(linear_mod, newdata = test_dataset)
#MSE
mse <- mean((test_dataset$serum_sodium - predictions)^2)
#RMSE
rmse <- sqrt(mse)
#R-squared
rsquared <- summary(linear_mod)$r.squared
# Print the evaluation metrics
cat("Mean Squared Error:", mse, "\n")
## Mean Squared Error: 16.34964
cat("Root Mean Squared Error:", rmse, "\n")
## Root Mean Squared Error: 4.043469
cat("R-squared:", rsquared, "\n")
## R-squared: 0.07733868
#5.Predictions
# Making predictions using trained data
predictions <- predict(linear_mod, newdata = test_dataset)
View(predictions)
# Comparison of Predictions with Actuals
plot(test_dataset$serum_creatinine, predictions,
main = "Actual vs. Predicted Serum Creatinine",
xlab = "Actual Serum Creatinine", ylab = "Predicted Serum Creatinine",
col = "blue", pch = 20)
abline(0, 1, col = "red") # Adding a diagonal line for reference
legend("topright", legend = "Ideal Prediction", col = "red", pch = 20, bty = "n")
plot(test_dataset$creatinine_phosphokinase, predictions,
main = "Actual vs. Predicted Creatinine Phosphokinase",
xlab = "Actual Creatinine Phosphokinase", ylab = "Predicted Creatinine Phosphokinase",
col = "green", pch = 20)
abline(0, 1, col = "red") # Adding a diagonal line for reference
legend("topright", legend = "Ideal Prediction", col = "red", pch = 20, bty = "n")
plot(test_dataset$ejection_fraction, predictions,
main = "Actual vs. Predicted Ejection Fraction",
xlab = "Actual Ejection Fraction", ylab = "Predicted Ejection Fraction",
col = "blue", pch = 20)
abline(0, 1, col = "red") # Adding a diagonal line for reference
legend("topright", legend = "Ideal Prediction", col = "red", pch = 20, bty = "n")
plot(test_dataset$platelets, predictions,
main = "Actual vs. Predicted Platelets",
xlab = "Actual Platelets", ylab = "Predicted Platelets",
col = "purple", pch = 20)
abline(0, 1, col = "red") # Adding a diagonal line for reference
legend("topright", legend = "Ideal Prediction", col = "red", pch = 20, bty = "n")
names(dataSheet)
## [1] "age" "anaemia"
## [3] "creatinine_phosphokinase" "diabetes"
## [5] "ejection_fraction" "high_blood_pressure"
## [7] "platelets" "serum_creatinine"
## [9] "serum_sodium" "sex"
## [11] "smoking"
coef(linear_mod)
## (Intercept) serum_creatinine platelets
## 1.337749e+02 -6.708165e-01 2.333260e-06
## creatinine_phosphokinase ejection_fraction
## 4.458492e-04 7.792989e-02
#additional analysis (For more clarifications)
#Scatter plots with regression lines and correlation co-efs
library(ggplot2)
# Scatter plot for Platelets and Serum Sodium
ggplot(dataSheet, aes(x = platelets, y = serum_sodium)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "purple") + # regreesion line
geom_text(aes(label = paste("cor =", round(cor(platelets, serum_sodium), 2))),
x = max(dataSheet$platelets), y = min(dataSheet$serum_sodium),
hjust = 1, vjust = 0) + # correlation coefficient
labs(title = "Platelets vs Serum Sodium", x = "Platelets", y = "Serum Sodium")
## Warning in geom_text(aes(label = paste("cor =", round(cor(platelets, serum_sodium), : All aesthetics have length 1, but the data has 299 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## `geom_smooth()` using formula = 'y ~ x'
# Scatter plot for Creatinine Phosphokinase and Serum Sodium
ggplot(dataSheet, aes(x = creatinine_phosphokinase, y = serum_sodium)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
geom_text(aes(label = paste("cor =", round(cor(creatinine_phosphokinase, serum_sodium), 2))),
x = max(dataSheet$creatinine_phosphokinase), y = min(dataSheet$serum_sodium),
hjust = 1, vjust = 0) +
labs(title = "Creatinine Phosphokinase and Serum Sodium", x = "Creatinine Phosphokinase", y = "Serum Sodium")
## Warning in geom_text(aes(label = paste("cor =", round(cor(creatinine_phosphokinase, : All aesthetics have length 1, but the data has 299 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## `geom_smooth()` using formula = 'y ~ x'
# Scatter plot for Ejection Fraction vs Serum Sodium
ggplot(dataSheet, aes(x = ejection_fraction, y = serum_sodium)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "blue") +
geom_text(aes(label = paste("cor =", round(cor(ejection_fraction, serum_sodium), 2))),
x = max(dataSheet$ejection_fraction), y = min(dataSheet$serum_sodium),
hjust = 1, vjust = 0) +
labs(title = "Ejection Fraction and Serum Sodium", x = "Ejection Fraction", y = "Serum Sodium")
## Warning in geom_text(aes(label = paste("cor =", round(cor(ejection_fraction, : All aesthetics have length 1, but the data has 299 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## `geom_smooth()` using formula = 'y ~ x'
# Scatter plot for Serum Creatinine and Serum Sodium
ggplot(dataSheet, aes(x = serum_creatinine, y = serum_sodium)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "green") +
geom_text(aes(label = paste("cor =", round(cor(serum_creatinine, serum_sodium), 2))),
x = max(dataSheet$serum_creatinine), y = min(dataSheet$serum_sodium),
hjust = 1, vjust = 0) +
labs(title = "Serum Creatinine and Serum Sodium", x = "Serum Creatinine", y = "Serum Sodium")
## Warning in geom_text(aes(label = paste("cor =", round(cor(serum_creatinine, : All aesthetics have length 1, but the data has 299 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## `geom_smooth()` using formula = 'y ~ x'