##Just Run This Chunk 
require(readxl)
## Loading required package: readxl
require(ggplot2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.0.5
require(plotly)
## Loading required package: plotly
## Warning: package 'plotly' was built under R version 4.0.5
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
require(emmeans)
## Loading required package: emmeans
require(multcomp)
## Loading required package: multcomp
## Loading required package: mvtnorm
## Loading required package: survival
## Loading required package: TH.data
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:plotly':
## 
##     select
## 
## Attaching package: 'TH.data'
## The following object is masked from 'package:MASS':
## 
##     geyser
require(tidyverse)
## Loading required package: tidyverse
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## v purrr   0.3.4
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag()    masks stats::lag()
## x dplyr::select() masks MASS::select(), plotly::select()
require(ggpubr)
## Loading required package: ggpubr
## Warning: package 'ggpubr' was built under R version 4.0.5
require(rstatix)
## Loading required package: rstatix
## Warning: package 'rstatix' was built under R version 4.0.5
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
require(lmerTest)
## Loading required package: lmerTest
## Warning: package 'lmerTest' was built under R version 4.0.5
## Loading required package: lme4
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Registered S3 methods overwritten by 'lme4':
##   method                          from
##   cooks.distance.influence.merMod car 
##   influence.merMod                car 
##   dfbeta.influence.merMod         car 
##   dfbetas.influence.merMod        car
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
require(Rmisc)
## Loading required package: Rmisc
## Warning: package 'Rmisc' was built under R version 4.0.5
## Loading required package: lattice
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 4.0.5
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:rstatix':
## 
##     desc, mutate
## The following object is masked from 'package:ggpubr':
## 
##     mutate
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following object is masked from 'package:purrr':
## 
##     compact
## The following objects are masked from 'package:plotly':
## 
##     arrange, mutate, rename, summarise
LeafLength = read_excel("C:/Users/PC/Desktop/La U/Quinto Semestre/Bioestadistica/Clase 10 -/LeafLength.xlsx")
LeafLength$Temp_C02=paste(LeafLength$Temp,"_and_",LeafLength$CO2,sep="")
LeafLength$id=as.factor(paste(LeafLength$Cab,"_",LeafLength$Obs,sep=""))
LeafLength$Genotype=as.factor(LeafLength$Genotype)
##Time Series Plot by Temp-CO2 Treatment

gd = summarySE(LeafLength, measurevar="LeafL", groupvars=c("Temp_C02","Time","Genotype"))


g1=ggplot(gd,aes(x=Time,y=LeafL,color=Temp_C02, group =Temp_C02))+ geom_line(data=gd)+geom_point(data=gd, size = 2)+ facet_grid(~Genotype)+theme_bw()+
  labs(title = "Leaf Length",
       x = "Time in days",
       y = "Length in cm")


g1

En la gráfica se observa que en el caso de la variedad PA107 al ser sometida a diferentes temperaturas y concentraciones de dióxido de carbono obtiene la mayor longitud de hojas entre 22 a 31 grados celsius con una concentración de dióxido de carbono de 400

##Anova de Medidas Repetidas

LeafLength$Time=as.factor(LeafLength$Time)
LeafLength$Temp=as.factor(LeafLength$Temp)
LeafLength$CO2=as.factor(LeafLength$CO2)

anova=aov(LeafL~Temp*CO2*Genotype+Time+Error(id/Genotype/Time),data=LeafLength)

summary(anova)
## 
## Error: id
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## Temp       2   6614    3307  17.188 1.06e-05 ***
## CO2        1    214     214   1.113     0.30    
## Temp:CO2   2    396     198   1.028     0.37    
## Residuals 30   5772     192                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: id:Genotype
##                   Df Sum Sq Mean Sq F value  Pr(>F)   
## Genotype           1    871     871   2.016 0.16598   
## Temp:Genotype      2   7270    3635   8.409 0.00126 **
## CO2:Genotype       1     46      46   0.108 0.74522   
## Temp:CO2:Genotype  2   2673    1337   3.092 0.06013 . 
## Residuals         30  12968     432                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: id:Genotype:Time
##             Df Sum Sq Mean Sq F value Pr(>F)    
## Time        18  17707   983.7   140.2 <2e-16 ***
## Residuals 1278   8964     7.0                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##Postanova CO2


bxp = ggboxplot(
  LeafLength, x = "Time", y = "LeafL",
  color = "CO2", palette = "jco"
)


pwc = LeafLength %>%
  group_by(Time) %>%
  pairwise_t_test(
    LeafL ~ CO2, paired = TRUE,
    p.adjust.method = "bonferroni"
  )


pwc = pwc %>% add_xy_position(x = "Time") 
bxp + 
  stat_pvalue_manual(pwc, tip.length = 0, hide.ns = TRUE) +
  labs(
    subtitle = paste("Anova, ","F(1)", " = ", "1.113, ", "p", " = ", "0.30"),
    caption = get_pwc_label(pwc)
  )

En esta gráfica se obseva que la cantidades de dioxido de carbono no son muy representativas repsecto al tamaño de la hoja y al tiempo solo el segundo y el tercer dato presentan significancia pero no es muy alta

##POstanova Temp
  
  
bxp = ggboxplot(
  LeafLength, x = "Time", y = "LeafL",
  color = "Temp", palette = "jco"
)

pwc = LeafLength %>%
  group_by(Time) %>%
  pairwise_t_test(
    LeafL ~ Temp, paired = TRUE,
    p.adjust.method = "bonferroni"
  )


pwc = pwc %>% add_xy_position(x = "Time") 
bxp + 
  stat_pvalue_manual(pwc, tip.length = 0, hide.ns = TRUE) +
  labs(
    subtitle = paste("Anova, ","F(2)", " = ", "17.188 , ", "p", " = ", "<0.0001 "),
    caption = get_pwc_label(pwc)
  )

En cuanto a la temperatura si se observan altos niveles de significancia en donde la temperatura que porporciona mayor tamaño en la hoja están entre los 22 y 31 grados

 ##Postanova Temp-Genotipo
  
  

bxp = ggboxplot(
  LeafLength, x = "Time", y = "LeafL",
  title= "Leaf Length",
  xlab ="time (days)", ylab= "Length (cm)",
  color = "Temp", palette = "lancet",
  facet.by = "Genotype"
)

pwc = LeafLength %>%
  group_by(Time, Genotype) %>%
  pairwise_t_test(
    LeafL ~ Temp, paired = TRUE,
    p.adjust.method = "bonferroni"
  )


pwc = pwc %>% add_xy_position(x = "Time") 
bxp + 
  stat_pvalue_manual(pwc, tip.length = 0, hide.ns = TRUE) +
  labs(
    subtitle = paste("Anova, ","F(2)", " = ", "8.409 , ", "p", " = ", "0.00126"),
    caption = get_pwc_label(pwc)
  )

en cuanto a la relación de la temperatura con la longitud de la hoja y los genotipos, se observa que el genotipo SCA6 tiene un alto nivel de significancia con la temperatura, donde los intervalos de 22 a 31 presentan mejores resultados respecto a los otros dos intervalos. En cuanto al genotipo PA107 este no presenta mucha variación al aumentar la temperatura indicando que puede ser plantada en gran variedad de lugares y obtener los mismos resultados