Week 1 Assignment

Author

David Melchor

Introduction

I’m very curious about the field of pharmaceutical research and clinical trials and I am wondering about what it would be like to work at a company like Genentech or Gilead Sciences after graduation.

For this first assignment, I want to analyze a clinical dataset focused on patient demographic and health-related attributes aimed at evaluating cardiovascular risk and resting blood pressure metrics. The raw data source can be accessed directly from the UCI Machine Learning Data Repository, using ucimlrepo R package. Technical setup and package usage guidelines follow the documentation outlined on the ucimlrepo R Package Site

Business & Data Science Question

  • Business Question: Is resting blood pressure a contributor to poor heart disease outcomes?
  • Data Science Question: How can I work with clinical patient records to evaluate elevated resting blood pressure and establish structured risk profiles using R?

Strategy & Technical Approach

  1. Data Import: I will use the ucimlrepo package to import the dataset direclty into R.
  2. Data Cleaning & Wrangling:
    • Check data types
    • Look for missing values
    • Clean column headers
    • Recode variables as needed
    • Select a subset of data needed for analysis
  3. Exploratory Visualizations: Use ggplot2 to visualize relationships and identify patient risks.

Anticipated Data Challenges

  • Missing Data Handling: The dataset may contain missing values records that may require filtering.
  • Data Types: May need to create factors to organize categorical variables.
  • Outliers: Check for extreme values in variables.

Installing and Loading Packages

# Install ucimlrepo and load the packages
pacman::p_load(ucimlrepo, tidyverse)

Loading the Data

# Fetch the dataset from the UCI data repository
uci_data <- fetch_ucirepo(name = "Heart Disease")

# Extract the original dataset
bp_data <- uci_data$data$original

Data Exploration and Cleaning

# Check out the data dictionary
data_dictionary <- uci_data$variables

# Check the data structure
glimpse(bp_data)
Rows: 303
Columns: 14
$ age      <int> 63, 67, 67, 37, 41, 56, 62, 57, 63, 53, 57, 56, 56, 44, 52, 5…
$ sex      <int> 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1…
$ cp       <int> 1, 4, 4, 3, 2, 2, 4, 4, 4, 4, 4, 2, 3, 2, 3, 3, 2, 4, 3, 2, 1…
$ trestbps <int> 145, 160, 120, 130, 130, 120, 140, 120, 130, 140, 140, 140, 1…
$ chol     <int> 233, 286, 229, 250, 204, 236, 268, 354, 254, 203, 192, 294, 2…
$ fbs      <int> 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0…
$ restecg  <int> 2, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2…
$ thalach  <int> 150, 108, 129, 187, 172, 178, 160, 163, 147, 155, 148, 153, 1…
$ exang    <int> 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1…
$ oldpeak  <dbl> 2.3, 1.5, 2.6, 3.5, 1.4, 0.8, 3.6, 0.6, 1.4, 3.1, 0.4, 1.3, 0…
$ slope    <int> 3, 2, 2, 3, 1, 1, 3, 1, 2, 3, 2, 2, 2, 1, 1, 1, 3, 1, 1, 1, 2…
$ ca       <dbl> 0, 3, 2, 0, 0, 0, 2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0…
$ thal     <dbl> 6, 3, 7, 3, 3, 3, 3, 3, 7, 7, 6, 3, 6, 7, 7, 3, 7, 3, 3, 3, 3…
$ num      <int> 0, 2, 1, 0, 0, 0, 3, 0, 2, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0…
# Rename variables
bp_data <- bp_data |> 
  rename(
    resting_bp  = trestbps,
    cholesterol = chol,
    fasting_bs  = fbs,
    max_hr      = thalach,
    exercise_angina = exang,
    st_depression = oldpeak,
    major_vessels = ca,
    severity    = num,
    chest_pain  = cp,
    thallium_test = thal
  )

# Recode multi-level categorical variables into labeled factors
bp_data <- bp_data |> 
  mutate(
    chest_pain = factor(chest_pain,
                        level = c(1, 2, 3, 4),
                        labels = c("Typical Angina", "Atypical Angina", "Non-Anginal", "Asymptomatic")),
    slope = factor(slope,
                   levels = c(1, 2, 3),
                   labels = c("Upsloping", "Flat", "Downsloping")),
    thallium_test = factor(thallium_test,
                           levels = c(3, 6, 7),
                           labels = c("Normal", "Fixed Defect", "Reversible Defect")),
    sex = factor(sex,
                 levels = c(0, 1),
                 labels = c("Male", "Female")),
    fasting_bs = factor(fasting_bs,
                        levels = c(0, 1),
                        labels = c("<120 mg/dl", ">120 mg/dl")),
    exercise_angina = factor(exercise_angina,
                             levels = c(0, 1),
                             labels = c("No", "Yes")),
    restecg = factor(restecg,
                     levels = c(0, 1, 2),
                     labels = c("Normal", "Having ST-T wave abnormality", "Showing ventricular hypertrophy")),
    severity = factor(severity,
                           levels = 0:4,
                           labels = c("No disease", "Mild", "Moderate", "Severe", "Critical"))
  )

Create a New Subset for Data Analysis

# Create a new subset for data analysis
heart_risk <- bp_data |> 
  select(age, sex, chest_pain, resting_bp, cholesterol, fasting_bs,
         max_hr, severity)

Explore Relationships With ggplot

heart_risk |> 
  ggplot(
    aes(
      x = severity,
      y = resting_bp,
      fill = severity)) +
  geom_boxplot() +
  labs(
    title = "Resting Blood Pressure Across Heart Disease Severity",
    x = "Heart Disease Severity",
    y = "Resting Blood Pressure (mmHg)",
    fill = "Severity") +
  theme(legend.position = "none")

Conclusion

The box plot of resting blood pressure across heart disease severity reveals the median resting blood pressure of patients experiencing No disease, and different levels of heart disease severity. For patients with No disease, Mild, Moderate and Severe heart disease, resting blood pressure remains stable at around 130 mmHg. Patients who experienced Critical heart disease severity have a noticeable higher resting blood pressure at about 145 mmHg. The graph also reveals an upward shift of the interquartile range of patients in the Critical heart disease severity group, suggesting that their resting blood pressure average tends to be higher than all the other groups and may contribute to more critical stages of heart disease.

Next Steps

Given that resting blood pressure is similar across the No disease, Mild, Moderate, and Severe categories, next steps asks for analytical expansion beyond resting blood pressure metrics to evaluate why patient groups have different cardiovascular risk profiles. Since resting blood pressure is very similar across these first 4 groups and severity is different, there must be other patient attributes besides resting blood pressure that contribute to the severity of heart disease.

AI Conversation Citation

Gemini. (2026, September 4). [Response to a query about UCI Heart Disease dataset variable definitions]. Google.