This article mainly focuses on distinguishing between absence and the presence of cardiac arrhythmia and when exists assign to one of the 16 groups. Therefore, group 1 is related to normal ECG , while groups 2 to 15 refers to different types of arrhythmia, and, lastly, group 16 refers to unclassified types of arrhythmia. The dataset has 279 attributes, where 206 are linear values and the rest of the attributes are nominal. The differences exist between programs and cardiologists classification. By taking cardiologists classification as a gold standard researchers want to minimize this inequality with help of machine learning tools.
Reasoning for Selecting a Dataset/ Research Question
I picked this data to see how patient’s demographic information such as age, gender, height, weight and other ECG variables contribute to predict the one of the 16 classes?
Independent and Dependent Variables of the Dataset
The independent variables are heart rate, age, sex, height, weight, and other ECG variables. The dependent variables are 16 classes.
Data source
For this analysis we will use the arrhythmia dataset from Arrhythmia package.
Warning: unable to access index for repository https://r-project.org/src/contrib:
cannot open URL 'https://r-project.org/src/contrib/PACKAGES'
Warning: package 'caret' is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Warning: unable to access index for repository https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5:
cannot open URL 'https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5/PACKAGES'
Code
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tibble' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'readr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
Warning: package 'lubridate' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.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
Code
#Data Loadingdata_url <-"https://raw.githubusercontent.com/Maximus2485/Assignment-1.-Arrhythmia-class./refs/heads/main/arrhythmia.data"data <-read.csv( data_url,header =FALSE,na.strings ="?")#Name variablesnames(data)[1:15] <-c("age","sex","height","weight","qrs_duration","pr_interval","qt_interval","t_interval","p_interval","qrs_angle","t_angle","p_angle","qrst_angle","j_angle","heart_rate")# Name the last columnnames(data)[280] <-"class"# Convert class into a factor because this is a classification problemdata$class <-as.factor(data$class)head(data)
#Class Distributiondata %>%count(class) %>%ggplot(aes(x = class, y = n)) +geom_col() +labs(title ="Distribution of Arrhythmia Classes",x ="Arrhythmia Class",y ="Number of Patients" ) +theme_minimal()
Data Preparation
Missing predictor values are replaced with the median of each variable
Code
library(tidyverse)# Separate predictors and responseX <- data %>%select(-class)y <- data$class# Find columns with all missing valuesall_missing <-colSums(!is.na(X)) ==0# Remove predictors that contain only missing valuesX <- X[, !all_missing]install.packages("caret")
Warning: unable to access index for repository https://r-project.org/src/contrib:
cannot open URL 'https://r-project.org/src/contrib/PACKAGES'
Warning: package 'caret' is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Warning: unable to access index for repository https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5:
cannot open URL 'https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5/PACKAGES'
Code
install.packages("recipes")
Warning: unable to access index for repository https://r-project.org/src/contrib:
cannot open URL 'https://r-project.org/src/contrib/PACKAGES'
Warning: package 'recipes' is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Warning: unable to access index for repository https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5:
cannot open URL 'https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5/PACKAGES'
Code
library(caret)
Loading required package: lattice
Warning: package 'lattice' was built under R version 4.5.2
Attaching package: 'caret'
The following object is masked from 'package:purrr':
lift
Code
# Median imputationpreprocess_model <-preProcess( X,method ="medianImpute")X_clean <-predict(preprocess_model, X)# Combine predictors and target againdata_clean <- X_cleandata_clean$class <- y# Confirm no missing values remainsum(is.na(data_clean))
[1] 0
Training and Testing Data
I divided into training and testing sets. The training set is used to build the model, while the testing set is used to evaluate how well the model performs on unseen observations
A Random Forest classifier is used because this is a multiclass classification problem with a large number of predictor variables.
Code
install.packages("randomForest")
Warning: unable to access index for repository https://r-project.org/src/contrib:
cannot open URL 'https://r-project.org/src/contrib/PACKAGES'
Warning: package 'randomForest' is not available for this version of R
A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Warning: unable to access index for repository https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5:
cannot open URL 'https://r-project.org/bin/macosx/big-sur-arm64/contrib/4.5/PACKAGES'
Code
library(randomForest)
randomForest 4.7-1.2
Type rfNews() to see new features/changes/bug fixes.
Attaching package: 'randomForest'
The following object is masked from 'package:dplyr':
combine
The following object is masked from 'package:ggplot2':
margin
Code
set.seed(123)rf_model <-randomForest( class ~ .,data = train_data,ntree =500,importance =TRUE)# Display model resultsrf_model
The Random Forest model was about 72.1% accurate. It worked well for Class 1 but had problems predicting classes with fewer patients.
Heart rate was the most important variable for predicting arrhythmia class. Other ECG variables, such as V228, V224, V91, and V197, were also important.
Overall, the model shows that patient and ECG information can be used to predict arrhythmia classes. However, the model works better for some classes than others. In the future, the model could be improved to better predict classes with fewer patients.