# Problem 1
Acyanotic <- c(13.1, 14.0, 13.0, 14.2, 11.0, 12.2, 13.1, 11.6, 14.2, 20.5, 13.4, 13.5, 11.6, 12.1, 13.5, 13.0, 14.1, 14.7, 12.8)


Cyanotic <- c(13.0, 16.8, 17.6, 14.8, 13.9, 14.6, 13.0, 17.5, 14.8, 15.1, 19.3, 5.4)

# counts

n_a <- length(Acyanotic)
n_a
## [1] 19
n_c <- length(Cyanotic)
n_c
## [1] 12
# means

mean_a <- mean(Acyanotic)
mean_a
## [1] 13.45263
mean_c <- mean(Cyanotic)
mean_c
## [1] 14.65
# standard deviations 

sd_a <- sd(Acyanotic)
sd_a
## [1] 1.978037
sd_c <- sd(Cyanotic)
sd_c
## [1] 3.501818
# median

median_a <- median(Acyanotic)
median_a
## [1] 13.1
median_c <- median(Cyanotic)
median_c
## [1] 14.8
# quantiles

quantile_25_a <- quantile(Acyanotic, probs=0.25)
quantile_25_a
##  25% 
## 12.5
quantile_25_c <- quantile(Cyanotic, probs=0.25)
quantile_25_c
##    25% 
## 13.675
quantile_75_a <- quantile(Acyanotic, probs=0.75)
quantile_75_a
##   75% 
## 14.05
quantile_75_c <- quantile(Cyanotic, probs=0.75)
quantile_75_c
##    75% 
## 16.975
# iqr

iqr_a <- IQR(Acyanotic)
iqr_a
## [1] 1.55
iqr_c <- IQR(Cyanotic)
iqr_c
## [1] 3.3
# range

range_a <- range(Acyanotic)
range_a
## [1] 11.0 20.5
range_c <- range(Cyanotic)
range_c
## [1]  5.4 19.3
#min
min_a <- min(Acyanotic)
min_a
## [1] 11
min_c <- min(Cyanotic)
min_c
## [1] 5.4
#max
max_a <- max(Acyanotic)
max_a
## [1] 20.5
max_c <- max(Cyanotic)
max_c
## [1] 19.3
# ---------------------------------
# Problem 2

install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
install.packages("gridExtra")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(ggplot2)
library(gridExtra)

df <- data.frame(c(Acyanotic, Cyanotic), c(rep('Acyanotic', n_a), rep('Cyanotic', n_c)))

colnames(df) <- c('Hemoglobin Concentration', 'Diagnosis')
df$Diagnosis <- factor(df$Diagnosis)

# the box plot
ggplot(data = df) + geom_boxplot(aes(x = Diagnosis, y = `Hemoglobin Concentration`, fill = Diagnosis))

# the histogram
g_a <- ggplot() + geom_histogram(aes(x = Acyanotic),
                                 fill = 'skyblue', 
                                 color = 'black', 
                                 binwidth = 1) + 
  xlim(0, 20)

g_c <- ggplot() + geom_histogram(aes(x = Cyanotic),
                                 fill = 'magenta', 
                                 color = 'black', 
                                 binwidth = 1) + 
  xlim(0,20)

# graphs 
grid.arrange(g_a, g_c, nrow = 2)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_bin()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_bar()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_bar()`).

# ---------------------------------
# Problem 3
geometric_mean <- function(vector_of_numbers) {
  prod(vector_of_numbers)^(1/length(vector_of_numbers))
}

geometric_mean(Acyanotic)
## [1] 13.33661
mean(Acyanotic)
## [1] 13.45263
acyan_concentration <- df[df$Diagnosis == 'Acyanotic', 'Hemoglobin Concentration']
geom_mean_acyan <- geometric_mean(acyan_concentration)
geom_mean_acyan
## [1] 13.33661
#Do the same for Cyanotic and compare the means 
geometric_mean(Cyanotic)
## [1] 14.08901
mean(Cyanotic)
## [1] 14.65
cyan_concentration <- df[df$Diagnosis == 'Cyanotic', 'Hemoglobin Concentration']
geom_mean_cyan <- geometric_mean(cyan_concentration)
geom_mean_cyan
## [1] 14.08901