# Import dataset CKD
CKD <- read.table(
  file.choose(),
  header = TRUE,
  sep = "\t",
  dec = ",",
  stringsAsFactors = FALSE
)

# Melihat data
CKD
##    Age_of_the_patient Blood_pressure_.mm.Hg. Red_blood_cells_in_urine
## 1                  54                    167                   normal
## 2                  42                    127                   normal
## 3                  38                    148                 abnormal
## 4                  67                    174                   normal
## 5                  67                    100                   normal
## 6                  42                    138                 abnormal
## 7                  84                    104                   normal
## 8                  49                    113                   normal
## 9                  65                    126                 abnormal
## 10                 25                    174                 abnormal
## 11                 89                    113                 abnormal
## 12                 90                    131                   normal
## 13                 27                    110                 abnormal
## 14                 48                    138                   normal
## 15                 55                     90                 abnormal
## 16                 51                    116                 abnormal
## 17                 60                     95                 abnormal
## 18                 31                    122                 abnormal
## 19                 90                    130                   normal
## 20                 76                    179                   normal
## 21                 40                     91                   normal
## 22                 65                    106                   normal
## 23                 38                    121                   normal
## 24                 90                     80                 abnormal
## 25                 25                    119                   normal
## 26                 74                    127                   normal
## 27                 80                    110                 abnormal
## 28                 35                     82                 abnormal
## 29                 73                    173                 abnormal
## 30                 54                    150                   normal
## 31                 41                    174                 abnormal
## 32                 65                     82                   normal
## 33                 83                    158                 abnormal
## 34                 75                    122                   normal
## 35                 56                    178                   normal
## 36                 59                    122                 abnormal
## 37                 89                    135                 abnormal
## 38                 52                    177                   normal
## 39                 67                    141                 abnormal
## 40                 54                    175                   normal
## 41                 37                    139                   normal
## 42                 37                    149                   normal
## 43                 53                    121                 abnormal
## 44                 68                    159                 abnormal
## 45                 37                    174                   normal
## 46                 68                    130                 abnormal
## 47                 27                     81                 abnormal
## 48                 89                    128                 abnormal
## 49                 38                    170                   normal
## 50                 26                    126                 abnormal
##    Bacteria_in_urine Blood_urea_.mg.dl. Serum_creatinine_.mg.dl.
## 1        not_present         169.101369                     7.55
## 2            present         183.223479                    13.37
## 3        not_present         193.141665                     9.49
## 4        not_present         197.188600                     3.01
## 5            present          27.308196                     8.73
## 6            present         169.656648                     6.92
## 7            present         198.725767                    10.15
## 8            present         108.353368                     5.84
## 9            present         174.729867                    10.77
## 10       not_present          84.970714                     8.35
## 11       not_present          35.942011                     5.98
## 12       not_present          81.313035                     2.12
## 13       not_present          83.172007                    12.32
## 14       not_present         138.709486                     1.37
## 15       not_present          58.010296                    11.01
## 16           present          31.452576                    10.33
## 17       not_present          40.847352                     5.27
## 18           present         143.249229                    10.36
## 19       not_present          51.308044                    12.48
## 20       not_present          46.533183                    12.52
## 21           present         189.114948                     8.09
## 22       not_present          88.949851                     5.06
## 23       not_present         153.430176                     0.80
## 24           present          35.636823                    13.71
## 25       not_present         110.202151                    11.91
## 26           present          46.072322                     4.85
## 27           present          61.629109                     7.12
## 28           present         186.082297                    10.27
## 29       not_present          16.864388                    10.19
## 30           present         148.142526                     4.90
## 31       not_present         141.177815                     9.64
## 32           present          31.784104                    11.39
## 33           present          87.966135                     2.59
## 34       not_present          89.935422                     0.97
## 35           present         105.299708                     4.32
## 36           present         130.689826                     2.60
## 37           present          49.525194                     5.81
## 38       not_present          40.788617                     6.41
## 39           present          89.437635                    12.20
## 40       not_present         110.888999                     0.76
## 41           present          36.337927                    10.18
## 42       not_present          24.931325                     1.33
## 43       not_present         182.286759                     1.75
## 44       not_present         110.198402                    11.77
## 45       not_present         149.083831                    14.58
## 46           present         132.146330                     6.07
## 47           present         143.654183                     7.72
## 48       not_present          26.139680                     8.31
## 49           present           8.685189                     9.18
## 50       not_present         119.298778                     8.25
##    Hemoglobin_level_.gms. Hypertension_.yes.no. Diabetes_mellitus_.yes.no.
## 1                    11.8                   yes                        yes
## 2                     8.2                    no                        yes
## 3                    10.1                    no                         no
## 4                    16.1                    no                         no
## 5                     7.7                    no                         no
## 6                     8.8                   yes                         no
## 7                     7.2                   yes                         no
## 8                    12.6                    no                        yes
## 9                     7.5                   yes                         no
## 10                   15.6                    no                         no
## 11                   15.6                    no                        yes
## 12                    9.6                    no                         no
## 13                   11.6                    no                         no
## 14                    9.2                    no                         no
## 15                    6.8                    no                        yes
## 16                   11.8                    no                        yes
## 17                   16.7                    no                         no
## 18                   17.5                    no                         no
## 19                   10.3                    no                         no
## 20                    8.0                   yes                        yes
## 21                   11.0                   yes                         no
## 22                   12.8                    no                        yes
## 23                   16.9                   yes                         no
## 24                    6.8                    no                        yes
## 25                   14.0                    no                         no
## 26                   10.5                    no                        yes
## 27                   14.1                   yes                         no
## 28                    6.0                    no                        yes
## 29                   13.5                   yes                         no
## 30                   16.9                    no                         no
## 31                   16.4                    no                        yes
## 32                   13.8                   yes                         no
## 33                    9.9                    no                         no
## 34                    8.7                    no                         no
## 35                   16.6                   yes                        yes
## 36                   17.3                    no                        yes
## 37                    9.5                    no                        yes
## 38                   11.9                   yes                        yes
## 39                   12.8                    no                        yes
## 40                   16.8                   yes                        yes
## 41                    9.8                    no                        yes
## 42                    8.2                    no                        yes
## 43                   13.1                   yes                        yes
## 44                   11.1                    no                        yes
## 45                   13.9                   yes                         no
## 46                   10.3                   yes                         no
## 47                   13.4                    no                         no
## 48                   13.3                    no                        yes
## 49                   16.3                   yes                         no
## 50                   16.3                   yes                         no
##    Coronary_artery_disease_.yes.no. Anemia_.yes.no.
## 1                                no              no
## 2                                no             yes
## 3                               yes              no
## 4                                no             yes
## 5                               yes             yes
## 6                                no             yes
## 7                                no             yes
## 8                               yes             yes
## 9                               yes             yes
## 10                              yes             yes
## 11                               no              no
## 12                              yes             yes
## 13                               no             yes
## 14                               no             yes
## 15                               no             yes
## 16                               no             yes
## 17                              yes             yes
## 18                              yes              no
## 19                               no             yes
## 20                              yes             yes
## 21                               no             yes
## 22                              yes              no
## 23                              yes             yes
## 24                              yes              no
## 25                               no             yes
## 26                               no             yes
## 27                              yes              no
## 28                               no             yes
## 29                               no             yes
## 30                              yes             yes
## 31                              yes              no
## 32                               no             yes
## 33                               no             yes
## 34                               no             yes
## 35                              yes             yes
## 36                              yes             yes
## 37                               no              no
## 38                               no             yes
## 39                               no             yes
## 40                               no              no
## 41                              yes              no
## 42                               no              no
## 43                               no              no
## 44                              yes              no
## 45                              yes             yes
## 46                               no              no
## 47                              yes              no
## 48                              yes             yes
## 49                               no              no
## 50                              yes             yes
##    Urine_protein.to.creatinine_ratio Urine_output_.ml.day. Cholesterol_level
## 1                               2.51                  1397               152
## 2                               4.27                  1632               242
## 3                               1.56                   889               103
## 4                               1.23                   893               149
## 5                               2.61                  1212               182
## 6                               2.42                  2720               260
## 7                               1.67                  2859               134
## 8                               0.41                  2076               229
## 9                               2.80                   862               276
## 10                              0.46                   663               206
## 11                              2.53                   461               113
## 12                              2.78                  1615               166
## 13                              2.16                  2444               158
## 14                              2.67                  1323               165
## 15                              4.49                  1682               117
## 16                              3.73                  1100               290
## 17                              4.33                   864               135
## 18                              0.54                  2058               256
## 19                              1.05                   948               292
## 20                              3.41                   308               205
## 21                              0.46                  2333               100
## 22                              0.94                  2899               246
## 23                              1.23                  1748               256
## 24                              0.24                  2650               110
## 25                              3.65                   841               228
## 26                              3.63                  1072               222
## 27                              2.10                  1845               135
## 28                              1.80                  2865               257
## 29                              0.75                  1733               296
## 30                              3.18                  2933               287
## 31                              2.79                  2118               159
## 32                              2.04                  2300               259
## 33                              1.35                  2911               298
## 34                              4.36                  2269               107
## 35                              3.42                   997               294
## 36                              3.31                  1922               255
## 37                              3.78                  1916               136
## 38                              2.34                  2661               267
## 39                              2.71                  2603               266
## 40                              0.58                   787               253
## 41                              3.10                   852               225
## 42                              4.21                   547               114
## 43                              1.71                  2766               151
## 44                              0.35                  1580               113
## 45                              2.10                  2873               260
## 46                              2.31                  2321               257
## 47                              0.64                   926               161
## 48                              2.10                   852               102
## 49                              2.60                  2684               104
## 50                              3.61                  2166               228
##    Family_history_of_chronic_kidney_disease Smoking_status
## 1                                        no            yes
## 2                                       yes             no
## 3                                        no             no
## 4                                        no            yes
## 5                                       yes            yes
## 6                                        no            yes
## 7                                       yes             no
## 8                                        no            yes
## 9                                        no             no
## 10                                       no             no
## 11                                       no             no
## 12                                       no             no
## 13                                       no             no
## 14                                       no            yes
## 15                                       no             no
## 16                                       no             no
## 17                                       no            yes
## 18                                      yes            yes
## 19                                      yes             no
## 20                                      yes            yes
## 21                                       no             no
## 22                                       no            yes
## 23                                      yes             no
## 24                                       no            yes
## 25                                       no            yes
## 26                                      yes            yes
## 27                                       no             no
## 28                                       no            yes
## 29                                      yes             no
## 30                                       no             no
## 31                                      yes            yes
## 32                                       no             no
## 33                                      yes            yes
## 34                                      yes             no
## 35                                      yes             no
## 36                                       no            yes
## 37                                      yes             no
## 38                                       no             no
## 39                                      yes            yes
## 40                                       no             no
## 41                                       no            yes
## 42                                       no            yes
## 43                                      yes             no
## 44                                       no             no
## 45                                       no            yes
## 46                                       no            yes
## 47                                       no            yes
## 48                                      yes            yes
## 49                                       no             no
## 50                                       no             no
##    Body_Mass_Index_.BMI. Physical_activity_level CKD_Status
## 1                   25.3                     low        Yes
## 2                   20.6                moderate        Yes
## 3                   38.4                    high         No
## 4                   17.6                    high        Yes
## 5                   37.2                moderate         No
## 6                   23.5                moderate         No
## 7                   23.0                moderate        Yes
## 8                   30.6                     low        Yes
## 9                   16.8                    high         No
## 10                  28.5                     low        Yes
## 11                  34.7                     low        Yes
## 12                  27.4                     low         No
## 13                  23.2                     low        Yes
## 14                  23.3                     low         No
## 15                  32.7                moderate         No
## 16                  16.6                     low         No
## 17                  19.7                moderate         No
## 18                  32.4                    high         No
## 19                  34.7                     low         No
## 20                  38.1                moderate         No
## 21                  32.0                    high         No
## 22                  38.1                moderate         No
## 23                  19.8                moderate        Yes
## 24                  38.9                    high        Yes
## 25                  18.1                    high         No
## 26                  31.7                    high         No
## 27                  18.5                     low         No
## 28                  24.9                moderate         No
## 29                  27.0                    high        Yes
## 30                  18.6                     low         No
## 31                  32.0                moderate        Yes
## 32                  18.0                moderate        Yes
## 33                  23.1                moderate         No
## 34                  31.5                     low         No
## 35                  15.2                    high        Yes
## 36                  37.2                    high         No
## 37                  29.6                     low         No
## 38                  16.6                    high         No
## 39                  18.0                moderate         No
## 40                  16.7                moderate         No
## 41                  29.0                     low        Yes
## 42                  16.4                    high         No
## 43                  17.5                    high         No
## 44                  37.9                     low        Yes
## 45                  30.4                moderate        Yes
## 46                  20.5                    high         No
## 47                  21.0                moderate         No
## 48                  31.9                     low        Yes
## 49                  22.9                    high         No
## 50                  39.0                    high        Yes
# Ukuran dataset
dim(CKD)
## [1] 50 19
# Jumlah baris
nrow(CKD)
## [1] 50
# Jumlah kolom
ncol(CKD)
## [1] 19
# Nama variabel
names(CKD)
##  [1] "Age_of_the_patient"                      
##  [2] "Blood_pressure_.mm.Hg."                  
##  [3] "Red_blood_cells_in_urine"                
##  [4] "Bacteria_in_urine"                       
##  [5] "Blood_urea_.mg.dl."                      
##  [6] "Serum_creatinine_.mg.dl."                
##  [7] "Hemoglobin_level_.gms."                  
##  [8] "Hypertension_.yes.no."                   
##  [9] "Diabetes_mellitus_.yes.no."              
## [10] "Coronary_artery_disease_.yes.no."        
## [11] "Anemia_.yes.no."                         
## [12] "Urine_protein.to.creatinine_ratio"       
## [13] "Urine_output_.ml.day."                   
## [14] "Cholesterol_level"                       
## [15] "Family_history_of_chronic_kidney_disease"
## [16] "Smoking_status"                          
## [17] "Body_Mass_Index_.BMI."                   
## [18] "Physical_activity_level"                 
## [19] "CKD_Status"
# Struktur data
str(CKD)
## 'data.frame':    50 obs. of  19 variables:
##  $ Age_of_the_patient                      : int  54 42 38 67 67 42 84 49 65 25 ...
##  $ Blood_pressure_.mm.Hg.                  : int  167 127 148 174 100 138 104 113 126 174 ...
##  $ Red_blood_cells_in_urine                : chr  "normal" "normal" "abnormal" "normal" ...
##  $ Bacteria_in_urine                       : chr  "not_present" "present" "not_present" "not_present" ...
##  $ Blood_urea_.mg.dl.                      : num  169.1 183.2 193.1 197.2 27.3 ...
##  $ Serum_creatinine_.mg.dl.                : num  7.55 13.37 9.49 3.01 8.73 ...
##  $ Hemoglobin_level_.gms.                  : num  11.8 8.2 10.1 16.1 7.7 8.8 7.2 12.6 7.5 15.6 ...
##  $ Hypertension_.yes.no.                   : chr  "yes" "no" "no" "no" ...
##  $ Diabetes_mellitus_.yes.no.              : chr  "yes" "yes" "no" "no" ...
##  $ Coronary_artery_disease_.yes.no.        : chr  "no" "no" "yes" "no" ...
##  $ Anemia_.yes.no.                         : chr  "no" "yes" "no" "yes" ...
##  $ Urine_protein.to.creatinine_ratio       : num  2.51 4.27 1.56 1.23 2.61 2.42 1.67 0.41 2.8 0.46 ...
##  $ Urine_output_.ml.day.                   : int  1397 1632 889 893 1212 2720 2859 2076 862 663 ...
##  $ Cholesterol_level                       : int  152 242 103 149 182 260 134 229 276 206 ...
##  $ Family_history_of_chronic_kidney_disease: chr  "no" "yes" "no" "no" ...
##  $ Smoking_status                          : chr  "yes" "no" "no" "yes" ...
##  $ Body_Mass_Index_.BMI.                   : num  25.3 20.6 38.4 17.6 37.2 23.5 23 30.6 16.8 28.5 ...
##  $ Physical_activity_level                 : chr  "low" "moderate" "high" "high" ...
##  $ CKD_Status                              : chr  "Yes" "Yes" "No" "Yes" ...
# Ringkasan data
summary(CKD)
##  Age_of_the_patient Blood_pressure_.mm.Hg. Red_blood_cells_in_urine
##  Min.   :25.00      Min.   : 80.0          Length:50               
##  1st Qu.:38.50      1st Qu.:113.0          Class :character        
##  Median :54.50      Median :127.5          Mode  :character        
##  Mean   :56.90      Mean   :131.9                                  
##  3rd Qu.:71.75      3rd Qu.:156.0                                  
##  Max.   :90.00      Max.   :179.0                                  
##  Bacteria_in_urine  Blood_urea_.mg.dl. Serum_creatinine_.mg.dl.
##  Length:50          Min.   :  8.685    Min.   : 0.760          
##  Class :character   1st Qu.: 46.188    1st Qu.: 4.940          
##  Mode  :character   Median : 97.618    Median : 8.170          
##                     Mean   :101.266    Mean   : 7.613          
##                     3rd Qu.:147.020    3rd Qu.:10.352          
##                     Max.   :198.726    Max.   :14.580          
##  Hemoglobin_level_.gms. Hypertension_.yes.no. Diabetes_mellitus_.yes.no.
##  Min.   : 6.000         Length:50             Length:50                 
##  1st Qu.: 9.525         Class :character      Class :character          
##  Median :11.850         Mode  :character      Mode  :character          
##  Mean   :12.092                                                         
##  3rd Qu.:15.225                                                         
##  Max.   :17.500                                                         
##  Coronary_artery_disease_.yes.no. Anemia_.yes.no.   
##  Length:50                        Length:50         
##  Class :character                 Class :character  
##  Mode  :character                 Mode  :character  
##                                                     
##                                                     
##                                                     
##  Urine_protein.to.creatinine_ratio Urine_output_.ml.day. Cholesterol_level
##  Min.   :0.240                     Min.   : 308.0        Min.   :100.0    
##  1st Qu.:1.260                     1st Qu.: 931.5        1st Qu.:135.2    
##  Median :2.380                     Median :1740.5        Median :214.0    
##  Mean   :2.300                     Mean   :1739.5        Mean   :199.5    
##  3rd Qu.:3.277                     3rd Qu.:2416.2        3rd Qu.:257.0    
##  Max.   :4.490                     Max.   :2933.0        Max.   :298.0    
##  Family_history_of_chronic_kidney_disease Smoking_status    
##  Length:50                                Length:50         
##  Class :character                         Class :character  
##  Mode  :character                         Mode  :character  
##                                                             
##                                                             
##                                                             
##  Body_Mass_Index_.BMI. Physical_activity_level  CKD_Status       
##  Min.   :15.20         Length:50               Length:50         
##  1st Qu.:18.88         Class :character        Class :character  
##  Median :25.10         Mode  :character        Mode  :character  
##  Mean   :26.33                                                   
##  3rd Qu.:32.00                                                   
##  Max.   :39.00
#Mengubah Variabel Kategorik Menjadi Factor
names(CKD) <- make.names(names(CKD))
names(CKD)
##  [1] "Age_of_the_patient"                      
##  [2] "Blood_pressure_.mm.Hg."                  
##  [3] "Red_blood_cells_in_urine"                
##  [4] "Bacteria_in_urine"                       
##  [5] "Blood_urea_.mg.dl."                      
##  [6] "Serum_creatinine_.mg.dl."                
##  [7] "Hemoglobin_level_.gms."                  
##  [8] "Hypertension_.yes.no."                   
##  [9] "Diabetes_mellitus_.yes.no."              
## [10] "Coronary_artery_disease_.yes.no."        
## [11] "Anemia_.yes.no."                         
## [12] "Urine_protein.to.creatinine_ratio"       
## [13] "Urine_output_.ml.day."                   
## [14] "Cholesterol_level"                       
## [15] "Family_history_of_chronic_kidney_disease"
## [16] "Smoking_status"                          
## [17] "Body_Mass_Index_.BMI."                   
## [18] "Physical_activity_level"                 
## [19] "CKD_Status"
CKD$Red_blood_cells_in_urine <- factor(CKD$Red_blood_cells_in_urine)
CKD$Bacteria_in_urine <- factor(CKD$Bacteria_in_urine)
CKD$Hypertension_.yes.no. <- factor(CKD$Hypertension_.yes.no.)
kategori <- c(
  "Red_blood_cells_in_urine",
  "Bacteria_in_urine",
  "Hypertension_.yes.no.",
  "Diabetes_mellitus_.yes.no.",
  "Coronary_artery_disease_.yes.no.",
  "Anemia_.yes.no.",
  "Family_history_of_chronic_kidney_disease",
  "Smoking_status",
  "Physical_activity_level",
  "CKD_Status"
)

CKD[kategori] <- lapply(CKD[kategori], factor)
library(ggplot2)

#Distribusi Status CKD
ggplot(CKD, aes(x = CKD_Status)) +
  geom_bar() +
  labs(
    title = "Distribusi Status Chronic Kidney Disease",
    x = "Status CKD",
    y = "Jumlah Pasien"
  ) +
  theme_minimal()

#Histogram Usia
ggplot(CKD, aes(x = Age_of_the_patient)) +
  geom_histogram(
    bins = 10,
    fill = "steelblue",
    color = "black"
  ) +
  labs(
    title = "Distribusi Usia Pasien",
    x = "Usia",
    y = "Frekuensi"
  ) +
  theme_minimal()

#Usia berdasarkan Status CKD
ggplot(CKD, aes(x = CKD_Status, y = Age_of_the_patient)) +
  geom_boxplot() +
  labs(
    title = "Distribusi Usia Berdasarkan Status CKD",
    x = "Status CKD",
    y = "Usia"
  ) +
  theme_minimal()

#Serum Creatinine
ggplot(CKD, aes(x = Serum_creatinine_.mg.dl.)) +
  geom_histogram(
    bins = 10,
    fill = "orange",
    color = "black"
  ) +
  labs(
    title = "Distribusi Serum Creatinine",
    x = "Serum Creatinine (mg/dl)",
    y = "Frekuensi"
  ) +
  theme_minimal()

#Blood Urea
ggplot(CKD, aes(x = Blood_urea_.mg.dl.)) +
  geom_histogram(
    bins = 10,
    fill = "skyblue",
    color = "black"
  ) +
  labs(
    title = "Distribusi Blood Urea",
    x = "Blood Urea (mg/dl)",
    y = "Frekuensi"
  ) +
  theme_minimal()

#BMI Berdasarkan Status CKD
ggplot(CKD, aes(x = CKD_Status, y = Body_Mass_Index_.BMI.)) +
  geom_boxplot() +
  labs(
    title = "BMI Berdasarkan Status CKD",
    x = "Status CKD",
    y = "BMI"
  ) +
  theme_minimal()

#Hubungan Blood Urea dan Serum Creatinine

cor(
  CKD$Blood_urea_.mg.dl.,
  CKD$Serum_creatinine_.mg.dl.,
  method = "pearson"
)
## [1] -0.08488131
ggplot(
  CKD,
  aes(
    x = Blood_urea_.mg.dl.,
    y = Serum_creatinine_.mg.dl.,
    color = CKD_Status
  )
) +
  geom_point(size = 3) +
  labs(
    title = "Hubungan Blood Urea dan Serum Creatinine",
    x = "Blood Urea (mg/dl)",
    y = "Serum Creatinine (mg/dl)",
    color = "Status CKD"
  ) +
  theme_minimal()

#Faktor Kategorik
#Hipertensi
ggplot(CKD, aes(x = Hypertension_.yes.no.)) +
  geom_bar() +
  labs(
    title = "Distribusi Hipertensi",
    x = "Hipertensi",
    y = "Jumlah"
  ) +
  theme_minimal()

#Diabetes
ggplot(CKD, aes(x = Diabetes_mellitus_.yes.no.)) +
  geom_bar() +
  labs(
    title = "Distribusi Diabetes Mellitus",
    x = "Diabetes Mellitus",
    y = "Jumlah"
  ) +
  theme_minimal()

#Anemia
ggplot(CKD, aes(x = Anemia_.yes.no.)) +
  geom_bar() +
  labs(
    title = "Distribusi Anemia",
    x = "Anemia",
    y = "Jumlah"
  ) +
  theme_minimal()

#Analisis Statistik Deskriptif
summary(
  CKD[, c(
    "Age_of_the_patient",
    "Blood_pressure_.mm.Hg.",
    "Blood_urea_.mg.dl.",
    "Serum_creatinine_.mg.dl.",
    "Hemoglobin_level_.gms.",
    "Urine_protein.to.creatinine_ratio",
    "Urine_output_.ml.day.",
    "Cholesterol_level",
    "Body_Mass_Index_.BMI."
  )]
)
##  Age_of_the_patient Blood_pressure_.mm.Hg. Blood_urea_.mg.dl.
##  Min.   :25.00      Min.   : 80.0          Min.   :  8.685   
##  1st Qu.:38.50      1st Qu.:113.0          1st Qu.: 46.188   
##  Median :54.50      Median :127.5          Median : 97.618   
##  Mean   :56.90      Mean   :131.9          Mean   :101.266   
##  3rd Qu.:71.75      3rd Qu.:156.0          3rd Qu.:147.020   
##  Max.   :90.00      Max.   :179.0          Max.   :198.726   
##  Serum_creatinine_.mg.dl. Hemoglobin_level_.gms.
##  Min.   : 0.760           Min.   : 6.000        
##  1st Qu.: 4.940           1st Qu.: 9.525        
##  Median : 8.170           Median :11.850        
##  Mean   : 7.613           Mean   :12.092        
##  3rd Qu.:10.352           3rd Qu.:15.225        
##  Max.   :14.580           Max.   :17.500        
##  Urine_protein.to.creatinine_ratio Urine_output_.ml.day. Cholesterol_level
##  Min.   :0.240                     Min.   : 308.0        Min.   :100.0    
##  1st Qu.:1.260                     1st Qu.: 931.5        1st Qu.:135.2    
##  Median :2.380                     Median :1740.5        Median :214.0    
##  Mean   :2.300                     Mean   :1739.5        Mean   :199.5    
##  3rd Qu.:3.277                     3rd Qu.:2416.2        3rd Qu.:257.0    
##  Max.   :4.490                     Max.   :2933.0        Max.   :298.0    
##  Body_Mass_Index_.BMI.
##  Min.   :15.20        
##  1st Qu.:18.88        
##  Median :25.10        
##  Mean   :26.33        
##  3rd Qu.:32.00        
##  Max.   :39.00
#Statistik Deskriptif Variabel Kategorik
table(CKD$CKD_Status)
## 
##  No Yes 
##  31  19
prop.table(table(CKD$CKD_Status)) * 100
## 
##  No Yes 
##  62  38
lapply(CKD[kategori], table)
## $Red_blood_cells_in_urine
## 
## abnormal   normal 
##       25       25 
## 
## $Bacteria_in_urine
## 
## not_present     present 
##          26          24 
## 
## $Hypertension_.yes.no.
## 
##  no yes 
##  32  18 
## 
## $Diabetes_mellitus_.yes.no.
## 
##  no yes 
##  27  23 
## 
## $Coronary_artery_disease_.yes.no.
## 
##  no yes 
##  27  23 
## 
## $Anemia_.yes.no.
## 
##  no yes 
##  17  33 
## 
## $Family_history_of_chronic_kidney_disease
## 
##  no yes 
##  33  17 
## 
## $Smoking_status
## 
##  no yes 
##  26  24 
## 
## $Physical_activity_level
## 
##     high      low moderate 
##       17       16       17 
## 
## $CKD_Status
## 
##  No Yes 
##  31  19
#Menentukan DIstribusi Data
ggplot(CKD, aes(x = Age_of_the_patient)) +
  geom_histogram(bins = 10) +
  theme_minimal()

ggplot(CKD, aes(sample = Age_of_the_patient)) +
  stat_qq() +
  stat_qq_line() +
  theme_minimal()

#Uji Normalitas Shapiro-Wilk
numerik <- CKD[, sapply(CKD, is.numeric)]

hasil_normalitas <- data.frame(
  Variabel = names(numerik),
  P_Value = sapply(numerik, function(x) {
    shapiro.test(x)$p.value
  })
)

hasil_normalitas
##                                                            Variabel
## Age_of_the_patient                               Age_of_the_patient
## Blood_pressure_.mm.Hg.                       Blood_pressure_.mm.Hg.
## Blood_urea_.mg.dl.                               Blood_urea_.mg.dl.
## Serum_creatinine_.mg.dl.                   Serum_creatinine_.mg.dl.
## Hemoglobin_level_.gms.                       Hemoglobin_level_.gms.
## Urine_protein.to.creatinine_ratio Urine_protein.to.creatinine_ratio
## Urine_output_.ml.day.                         Urine_output_.ml.day.
## Cholesterol_level                                 Cholesterol_level
## Body_Mass_Index_.BMI.                         Body_Mass_Index_.BMI.
##                                        P_Value
## Age_of_the_patient                0.0271433612
## Blood_pressure_.mm.Hg.            0.0242657877
## Blood_urea_.mg.dl.                0.0088667748
## Serum_creatinine_.mg.dl.          0.0676612273
## Hemoglobin_level_.gms.            0.0292847580
## Urine_protein.to.creatinine_ratio 0.0777071587
## Urine_output_.ml.day.             0.0052555589
## Cholesterol_level                 0.0005181311
## Body_Mass_Index_.BMI.             0.0021065104
#Melihat Skewness
library(moments)
## Warning: package 'moments' was built under R version 4.5.2
sapply(numerik, skewness)
##                Age_of_the_patient            Blood_pressure_.mm.Hg. 
##                        0.12935346                        0.05185305 
##                Blood_urea_.mg.dl.          Serum_creatinine_.mg.dl. 
##                        0.13535807                       -0.23633035 
##            Hemoglobin_level_.gms. Urine_protein.to.creatinine_ratio 
##                        0.02266149                       -0.02312773 
##             Urine_output_.ml.day.                 Cholesterol_level 
##                       -0.01194403                       -0.09909627 
##             Body_Mass_Index_.BMI. 
##                        0.21327223
#Kurtosis
kurtosis(CKD$Age_of_the_patient)
## [1] 1.911541
#Matriks Korelasi

variabel_numerik <- CKD[, c(
  "Age_of_the_patient",
  "Blood_pressure_.mm.Hg.",
  "Blood_urea_.mg.dl.",
  "Serum_creatinine_.mg.dl.",
  "Hemoglobin_level_.gms.",
  "Urine_protein.to.creatinine_ratio",
  "Urine_output_.ml.day.",
  "Cholesterol_level",
  "Body_Mass_Index_.BMI."
)]

# Menghitung korelasi Pearson
matriks_korelasi <- cor(variabel_numerik, method = "pearson")

# Menampilkan matriks korelasi
round(matriks_korelasi, 3)
##                                   Age_of_the_patient Blood_pressure_.mm.Hg.
## Age_of_the_patient                             1.000                 -0.056
## Blood_pressure_.mm.Hg.                        -0.056                  1.000
## Blood_urea_.mg.dl.                            -0.354                  0.005
## Serum_creatinine_.mg.dl.                      -0.110                 -0.129
## Hemoglobin_level_.gms.                        -0.265                  0.286
## Urine_protein.to.creatinine_ratio             -0.002                  0.007
## Urine_output_.ml.day.                         -0.026                 -0.163
## Cholesterol_level                             -0.122                  0.202
## Body_Mass_Index_.BMI.                          0.183                 -0.120
##                                   Blood_urea_.mg.dl. Serum_creatinine_.mg.dl.
## Age_of_the_patient                            -0.354                   -0.110
## Blood_pressure_.mm.Hg.                         0.005                   -0.129
## Blood_urea_.mg.dl.                             1.000                   -0.085
## Serum_creatinine_.mg.dl.                      -0.085                    1.000
## Hemoglobin_level_.gms.                         0.012                   -0.243
## Urine_protein.to.creatinine_ratio             -0.221                   -0.075
## Urine_output_.ml.day.                          0.242                    0.059
## Cholesterol_level                              0.065                    0.064
## Body_Mass_Index_.BMI.                         -0.136                    0.252
##                                   Hemoglobin_level_.gms.
## Age_of_the_patient                                -0.265
## Blood_pressure_.mm.Hg.                             0.286
## Blood_urea_.mg.dl.                                 0.012
## Serum_creatinine_.mg.dl.                          -0.243
## Hemoglobin_level_.gms.                             1.000
## Urine_protein.to.creatinine_ratio                 -0.154
## Urine_output_.ml.day.                             -0.056
## Cholesterol_level                                  0.171
## Body_Mass_Index_.BMI.                             -0.163
##                                   Urine_protein.to.creatinine_ratio
## Age_of_the_patient                                           -0.002
## Blood_pressure_.mm.Hg.                                        0.007
## Blood_urea_.mg.dl.                                           -0.221
## Serum_creatinine_.mg.dl.                                     -0.075
## Hemoglobin_level_.gms.                                       -0.154
## Urine_protein.to.creatinine_ratio                             1.000
## Urine_output_.ml.day.                                        -0.182
## Cholesterol_level                                            -0.044
## Body_Mass_Index_.BMI.                                        -0.161
##                                   Urine_output_.ml.day. Cholesterol_level
## Age_of_the_patient                               -0.026            -0.122
## Blood_pressure_.mm.Hg.                           -0.163             0.202
## Blood_urea_.mg.dl.                                0.242             0.065
## Serum_creatinine_.mg.dl.                          0.059             0.064
## Hemoglobin_level_.gms.                           -0.056             0.171
## Urine_protein.to.creatinine_ratio                -0.182            -0.044
## Urine_output_.ml.day.                             1.000             0.156
## Cholesterol_level                                 0.156             1.000
## Body_Mass_Index_.BMI.                            -0.021            -0.283
##                                   Body_Mass_Index_.BMI.
## Age_of_the_patient                                0.183
## Blood_pressure_.mm.Hg.                           -0.120
## Blood_urea_.mg.dl.                               -0.136
## Serum_creatinine_.mg.dl.                          0.252
## Hemoglobin_level_.gms.                           -0.163
## Urine_protein.to.creatinine_ratio                -0.161
## Urine_output_.ml.day.                            -0.021
## Cholesterol_level                                -0.283
## Body_Mass_Index_.BMI.                             1.000
library(ggplot2)

# Mengubah matriks korelasi menjadi data frame
korelasi_long <- as.data.frame(as.table(matriks_korelasi))

# Heatmap
ggplot(korelasi_long, aes(Var1, Var2, fill = Freq)) +
  geom_tile() +
  geom_text(aes(label = round(Freq, 2)), size = 3) +
  scale_fill_gradient2(
    low = "blue",
    mid = "white",
    high = "red",
    midpoint = 0,
    limits = c(-1, 1)
  ) +
  labs(
    title = "Heatmap Matriks Korelasi Variabel Numerik",
    x = "",
    y = "",
    fill = "Korelasi"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

#Tabel Kontingensi

# Sel darah merah dengan CKD
table(CKD$Red_blood_cells_in_urine, CKD$CKD_Status)
##           
##            No Yes
##   abnormal 16   9
##   normal   15  10
# Bakteri urine dengan CKD
table(CKD$Bacteria_in_urine, CKD$CKD_Status)
##              
##               No Yes
##   not_present 14  12
##   present     17   7
# Hipertensi dengan CKD
table(CKD$Hypertension_.yes.no., CKD$CKD_Status)
##      
##       No Yes
##   no  21  11
##   yes 10   8
# Diabetes dengan CKD
table(CKD$Diabetes_mellitus_.yes.no., CKD$CKD_Status)
##      
##       No Yes
##   no  18   9
##   yes 13  10
# Penyakit jantung koroner dengan CKD
table(CKD$Coronary_artery_disease_.yes.no., CKD$CKD_Status)
##      
##       No Yes
##   no  19   8
##   yes 12  11
# Anemia dengan CKD
table(CKD$Anemia_.yes.no., CKD$CKD_Status)
##      
##       No Yes
##   no  11   6
##   yes 20  13
# Riwayat keluarga CKD dengan CKD
table(CKD$Family_history_of_chronic_kidney_disease, CKD$CKD_Status)
##      
##       No Yes
##   no  21  12
##   yes 10   7
# Merokok dengan CKD
table(CKD$Smoking_status, CKD$CKD_Status)
##      
##       No Yes
##   no  15  11
##   yes 16   8
# Aktivitas fisik dengan CKD
table(CKD$Physical_activity_level, CKD$CKD_Status)
##           
##            No Yes
##   high     12   5
##   low       8   8
##   moderate 11   6
for (x in kategori) {
  cat("\n==============================\n")
  cat(x, "\n")
  cat("==============================\n")
  
  tab <- table(CKD[[x]], CKD$CKD_Status)
  print(tab)
  
  print(chisq.test(tab))
}
## 
## ==============================
## Red_blood_cells_in_urine 
## ==============================
##           
##            No Yes
##   abnormal 16   9
##   normal   15  10
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 0, df = 1, p-value = 1
## 
## 
## ==============================
## Bacteria_in_urine 
## ==============================
##              
##               No Yes
##   not_present 14  12
##   present     17   7
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 0.89257, df = 1, p-value = 0.3448
## 
## 
## ==============================
## Hypertension_.yes.no. 
## ==============================
##      
##       No Yes
##   no  21  11
##   yes 10   8
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 0.16049, df = 1, p-value = 0.6887
## 
## 
## ==============================
## Diabetes_mellitus_.yes.no. 
## ==============================
##      
##       No Yes
##   no  18   9
##   yes 13  10
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 0.19739, df = 1, p-value = 0.6568
## 
## 
## ==============================
## Coronary_artery_disease_.yes.no. 
## ==============================
##      
##       No Yes
##   no  19   8
##   yes 12  11
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 1.0586, df = 1, p-value = 0.3035
## 
## 
## ==============================
## Anemia_.yes.no. 
## ==============================
##      
##       No Yes
##   no  11   6
##   yes 20  13
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 1.7631e-31, df = 1, p-value = 1
## 
## 
## ==============================
## Family_history_of_chronic_kidney_disease 
## ==============================
##      
##       No Yes
##   no  21  12
##   yes 10   7
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 0.00060527, df = 1, p-value = 0.9804
## 
## 
## ==============================
## Smoking_status 
## ==============================
##      
##       No Yes
##   no  15  11
##   yes 16   8
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 0.13074, df = 1, p-value = 0.7177
## 
## 
## ==============================
## Physical_activity_level 
## ==============================
##           
##            No Yes
##   high     12   5
##   low       8   8
##   moderate 11   6
## 
##  Pearson's Chi-squared test
## 
## data:  tab
## X-squared = 1.563, df = 2, p-value = 0.4577
## 
## 
## ==============================
## CKD_Status 
## ==============================
##      
##       No Yes
##   No  31   0
##   Yes  0  19
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  tab
## X-squared = 45.846, df = 1, p-value = 1.28e-11