Author: Milagros Ines Rodriguez Ventura
data <- read.table("./test_heart.csv", header=TRUE, sep=",")
head(data)
## age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal
## 1 63 1 3 145 233 1 0 150 0 2.3 0 0 1
## 2 37 1 2 130 250 0 1 187 0 3.5 0 0 2
## 3 41 0 1 130 204 0 0 172 0 1.4 2 0 2
## 4 56 1 1 120 236 0 1 178 0 0.8 2 0 2
## 5 57 0 0 120 354 0 1 163 1 0.6 2 0 2
## 6 57 1 0 140 192 0 1 148 0 0.4 1 0 1
## target
## 1 1
## 2 1
## 3 1
## 4 1
## 5 1
## 6 1
All variables are numerical, except sex, cp, restecg and thal, that are categorical
303 observations
nulls_per_variable <- colSums(is.na(data)) # Nulls check per variable
# Convert categorical variables to factors
data$sexFactor <- factor(data$sex,
levels = c(0, 1),
labels = c("F", "M"))
data$thalFactor <- factor(data$thal,
levels = c(0, 1, 2),
labels = c("Normal", "Fixed defect", "Reversible defect"))
# Note: I only did it with the variables sex and thal because I didn't have the references of the other categorical variables
# I want to see how the cholesterol values behave depending of the gender
library(psych)
## Warning: package 'psych' was built under R version 4.3.3
describeBy(data$chol, data$sexFactor)
##
## Descriptive statistics by group
## group: F
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 96 261.3 65.09 253 255.82 60.79 141 564 423 1.31 3.71 6.64
## ------------------------------------------------------------
## group: M
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 207 239.29 42.78 235 238.48 43 126 353 227 0.15 -0.32 2.97
By this results, we can conclude that, on average, the women of this sample have higher cholesterol level than men. This is, 261.3 mg/dl vs. 239.29 mg/dl.
We have to take into account that the number of samples for women and men differ by over a double, so the conclusions might not be accurate, since the more samples we have, the more sure we can be of the results.
# I want to see how the maximum heart achieved behaves according to the gender
library(psych)
describeBy(data$thalach, data$sexFactor)
##
## Descriptive statistics by group
## group: F
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 96 151.12 20.05 157 152.78 17.79 96 192 96 -0.74 -0.07 2.05
## ------------------------------------------------------------
## group: M
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 207 148.96 24.13 151 150.17 26.69 71 202 131 -0.44 -0.22 1.68
We can see that the standard deviation of the maximum heart rate achieved by the patient is higher in men than in women. This means that the values of the variable thalach are more condensed around the mean for women than for men.
Also, by looking at the median, we can conclude that for women, 50% of them have a maximum heart rate smaller than 157 and the remaining 50% have a maximum heart rate greater than 157. On the other hand, for men, the value that divides the sample is 151.
# Summary of the variables
summary(data)
## age sex cp trestbps
## Min. :29.00 Min. :0.0000 Min. :0.000 Min. : 94.0
## 1st Qu.:47.50 1st Qu.:0.0000 1st Qu.:0.000 1st Qu.:120.0
## Median :55.00 Median :1.0000 Median :1.000 Median :130.0
## Mean :54.37 Mean :0.6832 Mean :0.967 Mean :131.6
## 3rd Qu.:61.00 3rd Qu.:1.0000 3rd Qu.:2.000 3rd Qu.:140.0
## Max. :77.00 Max. :1.0000 Max. :3.000 Max. :200.0
## chol fbs restecg thalach
## Min. :126.0 Min. :0.0000 Min. :0.0000 Min. : 71.0
## 1st Qu.:211.0 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:133.5
## Median :240.0 Median :0.0000 Median :1.0000 Median :153.0
## Mean :246.3 Mean :0.1485 Mean :0.5281 Mean :149.6
## 3rd Qu.:274.5 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:166.0
## Max. :564.0 Max. :1.0000 Max. :2.0000 Max. :202.0
## exang oldpeak slope ca
## Min. :0.0000 Min. :0.00 Min. :0.000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:0.00 1st Qu.:1.000 1st Qu.:0.0000
## Median :0.0000 Median :0.80 Median :1.000 Median :0.0000
## Mean :0.3267 Mean :1.04 Mean :1.399 Mean :0.7294
## 3rd Qu.:1.0000 3rd Qu.:1.60 3rd Qu.:2.000 3rd Qu.:1.0000
## Max. :1.0000 Max. :6.20 Max. :2.000 Max. :4.0000
## thal target sexFactor thalFactor
## Min. :0.000 Min. :0.0000 F: 96 Normal : 2
## 1st Qu.:2.000 1st Qu.:0.0000 M:207 Fixed defect : 18
## Median :2.000 Median :1.0000 Reversible defect:166
## Mean :2.314 Mean :0.5446 NA's :117
## 3rd Qu.:3.000 3rd Qu.:1.0000
## Max. :3.000 Max. :1.0000
With the summary function we can see the max and min value of each variable in the dataset, the mean (average), the median (value that is in the middle) and the 1st and 3rd quartile (the value below which 25% of the data falls and the value below which 75% of the data falls, respectively)
hist(data$age,
main = 'Distribution of age',
xlab = 'Level of age',
col = 'darkolivegreen3')
By the shape of the graph we can infer that the distribution of the variable age is approximately normal, because it’s somewhat symmetric, the histogram has a “bell shape” and there is an absence of significant outliers that skew the distribution.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(data, aes(y = age)) +
geom_boxplot() +
ggtitle("Boxplot - age")
From the boxplot we can also see that the distribution of the variable is quite symmetric
hist(data$trestbps,
main = 'Distribution of trestbps',
xlab = 'Level of trestbps',
col = 'lightpink2')
By looking at this histogram I can say that it’s right-skewed and also that there are some outliers
library(ggplot2)
ggplot(data, aes(y=trestbps)) +
geom_boxplot()+
ggtitle("Boxplot - trestbps")
By looking at the boxplot I confirm the existence of outliers that might be the responsible for the skewness of the distribution
hist(data$chol,
main = 'Distribution of chol',
xlab = 'Level of chol',
col = 'lightblue3')
The same as before, by looking at the histogram we can say that the distribution of the variable chol is right skewed due to the existence of outliers
library(ggplot2)
ggplot(data, aes(y=chol)) +
geom_boxplot()+
ggtitle("Boxplot - chol")
In the boxplot we can clearly see the existence of outliers and that if we removed them the distribution would be approximately normal
hist(data$thalach,
main = 'Distribution of thalach',
xlab = 'Level of thalach',
col = 'darkorange3')
I can conclude that the distribution of the variable is left-skewed because the majority of the data points are clustered on the right side of the distribution, and the tail extends towards the left
library(ggplot2)
ggplot(data, aes(y=thalach)) +
geom_boxplot()+
ggtitle("Boxplot - thalach")
We can see that the median is displaced into the side of Q3 and also that here is one outlier
hist(data$oldpeak,
main = 'Distribution of oldpeak',
xlab = 'Level of oldpeak',
col = 'plum4')
We can say that as the level of oldpeak increases, the frequency of appearance of the value decreases
library(ggplot2)
ggplot(data, aes(y=oldpeak)) +
geom_boxplot()+
ggtitle("Boxplot - oldpeak")
We see that most of the data points are concentrated in the smaller values of the variable and that there are some bigger outliers
Besides the previous analysis I was intrigued to see if there was any correlation between the age and the cholesterol levels so I did a scatterplot:
plot(x=data$age, y=data$chol,
main = "Correlation age-cholesterol",
xlab = "age",
ylab = "chol",
col = ifelse(data$sexFactor == "M", "deepskyblue3", "tomato3"),
pch = 16)
legend("topright", legend = c("Female", "Male"),
col = c("deepskyblue3", "tomato3"), pch = 16, bty = "n")
I can conclude that there is not an apparent correlation between the variables and I can’t neither see a clear pattern between men and women