Assignment 1.Arrhythmia

DATA 607: week 1 Assignment

Author

Maxim Arisov

Published

September 5, 2026

Arrhythmia

Approach

Introduction

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.

Data source

https://raw.githubusercontent.com/Maximus2485/Assignment-1.-Arrhythmia-class./refs/heads/main/arrhythmia.data

Guvenir, H., Acar, B., Muderrisoglu, H., & Quinlan, R. (1997). Arrhythmia [Dataset]. UCI Machine Learning Repository. https://doi.org/10.24432/C5BS32.

Code
options(repos = c(CRAN = "https://r-project.org"))
install.packages("caret", repos = "https://r-project.org")
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 Loading
data_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 variables
names(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 column
names(data)[280] <- "class"
# Convert class into a factor because this is a classification problem
data$class <- as.factor(data$class)
head(data)
  age sex height weight qrs_duration pr_interval qt_interval t_interval
1  75   0    190     80           91         193         371        174
2  56   1    165     64           81         174         401        149
3  54   0    172     95          138         163         386        185
4  55   0    175     94          100         202         380        179
5  75   0    190     80           88         181         360        177
6  13   0    169     51          100         167         321        174
  p_interval qrs_angle t_angle p_angle qrst_angle j_angle heart_rate V16 V17
1        121       -16      13      64         -2      NA         63   0  52
2         39        25      37     -17         31      NA         53   0  48
3        102        96      34      70         66      23         75   0  40
4        143        28      11      -5         20      NA         71   0  72
5        103       -16      13      61          3      NA         NA   0  48
6         91       107      66      52         88      NA         84   0  36
  V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36
1  44   0   0  32   0   0   0   0   0   0   0  44  20  36   0  28   0   0   0
2   0   0   0  24   0   0   0   0   0   0   0  64   0   0   0  24   0   0   0
3  80   0   0  24   0   0   0   0   0   0  20  56  52   0   0  40   0   0   0
4  20   0   0  48   0   0   0   0   0   0   0  64  36   0   0  36   0   0   0
5  40   0   0  28   0   0   0   0   0   0   0  40  24   0   0  24   0   0   0
6  48   0   0  20   0   0   0   0   0   0  20  44  36   0   0  44   0   0   0
  V37 V38 V39 V40 V41 V42 V43 V44 V45 V46 V47 V48 V49 V50 V51 V52 V53 V54 V55
1   0   0   0  52  40   0   0   0  60   0   0   0   0   0   0  52   0   0   0
2   0   0   0  32  24   0   0   0  40   0   0   0   0   0   0  48   0   0   0
3   0   0   0  28 116   0   0   0  52   0   0   0   0   0   0  52  64   0   0
4   0   0   0  20  52  48   0   0  56   0   0   0   0   0   0  64  32   0   0
5   0   0   0  52  36   0   0   0  60   0   0   0   0   0   0  48  28   0   0
6   0   0   0  24  64   0   0   0  48   0   0   0   0   0   0  44  36   0   0
  V56 V57 V58 V59 V60 V61 V62 V63 V64 V65 V66 V67 V68 V69 V70 V71 V72 V73 V74
1   0   0   0   0   0   0   0   0   0  56  36   0   0  32   0   0   0   0   0
2   0   0   0   0   0   0   0   0   0  44  20   0   0  24   0   0   0   0   0
3   0  88   0   0   0   0   0   0   0  36  92   0   0  24   0   0   0   0   0
4   0  72   0   0   0   0   0   0   0  60  12   0   0  44   0   0   0   0   0
5   0  56   0   0   0   0   0   0   0  48  36   0   0  28   0   0   0   0   0
6   0  52   0   0   0   0   0   0   0  28  64   0   0  16   0   0   0   0   0
  V75 V76 V77 V78 V79 V80 V81 V82 V83 V84 V85 V86 V87 V88 V89 V90 V91 V92 V93
1   0  48  32   0   0   0  56   0   0   0   0   0   0  80   0   0   0   0   0
2   0   0  60   0   0   0  20   0   0   0   0   0   0   0  24  52   0   0  16
3   0   0 128   0   0   0  24   0   1   0   0   0   0   0  24  36  76   0 100
4   0   0  60  44   0   0  32   0   0   0   0   0   0  56   0   0   0   0   0
5   0  44   0   0   0   0   0   0   0   0   0   0   0  88   0   0   0   0   0
6   0  24  44  40   0   0  44   0   0   0   0   0   0   0  36  60   0   0  24
  V94 V95 V96 V97 V98 V99 V100 V101 V102 V103 V104 V105 V106 V107 V108 V109
1   0   0   0   0   0   0    0   40   52    0    0   28    0    0    0    0
2   0   0   0   0   0   0    0   32   52    0    0   20    0    0    0    0
3   0   0   0   0   0   0    0   40   28   60    0   96    0    0    0    0
4   0   0   0   0   0   0    0   40   44    0    0   20    0    0    0    0
5   0   0   0   0   0   0    0   40   52    0    0   28    0    0    0    0
6   0   0   0   0   0   0   20   32   60    0    0   40    0    0    0    0
  V110 V111 V112 V113 V114 V115 V116 V117 V118 V119 V120 V121 V122 V123 V124
1    0    0    0   48   48    0    0   32    0    0    0    0    0    0    0
2    0    0    0   44   48    0    0   32    0    0    0    0    0    0    0
3    0    0    0   48   20   56   24   32    0    0    0    0    0    0    0
4    0    0    0   52   40    0    0   32    0    0    0    0    0    0    0
5    0    0    0   48   48    0    0   32    0    0    0    0    0    0    0
6    0    0   24   32   60    0    0   44    0    0    0    0    0    0    0
  V125 V126 V127 V128 V129 V130 V131 V132 V133 V134 V135 V136 V137 V138 V139
1   52   52    0    0   36    0    0    0    0    0    0    0   52   48    0
2   48   44    0    0   32    0    0    0    0    0    0    0   48   40    0
3   44   88    0    0   28    0    0    0    0    0    0    0   44   76    0
4   56   48    0    0   36    0    0    0    0    0    0    0   60   48    0
5   48   52    0    0   32    0    0    0    0    0    0    0   52   44    0
6   52   40    0    0   36    0    0    0    0    0    0    0   44   40    0
  V140 V141 V142 V143 V144 V145 V146 V147 V148 V149 V150 V151 V152 V153 V154
1    0   32    0    0    0    0    0    0    0   56   44    0    0   32    0
2    0   28    0    0    0    0    0    0    0   48    0    0    0   28    0
3    0   28    0    0    0    0    0    0    0   44   72    0    0   24    0
4    0   36    0    0    0    0    0    0    0   64   40    0    0   40    0
5    0   28    0    0    0    0    0    0    0   52   48    0    0   32    0
6    0   32    0    0    0    0    0    0   20   36   56    0    0   40    0
  V155 V156 V157 V158 V159 V160 V161 V162 V163 V164 V165 V166 V167  V168 V169
1    0    0    0    0    0 -0.2    0  6.1 -1.0    0    0  0.6  2.1  13.6 30.8
2    0    0    0    0    0 -0.6    0  7.2  0.0    0    0  0.4  1.5  17.2 26.5
3    0    0    0    0    0  1.0    0  4.5 -2.8    0    0  0.3  2.5  -2.2 19.8
4    0    0    0    0    0  0.9    0  7.8 -0.7    0    0  1.1  1.9  27.3 45.1
5    0    0    0    0    0  0.0    0  5.2 -1.4    0    0  0.9  2.3   9.6 31.6
6    0    0    0    0    0  0.5    0  2.7 -6.4    0    0  0.9  1.7 -10.5  7.1
  V170 V171 V172 V173 V174 V175 V176 V177 V178 V179 V180 V181 V182 V183 V184
1  0.0  0.0  1.7 -1.0  0.6    0  1.3  1.5  3.7 14.5  0.1 -5.2  1.4  0.0    0
2  0.0  0.0  5.5  0.0  0.0    0  0.1  1.7 17.6 29.5  0.3 -1.6  0.9  0.0    0
3  0.8 -0.4  6.4 -1.3  0.0    0  0.7  2.7 14.2 37.9 -0.2 -0.6  4.4  0.0    0
4  0.1  0.0  9.1 -2.6  0.0    0  0.4  1.5 24.5 36.8 -0.4 -0.4  1.6 -2.2    0
5  0.1  0.0  1.6 -0.5  0.0    0  1.9  1.7  2.6 18.9  0.2 -3.8  1.2  0.0    0
6  0.1 -1.2 19.1 -2.3  0.0    0  1.4  4.3 36.7 84.8 -0.4 -2.3 21.7  0.0    0
  V185 V186 V187  V188  V189 V190 V191 V192 V193 V194 V195 V196 V197  V198
1    0  0.8 -0.6 -10.7 -15.6  0.4 -3.9  0.0    0    0    0 -0.8 -1.7 -10.1
2    0 -0.3  0.4  -1.5   1.3  0.1 -6.4  0.0    0    0    0 -0.3 -1.6 -15.3
3    0  0.5  0.2  24.7  26.2 -1.0 -5.3  1.8    0    0    0 -0.5 -2.5  -8.0
4    0 -1.0 -0.9  -1.5  -9.2 -0.4 -8.2  1.8    0    0    0 -0.7 -1.7 -23.4
5    0  1.0 -0.6  -7.7 -13.4 -0.1 -3.4  0.8    0    0    0 -1.4 -1.5  -7.0
6    0  0.7  2.6  66.7  95.8 -0.2 -9.0  3.2    0    0    0 -1.1 -2.9 -14.1
   V199 V200 V201 V202  V203 V204 V205 V206 V207  V208  V209 V210 V211 V212
1 -22.0  0.0    0  5.7  -1.0    0    0 -0.1  1.2  14.1  22.5  0.0 -2.5  0.8
2 -25.5 -0.3    0  4.2  -0.9    0    0  0.4  0.7   8.3  12.3  0.2  0.0  2.2
3 -28.5  0.5    0  1.7  -2.7    0    0 -0.2  1.0  -9.4  -1.2  0.4  0.0  4.9
4 -35.6  0.9    0  3.2  -0.4    0    0  0.7  1.2   9.4  18.0 -0.1  0.0  5.1
5 -17.8 -0.1    0  4.4  -1.3    0    0 -0.1  1.1   8.2  16.5  0.6 -1.6  0.0
6 -39.0  0.5    0  1.8 -12.9    0    0  0.4 -0.4 -38.7 -42.1 -0.1 -1.6 19.9
  V213 V214 V215 V216 V217 V218 V219 V220 V221 V222  V223 V224 V225 V226 V227
1  0.0    0    0  1.0  0.4 -4.8 -2.7  0.1 -6.0  0.0   0.0  0.0    0 -0.8 -0.6
2  0.0    0    0 -0.2  0.8  6.6 11.7  0.4  0.0  1.0  -8.8  0.0    0  0.5 -0.6
3  0.0    0    0  0.6  1.4 31.3 42.7 -0.8  0.0  0.7  -3.8  6.5    0  0.3 -3.3
4 -2.5    0    0  0.3  0.6  9.8 12.6  1.6 -6.5  0.0   0.0  0.0    0 -0.4 -0.4
5  0.0    0    0  1.4  0.3 -3.5 -1.9  0.0 -5.7  0.0   0.0  0.0    0 -0.4 -0.5
6 -0.7    0    0  1.0  3.3 40.4 65.4  0.4  0.0  6.7 -24.4  0.0    0 -1.2  0.4
   V228  V229 V230 V231 V232  V233 V234 V235 V236 V237   V238  V239 V240 V241
1 -24.0 -29.7  0.0  0.0  2.0  -6.4  0.0    0  0.2  2.9  -12.6  15.2 -0.1  0.0
2 -21.6 -26.8  0.4  0.0  2.6  -7.9  0.0    0  0.8  2.0  -16.4   1.2  0.0  0.0
3  18.7 -13.6 -0.9  0.0  2.2  -4.1  7.4    0  0.5 -2.4   20.9  -2.6  0.0  0.0
4 -18.2 -22.4  2.1  0.0  1.2  -6.9  0.0    0 -0.5  2.9  -12.7  18.0  0.7  0.0
5 -25.0 -30.0 -0.2  0.0  1.6  -6.0  0.0    0 -0.7  2.1  -12.4   8.6 -0.5  0.0
6 -61.2 -59.9  0.9 -0.5 11.9 -43.3  0.0    0  0.8  3.4 -111.4 -95.1  2.0 -0.8
  V242  V243 V244 V245 V246 V247   V248  V249 V250 V251 V252  V253 V254 V255
1  8.4 -10.0    0  0.0  0.6  5.9   -3.9  52.7 -0.3    0 15.2  -8.4    0    0
2  5.8  -7.7    0  0.0  0.9  3.8   -5.7  27.7 -0.2    0  9.5  -5.0    0    0
3  5.8  -4.1    4 -0.5  0.4  0.3   20.4  23.3  0.7    0 10.0  -5.7    0    0
4  9.0  -7.9    0  0.0  0.1  4.1    7.6  51.0  0.4    0 15.0  -5.5    0    0
5  8.5 -10.2    0  0.0 -1.0  4.7   -4.0  43.0 -0.2    0 15.2  -7.8    0    0
6 19.8 -48.4    0  0.0  1.6  8.7 -114.5 -72.8  2.0    0 31.0 -25.7    0    0
  V256 V257 V258 V259 V260 V261 V262  V263 V264 V265 V266 V267 V268 V269 V270
1  0.9  5.1 17.7 70.7 -0.4    0 13.5  -4.0    0    0  0.9  3.9 25.5 62.9 -0.3
2  0.5  2.6 11.8 34.6 -0.4    0 11.0  -2.4    0    0  0.4  2.6 21.6 43.4 -0.5
3  0.5  2.2 -3.0 20.7  1.3    0 11.1  -3.4    0    0  0.4  3.4 11.5 48.2  0.9
4  0.1  3.3 28.8 63.1  0.1    0 15.2  -3.7    0    0  0.6  3.0 36.8 68.0  0.1
5 -0.1  4.9 16.2 63.2 -0.2    0  9.1  -0.9    0    0 -0.2  2.9 21.7 48.9 -0.4
6  0.8  5.9 29.2 85.8  0.6    0 19.5 -11.4    0    0  0.8  3.3 20.1 49.1  0.0
  V271 V272 V273 V274 V275 V276 V277 V278 V279 class
1  0.0  9.0 -0.9    0    0  0.9  2.9 23.3 49.4     8
2  0.0  8.5  0.0    0    0  0.2  2.1 20.4 38.8     6
3  0.0  9.5 -2.4    0    0  0.3  3.4 12.3 49.0    10
4  0.0 12.2 -2.2    0    0  0.4  2.6 34.6 61.6     1
5  0.0 13.1 -3.6    0    0 -0.1  3.9 25.4 62.8     7
6 -0.6 12.2 -2.8    0    0  0.9  2.2 13.5 31.1    14

Code Base Deliverable

Data Exploration

Code
# Number of missing values
sum(is.na(data))
[1] 408
Code
# Missing values by variable
missing_values <- colSums(is.na(data))
# Show variables containing missing values
missing_values[missing_values > 0]
   t_angle    p_angle qrst_angle    j_angle heart_rate 
         8         22          1        376          1 
Code
# Distribution of the class
table(data$class)

  1   2   3   4   5   6   7   8   9  10  14  15  16 
245  44  15  15  13  25   3   2   9  50   4   5  22 
Code
# Percentage distribution
round(prop.table(table(data$class)) * 100, 2)

    1     2     3     4     5     6     7     8     9    10    14    15    16 
54.20  9.73  3.32  3.32  2.88  5.53  0.66  0.44  1.99 11.06  0.88  1.11  4.87 
Code
#Class Distribution
data %>%
  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 response
X <- data %>%
  select(-class)

y <- data$class

# Find columns with all missing values
all_missing <- colSums(!is.na(X)) == 0
# Remove predictors that contain only missing values
X <- 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 imputation
preprocess_model <- preProcess(
  X,
  method = "medianImpute"
)

X_clean <- predict(preprocess_model, X)

# Combine predictors and target again
data_clean <- X_clean
data_clean$class <- y

# Confirm no missing values remain
sum(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

Code
set.seed(123)

train_index <- createDataPartition(
  data_clean$class,
  p = 0.80,
  list = FALSE
)

train_data <- data_clean[train_index, ]
test_data <- data_clean[-train_index, ]

# Check dimensions
dim(train_data)
[1] 366 280
Code
dim(test_data)
[1]  86 280

Random Forest Model

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 results
rf_model

Call:
 randomForest(formula = class ~ ., data = train_data, ntree = 500,      importance = TRUE) 
               Type of random forest: classification
                     Number of trees: 500
No. of variables tried at each split: 16

        OOB estimate of  error rate: 27.87%
Confusion matrix:
     1  2  3 4 5 6 7 8 9 10 14 15 16 class.error
1  185  6  0 1 0 2 0 0 0  2  0  0  0  0.05612245
2   11 23  0 1 0 0 0 0 0  1  0  0  0  0.36111111
3    1  0 11 0 0 0 0 0 0  0  0  0  0  0.08333333
4    5  2  0 5 0 0 0 0 0  0  0  0  0  0.58333333
5    6  1  0 0 1 0 0 0 0  3  0  0  0  0.90909091
6   14  1  1 0 0 3 0 0 0  1  0  0  0  0.85000000
7    2  0  1 0 0 0 0 0 0  0  0  0  0  1.00000000
8    2  0  0 0 0 0 0 0 0  0  0  0  0  1.00000000
9    0  0  0 0 0 0 0 0 7  0  0  0  1  0.12500000
10  10  1  0 0 0 0 0 0 0 29  0  0  0  0.27500000
14   3  1  0 0 0 0 0 0 0  0  0  0  0  1.00000000
15   1  1  1 1 0 0 0 0 0  0  0  0  0  1.00000000
16  15  1  1 0 0 0 0 0 0  1  0  0  0  1.00000000

Model Predictions

The trained Random Forest model is used to predict arrhythmia classes for patients in the testing dataset.

Code
rf_predictions <- predict(
  rf_model,
  newdata = test_data
)

head(rf_predictions)
 2  7  8 17 22 25 
 6  1  1 10  1  1 
Levels: 1 2 3 4 5 6 7 8 9 10 14 15 16

Model Evaluation

Code
confusionMatrix(
  rf_predictions,
  test_data$class
)
Confusion Matrix and Statistics

          Reference
Prediction  1  2  3  4  5  6  7  8  9 10 14 15 16
        1  46  5  0  1  2  3  0  0  0  4  0  0  2
        2   2  2  0  0  0  0  0  0  0  0  0  1  1
        3   0  0  3  0  0  0  0  0  0  0  0  0  0
        4   0  0  0  2  0  0  0  0  0  0  0  0  0
        5   0  0  0  0  0  0  0  0  0  0  0  0  0
        6   0  0  0  0  0  2  0  0  0  0  0  0  0
        7   0  0  0  0  0  0  0  0  0  0  0  0  0
        8   0  0  0  0  0  0  0  0  0  0  0  0  0
        9   0  0  0  0  0  0  0  0  1  0  0  0  0
        10  1  1  0  0  0  0  0  0  0  6  0  0  1
        14  0  0  0  0  0  0  0  0  0  0  0  0  0
        15  0  0  0  0  0  0  0  0  0  0  0  0  0
        16  0  0  0  0  0  0  0  0  0  0  0  0  0

Overall Statistics
                                          
               Accuracy : 0.7209          
                 95% CI : (0.6138, 0.8123)
    No Information Rate : 0.5698          
    P-Value [Acc > NIR] : 0.002757        
                                          
                  Kappa : 0.5021          
                                          
 Mcnemar's Test P-Value : NA              

Statistics by Class:

                     Class: 1 Class: 2 Class: 3 Class: 4 Class: 5 Class: 6
Sensitivity            0.9388  0.25000  1.00000  0.66667  0.00000  0.40000
Specificity            0.5405  0.94872  1.00000  1.00000  1.00000  1.00000
Pos Pred Value         0.7302  0.33333  1.00000  1.00000      NaN  1.00000
Neg Pred Value         0.8696  0.92500  1.00000  0.98810  0.97674  0.96429
Prevalence             0.5698  0.09302  0.03488  0.03488  0.02326  0.05814
Detection Rate         0.5349  0.02326  0.03488  0.02326  0.00000  0.02326
Detection Prevalence   0.7326  0.06977  0.03488  0.02326  0.00000  0.02326
Balanced Accuracy      0.7397  0.59936  1.00000  0.83333  0.50000  0.70000
                     Class: 7 Class: 8 Class: 9 Class: 10 Class: 14 Class: 15
Sensitivity                NA       NA  1.00000   0.60000        NA   0.00000
Specificity                 1        1  1.00000   0.96053         1   1.00000
Pos Pred Value             NA       NA  1.00000   0.66667        NA       NaN
Neg Pred Value             NA       NA  1.00000   0.94805        NA   0.98837
Prevalence                  0        0  0.01163   0.11628         0   0.01163
Detection Rate              0        0  0.01163   0.06977         0   0.00000
Detection Prevalence        0        0  0.01163   0.10465         0   0.00000
Balanced Accuracy          NA       NA  1.00000   0.78026        NA   0.50000
                     Class: 16
Sensitivity            0.00000
Specificity            1.00000
Pos Pred Value             NaN
Neg Pred Value         0.95349
Prevalence             0.04651
Detection Rate         0.00000
Detection Prevalence   0.00000
Balanced Accuracy      0.50000

Variable Importance

Code
importance_values <- importance(rf_model)

importance_df <- data.frame(
  variable = rownames(importance_values),
  importance = importance_values[, "MeanDecreaseGini"]
)

importance_df <- importance_df %>%
  arrange(desc(importance))

head(importance_df, 10)
             variable importance
heart_rate heart_rate  10.173224
V228             V228   5.800083
V224             V224   5.682901
V197             V197   4.973999
V277             V277   4.971129
V91               V91   4.650230
V177             V177   4.046743
V93               V93   4.044396
V267             V267   4.016443
V167             V167   3.647277

Conclusions

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.

References

Google DeppMind. (2025). Gemini 3 Flash [how to code random forest model in r example code]. [https://www.google.com/search?q=how+to+code+random+forest+model+in+r+example+code&sca_esv=b5a5ca0fe1ad4433&biw=1301&bih=666&sxsrf=APpeQntfXhFFdvTeY-GstNHtyANXyyJK_A%3A1788637571944&ei=g3GcaoGUObnX5NoPtvCxmQM&oq=how+to+code+random+forest+model+in+R+&gs_lp=Egxnd3Mtd2l6LXNlcnAiJWhvdyB0byBjb2RlIHJhbmRvbSBmb3Jlc3QgbW9kZWwgaW4gUiAqAggBMgUQIRigATIFECEYoAEyBRAhGJ8FMgUQIRifBUjoG1DcBljcBnABeAGQAQCYAWqgAWqqAQMwLjG4AQHIAQD4AQGYAgKgAnvCAgoQABhHGNYEGLADwgIOEAAY5AIY1gQYsAPYAQHCAhcQLhjcBhi4BhjaBhjYAhjIAxiwA9gBAZgDAIgGAZAGD7oGBggBEAEYCZIHAzEuMaAHkgSyBwMwLjG4B3TCBwUwLjEuMcgHCIAIAQ&sclient=gws-wiz-serp]. Accessed September 5th, 2026.