setwd("G:/Mi unidad/Agrosavia/FeCa/Fenoma/Análisis/AguaS")
datos<-read.table("humecta.csv", header=T, sep=';')

##Librerias
library(lme4)
## Cargando paquete requerido: Matrix
library(lmerTest)   # p-values
## Warning: package 'lmerTest' was built under R version 4.4.3
## 
## Adjuntando el paquete: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
library(emmeans)    # post hoc
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## 
## Adjuntando el paquete: 'tidyr'
## The following objects are masked from 'package:Matrix':
## 
##     expand, pack, unpack
library(agricolae)
## Warning: package 'agricolae' was built under R version 4.4.3
library(metan)
## Warning: package 'metan' was built under R version 4.4.3
## |=========================================================|
## | Multi-Environment Trial Analysis (metan) v1.19.0        |
## | Author: Tiago Olivoto                                   |
## | Type 'citation('metan')' to know how to cite metan      |
## | Type 'vignette('metan_start')' for a short tutorial     |
## | Visit 'https://bit.ly/metanpkg' for a complete tutorial |
## |=========================================================|
## 
## Adjuntando el paquete: 'metan'
## The following object is masked from 'package:tidyr':
## 
##     replace_na
## The following object is masked from 'package:dplyr':
## 
##     recode_factor
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
##Convertir a factor
datos$gen <- factor(datos$gen)
datos$municipio  <- factor(datos$municipio)
datos$mun  <- factor(datos$mun)
datos$reg  <- factor(datos$reg)

#Modelo 0
modelo <- lm (log(goteo) ~ gen + mun,
              data = datos)
anova(modelo)
## Analysis of Variance Table
## 
## Response: log(goteo)
##           Df  Sum Sq Mean Sq F value    Pr(>F)    
## gen        7 2.28288 0.32613 16.9444 3.919e-12 ***
## mun        9 0.47085 0.05232  2.7182   0.01001 *  
## Residuals 60 1.15481 0.01925                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Modelo bruto
modelo <- lm (goteo ~ gen + mun,
              data = datos)
anova(modelo)
## Analysis of Variance Table
## 
## Response: goteo
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## gen        7 7.0804 1.01148  21.207 4.496e-14 ***
## mun        9 1.2449 0.13832   2.900  0.006477 ** 
## Residuals 60 2.8618 0.04770                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##Contrastes a posteriori
#Genotipos
g<-emmeans(modelo, pairwise ~ gen)
g
## $emmeans
##  gen    emmean     SE df lower.CL upper.CL
##  CNCH12   1.90 0.0691 60     1.76     2.04
##  CNCH13   1.46 0.0691 60     1.32     1.60
##  FBO1     2.07 0.0691 60     1.93     2.21
##  FCHI8    1.59 0.0784 60     1.43     1.75
##  FEAR5    1.42 0.0691 60     1.28     1.56
##  FGI4     1.56 0.0691 60     1.42     1.70
##  FMA7     2.24 0.0691 60     2.10     2.38
##  FSV1     1.40 0.0734 60     1.26     1.55
## 
## Results are averaged over the levels of: mun 
## Confidence level used: 0.95 
## 
## $contrasts
##  contrast        estimate     SE df t.ratio p.value
##  CNCH12 - CNCH13   0.4400 0.0977 60   4.505  0.0008
##  CNCH12 - FBO1    -0.1700 0.0977 60  -1.741  0.6613
##  CNCH12 - FCHI8    0.3085 0.1040 60   2.953  0.0798
##  CNCH12 - FEAR5    0.4800 0.0977 60   4.915  0.0002
##  CNCH12 - FGI4     0.3400 0.0977 60   3.481  0.0198
##  CNCH12 - FMA7    -0.3400 0.0977 60  -3.481  0.0198
##  CNCH12 - FSV1     0.4972 0.1010 60   4.934  0.0002
##  CNCH13 - FBO1    -0.6100 0.0977 60  -6.246  <.0001
##  CNCH13 - FCHI8   -0.1315 0.1040 60  -1.258  0.9102
##  CNCH13 - FEAR5    0.0400 0.0977 60   0.410  0.9999
##  CNCH13 - FGI4    -0.1000 0.0977 60  -1.024  0.9689
##  CNCH13 - FMA7    -0.7800 0.0977 60  -7.986  <.0001
##  CNCH13 - FSV1     0.0572 0.1010 60   0.568  0.9991
##  FBO1 - FCHI8      0.4785 0.1040 60   4.580  0.0006
##  FBO1 - FEAR5      0.6500 0.0977 60   6.655  <.0001
##  FBO1 - FGI4       0.5100 0.0977 60   5.222  0.0001
##  FBO1 - FMA7      -0.1700 0.0977 60  -1.741  0.6613
##  FBO1 - FSV1       0.6672 0.1010 60   6.620  <.0001
##  FCHI8 - FEAR5     0.1715 0.1040 60   1.641  0.7237
##  FCHI8 - FGI4      0.0315 0.1040 60   0.301  1.0000
##  FCHI8 - FMA7     -0.6485 0.1040 60  -6.208  <.0001
##  FCHI8 - FSV1      0.1887 0.1070 60   1.771  0.6417
##  FEAR5 - FGI4     -0.1400 0.0977 60  -1.433  0.8381
##  FEAR5 - FMA7     -0.8200 0.0977 60  -8.396  <.0001
##  FEAR5 - FSV1      0.0172 0.1010 60   0.171  1.0000
##  FGI4 - FMA7      -0.6800 0.0977 60  -6.962  <.0001
##  FGI4 - FSV1       0.1572 0.1010 60   1.560  0.7715
##  FMA7 - FSV1       0.8372 0.1010 60   8.307  <.0001
## 
## Results are averaged over the levels of: mun 
## P value adjustment: tukey method for comparing a family of 8 estimates
pwpp(g, type = "response")
## Warning: `aes_()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`
## ℹ The deprecated feature was likely used in the emmeans package.
##   Please report the issue at <https://github.com/rvlenth/emmeans/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#Municipios
m<-emmeans(modelo, pairwise ~ mun)
m
## $emmeans
##  mun emmean     SE df lower.CL upper.CL
##  Chi   1.69 0.0772 60     1.53     1.84
##  Gig   1.69 0.0772 60     1.53     1.84
##  HtC   1.49 0.0772 60     1.33     1.64
##  Jam   1.63 0.0905 60     1.45     1.81
##  PtR   1.81 0.0832 60     1.65     1.98
##  RiN   1.55 0.0772 60     1.40     1.70
##  SnV   1.82 0.0772 60     1.67     1.98
##  Tam   1.74 0.0772 60     1.58     1.89
##  ViG   1.94 0.0772 60     1.78     2.09
##  Yac   1.70 0.0772 60     1.55     1.85
## 
## Results are averaged over the levels of: gen 
## Confidence level used: 0.95 
## 
## $contrasts
##  contrast  estimate    SE df t.ratio p.value
##  Chi - Gig   0.0000 0.109 60   0.000  1.0000
##  Chi - HtC   0.2000 0.109 60   1.832  0.7129
##  Chi - Jam   0.0570 0.119 60   0.479  1.0000
##  Chi - PtR  -0.1248 0.113 60  -1.099  0.9829
##  Chi - RiN   0.1375 0.109 60   1.259  0.9587
##  Chi - SnV  -0.1375 0.109 60  -1.259  0.9587
##  Chi - Tam  -0.0500 0.109 60  -0.458  1.0000
##  Chi - ViG  -0.2500 0.109 60  -2.289  0.4108
##  Chi - Yac  -0.0125 0.109 60  -0.114  1.0000
##  Gig - HtC   0.2000 0.109 60   1.832  0.7129
##  Gig - Jam   0.0570 0.119 60   0.479  1.0000
##  Gig - PtR  -0.1248 0.113 60  -1.099  0.9829
##  Gig - RiN   0.1375 0.109 60   1.259  0.9587
##  Gig - SnV  -0.1375 0.109 60  -1.259  0.9587
##  Gig - Tam  -0.0500 0.109 60  -0.458  1.0000
##  Gig - ViG  -0.2500 0.109 60  -2.289  0.4108
##  Gig - Yac  -0.0125 0.109 60  -0.114  1.0000
##  HtC - Jam  -0.1430 0.119 60  -1.203  0.9691
##  HtC - PtR  -0.3248 0.113 60  -2.861  0.1399
##  HtC - RiN  -0.0625 0.109 60  -0.572  0.9999
##  HtC - SnV  -0.3375 0.109 60  -3.091  0.0819
##  HtC - Tam  -0.2500 0.109 60  -2.289  0.4108
##  HtC - ViG  -0.4500 0.109 60  -4.121  0.0043
##  HtC - Yac  -0.2125 0.109 60  -1.946  0.6386
##  Jam - PtR  -0.1817 0.122 60  -1.489  0.8911
##  Jam - RiN   0.0805 0.119 60   0.677  0.9996
##  Jam - SnV  -0.1945 0.119 60  -1.635  0.8255
##  Jam - Tam  -0.1070 0.119 60  -0.899  0.9959
##  Jam - ViG  -0.3070 0.119 60  -2.581  0.2493
##  Jam - Yac  -0.0695 0.119 60  -0.584  0.9999
##  PtR - RiN   0.2623 0.113 60   2.311  0.3975
##  PtR - SnV  -0.0127 0.113 60  -0.112  1.0000
##  PtR - Tam   0.0748 0.113 60   0.659  0.9996
##  PtR - ViG  -0.1252 0.113 60  -1.103  0.9825
##  PtR - Yac   0.1123 0.113 60   0.989  0.9918
##  RiN - SnV  -0.2750 0.109 60  -2.518  0.2801
##  RiN - Tam  -0.1875 0.109 60  -1.717  0.7815
##  RiN - ViG  -0.3875 0.109 60  -3.549  0.0244
##  RiN - Yac  -0.1500 0.109 60  -1.374  0.9305
##  SnV - Tam   0.0875 0.109 60   0.801  0.9983
##  SnV - ViG  -0.1125 0.109 60  -1.030  0.9891
##  SnV - Yac   0.1250 0.109 60   1.145  0.9776
##  Tam - ViG  -0.2000 0.109 60  -1.832  0.7129
##  Tam - Yac   0.0375 0.109 60   0.343  1.0000
##  ViG - Yac   0.2375 0.109 60   2.175  0.4846
## 
## Results are averaged over the levels of: gen 
## P value adjustment: tukey method for comparing a family of 10 estimates
pwpp(m, type = "response")

# Modelo 1
modelo <- lmer(log(goteo) ~ gen +
                 (1|mun),
               data = datos)
anova(modelo)
## Type III Analysis of Variance Table with Satterthwaite's method
##     Sum Sq Mean Sq NumDF  DenDF F value    Pr(>F)    
## gen 2.2835 0.32621     7 60.431  16.976 3.487e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ranova(modelo)
## ANOVA-like table for random-effects: Single term deletions
## 
## Model:
## log(goteo) ~ gen + (1 | mun)
##           npar logLik     AIC   LRT Df Pr(>Chisq)  
## <none>      10 24.918 -29.836                      
## (1 | mun)    9 22.360 -26.720 5.116  1    0.02371 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Modelo 2
modelo_blup <- lmer(goteo ~ 1 +
                      (1|gen) +
                      (1|mun),
                    data = datos)
ranova(modelo_blup)
## boundary (singular) fit: see help('isSingular')
## ANOVA-like table for random-effects: Single term deletions
## 
## Model:
## goteo ~ (1 | gen) + (1 | mun)
##           npar  logLik    AIC    LRT Df Pr(>Chisq)    
## <none>       4  -9.709 27.418                         
## (1 | gen)    3 -37.204 80.408 54.990  1  1.211e-13 ***
## (1 | mun)    3 -12.628 31.256  5.838  1    0.01568 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Extraer Blups

blups <- ranef(modelo_blup)

#Blups Gen

blups$gen
##        (Intercept)
## CNCH12   0.1848321
## CNCH13  -0.2348607
## FBO1     0.3469861
## FCHI8   -0.1090417
## FEAR5   -0.2730145
## FGI4    -0.1394759
## FMA7     0.5091402
## FSV1    -0.2845656
#Valor predicho  
fixef(modelo_blup)[1] + blups$gen
##        (Intercept)
## CNCH12    1.891057
## CNCH13    1.471364
## FBO1      2.053211
## FCHI8     1.597183
## FEAR5     1.433210
## FGI4      1.566749
## FMA7      2.215365
## FSV1      1.421659
#Blups Parcela

blups$mun
##     (Intercept)
## Chi -0.01240165
## Gig -0.01240165
## HtC -0.14486525
## Jam -0.04275860
## PtR  0.06746099
## RiN -0.10347037
## SnV  0.07866708
## Tam  0.02071426
## ViG  0.15317786
## Yac -0.00412267
fixef(modelo_blup)[1] + blups$mun
##     (Intercept)
## Chi    1.693823
## Gig    1.693823
## HtC    1.561359
## Jam    1.663466
## PtR    1.773686
## RiN    1.602754
## SnV    1.784892
## Tam    1.726939
## ViG    1.859402
## Yac    1.702102
#Tabla blup_gen

blup_gen <- ranef(modelo_blup)$gen %>%
  tibble::rownames_to_column("gen") %>%
  rename(BLUP = `(Intercept)`)
blup_gen
##      gen       BLUP
## 1 CNCH12  0.1848321
## 2 CNCH13 -0.2348607
## 3   FBO1  0.3469861
## 4  FCHI8 -0.1090417
## 5  FEAR5 -0.2730145
## 6   FGI4 -0.1394759
## 7   FMA7  0.5091402
## 8   FSV1 -0.2845656
#Tabla blup_mun
blup_mun <- ranef(modelo_blup)$mun %>%
  tibble::rownames_to_column("mun") %>%
  rename(BLUP = `(Intercept)`)
blup_mun
##    mun        BLUP
## 1  Chi -0.01240165
## 2  Gig -0.01240165
## 3  HtC -0.14486525
## 4  Jam -0.04275860
## 5  PtR  0.06746099
## 6  RiN -0.10347037
## 7  SnV  0.07866708
## 8  Tam  0.02071426
## 9  ViG  0.15317786
## 10 Yac -0.00412267
#Predichos completos
datos$pred <- predict(modelo_blup)
datos$pred
##  [1] 1.787586 1.367894 1.949740 1.493713 1.329740 1.463278 2.111894 1.318189
##  [9] 1.969724 1.550031 2.131878 1.675850 1.511877 1.645416 2.294032 1.500326
## [17] 1.878655 1.458962 2.040809 1.584781 1.420808 1.554347 2.202963 1.409257
## [25] 1.878655 1.458962 2.040809 1.584781 1.420808 1.554347 2.202963 1.409257
## [33] 1.746191 1.326499 1.908345 1.452318 1.288345 1.421883 2.070500 1.276794
## [41] 1.848298 1.428605 2.010452 1.390451 1.523990 2.172606 1.958518 1.538825
## [49] 2.120672 1.500671 1.634210 2.282826 1.489120 1.911771 1.492078 2.073925
## [57] 1.617897 1.453924 1.587463 2.236079 1.442373 2.044235 1.624542 2.206389
## [65] 1.750361 1.586388 1.719927 2.368543 1.574837 1.886934 1.467241 2.049088
## [73] 1.593060 1.429087 1.562626 2.211242 1.417536
#Visualizar Blups gen
ggplot(blup_gen, aes(x=reorder(gen, BLUP), y=BLUP)) +
  geom_point(size=3) +
  geom_hline(yintercept=0, linetype="dashed") +
  labs(x = "Genotipo") +
  coord_flip()

#Visualizar Blups mun
ggplot(blup_mun, aes(x=reorder(mun, BLUP), y=BLUP)) +
  geom_point(size=3) +
  geom_hline(yintercept=0, linetype="dashed") +
  labs(x = "Municipio") +
  coord_flip()

##Componentes de varianza-heredabilidades
vc <- as.data.frame(VarCorr(modelo_blup))
vc
##        grp        var1 var2       vcov     sdcor
## 1      mun (Intercept) <NA> 0.01167779 0.1080638
## 2      gen (Intercept) <NA> 0.09844025 0.3137519
## 3 Residual        <NA> <NA> 0.04763125 0.2182458
VarCorr(modelo_blup)
##  Groups   Name        Std.Dev.
##  mun      (Intercept) 0.10806 
##  gen      (Intercept) 0.31375 
##  Residual             0.21825
varG  <- vc$vcov[vc$grp=="gen"]
varGE <- vc$vcov[vc$grp=="gen:mun"]
varE  <- vc$vcov[vc$grp=="Residual"]

e <- 10
r <- 4
# Heredabilidad genotipos y plasticidad
#H2 genotipos
H2g <- varG / (varG + varGE/e + varE/(r*e))
H2g
## numeric(0)
#H2 plasticidad
H2ge <- varGE / (varG + varGE/e + varE/(r*e))
H2ge
## numeric(0)
###ranking genotipos predichos

blup_gen <- ranef(modelo_blup)$gen
blup_gen
##        (Intercept)
## CNCH12   0.1848321
## CNCH13  -0.2348607
## FBO1     0.3469861
## FCHI8   -0.1090417
## FEAR5   -0.2730145
## FGI4    -0.1394759
## FMA7     0.5091402
## FSV1    -0.2845656
##Predicho de carbono por genotipo

media <- fixef(modelo_blup)[1]
blup_gen$pred <- media + blup_gen[,1]


# Ranking predichos (publicar)
blup_gen[order(-blup_gen$pred),]
##        (Intercept)     pred
## FMA7     0.5091402 2.215365
## FBO1     0.3469861 2.053211
## CNCH12   0.1848321 1.891057
## FCHI8   -0.1090417 1.597183
## FGI4    -0.1394759 1.566749
## CNCH13  -0.2348607 1.471364
## FEAR5   -0.2730145 1.433210
## FSV1    -0.2845656 1.421659
# Visualización ranking predichos
blup_gen$gen <- rownames(blup_gen)

ggplot(blup_gen, aes(x=reorder(gen,pred), y=pred))+
  geom_point(size=3)+
  coord_flip()+
  ylab("Fenotipo predicho (BLUP)")+
  xlab("Genotipo")

### Estabilidad con Metan
modelo_metan <- gge(datos, mun, gen, goteo)
## Warning: Data imputation used to fill the GxE matrix
modelo_metan
## $goteo
## $coordgen
##            [,1]         [,2]        [,3]       [,4]        [,5]        [,6]
## [1,]  0.1600946 -0.139968429 -0.26436698 -0.3480480 -0.34740840 -0.41131295
## [2,] -0.1685562  0.468050894  0.08717483  0.2611966  0.14561381 -0.40775963
## [3,]  0.3677286  0.274203612 -0.38705936  0.1623613 -0.02678087  0.37940894
## [4,] -0.1703866 -0.510973144 -0.12805152  0.3513171  0.13547495  0.02478650
## [5,] -0.2690118 -0.001775904 -0.20482893 -0.3550078  0.44608955  0.07374474
## [6,] -0.1209513  0.085420755  0.37625561 -0.3191126 -0.11376313  0.31643899
## [7,]  0.4775974 -0.165915404  0.41369086  0.0453296  0.20313649 -0.09711778
## [8,] -0.2765146 -0.009042380  0.10718549  0.2019638 -0.44236240  0.12181119
##            [,7]      [,8]
## [1,]  0.0866149 0.2761009
## [2,]  0.1511442 0.2761009
## [3,]  0.0486352 0.2761009
## [4,]  0.2910463 0.2761009
## [5,] -0.2980465 0.2761009
## [6,]  0.3939785 0.2761009
## [7,] -0.2326015 0.2761009
## [8,] -0.4407711 0.2761009
## 
## $coordenv
##            [,1]         [,2]        [,3]        [,4]        [,5]         [,6]
##  [1,] 0.9910139  0.100032482  0.07859718  0.03747377 -0.03888360  0.085453539
##  [2,] 0.3936742 -0.869527184  0.29889434 -0.09911056  0.13555680  0.012304573
##  [3,] 0.4582379 -0.913333991 -0.24242485  0.01759385 -0.15759050 -0.022661772
##  [4,] 0.9484455 -0.058707163  0.01590355  0.10125521 -0.07223763  0.046765267
##  [5,] 0.7285261  0.059065890 -0.49152908 -0.10884331  0.10640195  0.024601143
##  [6,] 0.8984904  0.200593849  0.05456680  0.04020143 -0.04560041 -0.046603715
##  [7,] 0.9627424  0.001173823 -0.06502177  0.08586776  0.09830779 -0.059758362
##  [8,] 0.9679910 -0.003306643  0.03553041  0.16210257  0.11216080 -0.003926765
##  [9,] 1.1477861  0.280718794  0.06648557 -0.19377924 -0.02212265 -0.007934306
## [10,] 0.9491722  0.183301375  0.17004882 -0.08028852 -0.09321000 -0.030667962
##               [,7]          [,8]
##  [1,] -0.015459091  6.340096e-18
##  [2,] -0.002303847 -2.463758e-17
##  [3,] -0.012702111  2.227592e-17
##  [4,]  0.049569175 -4.369481e-17
##  [5,] -0.006025623 -1.011005e-17
##  [6,] -0.060266400 -7.898814e-17
##  [7,]  0.057351144 -1.232763e-17
##  [8,] -0.039966981  6.650929e-17
##  [9,] -0.003291780  1.219777e-17
## [10,]  0.021939363  4.896221e-17
## 
## $eigenvalues
## [1] 2.768912e+00 1.326594e+00 6.617731e-01 3.360052e-01 3.078366e-01
## [6] 1.323682e-01 1.091319e-01 1.285128e-16
## 
## $totalvar
## [1] 10.1
## 
## $varexpl
## [1] 75.91 17.42  4.34  1.12  0.94  0.17  0.12  0.00
## 
## $labelgen
## [1] "CNCH12" "CNCH13" "FBO1"   "FCHI8"  "FEAR5"  "FGI4"   "FMA7"   "FSV1"  
## 
## $labelenv
##  [1] "Chi" "Gig" "HtC" "Jam" "PtR" "RiN" "SnV" "Tam" "ViG" "Yac"
## 
## $labelaxes
## [1] "PC1" "PC2" "PC3" "PC4" "PC5" "PC6" "PC7" "PC8"
## 
## $ge_mat
##            Chi     Gig     HtC        Jam         PtR   RiN    SnV     Tam
## CNCH12  0.1125  0.1125  0.4125  0.1674492  0.29271064  0.15  0.175  0.0625
## CNCH13 -0.1875 -0.5875 -0.6875 -0.2325508 -0.20728936 -0.05 -0.125 -0.1375
## FBO1    0.5125 -0.2875  0.0125  0.4674492  0.59271064  0.45  0.475  0.4625
## FCHI8  -0.2875  0.4125  0.5125 -0.1181522 -0.14897448 -0.35 -0.125 -0.1375
## FEAR5  -0.3875 -0.0875 -0.1875 -0.4325508 -0.00728936 -0.35 -0.325 -0.3375
## FGI4   -0.0875  0.0125 -0.2875 -0.1325508 -0.30728936 -0.15 -0.225 -0.2375
## FMA7    0.6125  0.6125  0.3125  0.5674492  0.19271064  0.55  0.575  0.6625
## FSV1   -0.2875 -0.1875 -0.0875 -0.2865432 -0.40728936 -0.25 -0.425 -0.3375
##            ViG  Yac
## CNCH12  0.2625  0.2
## CNCH13 -0.1375 -0.1
## FBO1    0.5625  0.4
## FCHI8  -0.5375 -0.4
## FEAR5  -0.3375 -0.4
## FGI4   -0.0375  0.0
## FMA7    0.6625  0.6
## FSV1   -0.4375 -0.3
## 
## $centering
## [1] "environment"
## 
## $scaling
## [1] "none"
## 
## $svp
## [1] "environment"
## 
## $d
## [1] 1.280523
## 
## $grand_mean
## [1] 1.705234
## 
## $mean_gen
##   CNCH12   CNCH13     FBO1    FCHI8    FEAR5     FGI4     FMA7     FSV1 
## 1.900000 1.460000 2.070000 1.587271 1.420000 1.560000 2.240000 1.404601 
## 
## $mean_env
##      Chi      Gig      HtC      Jam      PtR      RiN      SnV      Tam 
## 1.687500 1.687500 1.487500 1.632551 1.807289 1.550000 1.825000 1.737500 
##      ViG      Yac 
## 1.937500 1.700000 
## 
## $scale_val
##       Chi       Gig       HtC       Jam       PtR       RiN       SnV       Tam 
## 0.3796145 0.3833592 0.4015595 0.3631929 0.3379877 0.3505098 0.3693624 0.3739270 
##       ViG       Yac 
## 0.4533605 0.3741657 
## 
## attr(,"class")
## [1] "gge"
## 
## attr(,"class")
## [1] "gge"
#Grafica metan
plot(modelo_metan)
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## ℹ The deprecated feature was likely used in the metan package.
##   Please report the issue at <https://github.com/nepem-ufsc/metan/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## ℹ The deprecated feature was likely used in the metan package.
##   Please report the issue at <https://github.com/nepem-ufsc/metan/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

### Selección ideotípica
##Integración de variables
##Blup gen
blup_gen <- ranef(modelo_blup)$gen
media <- fixef(modelo_blup)[1]

blup_gen$BLUP_C <- media + blup_gen[,1]
blup_gen$gen <- rownames(blup_gen)
blup_gen <- blup_gen[,c("gen","BLUP_C")]
blup_gen
##           gen   BLUP_C
## CNCH12 CNCH12 1.891057
## CNCH13 CNCH13 1.471364
## FBO1     FBO1 2.053211
## FCHI8   FCHI8 1.597183
## FEAR5   FEAR5 1.433210
## FGI4     FGI4 1.566749
## FMA7     FMA7 2.215365
## FSV1     FSV1 1.421659
##Plasticidad usando Fisher environments (joint regression)
#índice (creando valores de x para definir env = promedio de tasas en c/parcela)
indice_env <- datos %>%
  group_by(mun) %>%
  summarise(env = mean(goteo))

datos <- left_join(datos, indice_env, by="mun")

#visualización Normas de reacción joint regression env
ggplot(datos, aes(x = env, y = goteo,
                  color = gen)) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Ambiente (local)", 
       y = expression(Fenotipo)) +
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

#visualización Normas de reacción clima local
ggplot(datos, aes(x = E, y = goteo,
                  color = gen)) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Ambiente (Estrés)", 
       y = expression(Fenotipo)) +
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

## plasticidad joint
# modelo factores fijos
mod_plas_lm <- lm(goteo ~ gen*env, 
                  data=datos)
y=emtrends(mod_plas_lm, "gen", var = "env")
multcomp::cld(y, Letters = LETTERS)
##  gen    env.trend    SE df lower.CL upper.CL .group
##  FCHI8     -0.450 0.509 61  -1.4674    0.568  A    
##  FSV1       0.231 0.485 61  -0.7388    1.200  AB   
##  CNCH12     0.886 0.485 61  -0.0826    1.855  AB   
##  FEAR5      1.017 0.485 61   0.0482    1.986  AB   
##  FGI4       1.116 0.485 61   0.1471    2.085  AB   
##  FMA7       1.242 0.485 61   0.2728    2.211  AB   
##  CNCH13     1.746 0.485 61   0.7769    2.715  AB   
##  FBO1       2.030 0.485 61   1.0610    2.999   B   
## 
## Confidence level used: 0.95 
## P value adjustment: tukey method for comparing a family of 8 estimates 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.
# modelo blup  factores aleatorios
modelo_plasticidad <- lmer(goteo ~ env +
                             (env|gen) +
                             (1|mun),
                           data=datos)
## boundary (singular) fit: see help('isSingular')
pend <- ranef(modelo_plasticidad)$gen
pend$gen <- rownames(pend)

plasticidad <- pend[,c("gen","env")]
colnames(plasticidad)[2] <- "Pendiente"

#plasticidad Estrés
# modelo factores fijos
mod_plas2_lm <- lm(goteo ~ gen*E, 
                   data=datos)
z<-emtrends(mod_plas2_lm, "gen", var = "E")
multcomp::cld(z, Letters = LETTERS)
##  gen    E.trend   SE df lower.CL upper.CL .group
##  FBO1   -2.6401 1.31 61  -5.2511  -0.0291  A    
##  CNCH13 -2.4119 1.31 61  -5.0229   0.1991  A    
##  FGI4   -1.1190 1.31 61  -3.7301   1.4920  A    
##  FMA7   -0.5074 1.31 61  -3.1184   2.1036  A    
##  CNCH12 -0.4157 1.31 61  -3.0268   2.1953  A    
##  FEAR5  -0.0352 1.31 61  -2.6462   2.5759  A    
##  FSV1    0.1592 1.32 61  -2.4866   2.8050  A    
##  FCHI8   2.6921 1.38 61  -0.0687   5.4528  A    
## 
## Confidence level used: 0.95 
## P value adjustment: tukey method for comparing a family of 8 estimates 
## significance level used: alpha = 0.05 
## NOTE: If two or more means share the same grouping symbol,
##       then we cannot show them to be different.
##       But we also did not show them to be the same.