library(readxl)
## Warning: package 'readxl' was built under R version 4.5.3
library(janitor)
## Warning: package 'janitor' was built under R version 4.5.3
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
ckd <- read_excel(file.choose(), sheet = "Sheet1") %>%
  clean_names() %>%
  mutate(across(where(is.character), as.factor))

# Cek nama kolom
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"
library(tidyr)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
num_vars <- 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")

ckd %>%
  select(all_of(num_vars)) %>%
  pivot_longer(everything(), names_to = "variabel", values_to = "nilai") %>%
  ggplot(aes(x = nilai, fill = variabel)) +
  geom_histogram(bins = 12, color = "white", alpha = 0.85) +
  facet_wrap(~ variabel, scales = "free", ncol = 3) +
  labs(title = "Distribusi Semua Variabel Numerik CKD",
       x = NULL, y = "Frekuensi") +
  theme_minimal(base_size = 10) +
  theme(legend.position = "none",
        strip.text = element_text(face = "bold", size = 10))

ckd %>%
  select(all_of(num_vars)) %>%
  pivot_longer(everything(), names_to = "variabel", values_to = "nilai") %>%
  ggplot(aes(x = variabel, y = nilai, fill = variabel)) +
  geom_boxplot(alpha = 0.85) +
  facet_wrap(~ variabel, scales = "free", ncol = 3) +
  labs(title = "Boxplot Semua Variabel Numerik CKD",
       x = NULL, y = NULL) +
  theme_minimal(base_size = 10) +
  theme(legend.position = "none",
        strip.text = element_text(face = "bold", size = 9),
        axis.text.x = element_text(angle = 45, hjust = 1, size = 7))

GGally::ggpairs(
  ckd %>% select(age_of_the_patient,
                 blood_pressure_mm_hg,
                 serum_creatinine_mg_dl,
                 hemoglobin_level_gms,
                 body_mass_index_bmi,
                 ckd_status),
  aes(color = ckd_status, alpha = 0.6),
  lower = list(continuous = GGally::wrap("smooth", alpha = 0.3, size = 0.8)),
  upper = list(continuous = GGally::wrap("cor", size = 3)),
  diag  = list(continuous = GGally::wrap("densityDiag", alpha = 0.4))
) +
  scale_color_manual(values = c("No" = "#4CAF50", "Yes" = "#E53935")) +
  scale_fill_manual(values = c("No" = "#4CAF50", "Yes" = "#E53935")) +
  theme_minimal(base_size = 10)
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## No shared levels found between `names(values)` of the manual scale and the
## data's fill values.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: No shared levels found between `names(values)` of the manual scale and the
## data's colour values.
## No shared levels found between `names(values)` of the manual scale and the
## data's colour values.

# Buat data_num — hanya kolom numerik
data_num <- ckd %>% select(where(is.numeric))

# Cek
dim(data_num)   # 50 9
## [1] 50  9
names(data_num)
## [1] "age_of_the_patient"                "blood_pressure_mm_hg"             
## [3] "blood_urea_mg_dl"                  "serum_creatinine_mg_dl"           
## [5] "hemoglobin_level_gms"              "urine_protein_to_creatinine_ratio"
## [7] "urine_output_ml_day"               "cholesterol_level"                
## [9] "body_mass_index_bmi"
# Statistik deskriptif
hasil <- data.frame(
  Mean   = sapply(data_num, mean,   na.rm = TRUE),
  Median = sapply(data_num, median, na.rm = TRUE),
  SD     = sapply(data_num, sd,     na.rm = TRUE),
  Min    = sapply(data_num, min,    na.rm = TRUE),
  Max    = sapply(data_num, max,    na.rm = TRUE)
)

print(round(hasil, 2))
##                                      Mean  Median     SD    Min     Max
## age_of_the_patient                  56.90   54.50  20.08  25.00   90.00
## blood_pressure_mm_hg               131.90  127.50  29.45  80.00  179.00
## blood_urea_mg_dl                   101.27   97.62  58.01   8.69  198.73
## serum_creatinine_mg_dl               7.61    8.17   3.88   0.76   14.58
## hemoglobin_level_gms                12.09   11.85   3.37   6.00   17.50
## urine_protein_to_creatinine_ratio    2.30    2.38   1.23   0.24    4.49
## urine_output_ml_day               1739.52 1740.50 802.09 308.00 2933.00
## cholesterol_level                  199.52  214.00  66.92 100.00  298.00
## body_mass_index_bmi                 26.33   25.10   7.64  15.20   39.00
# ============================================================
# HEATMAP KORELASI — TANPA reshape2
# ============================================================

library(readxl); library(janitor); library(dplyr)
library(tidyr); library(ggplot2)

# Import
ckd <- read_excel(file.choose(), sheet = "Sheet1") %>%
  clean_names() %>%
  mutate(across(where(is.character), as.factor))

# Data numerik
data_num <- ckd %>% select(where(is.numeric))

# Matriks korelasi (Spearman)
cor_mat <- cor(data_num, use = "complete.obs", method = "spearman")

# Long format dengan pivot_longer
cor_long <- cor_mat %>%
  as.data.frame() %>%
  tibble::rownames_to_column("Var1") %>%
  pivot_longer(-Var1, names_to = "Var2", values_to = "value")

# Heatmap
ggplot(cor_long, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(color = "white") +
  geom_text(aes(label = round(value, 2)), size = 3.5, color = "black") +
  scale_fill_gradient2(
    low      = "#E63946",
    mid      = "white",
    high     = "#2E86AB",
    midpoint = 0,
    limits   = c(-1, 1),
    name     = "r"
  ) +
  labs(
    title = "Heatmap Korelasi Antar Variabel Numerik Pasien CKD",
    x = "", y = ""
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title   = element_text(hjust = 0.5, face = "bold"),
    axis.text.x  = element_text(angle = 45, hjust = 1, face = "bold"),
    axis.text.y  = element_text(face = "bold"),
    panel.grid   = element_blank()
  )