Homework 1

Saša Kočar

This data set was retrieved from Kaggle.com. Heart Disease Cleveland UCI. (https://www.kaggle.com/datasets/cherngs/heart-disease-cleveland-uci/data)

data <- read.csv("heart_cleveland_upload.csv")
data1 <- data[ , c(-3, -6, -7, -9, -10, -11, -12, -13)] #Deleting columns that are not relevant for my research
library(psych)
colnames(data1) <- c("Age","Sex","RestingBloodPressure","Cholesterol","MaxHeartRate","Condition")
data1$MaxHeartRate <- as.numeric(data1$MaxHeartRate)
data1$RestingBloodPressure <- as.numeric(data1$RestingBloodPressure)
data1$Cholesterol <- as.numeric(data1$Cholesterol)
data1$ConditionF <- factor(data1$Condition,
                           levels = c(0, 1),
                           labels = c("No_Disease", "Disease"))
head(data1)
##   Age Sex RestingBloodPressure Cholesterol MaxHeartRate Condition ConditionF
## 1  69   1                  160         234          131         0 No_Disease
## 2  69   0                  140         239          151         0 No_Disease
## 3  66   0                  150         226          114         0 No_Disease
## 4  65   1                  138         282          174         1    Disease
## 5  64   1                  110         211          144         0 No_Disease
## 6  64   1                  170         227          155         0 No_Disease

Description of data

Unit of Observation: Patient
Sample Size: 297
Age: Age of the patient in years (Numeric)
Sex: Gender of the patient (0 = Female, 1 = Male) (Categorical)
Resting blood pressure: resting blood pressure in mm Hg on admission to the hospital (Numeric)
Cholesterol: serum cholestoral in mg/dl (Numeric)
Max Heart Rate: Maximum heart rate achieved (Numeric)
Condition: Presence of heart disease (0 = No, 1 = Yes) (Categorical)
summary(data1[ ,c( -2, -6 )])
##       Age        RestingBloodPressure  Cholesterol     MaxHeartRate  
##  Min.   :29.00   Min.   : 94.0        Min.   :126.0   Min.   : 71.0  
##  1st Qu.:48.00   1st Qu.:120.0        1st Qu.:211.0   1st Qu.:133.0  
##  Median :56.00   Median :130.0        Median :243.0   Median :153.0  
##  Mean   :54.54   Mean   :131.7        Mean   :247.4   Mean   :149.6  
##  3rd Qu.:61.00   3rd Qu.:140.0        3rd Qu.:276.0   3rd Qu.:166.0  
##  Max.   :77.00   Max.   :200.0        Max.   :564.0   Max.   :202.0  
##       ConditionF 
##  No_Disease:160  
##  Disease   :137  
##                  
##                  
##                  
## 

Interpretation:

In my sample of 297 observations the median age of the sample population is 56 years old, with the youngest being 29 years old and the oldest being 77 years old. The median resting blood pressure is 130 mm Hg, with the lowest being 94 mm Hg and the highest being 200 mm Hg. The median cholesterol level is 243 mg/dl, with the lowest being 126 mg/dl and the highest being 564 mg/dl. The median maximum heart rate is 153 beats per minute, with the lowest being 71 beats per minute and the highest being 202 beats per minute. There is 160 patients without disease and 137 with disease.

Research Question:Is there a difference between maximum heart rate achieved between patients with and without heart disease?”

Null hypothesis (H0): The mean maximum heart rate achieved is the same for patients with heart disease and those without.
Alternate hypothesis (H1): The mean maximum heart rate achieved is different between patients with heart disease and those without.
I decided to use independent t-test to compare the means of the maximum heart rate achieved between patients with heart disease and those without (2 pupulations). We will use either parametric or non parametric test. Parametric tests have greater statistical power than non parametric tests, but can be used only if certain assumptions are met.

Assumptions:

Variable is numeric.
The distribution of the variable is normal in both populations (checked with Shapiro-Wilk test)
Data comes from two independant populations, which are in my case Disease and No Disease.
Variable has the same variance in both populations.
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggplot(data1, aes(x = MaxHeartRate, fill = factor(ConditionF))) +
  geom_histogram(position = "dodge", bins = 10) +
  scale_fill_manual(values = c("coral1", "darkolivegreen"), 
                    labels = c("Disease", "No_Disease")) +
  labs(title = "Distribution of Max Heart Rate by Condition",
       x = "HeartRate", y = "Frequency", fill = "Condition") +
  theme_bw()

Interpretation

The histogram shows the distribution of maximum heart rate by condition (Disease/No Disease). For those with a disease, the frequency is higher in the heart rate range of around 150-170. For individuals without a disease, the highest frequency is also in the same range but slightly less than those with a disease. For both conditions peak frequency is at around 150-170. However for maximum heart rate below 150, frequencies of no disease are higher than for disease, meanwhile for heart rate above 150, frequencies for disease are moslty higher than no disease.The range of heart rates is from approximately 90 to 200. To determine normality, we will have to perform Shapiro-Wilk test.

describeBy(data1$MaxHeartRate, data1$ConditionF)
## 
##  Descriptive statistics by group 
## group: No_Disease
##    vars   n   mean    sd median trimmed   mad min max range  skew kurtosis   se
## X1    1 160 158.58 19.04    161  160.05 16.31  96 202   106 -0.68     0.41 1.51
## ------------------------------------------------------------ 
## group: Disease
##    vars   n   mean    sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 137 139.11 22.71    142  139.78 25.2  71 195   124 -0.29     -0.3 1.94
# Shapiro-wilk test for checking normality
# For disease
shapiro.test(data1$MaxHeartRate[data1$ConditionF == "Disease"])
## 
##  Shapiro-Wilk normality test
## 
## data:  data1$MaxHeartRate[data1$ConditionF == "Disease"]
## W = 0.98967, p-value = 0.4053

H0: variable is normally distributed

H1: variable is not normally distributed

BSD, we do not reject H0 (P>0.05) and conclude that normality is not violated in group Disease.

#For No disease
shapiro.test(data1$MaxHeartRate[data1$ConditionF == "No_Disease"])
## 
##  Shapiro-Wilk normality test
## 
## data:  data1$MaxHeartRate[data1$ConditionF == "No_Disease"]
## W = 0.96822, p-value = 0.0009663

H0: variable is normally distributed

H1: variable is not normally distributed

BSD, we reject H0 (P<0.001) and conclude that normality is violated.

If normality is violated in one group, we perform non-parametric test (Wilcoxon rank-sum test). This test is used when comparing two independent samples to determine if there is a difference between their distributions.

wilcox.test(MaxHeartRate ~ ConditionF, data = data1, exact = FALSE, subset = ConditionF %in% c("Disease", "No_Disease")) #Non-Parametric Test (Robustness Check): using a Mann-Whitney U test as a non-parametric alternative
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  MaxHeartRate by ConditionF
## W = 16399, p-value = 1.675e-13
## alternative hypothesis: true location shift is not equal to 0

H0: The distribution location of max heart rate, is the same for patients with disease and without disease.

H1: The distribution location of max heart rate, is no the same for patients with disease and without disease.

We reject H0 at p<0.001.

#install.packages(effectsize)
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following object is masked from 'package:psych':
## 
##     phi
effectsize(wilcox.test(data1$MaxHeartRate,
                       mu = 142,
                       correct = FALSE))
## r (rank biserial) |       95% CI
## --------------------------------
## 0.39              | [0.27, 0.49]
## 
## - Deviation from a difference of 142.
interpret_rank_biserial(0.39, rules = "funder2019")
## [1] "large"
## (Rules: funder2019)

Based on the sample data, we find that max heart is not the same for patients with disease and without disease. We reject null hypothesis (p<0.001). Patients with disease have on average higher max heart rate, the difference in distribution is large (𝑟 = 0.39)