setwd("~/Google Drive/Agrosavia/colaboraciones/Laura")
datos3<-read.table("sanluis.csv", header=T, sep=',')
datos3$gen<-as.factor(datos3$gen)
datos3$forestal<-as.factor(datos3$forestal)
datos3$bloque<-as.factor(datos3$bloque)
attach(datos3)
library(ggplot2)
#Gráfica diámetro
ggplot(datos3, 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 137 rows containing non-finite values (stat_smooth).

# Gráfica altura
ggplot(datos3, 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 137 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=datos3, corr=corCompSymm(, form= ~ 1 | gen),na.action=na.exclude)
fit.ar1.diam <- gls(diam ~ semana*forestal*gen+bloque, data=datos3, corr=corAR1(, form= ~ 1 | gen), na.action=na.exclude)
fit.ar1het.diam <- gls(diam ~ semana*forestal*gen+bloque, data=datos3, 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 2341.345 2493.088 -1136.673                          
## fit.ar1.diam         2 34 2292.572 2444.315 -1112.286                          
## fit.ar1het.diam      3 36 2296.544 2457.213 -1112.272 2 vs 3 0.02815748   0.986
anova(fit.ar1.diam)
## Denom. DF: 641 
##                     numDF  F-value p-value
## (Intercept)             1 7420.471  <.0001
## semana                  1   40.360  <.0001
## forestal                2    3.978  0.0192
## gen                     4    1.139  0.3372
## bloque                  2    2.319  0.0991
## semana:forestal         2    0.733  0.4810
## semana:gen              4    0.149  0.9634
## forestal:gen            8    3.223  0.0013
## semana:forestal:gen     8    0.120  0.9984
anova(fit.ar1het.diam)
## Denom. DF: 641 
##                     numDF  F-value p-value
## (Intercept)             1 7415.488  <.0001
## semana                  1   40.093  <.0001
## forestal                2    3.992  0.0189
## gen                     4    1.139  0.3371
## bloque                  2    2.311  0.1000
## semana:forestal         2    0.728  0.4833
## semana:gen              4    0.148  0.9637
## forestal:gen            8    3.225  0.0013
## semana:forestal:gen     8    0.120  0.9985
# Análisis para altura
fit.compsym.alt <- gls(alt ~ semana*forestal*gen+bloque, data=datos3, corr=corCompSymm(, form= ~ 1 | gen),na.action=na.exclude)
fit.ar1.alt <- gls(alt ~ semana*forestal*gen+bloque, data=datos3, corr=corAR1(, form= ~ 1 | gen), na.action=na.exclude)
fit.ar1het.alt <- gls(alt ~ semana*forestal*gen+bloque, data=datos3, 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 6999.878 7151.621 -3465.939                       
## fit.ar1.alt         2 34 6962.748 7114.491 -3447.374                       
## fit.ar1het.alt      3 36 6965.877 7126.546 -3446.939 2 vs 3 0.87086   0.647
anova(fit.ar1het.alt)
## Denom. DF: 641 
##                     numDF  F-value p-value
## (Intercept)             1 5993.385  <.0001
## semana                  1  110.536  <.0001
## forestal                2    7.219  0.0008
## gen                     4    6.155  0.0001
## bloque                  2    1.822  0.1626
## semana:forestal         2    0.769  0.4637
## semana:gen              4    0.308  0.8725
## forestal:gen            8    7.771  <.0001
## semana:forestal:gen     8    0.190  0.9923
#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           b     Abarco
## Roble            b      Roble
## Terminalia       a 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         abc     Abarco:CCN51
## Abarco:TCS01           a     Abarco:TCS01
## Abarco:TCS06          ab     Abarco:TCS06
## Abarco:TCS13         abc     Abarco:TCS13
## Abarco:TCS19         abc     Abarco:TCS19
## Roble:CCN51           bc      Roble:CCN51
## Roble:TCS01          abc      Roble:TCS01
## Roble:TCS06          abc      Roble:TCS06
## Roble:TCS13          abc      Roble:TCS13
## Roble:TCS19          abc      Roble:TCS19
## Terminalia:CCN51     abc Terminalia:CCN51
## Terminalia:TCS01       c Terminalia:TCS01
## Terminalia:TCS06       a Terminalia:TCS06
## Terminalia:TCS13      ab Terminalia:TCS13
## Terminalia:TCS19     abc 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       b     CCN51
## TCS01       a     TCS01
## TCS06       a     TCS06
## TCS13       a     TCS13
## TCS19       a     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           b     Abarco
## Roble            b      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          de     Abarco:CCN51
## Abarco:TCS01           b     Abarco:TCS01
## Abarco:TCS06          ab     Abarco:TCS06
## Abarco:TCS13        acde     Abarco:TCS13
## Abarco:TCS19          ab     Abarco:TCS19
## Roble:CCN51            e      Roble:CCN51
## Roble:TCS01          cde      Roble:TCS01
## Roble:TCS06          abc      Roble:TCS06
## Roble:TCS13          acd      Roble:TCS13
## Roble:TCS19          acd      Roble:TCS19
## Terminalia:CCN51     acd Terminalia:CCN51
## Terminalia:TCS01     cde Terminalia:TCS01
## Terminalia:TCS06     acd Terminalia:TCS06
## Terminalia:TCS13      ab Terminalia:TCS13
## Terminalia:TCS19     abc Terminalia:TCS19
detach(datos3)