# Load necessary libraries
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.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
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.2
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
# Read the dataset
mpg<- read_delim("C:/Users/Jamil Shaikh/Desktop/heart.csv", delim = ",",show_col_types = FALSE)
# View the first few rows of the dataset
head(mpg)
## # A tibble: 6 × 13
## age sex cp trestbps chol fbs restecg thalach exang oldpeak slope
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 63 1 3 145 233 1 0 150 0 2.3 0
## 2 37 1 2 130 250 0 1 187 0 3.5 0
## 3 41 0 1 130 204 0 0 172 0 1.4 2
## 4 56 1 1 120 236 0 1 178 0 0.8 2
## 5 57 0 0 120 354 0 1 163 1 0.6 2
## 6 57 1 0 140 192 0 1 148 0 0.4 1
## # ℹ 2 more variables: ca <dbl>, target <dbl>
# Summary statistics of the dataset
summary(mpg)
## 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
## target
## Min. :0.0000
## 1st Qu.:0.0000
## Median :1.0000
## Mean :0.5446
## 3rd Qu.:1.0000
## Max. :1.0000
# Check for missing values
missing_values <- colSums(is.na(mpg))
print("Missing Values:")
## [1] "Missing Values:"
print(missing_values)
## age sex cp trestbps chol fbs restecg thalach
## 0 0 0 0 0 0 0 0
## exang oldpeak slope ca target
## 0 0 0 0 0
# Data distribution by the target variable
table(mpg$target)
##
## 0 1
## 138 165
# Distribution of age by target variable
ggplot(mpg, aes(x = age, fill = factor(target))) +
geom_histogram(binwidth = 5, position = "dodge", alpha = 0.7) +
labs(title = "Distribution of Age by Heart Disease Status",
x = "Age",
y = "Count",
fill = "Heart Disease") +
theme_minimal()

# Correlation matrix
cor_matrix <- cor(mpg[, -c(2, 6, 9, 11)]) # Exclude non-numeric columns
print("Correlation Matrix:")
## [1] "Correlation Matrix:"
print(cor_matrix)
## age cp trestbps chol restecg
## age 1.00000000 -0.06865302 0.27935091 0.213677957 -0.11621090
## cp -0.06865302 1.00000000 0.04760776 -0.076904391 0.04442059
## trestbps 0.27935091 0.04760776 1.00000000 0.123174207 -0.11410279
## chol 0.21367796 -0.07690439 0.12317421 1.000000000 -0.15104008
## restecg -0.11621090 0.04442059 -0.11410279 -0.151040078 1.00000000
## thalach -0.39852194 0.29576212 -0.04669773 -0.009939839 0.04412344
## oldpeak 0.21001257 -0.14923016 0.19321647 0.053951920 -0.05877023
## ca 0.27632624 -0.18105303 0.10138899 0.070510925 -0.07204243
## target -0.22543872 0.43379826 -0.14493113 -0.085239105 0.13722950
## thalach oldpeak ca target
## age -0.398521938 0.21001257 0.27632624 -0.22543872
## cp 0.295762125 -0.14923016 -0.18105303 0.43379826
## trestbps -0.046697728 0.19321647 0.10138899 -0.14493113
## chol -0.009939839 0.05395192 0.07051093 -0.08523911
## restecg 0.044123444 -0.05877023 -0.07204243 0.13722950
## thalach 1.000000000 -0.34418695 -0.21317693 0.42174093
## oldpeak -0.344186948 1.00000000 0.22268232 -0.43069600
## ca -0.213176928 0.22268232 1.00000000 -0.39172399
## target 0.421740934 -0.43069600 -0.39172399 1.00000000
#Pair plot for age, trestbps, chol, thalach, and target
ggpairs(mpg[, c("age", "trestbps", "chol", "thalach", "target")],
aes(color = factor(target)))
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero

#Pair plot for age and target
ggpairs(mpg[, c("age", "target")],
aes(color = factor(target)))
## Warning in cor(x, y): the standard deviation is zero
## Warning in cor(x, y): the standard deviation is zero
