data1=read.csv(file.choose(),header = TRUE)
View(data1)
attach(data1)

library(ggplot2)
# Visualize the relationship between age and diabetes
ggplot(data1, aes(x = age, fill = factor(diabetes))) +
  geom_histogram(binwidth = 5, position = "identity", alpha = 0.5) +
  labs(title = "Distribution of Diabetes by Age", x = "Age", y = "Frequency", fill = "Diabetes") +
  theme_minimal()

linearMod <- lm(platelets ~ diabetes , data = data1)
print(linearMod)
## 
## Call:
## lm(formula = platelets ~ diabetes, data = data1)
## 
## Coefficients:
## (Intercept)     diabetes  
##      255728        18250
summary(linearMod)
## 
## Call:
## lm(formula = platelets ~ diabetes, data = data1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -248879  -51979   -9728   38772  576021 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   255728       7395  34.580   <2e-16 ***
## diabetes       18250      11438   1.596    0.112    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 97550 on 297 degrees of freedom
## Multiple R-squared:  0.0085, Adjusted R-squared:  0.005161 
## F-statistic: 2.546 on 1 and 297 DF,  p-value: 0.1116
# Check for missing values
any(is.na(data1))
## [1] FALSE
#Split the data_set
split <- split(data1, sex)
print(split)
## $`0`
##        age anaemia creatinine_phosphokinase diabetes ejection_fraction
## 5   65.000       1                      160        1                20
## 9   65.000       0                      157        0                65
## 15  49.000       1                       80        0                30
## 19  70.000       1                      125        0                25
## 20  48.000       1                      582        1                55
## 21  65.000       1                       52        0                25
## 22  65.000       1                      128        1                30
## 25  75.000       0                      582        1                30
## 27  95.000       1                      112        0                40
## 33  50.000       1                      249        1                35
## 34  50.000       1                      159        1                30
## 38  82.000       1                      855        1                50
## 40  60.000       0                      235        1                38
## 42  50.000       0                      124        1                30
## 45  60.000       1                      588        1                60
## 50  57.000       1                      129        0                30
## 52  53.000       1                       91        0                20
## 53  60.000       0                     3964        1                62
## 54  70.000       1                       69        1                50
## 55  60.000       1                      260        1                38
## 62  50.000       0                      318        0                40
## 65  45.000       0                      582        0                80
## 67  42.000       1                      250        1                15
## 69  70.000       0                      161        0                25
## 77  70.000       0                       92        0                60
## 80  55.000       0                      336        0                45
## 81  70.000       0                       69        0                40
## 83  60.000       1                       76        1                25
## 85  59.000       1                      280        1                25
## 93  42.000       0                      582        0                60
## 95  58.000       0                      144        1                38
## 98  70.000       1                       59        0                60
## 99  60.000       1                      156        1                25
## 100 63.000       1                       61        1                40
## 105 60.000       0                       53        0                50
## 106 72.000       1                      328        0                30
## 114 70.000       1                      143        0                60
## 116 58.000       1                      400        0                40
## 117 60.000       1                       96        1                60
## 118 85.000       1                      102        0                60
## 119 65.000       1                      113        1                60
## 120 86.000       0                      582        0                38
## 122 66.000       1                       68        1                38
## 123 60.000       0                       96        1                38
## 124 60.000       1                      582        0                30
## 126 43.000       1                      358        0                50
## 127 46.000       0                      168        1                17
## 128 58.000       1                      200        1                60
## 133 46.000       0                      719        0                40
## 137 65.000       1                       59        1                60
## 139 62.000       0                      281        1                35
## 142 46.000       1                      291        0                35
## 143 50.000       0                      482        1                30
## 144 61.000       1                       84        0                40
## 151 72.000       0                      233        0                45
## 155 65.000       1                      335        0                35
## 157 52.000       1                       58        0                35
## 164 50.000       1                     2334        1                35
## 166 80.000       0                      776        1                38
## 169 65.000       0                      582        1                40
## 170 70.000       0                      835        0                35
## 178 49.000       1                       69        0                50
## 187 50.000       0                      582        0                50
## 188 60.000       0                     1896        1                25
## 189 60.667       1                      151        1                40
## 190 40.000       0                      244        0                45
## 192 64.000       1                       62        0                60
## 197 45.000       0                      582        1                38
## 198 65.000       0                      167        0                30
## 199 50.000       1                      582        1                20
## 206 50.000       1                      167        1                45
## 207 40.000       1                      101        0                40
## 209 60.000       1                     2281        1                40
## 210 49.000       0                      972        1                35
## 214 48.000       1                      131        1                30
## 217 70.000       0                     1202        0                50
## 218 54.000       1                      427        0                70
## 220 55.000       0                      582        1                35
## 226 75.000       0                      675        1                60
## 229 65.000       0                       56        0                25
## 230 72.000       0                      211        0                25
## 231 60.000       0                      166        0                30
## 239 65.000       1                      720        1                40
## 241 70.000       0                       81        1                35
## 244 73.000       1                     1185        0                40
## 250 53.000       0                      207        1                40
## 251 50.000       0                     2522        0                30
## 252 55.000       0                      572        1                35
## 254 70.000       0                       88        1                35
## 257 65.000       0                      326        0                38
## 262 62.000       1                      655        0                40
## 264 68.000       1                      157        1                60
## 269 45.000       0                      582        1                38
## 272 51.000       0                      582        1                40
## 273 67.000       0                      213        0                38
## 276 45.000       0                      582        0                38
## 277 70.000       0                      618        0                35
## 279 50.000       1                     1051        1                30
## 280 55.000       0                       84        1                38
## 288 45.000       0                      582        1                55
## 289 65.000       0                      892        1                35
## 290 90.000       1                      337        0                38
## 291 45.000       0                      615        1                55
## 296 55.000       0                     1820        0                38
## 297 45.000       0                     2060        1                60
##     high_blood_pressure platelets serum_creatinine serum_sodium sex smoking
## 5                     0    327000             2.70          116   0       0
## 9                     0    263358             1.50          138   0       0
## 15                    1    427000             1.00          138   0       0
## 19                    1    237000             1.00          140   0       0
## 20                    0     87000             1.90          121   0       0
## 21                    1    276000             1.30          137   0       0
## 22                    1    297000             1.60          136   0       0
## 25                    1    263358             1.83          134   0       0
## 27                    1    196000             1.00          138   0       0
## 33                    1    319000             1.00          128   0       0
## 34                    0    302000             1.20          138   0       0
## 38                    1    321000             1.00          145   0       0
## 40                    0    329000             3.00          142   0       0
## 42                    1    153000             1.20          136   0       1
## 45                    0    194000             1.10          142   0       0
## 50                    0    395000             1.00          140   0       0
## 52                    1    418000             1.40          139   0       0
## 53                    0    263358             6.80          146   0       0
## 54                    1    351000             1.00          134   0       0
## 55                    0    255000             2.20          132   0       1
## 62                    1    216000             2.30          131   0       0
## 65                    0    263358             1.18          137   0       0
## 67                    0    213000             1.30          136   0       0
## 69                    0    244000             1.20          142   0       0
## 77                    1    317000             0.80          140   0       1
## 80                    1    324000             0.90          140   0       0
## 81                    0    293000             1.70          136   0       0
## 83                    0    196000             2.50          132   0       0
## 85                    1    302000             1.00          141   0       0
## 93                    0    263358             1.18          137   0       0
## 95                    1    327000             0.70          142   0       0
## 98                    0    255000             1.10          136   0       0
## 99                    1    318000             1.20          137   0       0
## 100                   0    221000             1.10          140   0       0
## 105                   1    286000             2.30          143   0       0
## 106                   1    621000             1.70          138   0       1
## 114                   0    351000             1.30          137   0       0
## 116                   0    164000             1.00          139   0       0
## 117                   1    271000             0.70          136   0       0
## 118                   0    507000             3.20          138   0       0
## 119                   1    203000             0.90          140   0       0
## 120                   0    263358             1.83          134   0       0
## 122                   1    162000             1.00          136   0       0
## 123                   0    228000             0.75          140   0       0
## 124                   1    127000             0.90          145   0       0
## 126                   0    237000             1.30          135   0       0
## 127                   1    271000             2.10          124   0       0
## 128                   0    300000             0.80          137   0       0
## 133                   1    263358             1.18          137   0       0
## 137                   0    172000             0.90          137   0       0
## 139                   0    221000             1.00          136   0       0
## 142                   0    348000             0.90          140   0       0
## 143                   0    329000             0.90          132   0       0
## 144                   1    229000             0.90          141   0       0
## 151                   1    235000             2.50          135   0       0
## 155                   1    235000             0.80          136   0       0
## 157                   0    277000             1.40          136   0       0
## 164                   0     75000             0.90          142   0       0
## 166                   1    192000             1.30          135   0       0
## 169                   0    270000             1.00          138   0       0
## 170                   1    305000             0.80          133   0       0
## 178                   0    132000             1.00          140   0       0
## 187                   0    153000             0.60          134   0       0
## 188                   0    365000             2.10          144   0       0
## 189                   1    201000             1.00          136   0       0
## 190                   1    275000             0.90          140   0       0
## 192                   0    309000             1.50          135   0       0
## 197                   1    263358             1.18          137   0       0
## 198                   0    259000             0.80          138   0       0
## 199                   1    279000             1.00          134   0       0
## 206                   0    362000             1.00          136   0       0
## 207                   0    226000             0.80          141   0       0
## 209                   0    283000             1.00          141   0       0
## 210                   1    268000             0.80          130   0       0
## 214                   1    244000             1.60          130   0       0
## 217                   1    358000             0.90          141   0       0
## 218                   1    151000             9.00          137   0       0
## 220                   1    371000             0.70          140   0       0
## 226                   0    265000             1.40          125   0       0
## 229                   0    237000             5.00          130   0       0
## 230                   0    274000             1.20          134   0       0
## 231                   0     62000             1.70          127   0       0
## 239                   0    257000             1.00          136   0       0
## 241                   1    533000             1.30          139   0       0
## 244                   1    220000             0.90          141   0       0
## 250                   0    223000             1.20          130   0       0
## 251                   1    404000             0.50          139   0       0
## 252                   0    231000             0.80          143   0       0
## 254                   1    236000             1.20          132   0       0
## 257                   0    294000             1.70          139   0       0
## 262                   0    283000             0.70          133   0       0
## 264                   0    208000             1.00          140   0       0
## 269                   0    302000             0.90          140   0       0
## 272                   0    221000             0.90          134   0       0
## 273                   0    215000             1.20          133   0       0
## 276                   1    422000             0.80          137   0       0
## 277                   0    327000             1.10          142   0       0
## 279                   0    232000             0.70          136   0       0
## 280                   0    451000             1.30          136   0       0
## 288                   0    543000             1.00          132   0       0
## 289                   0    263358             1.10          142   0       0
## 290                   0    390000             0.90          144   0       0
## 291                   0    222000             0.80          141   0       0
## 296                   0    270000             1.20          139   0       0
## 297                   0    742000             0.80          138   0       0
## 
## $`1`
##        age anaemia creatinine_phosphokinase diabetes ejection_fraction
## 1   75.000       0                      582        0                20
## 2   55.000       0                     7861        0                38
## 3   65.000       0                      146        0                20
## 4   50.000       1                      111        0                20
## 6   90.000       1                       47        0                40
## 7   75.000       1                      246        0                15
## 8   60.000       1                      315        1                60
## 10  80.000       1                      123        0                35
## 11  75.000       1                       81        0                38
## 12  62.000       0                      231        0                25
## 13  45.000       1                      981        0                30
## 14  50.000       1                      168        0                38
## 16  82.000       1                      379        0                50
## 17  87.000       1                      149        0                38
## 18  45.000       0                      582        0                14
## 23  68.000       1                      220        0                35
## 24  53.000       0                       63        1                60
## 26  80.000       0                      148        1                38
## 28  70.000       0                      122        1                45
## 29  58.000       1                       60        0                38
## 30  82.000       0                       70        1                30
## 31  94.000       0                      582        1                38
## 32  85.000       0                       23        0                45
## 35  65.000       0                       94        1                50
## 36  69.000       0                      582        1                35
## 37  90.000       1                       60        1                50
## 39  60.000       0                     2656        1                30
## 41  70.000       0                      582        0                20
## 43  70.000       0                      571        1                45
## 44  72.000       0                      127        1                50
## 46  50.000       0                      582        1                38
## 47  51.000       0                     1380        0                25
## 48  60.000       0                      582        1                38
## 49  80.000       1                      553        0                20
## 51  68.000       1                      577        0                25
## 56  95.000       1                      371        0                30
## 57  70.000       1                       75        0                35
## 58  60.000       1                      607        0                40
## 59  49.000       0                      789        0                20
## 60  72.000       0                      364        1                20
## 61  45.000       0                     7702        1                25
## 63  55.000       0                      109        0                35
## 64  45.000       0                      582        0                35
## 66  60.000       0                       68        0                20
## 68  72.000       1                      110        0                25
## 70  65.000       0                      113        1                25
## 71  41.000       0                      148        0                40
## 72  58.000       0                      582        1                35
## 73  85.000       0                     5882        0                35
## 74  65.000       0                      224        1                50
## 75  69.000       0                      582        0                20
## 76  60.000       1                       47        0                20
## 78  42.000       0                      102        1                40
## 79  75.000       1                      203        1                38
## 82  67.000       0                      582        0                50
## 84  79.000       1                       55        0                50
## 86  51.000       0                       78        0                50
## 87  55.000       0                       47        0                35
## 88  65.000       1                       68        1                60
## 89  44.000       0                       84        1                40
## 90  57.000       1                      115        0                25
## 91  70.000       0                       66        1                45
## 92  60.000       0                      897        1                45
## 94  60.000       1                      154        0                25
## 96  58.000       1                      133        0                60
## 97  63.000       1                      514        1                25
## 101 65.000       1                      305        0                25
## 102 75.000       0                      582        0                45
## 103 80.000       0                      898        0                25
## 104 42.000       0                     5209        0                30
## 107 55.000       0                      748        0                45
## 108 45.000       1                     1876        1                35
## 109 63.000       0                      936        0                38
## 110 45.000       0                      292        1                35
## 111 85.000       0                      129        0                60
## 112 55.000       0                       60        0                35
## 113 50.000       0                      369        1                25
## 115 60.000       1                      754        1                40
## 121 60.000       1                      737        0                60
## 125 60.000       0                      582        0                40
## 129 61.000       0                      248        0                30
## 130 53.000       1                      270        1                35
## 131 53.000       1                     1808        0                60
## 132 60.000       1                     1082        1                45
## 134 63.000       0                      193        0                60
## 135 81.000       0                     4540        0                35
## 136 75.000       0                      582        0                40
## 138 68.000       1                      646        0                25
## 140 50.000       0                     1548        0                30
## 141 80.000       0                      805        0                38
## 145 72.000       1                      943        0                25
## 146 50.000       0                      185        0                30
## 147 52.000       0                      132        0                30
## 148 64.000       0                     1610        0                60
## 149 75.000       1                      582        0                30
## 150 60.000       0                     2261        0                35
## 152 62.000       0                       30        1                60
## 153 50.000       0                      115        0                45
## 154 50.000       0                     1846        1                35
## 156 60.000       1                      231        1                25
## 158 50.000       0                      250        0                25
## 159 85.000       1                      910        0                50
## 160 59.000       1                      129        0                45
## 161 66.000       1                       72        0                40
## 162 45.000       1                      130        0                35
## 163 63.000       1                      582        0                40
## 165 45.000       0                     2442        1                30
## 167 53.000       0                      196        0                60
## 168 59.000       0                       66        1                20
## 171 51.000       1                      582        1                35
## 172 52.000       0                     3966        0                40
## 173 70.000       1                      171        0                60
## 174 50.000       1                      115        0                20
## 175 65.000       0                      198        1                35
## 176 60.000       1                       95        0                60
## 177 69.000       0                     1419        0                40
## 179 63.000       1                      122        1                60
## 180 55.000       0                      835        0                40
## 181 40.000       0                      478        1                30
## 182 59.000       1                      176        1                25
## 183 65.000       0                      395        1                25
## 184 75.000       0                       99        0                38
## 185 58.000       1                      145        0                25
## 186 60.667       1                      104        1                30
## 191 80.000       0                      582        1                35
## 193 50.000       1                      121        1                40
## 194 73.000       1                      231        1                30
## 195 45.000       0                      582        0                20
## 196 77.000       1                      418        0                45
## 200 60.000       0                     1211        1                35
## 201 63.000       1                     1767        0                45
## 202 45.000       0                      308        1                60
## 203 70.000       0                       97        0                60
## 204 60.000       0                       59        0                25
## 205 78.000       1                       64        0                40
## 208 85.000       0                      212        0                38
## 211 70.000       0                      212        1                17
## 212 50.000       0                      582        0                62
## 213 78.000       0                      224        0                50
## 215 65.000       1                      135        0                35
## 216 73.000       0                      582        0                35
## 219 68.000       1                     1021        1                35
## 221 73.000       0                      582        0                20
## 222 65.000       0                      118        0                50
## 223 42.000       1                       86        0                35
## 224 47.000       0                      582        0                25
## 225 58.000       0                      582        1                25
## 227 58.000       1                       57        0                25
## 228 55.000       1                     2794        0                35
## 232 70.000       0                       93        0                35
## 233 40.000       1                      129        0                35
## 234 53.000       1                      707        0                38
## 235 53.000       1                      582        0                45
## 236 77.000       1                      109        0                50
## 237 75.000       0                      119        0                50
## 238 70.000       0                      232        0                30
## 240 55.000       1                      180        0                45
## 242 65.000       0                      582        1                30
## 243 40.000       0                       90        0                35
## 245 54.000       0                      582        1                38
## 246 61.000       1                       80        1                38
## 247 55.000       0                     2017        0                25
## 248 64.000       0                      143        0                25
## 249 40.000       0                      624        0                35
## 253 50.000       0                      245        0                45
## 255 53.000       1                      446        0                60
## 256 52.000       1                      191        1                30
## 258 58.000       0                      132        1                38
## 259 45.000       1                       66        1                25
## 260 53.000       0                       56        0                50
## 261 55.000       0                       66        0                40
## 263 65.000       1                      258        1                25
## 265 61.000       0                      582        1                38
## 266 50.000       1                      298        0                35
## 267 55.000       0                     1199        0                20
## 268 56.000       1                      135        1                38
## 270 40.000       0                      582        1                35
## 271 44.000       0                      582        1                30
## 274 42.000       0                       64        0                40
## 275 60.000       1                      257        1                30
## 278 70.000       0                      582        1                38
## 281 70.000       0                     2695        1                40
## 282 70.000       0                      582        0                40
## 283 42.000       0                       64        0                30
## 284 65.000       0                     1688        0                38
## 285 50.000       1                       54        0                40
## 286 55.000       1                      170        1                40
## 287 60.000       0                      253        0                35
## 292 60.000       0                      320        0                35
## 293 52.000       0                      190        1                38
## 294 63.000       1                      103        1                35
## 295 62.000       0                       61        1                38
## 298 45.000       0                     2413        0                38
## 299 50.000       0                      196        0                45
##     high_blood_pressure platelets serum_creatinine serum_sodium sex smoking
## 1                     1    265000             1.90          130   1       0
## 2                     0    263358             1.10          136   1       0
## 3                     0    162000             1.30          129   1       1
## 4                     0    210000             1.90          137   1       0
## 6                     1    204000             2.10          132   1       1
## 7                     0    127000             1.20          137   1       0
## 8                     0    454000             1.10          131   1       1
## 10                    1    388000             9.40          133   1       1
## 11                    1    368000             4.00          131   1       1
## 12                    1    253000             0.90          140   1       1
## 13                    0    136000             1.10          137   1       0
## 14                    1    276000             1.10          137   1       0
## 16                    0     47000             1.30          136   1       0
## 17                    0    262000             0.90          140   1       0
## 18                    0    166000             0.80          127   1       0
## 23                    1    289000             0.90          140   1       1
## 24                    0    368000             0.80          135   1       0
## 26                    0    149000             1.90          144   1       1
## 28                    1    284000             1.30          136   1       1
## 29                    0    153000             5.80          134   1       0
## 30                    0    200000             1.20          132   1       1
## 31                    1    263358             1.83          134   1       0
## 32                    0    360000             3.00          132   1       0
## 35                    1    188000             1.00          140   1       0
## 36                    0    228000             3.50          134   1       0
## 37                    0    226000             1.00          134   1       0
## 39                    0    305000             2.30          137   1       0
## 41                    1    263358             1.83          134   1       1
## 43                    1    185000             1.20          139   1       1
## 44                    1    218000             1.00          134   1       0
## 46                    0    310000             1.90          135   1       1
## 47                    1    271000             0.90          130   1       0
## 48                    1    451000             0.60          138   1       1
## 49                    1    140000             4.40          133   1       0
## 51                    1    166000             1.00          138   1       0
## 56                    0    461000             2.00          132   1       0
## 57                    0    223000             2.70          138   1       1
## 58                    0    216000             0.60          138   1       1
## 59                    1    319000             1.10          136   1       1
## 60                    1    254000             1.30          136   1       1
## 61                    1    390000             1.00          139   1       0
## 63                    0    254000             1.10          139   1       1
## 64                    0    385000             1.00          145   1       0
## 66                    0    119000             2.90          127   1       1
## 68                    0    274000             1.00          140   1       1
## 70                    0    497000             1.83          135   1       0
## 71                    0    374000             0.80          140   1       1
## 72                    0    122000             0.90          139   1       1
## 73                    0    243000             1.00          132   1       1
## 74                    0    149000             1.30          137   1       1
## 75                    0    266000             1.20          134   1       1
## 76                    0    204000             0.70          139   1       1
## 78                    0    237000             1.20          140   1       0
## 79                    1    283000             0.60          131   1       1
## 82                    0    263358             1.18          137   1       1
## 84                    1    172000             1.80          133   1       0
## 86                    0    406000             0.70          140   1       0
## 87                    1    173000             1.10          137   1       0
## 88                    1    304000             0.80          140   1       0
## 89                    1    235000             0.70          139   1       0
## 90                    1    181000             1.10          144   1       0
## 91                    0    249000             0.80          136   1       1
## 92                    0    297000             1.00          133   1       0
## 94                    0    210000             1.70          135   1       0
## 96                    1    219000             1.00          141   1       0
## 97                    1    254000             1.30          134   1       0
## 101                   0    298000             1.10          141   1       0
## 102                   1    263358             1.18          137   1       0
## 103                   0    149000             1.10          144   1       1
## 104                   0    226000             1.00          140   1       1
## 107                   0    263000             1.30          137   1       0
## 108                   0    226000             0.90          138   1       0
## 109                   0    304000             1.10          133   1       1
## 110                   0    850000             1.30          142   1       1
## 111                   0    306000             1.20          132   1       1
## 112                   0    228000             1.20          135   1       1
## 113                   0    252000             1.60          136   1       0
## 115                   1    328000             1.20          126   1       0
## 121                   1    210000             1.50          135   1       1
## 125                   0    217000             3.70          134   1       0
## 129                   1    267000             0.70          136   1       1
## 130                   0    227000             3.40          145   1       0
## 131                   1    249000             0.70          138   1       1
## 132                   0    250000             6.10          131   1       0
## 134                   1    295000             1.30          145   1       1
## 135                   0    231000             1.18          137   1       1
## 136                   0    263358             1.18          137   1       0
## 138                   0    305000             2.10          130   1       0
## 140                   1    211000             0.80          138   1       0
## 141                   0    263358             1.10          134   1       0
## 145                   1    338000             1.70          139   1       1
## 146                   0    266000             0.70          141   1       1
## 147                   0    218000             0.70          136   1       1
## 148                   0    242000             1.00          137   1       0
## 149                   0    225000             1.83          134   1       0
## 150                   1    228000             0.90          136   1       0
## 152                   1    244000             0.90          139   1       0
## 153                   1    184000             0.90          134   1       1
## 154                   0    263358             1.18          137   1       1
## 156                   0    194000             1.70          140   1       0
## 158                   0    262000             1.00          136   1       1
## 159                   0    235000             1.30          134   1       0
## 160                   1    362000             1.10          139   1       1
## 161                   1    242000             1.20          134   1       0
## 162                   0    174000             0.80          139   1       1
## 163                   0    448000             0.90          137   1       1
## 165                   0    334000             1.10          139   1       0
## 167                   0    220000             0.70          133   1       1
## 168                   0     70000             2.40          134   1       0
## 171                   0    263358             1.50          136   1       1
## 172                   0    325000             0.90          140   1       1
## 173                   1    176000             1.10          145   1       1
## 174                   0    189000             0.80          139   1       0
## 175                   1    281000             0.90          137   1       1
## 176                   0    337000             1.00          138   1       1
## 177                   0    105000             1.00          135   1       1
## 179                   0    267000             1.20          145   1       0
## 180                   0    279000             0.70          140   1       1
## 181                   0    303000             0.90          136   1       0
## 182                   0    221000             1.00          136   1       1
## 183                   0    265000             1.20          136   1       1
## 184                   1    224000             2.50          134   1       0
## 185                   0    219000             1.20          137   1       1
## 186                   0    389000             1.50          136   1       0
## 191                   0    350000             2.10          134   1       0
## 193                   0    260000             0.70          130   1       0
## 194                   0    160000             1.18          142   1       1
## 195                   1    126000             1.60          135   1       0
## 196                   0    223000             1.80          145   1       0
## 200                   0    263358             1.80          113   1       1
## 201                   0     73000             0.70          137   1       0
## 202                   1    377000             1.00          136   1       0
## 203                   1    220000             0.90          138   1       0
## 204                   1    212000             3.50          136   1       1
## 205                   0    277000             0.70          137   1       1
## 208                   0    186000             0.90          136   1       0
## 211                   1    389000             1.00          136   1       1
## 212                   1    147000             0.80          140   1       1
## 213                   0    481000             1.40          138   1       1
## 215                   1    290000             0.80          134   1       0
## 216                   1    203000             1.30          134   1       0
## 219                   0    271000             1.10          134   1       0
## 221                   0    263358             1.83          134   1       0
## 222                   0    194000             1.10          145   1       1
## 223                   0    365000             1.10          139   1       1
## 224                   0    130000             0.80          134   1       0
## 225                   0    504000             1.00          138   1       0
## 227                   0    189000             1.30          132   1       1
## 228                   1    141000             1.00          140   1       0
## 232                   0    185000             1.10          134   1       1
## 233                   0    255000             0.90          137   1       0
## 234                   0    330000             1.40          137   1       1
## 235                   0    305000             1.10          137   1       1
## 236                   1    406000             1.10          137   1       0
## 237                   1    248000             1.10          148   1       0
## 238                   0    173000             1.20          132   1       0
## 240                   0    263358             1.18          137   1       1
## 242                   0    249000             1.30          136   1       1
## 243                   0    255000             1.10          136   1       1
## 245                   0    264000             1.80          134   1       0
## 246                   0    282000             1.40          137   1       0
## 247                   0    314000             1.10          138   1       0
## 248                   0    246000             2.40          135   1       0
## 249                   0    301000             1.00          142   1       1
## 253                   1    274000             1.00          133   1       0
## 255                   1    263358             1.00          139   1       0
## 256                   1    334000             1.00          142   1       1
## 258                   1    253000             1.00          139   1       0
## 259                   0    233000             0.80          135   1       0
## 260                   0    308000             0.70          135   1       1
## 261                   0    203000             1.00          138   1       0
## 263                   0    198000             1.40          129   1       0
## 265                   0    147000             1.20          141   1       0
## 266                   0    362000             0.90          140   1       1
## 267                   0    263358             1.83          134   1       1
## 268                   0    133000             1.70          140   1       0
## 270                   0    222000             1.00          132   1       0
## 271                   1    263358             1.60          130   1       1
## 274                   0    189000             0.70          140   1       0
## 275                   0    150000             1.00          137   1       1
## 278                   0     25100             1.10          140   1       0
## 281                   0    241000             1.00          137   1       0
## 282                   0     51000             2.70          136   1       1
## 283                   0    215000             3.80          128   1       1
## 284                   0    263358             1.10          138   1       1
## 285                   0    279000             0.80          141   1       0
## 286                   0    336000             1.20          135   1       0
## 287                   0    279000             1.70          140   1       0
## 292                   0    133000             1.40          139   1       0
## 293                   0    382000             1.00          140   1       1
## 294                   0    179000             0.90          136   1       1
## 295                   1    155000             1.10          143   1       1
## 298                   0    140000             1.40          140   1       1
## 299                   0    395000             1.60          136   1       1
# Count the occurrences of each category in categorical variables
anaemia_counts <- table(data1$anaemia)
high_bp_counts <- table(data1$high_blood_pressure)
sex_counts <- table(data1$sex)
smoking_counts <- table(data1$smoking)
diabetes_counts <- table(data1$diabetes)

# Display the counts
print(anaemia_counts)
## 
##   0   1 
## 170 129
print(high_bp_counts)
## 
##   0   1 
## 194 105
print(sex_counts)
## 
##   0   1 
## 105 194
print(smoking_counts)
## 
##   0   1 
## 203  96
print(diabetes_counts)
## 
##   0   1 
## 174 125
barplot(anaemia_counts, main = "Frequency of Anaemia", xlab = "Anaemia (0 = Yes, 1 = No)", ylab = "Frequency")

barplot(high_bp_counts, main = "Frequency of High Blood Pressure", xlab = "High Blood Pressure (0 = Yes, 1 = No)", ylab = "Frequency")

barplot(smoking_counts, main = "Frequency of Smoking", xlab = "Smoking (0 = Yes, 1 = No)", ylab = "Frequency")

# Create pie charts for binary categorical variables
pie(diabetes_counts, main = "Distribution of Diabetes Risk (0 = Yes, 1 = No)")

#Linear Regression Model 
# Implement a multiple linear regression model using R Studio.
#Train the model on the training data
linearMod <- lm(serum_creatinine ~ serum_sodium+ejection_fraction , data = data1)
####build linear regression model on full data
print(linearMod)
## 
## Call:
## lm(formula = serum_creatinine ~ serum_sodium + ejection_fraction, 
##     data = data1)
## 
## Coefficients:
##       (Intercept)       serum_sodium  ejection_fraction  
##          7.503218          -0.045268           0.001981
summary(linearMod)
## 
## Call:
## lm(formula = serum_creatinine ~ serum_sodium + ejection_fraction, 
##     data = data1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.0484 -0.4485 -0.2403  0.0637  7.8481 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        7.503218   1.833586   4.092 5.52e-05 ***
## serum_sodium      -0.045268   0.013590  -3.331 0.000975 ***
## ejection_fraction  0.001981   0.005067   0.391 0.696117    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.019 on 296 degrees of freedom
## Multiple R-squared:  0.03625,    Adjusted R-squared:  0.02974 
## F-statistic: 5.568 on 2 and 296 DF,  p-value: 0.004231
# Set the CRAN mirror location programmatically
options(repos = "https://cran.rstudio.com")

# Install and load the caTools package
install.packages("caTools")
## Installing package into 'C:/Users/ravindu yasas/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'caTools' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\ravindu yasas\AppData\Local\Temp\RtmpsZuQla\downloaded_packages
library(caTools)



# Display the first few rows of the dataset
head(data1)
##   age anaemia creatinine_phosphokinase diabetes ejection_fraction
## 1  75       0                      582        0                20
## 2  55       0                     7861        0                38
## 3  65       0                      146        0                20
## 4  50       1                      111        0                20
## 5  65       1                      160        1                20
## 6  90       1                       47        0                40
##   high_blood_pressure platelets serum_creatinine serum_sodium sex smoking
## 1                   1    265000              1.9          130   1       0
## 2                   0    263358              1.1          136   1       0
## 3                   0    162000              1.3          129   1       1
## 4                   0    210000              1.9          137   1       0
## 5                   0    327000              2.7          116   0       0
## 6                   1    204000              2.1          132   1       1
# Split the data into training and testing sets
ind <- sample.split(Y = data1$age, SplitRatio = 0.8)

# Subset into training data
train <- data1[ind, ]

# Subset into testing data
test <- data1[!ind, ]

# Check the dimensions of the training and testing sets
dim(train)
## [1] 244  11
dim(test)
## [1] 55 11
# Fit the multiple linear regression model on the training data
model <- lm(age ~ ., data = train)

# Make predictions on the test data
predictions <- predict(model, newdata = test)

# Calculate Mean Square Error (MSE)
mse <- mean((test$age - predictions)^2)

# Calculate Root Mean Squared Error (RMSE)
rmse <- sqrt(mse)

# Calculate R-Squared
rsquared <- summary(model)$r.squared

# Create a dataframe with actual and predicted ejection_fraction values
comparison <- data.frame(Actual = test$ejection_fraction, Predicted = predictions)

# Display the first few rows of the comparison dataframe
head(comparison)
##    Actual Predicted
## 12     25  62.35362
## 16     50  62.16032
## 22     30  61.45255
## 29     38  68.99561
## 33     35  61.01248
## 35     50  61.50065