library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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(readxl)
library(ggplot2)

# -------------------------
# Problem 1

df_1 <- 2
df_2 <- 7
df_3 <- 15
df_4 <- 35

# generate data
x_values <- seq(-5, 5, 0.01)
n <- length(x_values)
y_values_norm <- dnorm(x_values)
y_values_1 <- dt(x_values, df = df_1)
y_values_2 <- dt(x_values, df = df_2)
y_values_3 <- dt(x_values, df = df_3)
y_values_4 <- dt(x_values, df = df_4)


ordering <- c('normal', 
              paste('t, df = ', as.character(df_4)),
              paste('t, df = ', as.character(df_3)),
              paste('t, df = ', as.character(df_2)),
              paste('t, df = ', as.character(df_1)))

categories <- factor(rep(ordering, each = n), levels = ordering)

#create the dataframe 
dat <- data.frame(rep(x_values, 5), 
                  c(y_values_norm, y_values_4, y_values_3, y_values_2, y_values_1),
                  categories)

# label the columns of the dataframe
colnames(dat) <- c('x', 'y', 'distribution')

# plot the curves
bell_curve <- ggplot(data = dat) + geom_line(aes(x = x, y = y, color = distribution))

bell_curve

# ------------------------------
# Problem 2

kidney_data <- read_excel('kidney_data.xlsx')

untrimmed_CIT <- kidney_data$cith
  

trimmed_CIT <- untrimmed_CIT[which(between(untrimmed_CIT, 10, 40))]


# summary statistics prepared for you

summary(trimmed_CIT)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   11.00   21.00   24.00   25.18   30.00   40.00
length(trimmed_CIT)
## [1] 134
sd(trimmed_CIT)
## [1] 6.273049
max(trimmed_CIT)-min(trimmed_CIT)
## [1] 29
# --------------------------------
# Problem 3

ggplot() + geom_boxplot(aes(y = trimmed_CIT))

ggplot() + geom_histogram(aes(x = trimmed_CIT), 
                          fill = 'skyblue', 
                          color = 'black', 
                          binwidth = 3)

# --------------------------
# Problem 4 - qqplots

quantile_values <- seq(0.01,0.99,0.01)

x <- qnorm(quantile_values)

y <- quantile(trimmed_CIT, probs = quantile_values)

ggplot() + geom_point(aes(x = x, y = y)) + 
  ggtitle('q-q-plot for cith')