---
title: "Chronic Kidney Disease Project Analysis"
author: "OJALA BRIAN OLOO"
output: html_document
toc: true
code-fold: true
code-tools: true
editor: visual
---
**About the data set**
The dataset provides a comprehensive collection of patient clinical information, drug exposure profiles, and drug-related biochemical characteristics to support research on the early identification of Chronic Kidney Disease (CKD). It combines real-world–style patient health indicators with detailed properties of nephrotoxic and non-nephrotoxic medications that may influence kidney function. The data set contains:
**Patient Clinical Information**
Includes age, gender, blood pressure, blood urea, serum creatinine, albumin levels, random blood glucose, and health conditions such as diabetes and hypertension. These features reflect common clinical factors associated with kidney health.
**Drug Exposure Profiles**
Each patient was linked to a drug along with dosage and duration of use. A separate label indicates whether the drug is considered **nephrotoxic effects**.
**N/B**: This data set was downloaded from Kaggle
Aim of the Project: *To analyze patient data associated with neurotoxic effects*
```{r}
# Clearing R environment
rm(list = ls(all.names = TRUE))
# Setting working directory
setwd("C:/Epidemiology")
# Import data set
library(readr)
CDK <- read_csv("CDK.csv")
# Explore the first 10 observations
head(CDK, n = 10)
```
Data Management
```{r}
# Load packages
library(tidyverse)
library(expss)
library(summarytools)
library(table1)
library(psych)
library(gtsummary)
library(flextable)
library(officer)
library(broom)
library(broom.helpers)
library(gt)
library(readxl)
library(writexl)
library(finalfit)
df<-CDK|>
select(patient_age,gender,bp_systolic,bp_diastolic,blood_urea,
serum_creatinine,diabetes,hypertension,ckd_risk_label,nephrotoxic_label,drug_name)|>
mutate(
gender= factor(gender,
levels = c(0,1),
labels = c("Female","Male"),
exclude = NA),
diabetes=factor(diabetes,
levels = c(0,1),
labels = c("No","Yes"),
exclude = NA),
hypertension=factor(hypertension,
levels = c(0,1),
labels = c("No","Yes"),
exclude = NA),
nephrotoxic_label=factor(nephrotoxic_label,
levels = c(0,1),
labels = c("non-nephrotoxic","nephrotoxic"),
exclude = NA),
ckd_risk_label=factor(ckd_risk_label,
levels = c(0,1,2),
labels=c("Low risk","Moderate risk","High risk"),
exclude = NA))|>
apply_labels(
patient_age= " Age(years)" ,
gender= "Sex",
bp_systolic= "Systolic blood pressure(mm Hg)",
bp_diastolic= "Diastolic blood pressure(mm/HG)" ,
blood_urea= "Blood urea(mg/dl)",
serum_creatinine= "Serum creatinine(mg/dl)",
ckd_risk_label= "Risk of chronic Kidney disease" ,
nephrotoxic_label= "Nephrotoxicity" ,
drug_name= "Type of drug")|>
mutate(Age_cat=case_when(
patient_age<25~1,
patient_age>=25 & patient_age<29~2,
patient_age>=30 & patient_age<35~3,
patient_age>=36 & patient_age<44~4,
patient_age>=45 & patient_age<54~5,
patient_age>=55 & patient_age<64~6,
patient_age>=65~7
))|>
mutate(
Age_cat=factor(Age_cat,
levels = c(1,2,3,4,5,6,7),
labels = c("18-24","25-29","30-35","36-44","45-54",
"55-64","65+"),))|>
apply_labels(
Age_cat= " Patient Age group(years)")
head(df,n=10)
```
Exploratory Data Analysis
Gender distribution
```{r}
# Pre-calculate Frequencies and Percentages
plot_data <- df %>%
count(gender) %>% # Generates 'n' column for counts
mutate(
Percentage = (n / sum(n)) * 100,
Label = paste0(n, " (", round(Percentage, 1), "%)") # Combine Count + %
)
# Create the Bar Chart
ggplot(plot_data, aes(x = gender, y = n, fill = gender)) +
geom_col(alpha = 0.8, show.legend = FALSE) + # geom_col plots the exact values
geom_text(aes(label = Label), vjust = -0.5, size = 4.5) + # Add labels above bars
labs(
title = "Gender Distribution",
x = "Sex Category",
y = "Frequency (Count)"
) +
ylim(0, max(plot_data$n) * 1.1) + # Leave space at the top for labels
theme_minimal()
```
Prevalence of nephrotoxicity
```{r}
# Pre-calculate Frequencies and Percentages
plot_data_1 <- df %>%
count(nephrotoxic_label) %>% # Generates 'n' column for counts
mutate(
Percentage = (n / sum(n)) * 100,
Label = paste0(n, " (", round(Percentage, 1), "%)") # Combine Count + %
)
# Create the Bar Chart
ggplot(plot_data_1, aes(x = nephrotoxic_label, y = n, fill = nephrotoxic_label)) +
geom_col(alpha = 0.8, show.legend = FALSE) +
geom_text(aes(label = Label), vjust = -0.5, size = 4.5) + # Add labels above bars
labs(
title = "Prevalence of nephrotoxicity",
x = "Nephrotoxicity",
y = "Frequency (Count)"
) +
ylim(0, max(plot_data_1$n) * 1.1) + # Leave space at the top for labels
theme_minimal()
```
Diabetes distribution
```{r}
# Pre-calculate Frequencies and Percentages
plot_data_2 <- df %>%
count(diabetes) %>% # Generates 'n' column for counts
mutate(
Percentage = (n / sum(n)) * 100,
Label = paste0(n, " (", round(Percentage, 1), "%)") # Combine Count + %
)
# Create the Bar Chart
ggplot(plot_data_2, aes(x = diabetes, y = n, fill = diabetes)) +
geom_col(alpha = 0.8, show.legend = FALSE) +
geom_text(aes(label = Label), vjust = -0.5, size = 4.5) + # Add labels above bars
labs(
title = "Diabetes distribution",
x = "Patients with diabetes",
y = "Frequency (Count)"
) +
ylim(0, max(plot_data_2$n) * 1.1) + # Leave space at the top for labels
theme_minimal()
```
Hypertension distribution
```{r}
# Pre-calculate Frequencies and Percentages
plot_data_3 <- df %>%
count(hypertension) %>% # Generates 'n' column for counts
mutate(
Percentage = (n / sum(n)) * 100,
Label = paste0(n, " (", round(Percentage, 1), "%)") # Combine Count + %
)
# Create the Bar Chart
ggplot(plot_data_3, aes(x = hypertension, y = n, fill = hypertension)) +
geom_col(alpha = 0.8, show.legend = FALSE) +
geom_text(aes(label = Label), vjust = -0.5, size = 4.5) + # Add labels above bars
labs(
title = "Hypertension distribution",
x = "Patients with hypertension",
y = "Frequency (Count)"
) +
ylim(0, max(plot_data_3$n) * 1.1) + # Leave space at the top for labels
theme_minimal()
```
```{r}
# Pre-calculate Frequencies and Percentages
plot_data_4 <- df %>%
count(ckd_risk_label) %>% # Generates 'n' column for counts
mutate(
Percentage = (n / sum(n)) * 100,
Label = paste0(n, " (", round(Percentage, 1), "%)") # Combine Count + %
)
# Create the Bar Chart
ggplot(plot_data_4, aes(x = ckd_risk_label, y = n, fill = ckd_risk_label)) +
geom_col(alpha = 0.8, show.legend = FALSE) +
geom_text(aes(label = Label), vjust = -0.5, size = 4.5) + # Add labels above bars
labs(
title = "Risk of Chronic Kidney Disease ",
x = "Risk of CKD",
y = "Frequency (Count)"
) +
ylim(0, max(plot_data_4$n) * 1.1) + # Leave space at the top for labels
theme_minimal()
```
Drug names
```{r}
# Pre-calculate Frequencies and Percentages
plot_data_5 <- df %>%
count(drug_name) %>% # Generates 'n' column for counts
mutate(
Percentage = (n / sum(n)) * 100,
Label = paste0(n, " (", round(Percentage, 1), "%)") # Combine Count + %
)
# Create the Bar Chart
ggplot(plot_data_5, aes(x = drug_name, y = n, fill = drug_name)) +
geom_col(alpha = 0.8, show.legend = FALSE) +
geom_text(aes(label = Label), vjust = -0.5, size = 4.5) +
# Add labels above bars
labs(
title = "Drugs names used in CKD Medications ",
x = "Drug Name",
y = "Frequency (Count)"
) +
ylim(0, max(plot_data_5$n) * 1.1) + # Leave space at the top for labels
theme_minimal()
```
Explanatory Data Analysis of Continuous Variables
```{r}
# Distribution of patients age
df%>%
ggplot(aes(gender,patient_age,fill = gender))+
geom_boxplot(show.legend = FALSE)+
labs(
title = "Patients Age distribution ",
x = "Age distribution",
y = "Median"
)+
theme_minimal()
# Systolic Blood bressure
df%>%
ggplot(aes(gender,bp_systolic,fill = gender))+
geom_boxplot(show.legend = FALSE)+
labs(
title = "Distribution in systolic blood pressure ",
x = "Systolic blood pressure",
y = "Median"
)+
theme_minimal()
# Diastolic blood pressure
df%>%
ggplot(aes(gender,bp_diastolic,fill = gender))+
geom_boxplot(show.legend = FALSE)+
labs(
title = "Distribution in diastolic blood pressure ",
x = "Diastolic blood pressure",
y = "Median"
)+
theme_minimal()
df%>%
ggplot(aes(gender,blood_urea,fill = gender))+
geom_boxplot(show.legend = FALSE)+
labs(
title = "Blood Urea distribution ",
x = "Blood urea distribution",
y = "Median"
)+
theme_minimal()
df%>%
ggplot(aes(gender,serum_creatinine,fill = gender))+
geom_boxplot(show.legend = FALSE)+
labs(
title = "Serum creatinine distribution ",
x = "Serum creatinine",
y = "Median"
)+
theme_minimal()
```
Descriptive Statistics
```{r}
Table1<-df
Mystat<-list(all_continuous()~"{mean} ± {sd}",
all_categorical()~"{n} ({p})")
MyDigit<-list(all_continuous()~c(2,2),all_categorical()~c(0,2))
Table1<-df%>%
tbl_summary(by=nephrotoxic_label,missing = "no",statistic = Mystat,digits = MyDigit)%>%
bold_labels()
Table1
```
Bivariate Analysis
```{r}
Table <-df%>%
tbl_summary(
by = nephrotoxic_label, # Uncomment if you want group-wise summary
statistic = list(
all_continuous() ~ "{median} ({p25}, {p75})",
all_categorical() ~ "{n} ({p}%)"
),
percent = "column",
missing = "no"
) %>%
add_overall() %>%
add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
modify_footnote(all_stat_cols() ~ "Median (IQR)") %>%
modify_spanning_header(c("stat_1", "stat_2") ~ "**nephrotoxic_label **") %>%
modify_caption("Table 1: Patient Clinical Information") %>%
bold_labels() %>%
add_n() %>%
as_flex_table()
sect_properties <- prop_section(page_size = page_size(orient = "portrait"))#, width = 8.3, height = 11.7)
save_as_docx(Table,path="Table1a.docx", pr_section = sect_properties)
Table
```
```
```
```
```
***N/B: Please call this number 0743670039 if you need the following services***
- ·Designing data collection tools in Kobo toolbox & Google form
- ·Statistical Data Analysis
- ·Writing research Methodology