packages <- c("tidyverse", "tidymodels", "vip", "pROC", "ggplot2", "scales", "janitor", "skimr")
installed <- rownames(installed.packages())
to_install <- packages[!packages %in% installed]
if (length(to_install)) install.packages(to_install)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(tidyverse)
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ lubridate 1.9.5 ✔ tibble 3.3.1
## ✔ purrr 1.2.2 ✔ tidyr 1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggrepel)
# library(tidymodels)
# library(vip)
# library(pROC)
# library(scales)
# library(janitor)
# library(skimr)
# set.seed(42) - ??
# data_url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00296/dataset_diabetes'
# data_path = 'diabetic_data.csv'
#
# if (!file.exists(data_path)) {
# message("Downloading dataset...")
# tmp <- tempfile(fileext = ".zip")
# download.file(data_url, tmp, mode = "wb")
# unzip(tmp, files = "dataset_diabetes/diabetic_data.csv", junkpaths = TRUE)
# message("Done. File saved as: ", data_path)
# }
#
# raw <- read_csv(data_path, na = c("?", "Unknown", "")) |>
# clean_names()
# message("Rows: ", nrow(raw), " | Cols: ", ncol(raw))
diabetic_data = read.csv('diabetes+130-us+hospitals+for+years+1999-2008/diabetic_data.csv')
# diabetic_data
cat("Raw dimensions:", nrow(diabetic_data), "rows x", ncol(diabetic_data), "columns\n")
## Raw dimensions: 101766 rows x 50 columns
unique_types_race = unique(diabetic_data$race)
# unique_types_race
diabetic_data$race <- factor(diabetic_data$race,
levels = c("Caucasian", "AfricanAmerican", "?", "Other", "Asian", "Hispanic"))
summary_race = data.frame(table(diabetic_data$race))
colnames(summary_race)[colnames(summary_race) == 'Var1'] <- 'Race'
colnames(summary_race)[colnames(summary_race) == 'Freq'] <- 'Count'
# summary_race
summary_race <- summary_race %>%
mutate(csum = rev(cumsum(rev(Count))),
pos = Count/2 + lead(csum, 1),
pos = if_else(is.na(pos), Count/2, pos))
summary_race
## Race Count csum pos
## 1 Caucasian 76099 101766 63716.5
## 2 AfricanAmerican 19210 25667 16062.0
## 3 ? 2273 6457 5320.5
## 4 Other 1506 4184 3431.0
## 5 Asian 641 2678 2357.5
## 6 Hispanic 2037 2037 1018.5
unique_types_gender = unique(diabetic_data$gender)
# unique_types_gender
diabetic_data$gender = factor(diabetic_data$gender,
levels = c('Female', 'Male', 'Unknown/Invalid'))
summary_gender = data.frame(table(diabetic_data$gender))
colnames(summary_gender)[colnames(summary_gender) == 'Var1'] <- 'Gender'
colnames(summary_gender)[colnames(summary_gender) == 'Freq'] <- 'Count'
# summary_gender
summary_gender <- summary_gender %>%
mutate(csum = rev(cumsum(rev(Count))),
pos = Count/2 + lead(csum, 1),
pos = if_else(is.na(pos), Count/2, pos))
summary_gender
## Gender Count csum pos
## 1 Female 54708 101766 74412.0
## 2 Male 47055 47058 23530.5
## 3 Unknown/Invalid 3 3 1.5
require(gridExtra)
## Loading required package: gridExtra
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
plot1 = ggplot(summary_race, aes(x = "" , y = Count, fill = Race)) +
geom_col(width = 1, color = 1) +
coord_polar(theta = "y") +
scale_fill_brewer(palette = "Pastel1") +
geom_label_repel(data = summary_race,
aes(y = pos, label = paste0(Count)),
size = 4.5, nudge_x = 1, show.legend = FALSE) +
guides(fill = guide_legend(title = "Race")) +
theme_void() +
labs(title = "Race distribution")
plot2 = ggplot(summary_gender, aes(x = "" , y = Count, fill = Gender)) +
geom_col(width = 1, color = 1) +
coord_polar(theta = "y") +
scale_fill_brewer(palette = "Pastel1") +
geom_label_repel(data = summary_gender,
aes(y = pos, label = paste0(Count)),
size = 4.5, nudge_x = 1, show.legend = FALSE) +
guides(fill = guide_legend(title = "Gender")) +
theme_void() +
theme(panel.background = element_rect(fill = "#F5F5F5"))+
labs(title = "Gender distribution")
grid.arrange(plot1, plot2, ncol = 2)
#
ggplot(summary_gender, aes(x="", y = Count, fill = Gender)) +
#geom_bar(stat="identity", width=1) +
geom_col(width = 1, color = 1) +
coord_polar("y", start=0) +
scale_fill_brewer(palette = "Pastel1") +
geom_label_repel(data = summary_gender,
aes(y = Count, label = paste0(Count)),
size = 4.5, nudge_x = 1, show.legend = FALSE) +
theme_void()
#
df3 <- summary_gender %>%
mutate(csum = rev(cumsum(rev(Count))),
pos = Count/2 + lead(csum, 1),
pos = if_else(is.na(pos), Count/2, pos))
df3
## Gender Count csum pos
## 1 Female 54708 101766 74412.0
## 2 Male 47055 47058 23530.5
## 3 Unknown/Invalid 3 3 1.5
ggplot(df3, aes(x = "" , y = Count, fill = Gender)) +
geom_col(width = 1, color = 1) +
coord_polar(theta = "y") +
scale_fill_brewer(palette = "Pastel1") +
geom_label_repel(data = df3,
aes(y = pos, label = paste0(Count)),
size = 4.5, nudge_x = 1, show.legend = FALSE) +
guides(fill = guide_legend(title = "Group")) +
theme_void()
str(diabetic_data)
## 'data.frame': 101766 obs. of 50 variables:
## $ encounter_id : int 2278392 149190 64410 500364 16680 35754 55842 63768 12522 15738 ...
## $ patient_nbr : int 8222157 55629189 86047875 82442376 42519267 82637451 84259809 114882984 48330783 63555939 ...
## $ race : Factor w/ 6 levels "Caucasian","AfricanAmerican",..: 1 1 2 1 1 1 1 1 1 1 ...
## $ gender : Factor w/ 3 levels "Female","Male",..: 1 1 1 2 2 2 2 2 1 1 ...
## $ age : chr "[0-10)" "[10-20)" "[20-30)" "[30-40)" ...
## $ weight : chr "?" "?" "?" "?" ...
## $ admission_type_id : int 6 1 1 1 1 2 3 1 2 3 ...
## $ discharge_disposition_id: int 25 1 1 1 1 1 1 1 1 3 ...
## $ admission_source_id : int 1 7 7 7 7 2 2 7 4 4 ...
## $ time_in_hospital : int 1 3 2 2 1 3 4 5 13 12 ...
## $ payer_code : chr "?" "?" "?" "?" ...
## $ medical_specialty : chr "Pediatrics-Endocrinology" "?" "?" "?" ...
## $ num_lab_procedures : int 41 59 11 44 51 31 70 73 68 33 ...
## $ num_procedures : int 0 0 5 1 0 6 1 0 2 3 ...
## $ num_medications : int 1 18 13 16 8 16 21 12 28 18 ...
## $ number_outpatient : int 0 0 2 0 0 0 0 0 0 0 ...
## $ number_emergency : int 0 0 0 0 0 0 0 0 0 0 ...
## $ number_inpatient : int 0 0 1 0 0 0 0 0 0 0 ...
## $ diag_1 : chr "250.83" "276" "648" "8" ...
## $ diag_2 : chr "?" "250.01" "250" "250.43" ...
## $ diag_3 : chr "?" "255" "V27" "403" ...
## $ number_diagnoses : int 1 9 6 7 5 9 7 8 8 8 ...
## $ max_glu_serum : chr "None" "None" "None" "None" ...
## $ A1Cresult : chr "None" "None" "None" "None" ...
## $ metformin : chr "No" "No" "No" "No" ...
## $ repaglinide : chr "No" "No" "No" "No" ...
## $ nateglinide : chr "No" "No" "No" "No" ...
## $ chlorpropamide : chr "No" "No" "No" "No" ...
## $ glimepiride : chr "No" "No" "No" "No" ...
## $ acetohexamide : chr "No" "No" "No" "No" ...
## $ glipizide : chr "No" "No" "Steady" "No" ...
## $ glyburide : chr "No" "No" "No" "No" ...
## $ tolbutamide : chr "No" "No" "No" "No" ...
## $ pioglitazone : chr "No" "No" "No" "No" ...
## $ rosiglitazone : chr "No" "No" "No" "No" ...
## $ acarbose : chr "No" "No" "No" "No" ...
## $ miglitol : chr "No" "No" "No" "No" ...
## $ troglitazone : chr "No" "No" "No" "No" ...
## $ tolazamide : chr "No" "No" "No" "No" ...
## $ examide : chr "No" "No" "No" "No" ...
## $ citoglipton : chr "No" "No" "No" "No" ...
## $ insulin : chr "No" "Up" "No" "Up" ...
## $ glyburide.metformin : chr "No" "No" "No" "No" ...
## $ glipizide.metformin : chr "No" "No" "No" "No" ...
## $ glimepiride.pioglitazone: chr "No" "No" "No" "No" ...
## $ metformin.rosiglitazone : chr "No" "No" "No" "No" ...
## $ metformin.pioglitazone : chr "No" "No" "No" "No" ...
## $ change : chr "No" "Ch" "No" "Ch" ...
## $ diabetesMed : chr "No" "Yes" "Yes" "Yes" ...
## $ readmitted : chr "NO" ">30" "NO" "NO" ...
# na_count = sapply(diabetic_data_raw, function(y) sum(length(which(is.na(y)))))
# na_count = data.frame(na_count)
# na_count
# data$epc_label <- factor(data$epc_label,
# levels = c("A", "B", "C", "D", "E", "F"))
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.