#Installation of some packages
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
install.packages("readxl")
## 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(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(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
set.seed(100)

source('Lab3_Helper_Functions.R')

# Problem 1a
lower = 0
upper = 10

fn_square = function(x) {
  return(x^2)
}

curve(fn_square, from=lower, to=upper,
      main="Function Square",
      xlab="x", ylab="x^2")

curve(sqrt, from=lower, to=upper,
      main="Function Square Root",
      xlab="x", ylab="sqrt(x)")

# ------------------------------------
# Problem 3a

#Plots for dage
kidney_data_2 <- read_excel('kidney_data-2.xlsx')
x <- kidney_data_2$dage
par(mfrow = c(3, 3))
par(mar = c(2, 2, 2, 1))

hist(x^3, main = "Cubed")
hist(x^2, main = "Squared")
hist(x, main = "Raw Data")
hist(sqrt(x), main = "Square Root")
hist(log(x), main = "Logarithm")
hist(1/sqrt(x), main = "Reciprocal Root")
hist(1/x, main = "Reciprocal")
hist(1/x^2, main = "Reciprocal Square")
hist(1/x^3, main = "Reciprocal Cube")

#Plots for cith
kidney_data_2 <- read_excel('kidney_data-2.xlsx')
x <- kidney_data_2$cith
par(mfrow = c(3, 3))
par(mar = c(2, 2, 2, 1))

hist(x^3, main = "Cubed")
hist(x^2, main = "Squared")
hist(x, main = "Raw Data")
hist(sqrt(x), main = "Square Root")
hist(log(x), main = "Logarithm")
hist(1/sqrt(x), main = "Reciprocal Root")
hist(1/x, main = "Reciprocal")
hist(1/x^2, main = "Reciprocal Square")
hist(1/x^3, main = "Reciprocal Cube")

# Problem 3b
dage <- kidney_data_2$dage
cith <- kidney_data_2$cith

dage_transformations <- data.frame(
  cube = dage^3,
  square = dage^2,
  raw = dage,
  sq = sqrt(dage),
  log_e = log(dage),
  recip_root = dage^(-1/2),
  recip = 1/dage,
  recip_sq = dage^(-2),
  recip_cube = dage^(-3)
)

cith_transformations <- data.frame(
  cube = cith^3,
  square = cith^2,
  raw = cith,
  sq = sqrt(cith),
  log_e = log(cith),
  recip_root = cith^(-1/2),
  recip = 1/cith,
  recip_sq = cith^(-2),
  recip_cube = cith^(-3)
)
transform_names <- c("Cube","Square","Raw",
                     "Square Root","Log","Reciprocal Root",
                     "Reciprocal","Reciprocal Square",
                     "Reciprocal Cube")

colnames(dage_transformations)<-transform_names
colnames(cith_transformations)<-transform_names

summary(dage_transformations)
##       Cube            Square          Raw         Square Root   
##  Min.   :  5832   Min.   : 324   Min.   :18.00   Min.   :4.243  
##  1st Qu.: 13824   1st Qu.: 576   1st Qu.:24.00   1st Qu.:4.899  
##  Median : 32768   Median :1024   Median :32.00   Median :5.657  
##  Mean   : 52212   Mean   :1262   Mean   :33.63   Mean   :5.718  
##  3rd Qu.: 68921   3rd Qu.:1681   3rd Qu.:41.00   3rd Qu.:6.403  
##  Max.   :328509   Max.   :4761   Max.   :69.00   Max.   :8.307  
##       Log        Reciprocal Root    Reciprocal      Reciprocal Square  
##  Min.   :2.890   Min.   :0.1204   Min.   :0.01449   Min.   :0.0002100  
##  1st Qu.:3.178   1st Qu.:0.1562   1st Qu.:0.02439   1st Qu.:0.0005949  
##  Median :3.466   Median :0.1768   Median :0.03125   Median :0.0009766  
##  Mean   :3.459   Mean   :0.1799   Mean   :0.03326   Mean   :0.0012286  
##  3rd Qu.:3.714   3rd Qu.:0.2041   3rd Qu.:0.04167   3rd Qu.:0.0017361  
##  Max.   :4.234   Max.   :0.2357   Max.   :0.05556   Max.   :0.0030864  
##  Reciprocal Cube    
##  Min.   :3.044e-06  
##  1st Qu.:1.451e-05  
##  Median :3.052e-05  
##  Mean   :4.959e-05  
##  3rd Qu.:7.234e-05  
##  Max.   :1.715e-04
apply(dage_transformations, 2, sd)
##              Cube            Square               Raw       Square Root 
##      5.581801e+04      8.742744e+02      1.146277e+01      9.694030e-01 
##               Log   Reciprocal Root        Reciprocal Reciprocal Square 
##      3.374978e-01      3.019698e-02      1.108993e-02      8.009439e-04 
##   Reciprocal Cube 
##      4.680139e-05
summary(cith_transformations)
##       Cube            Square            Raw         Square Root   
##  Min.   :     0   Min.   :   0.0   Min.   : 0.00   Min.   :0.000  
##  1st Qu.:  8946   1st Qu.: 430.8   1st Qu.:20.75   1st Qu.:4.555  
##  Median : 14724   Median : 600.5   Median :24.50   Median :4.949  
##  Mean   : 23461   Mean   : 744.6   Mean   :25.90   Mean   :5.005  
##  3rd Qu.: 27000   3rd Qu.: 900.0   3rd Qu.:30.00   3rd Qu.:5.477  
##  Max.   :157464   Max.   :2916.0   Max.   :54.00   Max.   :7.348  
##       Log        Reciprocal Root    Reciprocal      Reciprocal Square  
##  Min.   : -Inf   Min.   :0.1361   Min.   :0.01852   Min.   :0.0003429  
##  1st Qu.:3.032   1st Qu.:0.1826   1st Qu.:0.03333   1st Qu.:0.0011111  
##  Median :3.198   Median :0.2021   Median :0.04083   Median :0.0016681  
##  Mean   : -Inf   Mean   :   Inf   Mean   :    Inf   Mean   :      Inf  
##  3rd Qu.:3.401   3rd Qu.:0.2196   3rd Qu.:0.04821   3rd Qu.:0.0023257  
##  Max.   :3.989   Max.   :   Inf   Max.   :    Inf   Max.   :      Inf  
##  Reciprocal Cube    
##  Min.   :6.351e-06  
##  1st Qu.:3.704e-05  
##  Median :6.817e-05  
##  Mean   :      Inf  
##  3rd Qu.:1.122e-04  
##  Max.   :      Inf
apply(cith_transformations, 2, sd)
##              Cube            Square               Raw       Square Root 
##      2.593095e+04      5.050169e+02      8.634553e+00      9.253777e-01 
##               Log   Reciprocal Root        Reciprocal Reciprocal Square 
##               NaN               NaN               NaN               NaN 
##   Reciprocal Cube 
##               NaN