#Description-------------------
# This is a re-work of the eye size data I created in homework 8 using structural programming
 #  23 Mar 2022
 #JAG
 #-----------------------------
 
 #Intialize------------------
 library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(wesanderson)
## Warning: package 'wesanderson' was built under R version 4.1.3
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.1.3
library(patchwork)
## Warning: package 'patchwork' was built under R version 4.1.3
 #set.seed(100)
 
 #Load Functions--------------
 ###############################
 #FUNCTION:createDataframe
 # purpose:To take eyesize data and set them to adjust the size,mean, and SD affects the output
 # input: variables of eye size
 # output: a long dataframe that can be manipulated
 #------------------------------
createDataframe <- function(size=30, meanWT=290 , meanN= 240, sd=50 )
  {

WTeyesize <- rnorm(n=size, mean=meanWT, sd=sd)
  Nulleyesize <- rnorm(n=size, mean=meanN, sd=sd)
  
eye_size_df <- data.frame (WTeyesize,Nulleyesize)

long_df <- eye_size_df %>%
      pivot_longer(cols=WTeyesize:Nulleyesize, names_to= "Genotype" , values_to= "EyeSize") 


}
 
 data<-createDataframe()
 

###############################
#FUNCTION:t- test
# purpose: To perform a statistical analysis on the data
# input: an x variable and y variable
# output : the summary of the t-test 
#------------------------------
test <- function(x_var=eye_size_df$WTeyesize,y_var=eye_size_df$Nulleyesize)  {
  WTeyesize <- rnorm(n=100, mean=290 , sd=50)


Nulleyesize <-rnorm( n=100, mean=220 , sd=50)
.<-t.test(WTeyesize,Nulleyesize, paired=TRUE)

.<-print(.)
return(.)
}
 
###############################
#FUNCTION: graphData
# purpose: To visualize the differences in eyesize
# input:long eyesize dataframe
# output: a box plot of the two catergories of eye
#------------------------------
graphData <- function(data)  {

  p1 <- ggplot(data=data, aes(x=Genotype, y=EyeSize)) + geom_point(color="white" , fill="purple", size=0.2) +
  stat_boxplot()
  
  
  
  return(print(p1))
  
}

graphData(data)

#Second function here!
###############################
#FUNCTION: graphData2
# purpose: To visualize the differences in eyesize
# input:long eyesize dataframe
# output: a colored violin plot of the two categories of eyesize, with the t-test value on graph
#------------------------------
graphData2 <- function(data)  {
my_cols<-wes_palettes$IsleofDogs1[1:2]
  p2 <- ggplot(data=data, aes(x=Genotype, y=EyeSize, fill=Genotype))+geom_violin()+scale_fill_manual(values=my_cols)+ annotate("text", x = 1.19, y = 400, label = "p-value should be less the 0.05", size=2)
  
   
  
  
  return(print(p2))
  
}

graphData2(data)

 #Global Variables------------
 
 
 
 #Program Body----------------
 
data<- createDataframe()
print(data)
## # A tibble: 60 x 2
##    Genotype    EyeSize
##    <chr>         <dbl>
##  1 WTeyesize      250.
##  2 Nulleyesize    185.
##  3 WTeyesize      290.
##  4 Nulleyesize    168.
##  5 WTeyesize      286.
##  6 Nulleyesize    268.
##  7 WTeyesize      224.
##  8 Nulleyesize    302.
##  9 WTeyesize      337.
## 10 Nulleyesize    220.
## # ... with 50 more rows
test(x_var, y_var)
## 
##  Paired t-test
## 
## data:  WTeyesize and Nulleyesize
## t = 8.6605, df = 99, p-value = 9.044e-14
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  49.59785 79.07898
## sample estimates:
## mean of the differences 
##                64.33842
## 
##  Paired t-test
## 
## data:  WTeyesize and Nulleyesize
## t = 8.6605, df = 99, p-value = 9.044e-14
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  49.59785 79.07898
## sample estimates:
## mean of the differences 
##                64.33842
g1<-graphData(data)

g2<-graphData2(data)

g1+g2