Myriam Aicha Mbongo 10/16/2023
| Student ID Numbers and Names of Group Members | GitHub Classroom Group Name |
|---|---|
| 2. 134141 - C - Aicha Mbongo |
| Course Code | Course Name | Program | Semester Duration |
|---|---|---|---|
| BBT4206 | Business Intelligence II | Bachelor of Business Information Technology | 21st August 2023 to 28th November 2023 |
library(readr)
heart <- read_csv(
"../data/heart.csv",
col_types = cols(
age = col_double(),
sex = col_factor(levels = c("0", "1")),
cp = col_factor(levels = c("0", "1", "2", "3")),
trestbps = col_double(),
chol = col_double(),
fbs = col_factor(levels = c("0", "1")),
restecg = col_factor(levels = c("0", "1", "2")),
thalach = col_double(),
exang = col_factor(levels = c("0", "1")),
oldpeak = col_double(),
slope = col_factor(levels = c("0", "1", "2")),
ca = col_double(),
thal = col_factor(levels = c("0", "1", "2", "3")),
target = col_factor(levels = c("neg", "pos"))
)
)
#View(heart)### STEP 3a. Pre#View the Loaded Datasets, Identify the Data Types ----
# Dimensions refer to the number of observations (rows) and the number of
# attributes/variables/features (columns).
#Understanding data types is key for effective analysis.It helps choose suitable visualizations and algorithms,
#and highlights the need for conversions between categorical and numerical data when necessary.
dim(heart)## [1] 1025 14
## age sex cp trestbps chol fbs restecg thalach
## "numeric" "factor" "factor" "numeric" "numeric" "factor" "factor" "numeric"
## exang oldpeak slope ca thal target
## "factor" "numeric" "factor" "numeric" "factor" "factor"
# It is more sensible to count categorical variables (factors or dimensions)
# than numeric variables, e.g., counting the number of male and female
# participants instead of counting the frequency of each participant’s height.
heart_freq <- heart$target
cbind(frequency = table(heart_freq),
percentage = prop.table(table(heart_freq)) * 100)## frequency percentage
## neg 499 48.68293
## pos 526 51.31707
# We, therefore, must manually create a function that can calculate the mode.
heart_target_mode <- names(table(heart$target))[
which(table(heart$target) == max(table(heart$target)))
]
print(heart_target_mode)## [1] "pos"
## age sex cp trestbps chol fbs restecg
## Min. :29.00 0:312 0:497 Min. : 94.0 Min. :126 0:872 0:497
## 1st Qu.:48.00 1:713 1:167 1st Qu.:120.0 1st Qu.:211 1:153 1:513
## Median :56.00 2:284 Median :130.0 Median :240 2: 15
## Mean :54.43 3: 77 Mean :131.6 Mean :246
## 3rd Qu.:61.00 3rd Qu.:140.0 3rd Qu.:275
## Max. :77.00 Max. :200.0 Max. :564
## thalach exang oldpeak slope ca thal
## Min. : 71.0 0:680 Min. :0.000 0: 74 Min. :0.0000 0: 7
## 1st Qu.:132.0 1:345 1st Qu.:0.000 1:482 1st Qu.:0.0000 1: 64
## Median :152.0 Median :0.800 2:469 Median :0.0000 2:544
## Mean :149.1 Mean :1.072 Mean :0.7541 3:410
## 3rd Qu.:166.0 3rd Qu.:1.800 3rd Qu.:1.0000
## Max. :202.0 Max. :6.200 Max. :4.0000
## target
## neg:499
## pos:526
##
##
##
##
# calculate the standard deviation of only columns that are numeric, thus
# leaving out the columns termed as “factors” (categorical) or those that have
# a string data type.
sapply(heart[, -c(2, 3, 6, 7, 9, 11, 13, 14)], sd)## age trestbps chol thalach oldpeak ca
## 9.072290 17.516718 51.592510 23.005724 1.175053 1.030798
## age trestbps chol thalach oldpeak ca
## 9.072290 17.516718 51.592510 23.005724 1.175053 1.030798
# The Kurtosis informs you of how often outliers occur in the results.
# There are different formulas for calculating kurtosis.
# Specifying “type = 2” allows us to use the 2nd formula which is the same
# kurtosis formula used in SPSS and SAS.
# In “type = 2” (used in SPSS and SAS):
# 1. Kurtosis < 3 implies a low number of outliers
# 2. Kurtosis = 3 implies a medium number of outliers
# 3. Kurtosis > 3 implies a high number of outliers
if (!is.element("e1071", installed.packages()[, 1])) {
install.packages("e1071", dependencies = TRUE)
}
require("e1071")## Loading required package: e1071
## age trestbps chol thalach oldpeak ca
## -0.52561781 0.99122074 3.99680305 -0.08882249 1.31447089 0.70112287
# The skewness informs you of the asymmetry of the distribution of results.
# Using “type = 2” can be interpreted as:
# 1. Skewness between -0.4 and 0.4 (inclusive) implies that there is no skew
# in the distribution of results; the distribution of results is symmetrical;
# it is a normal distribution.
# 2. Skewness above 0.4 implies a positive skew; a right-skewed distribution.
# 3. Skewness below -0.4 implies a negative skew; a left-skewed distribution.
sapply(heart[, -c(2, 3, 6, 7, 9, 11, 13, 14)], skewness, type = 2)## age trestbps chol thalach oldpeak ca
## -0.2488659 0.7397682 1.0740728 -0.5137772 1.2108994 1.2611886
# Note that the covariance and the correlation are computed for numeric values
# only, not categorical values.
heart_cov <- cov(heart[, -c(2, 3, 6, 7, 9, 11, 13, 14)])
#View(heart_cov)# One-Way ANOVA can be used to test the effect of the 3 types of fertilizer on
# crop yield whereas,
# Two-Way ANOVA can be used to test the effect of the 3 types of fertilizer and
# the 2 types of planting density on crop yield.
heart_one_way_anova <- aov(trestbps ~ age, data = heart)
summary(heart_one_way_anova)## Df Sum Sq Mean Sq F value Pr(>F)
## age 1 23096 23096 81.16 <2e-16 ***
## Residuals 1023 291104 285
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#The ANOVA rejects the null hypothesis,The ANOVA indicates a significant difference in resting blood pressure among age groups
#(F(1, 1023) = 81.16, p < 2e-16), highlighting age as a key factor
#in determining blood pressure.
#This aligns with cardiovascular knowledge, correlating increased age with a higher risk of cardiovascular disease.
heart_two_way_anova <- aov(trestbps ~ exang + ca, # nolint
data = heart)
summary(heart_two_way_anova)## Df Sum Sq Mean Sq F value Pr(>F)
## exang 1 1177 1176.7 3.88 0.04914 *
## ca 1 3050 3050.2 10.06 0.00156 **
## Residuals 1022 309973 303.3
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#both variables, exercise-induced angina and the number of major vessels, are associated with statistically significant differences in
#resting blood pressure.# Histograms help in determining whether an attribute has a Gaussian
# distribution. They can also be used to identify the presence of outliers.
#for (i in c(1, 4, 5)) {
# hist(heart[, i], main = names(heart)[i])
#}
#hist(heart[, 8], main = names(heart)[8])
#hist(heart[, 10], main = names(heart)[10])
#hist(heart[, 12], main = names(heart)[12])# Box and whisker plots are useful in understanding the distribution of data.
par(mfrow = c(1, 3))
for (i in c(1, 4, 5)) {
boxplot(heart[, i], main = names(heart)[i])
}boxplot(heart[, 8], main = names(heart)[8])
boxplot(heart[, 10], main = names(heart)[10])
boxplot(heart[, 12], main = names(heart)[12])# Categorical attributes (factors) can also be visualized. This is done using a
# bar chart to give an idea of the proportion of instances that belong to each
# category.
barplot(table(heart[, 2]), main = names(heart)[2])







# Execute the following to create a map to identify the missing data in each
# dataset:
if (!is.element("Amelia", installed.packages()[, 1])) {
install.packages("Amelia", dependencies = TRUE)
}
require("Amelia")## Loading required package: Amelia
## Loading required package: Rcpp
## ##
## ## Amelia II: Multiple Imputation
## ## (Version 1.8.1, built: 2022-11-18)
## ## Copyright (C) 2005-2023 James Honaker, Gary King and Matthew Blackwell
## ## Refer to http://gking.harvard.edu/amelia/ for more information
## ##

# Correlation plots can be used to get an idea of which attributes change
# together. The function “corrplot()” found in the package “corrplot” is
# required to perform this. The larger the dot in the correlation plot, the
# larger the correlation. Blue represents a positive correlation whereas red
# represents a negative correlation.
if (!is.element("corrplot", installed.packages()[, 1])) {
install.packages("corrplot", dependencies = TRUE)
}
require("corrplot")## Loading required package: corrplot
## corrplot 0.92 loaded
corrplot(cor(heart[, -c(2, 3, 6, 7, 9, 11, 13, 14)]), method = "circle")
#heart <- heart[, -which(names(heart) == "target_numeric")]
# Alternatively, the 'ggcorrplot::ggcorrplot()' function can be used to plot a
# more visually appealing plot.
# The code below shows how to install a package in R:
if (!is.element("ggcorrplot", installed.packages()[, 1])) {
install.packages("ggcorrplot", dependencies = TRUE)
}
require("ggcorrplot")## Loading required package: ggcorrplot
## Loading required package: ggplot2

# Alternatively, the ggcorrplot package can be used to make the plots more
# appealing:
ggplot(heart,
aes(x = age, y = sex, shape = target, color = target)) +
geom_point() +
geom_smooth(method = lm)## `geom_smooth()` using formula = 'y ~ x'

## The following package(s) will be updated in the lockfile:
##
## # CRAN -----------------------------------------------------------------------
## - formatR [* -> 1.14]
##
## - Lockfile written to "C:/Users/HP/Desktop/BI labs/BIProject/markdown/renv.lock".