setwd("~/Google Drive/Agrosavia/colaboraciones/Laura")
datos2<-read.table("paez.csv", header=T, sep=',')
datos2$gen<-as.factor(datos2$gen)
datos2$forestal<-as.factor(datos2$forestal)
datos2$bloque<-as.factor(datos2$bloque)
attach(datos2)
library(ggplot2)
#Gráfica diámetro
ggplot(datos2, aes(semana, diam, group = gen, colour = gen)) +
  facet_grid(~forestal) +
  geom_smooth(method="lm", se=F) +
  theme_classic() +
  xlab ("Semana") +
  ylab ("Diámetro") +
  labs(colour = "Genotipo") +
  theme_linedraw() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16),
    strip.text = element_text(size = 16),
    axis.title.y = element_text(size = 16),
    axis.title.x = element_text(size = 16),
    axis.text.x = element_text(size = 14),
    axis.text.y = element_text(size = 14)
  )  
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 275 rows containing non-finite values (stat_smooth).

# Gráfica altura
ggplot(datos2, aes(semana, alt, group = gen, colour = gen)) +
  facet_grid(~forestal) +
  geom_smooth(method="lm", se=F) +
  theme_classic() +
  xlab ("Semana") +
  ylab ("Altura") +
  labs(colour = "Genotipo") +
  theme_linedraw() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16),
    strip.text = element_text(size = 16),
    axis.title.y = element_text(size = 16),
    axis.title.x = element_text(size = 16),
    axis.text.x = element_text(size = 14),
    axis.text.y = element_text(size = 14)
  )
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 275 rows containing non-finite values (stat_smooth).

# Anova general
aov.diam<-aov(diam~semana*forestal*gen+bloque)
aov.alt<-aov(alt~semana*forestal*gen+bloque)
#Análisis para diámetro
library(nlme)
fit.compsym.diam <- gls(diam ~ semana*forestal*gen+bloque, data=datos2, corr=corCompSymm(, form= ~ 1 | gen),na.action=na.exclude)
fit.ar1.diam <- gls(diam ~ semana*forestal*gen+bloque, data=datos2, corr=corAR1(, form= ~ 1 | gen), na.action=na.exclude)
fit.ar1het.diam <- gls(diam ~ semana*forestal*gen+bloque, data=datos2, corr=corAR1(, form= ~ 1 | gen), weights=varIdent(form = ~ 1 | semana), na.action=na.exclude)
anova(fit.compsym.diam, fit.ar1.diam, fit.ar1het.diam) #compares the models
##                  Model df      AIC      BIC    logLik   Test  L.Ratio p-value
## fit.compsym.diam     1 34 2325.655 2483.765 -1128.828                        
## fit.ar1.diam         2 34 2313.326 2471.436 -1122.663                        
## fit.ar1het.diam      3 37 2312.321 2484.381 -1119.160 2 vs 3 7.005676  0.0717
anova(fit.ar1.diam)
## Denom. DF: 773 
##                     numDF  F-value p-value
## (Intercept)             1 9933.582  <.0001
## semana                  1  219.131  <.0001
## forestal                2   14.401  <.0001
## gen                     4    1.644  0.1612
## bloque                  2   33.825  <.0001
## semana:forestal         2    5.243  0.0055
## semana:gen              4    0.284  0.8886
## forestal:gen            8    2.082  0.0352
## semana:forestal:gen     8    1.055  0.3929
anova(fit.ar1het.diam)
## Denom. DF: 773 
##                     numDF   F-value p-value
## (Intercept)             1 10072.937  <.0001
## semana                  1   258.211  <.0001
## forestal                2    12.715  <.0001
## gen                     4     1.532  0.1911
## bloque                  2    35.498  <.0001
## semana:forestal         2     5.858  0.0030
## semana:gen              4     0.302  0.8768
## forestal:gen            8     1.939  0.0515
## semana:forestal:gen     8     1.092  0.3662
# Análisis para altura
fit.compsym.alt <- gls(alt ~ semana*forestal*gen+bloque, data=datos2, corr=corCompSymm(, form= ~ 1 | gen),na.action=na.exclude)
fit.ar1.alt <- gls(alt ~ semana*forestal*gen+bloque, data=datos2, corr=corAR1(, form= ~ 1 | gen), na.action=na.exclude)
fit.ar1het.alt <- gls(alt ~ semana*forestal*gen+bloque, data=datos2, corr=corAR1(, form= ~ 1 | gen), weights=varIdent(form = ~ 1 | semana), na.action=na.exclude)
anova(fit.compsym.alt, fit.ar1.alt, fit.ar1het.alt) #compares the models
##                 Model df      AIC      BIC    logLik   Test  L.Ratio p-value
## fit.compsym.alt     1 34 7958.484 8116.593 -3945.242                        
## fit.ar1.alt         2 34 7941.956 8100.066 -3936.978                        
## fit.ar1het.alt      3 37 7878.555 8050.615 -3902.277 2 vs 3 69.40135  <.0001
anova(fit.ar1het.alt)
## Denom. DF: 773 
##                     numDF  F-value p-value
## (Intercept)             1 7690.972  <.0001
## semana                  1  542.437  <.0001
## forestal                2   30.987  <.0001
## gen                     4    6.351  <.0001
## bloque                  2   23.493  <.0001
## semana:forestal         2   11.574  <.0001
## semana:gen              4    4.154  0.0025
## forestal:gen            8    2.108  0.0329
## semana:forestal:gen     8    1.871  0.0615
#Tukey diámetro
library(multcompView)
interac.tuk.diam<-TukeyHSD(aov.diam, "forestal:gen", ordered = TRUE)
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## gen
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal, gen
fores.tuk.diam<-TukeyHSD(aov.diam, "forestal", ordered = TRUE)
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## gen
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal, gen
gen.tuk.diam<-TukeyHSD(aov.diam, "gen", ordered = TRUE)
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## gen
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal, gen
#Tukey altura
interac.tuk.alt<-TukeyHSD(aov.alt, "forestal:gen", ordered = TRUE)
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## gen
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal, gen
fores.tuk.alt<-TukeyHSD(aov.alt, "forestal", ordered = TRUE)
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## gen
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal, gen
gen.tuk.alt<-TukeyHSD(aov.alt, "gen", ordered = TRUE)
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## gen
## Warning in replications(paste("~", xx), data = mf): non-factors ignored: semana,
## forestal, gen
#Etiquetas Tukey diámetro
#Genotipos
generate_label_df_gen_diam <- function(gen.tuk.diam, variable){
  Tukey.levels <- gen.tuk.diam[[variable]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}
labels.gen.diam <- generate_label_df_gen_diam(gen.tuk.diam, "gen")
labels.gen.diam
##       Letters treatment
## CCN51       a     CCN51
## TCS01       a     TCS01
## TCS06       a     TCS06
## TCS13       a     TCS13
## TCS19       a     TCS19
# Forestal
generate_label_df_forestal_diam <- function(fores.tuk.diam, variable){
  Tukey.levels <- fores.tuk.diam[[variable]][,2]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}
labels.forestal.diam <- generate_label_df_forestal_diam(fores.tuk.diam, "forestal")
labels.forestal.diam
##            Letters  treatment
## Abarco           a     Abarco
## Roble           ab      Roble
## Terminalia       b Terminalia
# Interacción Forestal:Genotipo
generate_label_df_interac_diam <- function(interac.tuk.diam, variable){
  Tukey.levels <- interac.tuk.diam[[variable]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}
labels.interac.diam <- generate_label_df_interac_diam(interac.tuk.diam, "forestal:gen")
labels.interac.diam
##                  Letters        treatment
## Abarco:CCN51          ac     Abarco:CCN51
## Abarco:TCS01          ac     Abarco:TCS01
## Abarco:TCS06          ac     Abarco:TCS06
## Abarco:TCS13          ac     Abarco:TCS13
## Abarco:TCS19          ac     Abarco:TCS19
## Roble:CCN51           ac      Roble:CCN51
## Roble:TCS01          abc      Roble:TCS01
## Roble:TCS06           ab      Roble:TCS06
## Roble:TCS13            b      Roble:TCS13
## Roble:TCS19           ab      Roble:TCS19
## Terminalia:CCN51      ac Terminalia:CCN51
## Terminalia:TCS01      ac Terminalia:TCS01
## Terminalia:TCS06       c Terminalia:TCS06
## Terminalia:TCS13      ac Terminalia:TCS13
## Terminalia:TCS19       c Terminalia:TCS19
#Etiquetas Tukey altura
#Genotipos
generate_label_df_gen_alt <- function(gen.tuk.alt, variable){
  Tukey.levels <- gen.tuk.alt[[variable]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}
labels.gen.alt <- generate_label_df_gen_alt(gen.tuk.alt, "gen")
labels.gen.alt
##       Letters treatment
## CCN51      ac     CCN51
## TCS01       c     TCS01
## TCS06      ab     TCS06
## TCS13       b     TCS13
## TCS19      ab     TCS19
# Forestal
generate_label_df_forestal_alt <- function(fores.tuk.alt, variable){
  Tukey.levels <- fores.tuk.alt[[variable]][,2]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}
labels.forestal.alt <- generate_label_df_forestal_alt(fores.tuk.alt, "forestal")
labels.forestal.alt
##            Letters  treatment
## Abarco           a     Abarco
## Roble            a      Roble
## Terminalia       a Terminalia
# Interacción Forestal:Genotipo
generate_label_df_interac_alt <- function(interac.tuk.alt, variable){
  Tukey.levels <- interac.tuk.alt[[variable]][,4]
  Tukey.labels <- data.frame(multcompLetters(Tukey.levels)['Letters'])
  Tukey.labels$treatment=rownames(Tukey.labels)
  Tukey.labels=Tukey.labels[order(Tukey.labels$treatment) , ]
  return(Tukey.labels)
}
labels.interac.alt <- generate_label_df_interac_alt(interac.tuk.alt, "forestal:gen")
labels.interac.alt
##                  Letters        treatment
## Abarco:CCN51         cde     Abarco:CCN51
## Abarco:TCS01          de     Abarco:TCS01
## Abarco:TCS06          cd     Abarco:TCS06
## Abarco:TCS13          cd     Abarco:TCS13
## Abarco:TCS19         acd     Abarco:TCS19
## Roble:CCN51          acd      Roble:CCN51
## Roble:TCS01          acd      Roble:TCS01
## Roble:TCS06           ab      Roble:TCS06
## Roble:TCS13            b      Roble:TCS13
## Roble:TCS19           ac      Roble:TCS19
## Terminalia:CCN51      de Terminalia:CCN51
## Terminalia:TCS01       e Terminalia:TCS01
## Terminalia:TCS06      de Terminalia:TCS06
## Terminalia:TCS13      de Terminalia:TCS13
## Terminalia:TCS19    acde Terminalia:TCS19
detach(datos2)