setwd("G:/Mi unidad/Agrosavia/FeCa/Fenoma/Análisis/AguaT")
datos<-read.table("conductance.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(cond) ~ gen + mun,
              data = datos)
anova(modelo)
## Analysis of Variance Table
## 
## Response: log(cond)
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## gen        7 0.2957 0.04225  0.5925    0.7595    
## mun        9 4.5029 0.50032  7.0165 7.541e-07 ***
## Residuals 60 4.2784 0.07131                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Modelo bruto
modelo <- lm (cond ~ gen + mun,
              data = datos)
anova(modelo)
## Analysis of Variance Table
## 
## Response: cond
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## gen        7  34.43   4.919  0.6519    0.7113    
## mun        9 338.26  37.584  4.9807 5.249e-05 ***
## Residuals 60 452.76   7.546                      
## ---
## 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  10.84 0.869 60     9.11     12.6
##  CNCH13   8.78 0.869 60     7.04     10.5
##  FBO1     9.31 0.869 60     7.57     11.0
##  FCHI8    9.00 0.986 60     7.02     11.0
##  FEAR5    8.97 0.869 60     7.23     10.7
##  FGI4     9.63 0.869 60     7.89     11.4
##  FMA7     8.91 0.869 60     7.18     10.7
##  FSV1     9.06 0.923 60     7.21     10.9
## 
## Results are averaged over the levels of: mun 
## Confidence level used: 0.95 
## 
## $contrasts
##  contrast        estimate   SE df t.ratio p.value
##  CNCH12 - CNCH13   2.0676 1.23 60   1.683  0.6978
##  CNCH12 - FBO1     1.5363 1.23 60   1.251  0.9129
##  CNCH12 - FCHI8    1.8472 1.31 60   1.406  0.8511
##  CNCH12 - FEAR5    1.8783 1.23 60   1.529  0.7888
##  CNCH12 - FGI4     1.2176 1.23 60   0.991  0.9740
##  CNCH12 - FMA7     1.9301 1.23 60   1.571  0.7652
##  CNCH12 - FSV1     1.7879 1.27 60   1.410  0.8489
##  CNCH13 - FBO1    -0.5313 1.23 60  -0.432  0.9999
##  CNCH13 - FCHI8   -0.2205 1.31 60  -0.168  1.0000
##  CNCH13 - FEAR5   -0.1893 1.23 60  -0.154  1.0000
##  CNCH13 - FGI4    -0.8500 1.23 60  -0.692  0.9969
##  CNCH13 - FMA7    -0.1375 1.23 60  -0.112  1.0000
##  CNCH13 - FSV1    -0.2797 1.27 60  -0.221  1.0000
##  FBO1 - FCHI8      0.3109 1.31 60   0.237  1.0000
##  FBO1 - FEAR5      0.3420 1.23 60   0.278  1.0000
##  FBO1 - FGI4      -0.3187 1.23 60  -0.259  1.0000
##  FBO1 - FMA7       0.3938 1.23 60   0.321  1.0000
##  FBO1 - FSV1       0.2516 1.27 60   0.198  1.0000
##  FCHI8 - FEAR5     0.0312 1.31 60   0.024  1.0000
##  FCHI8 - FGI4     -0.6296 1.31 60  -0.479  0.9997
##  FCHI8 - FMA7      0.0830 1.31 60   0.063  1.0000
##  FCHI8 - FSV1     -0.0593 1.34 60  -0.044  1.0000
##  FEAR5 - FGI4     -0.6608 1.23 60  -0.538  0.9994
##  FEAR5 - FMA7      0.0518 1.23 60   0.042  1.0000
##  FEAR5 - FSV1     -0.0905 1.27 60  -0.071  1.0000
##  FGI4 - FMA7       0.7125 1.23 60   0.580  0.9990
##  FGI4 - FSV1       0.5703 1.27 60   0.450  0.9998
##  FMA7 - FSV1      -0.1422 1.27 60  -0.112  1.0000
## 
## 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   7.89 0.971 60     5.95     9.84
##  Gig  11.77 0.971 60     9.83    13.71
##  HtC   6.27 0.971 60     4.33     8.21
##  Jam  12.77 1.140 60    10.50    15.05
##  PtR   5.71 1.050 60     3.61     7.80
##  RiN  10.20 0.971 60     8.26    12.15
##  SnV   8.27 0.971 60     6.33    10.21
##  Tam   9.18 0.971 60     7.24    11.13
##  ViG  10.48 0.971 60     8.54    12.43
##  Yac  10.55 0.971 60     8.60    12.49
## 
## Results are averaged over the levels of: gen 
## Confidence level used: 0.95 
## 
## $contrasts
##  contrast  estimate   SE df t.ratio p.value
##  Chi - Gig   -3.878 1.37 60  -2.823  0.1521
##  Chi - HtC    1.622 1.37 60   1.181  0.9726
##  Chi - Jam   -4.881 1.50 60  -3.263  0.0531
##  Chi - PtR    2.184 1.43 60   1.530  0.8745
##  Chi - RiN   -2.310 1.37 60  -1.682  0.8009
##  Chi - SnV   -0.378 1.37 60  -0.275  1.0000
##  Chi - Tam   -1.292 1.37 60  -0.941  0.9943
##  Chi - ViG   -2.591 1.37 60  -1.886  0.6778
##  Chi - Yac   -2.655 1.37 60  -1.933  0.6472
##  Gig - HtC    5.500 1.37 60   4.004  0.0062
##  Gig - Jam   -1.003 1.50 60  -0.670  0.9996
##  Gig - PtR    6.062 1.43 60   4.247  0.0029
##  Gig - RiN    1.568 1.37 60   1.142  0.9780
##  Gig - SnV    3.500 1.37 60   2.548  0.2652
##  Gig - Tam    2.586 1.37 60   1.883  0.6801
##  Gig - ViG    1.287 1.37 60   0.937  0.9945
##  Gig - Yac    1.223 1.37 60   0.890  0.9962
##  HtC - Jam   -6.502 1.50 60  -4.347  0.0021
##  HtC - PtR    0.563 1.43 60   0.394  1.0000
##  HtC - RiN   -3.932 1.37 60  -2.863  0.1395
##  HtC - SnV   -2.000 1.37 60  -1.456  0.9036
##  HtC - Tam   -2.914 1.37 60  -2.121  0.5205
##  HtC - ViG   -4.213 1.37 60  -3.067  0.0868
##  HtC - Yac   -4.277 1.37 60  -3.114  0.0774
##  Jam - PtR    7.065 1.53 60   4.603  0.0009
##  Jam - RiN    2.571 1.50 60   1.718  0.7807
##  Jam - SnV    4.503 1.50 60   3.010  0.0995
##  Jam - Tam    3.589 1.50 60   2.399  0.3448
##  Jam - ViG    2.290 1.50 60   1.531  0.8743
##  Jam - Yac    2.226 1.50 60   1.488  0.8916
##  PtR - RiN   -4.495 1.43 60  -3.148  0.0711
##  PtR - SnV   -2.563 1.43 60  -1.795  0.7356
##  PtR - Tam   -3.476 1.43 60  -2.435  0.3245
##  PtR - ViG   -4.775 1.43 60  -3.345  0.0427
##  PtR - Yac   -4.839 1.43 60  -3.390  0.0378
##  RiN - SnV    1.932 1.37 60   1.407  0.9204
##  RiN - Tam    1.018 1.37 60   0.741  0.9991
##  RiN - ViG   -0.281 1.37 60  -0.204  1.0000
##  RiN - Yac   -0.345 1.37 60  -0.251  1.0000
##  SnV - Tam   -0.914 1.37 60  -0.665  0.9996
##  SnV - ViG   -2.213 1.37 60  -1.611  0.8375
##  SnV - Yac   -2.277 1.37 60  -1.658  0.8139
##  Tam - ViG   -1.299 1.37 60  -0.946  0.9941
##  Tam - Yac   -1.363 1.37 60  -0.992  0.9916
##  ViG - Yac   -0.064 1.37 60  -0.047  1.0000
## 
## 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(cond) ~ 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 0.2815 0.040214     7 59.936  0.5632 0.7827
ranova(modelo)
## ANOVA-like table for random-effects: Single term deletions
## 
## Model:
## log(cond) ~ gen + (1 | mun)
##           npar  logLik    AIC    LRT Df Pr(>Chisq)    
## <none>      10 -24.843 69.686                         
## (1 | mun)    9 -35.831 89.663 21.977  1  2.759e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Modelo 2
modelo_blup <- lmer(cond ~ 1 +
                      (1|gen) +
                      (1|mun),
                    data = datos)
## boundary (singular) fit: see help('isSingular')
ranova(modelo_blup)
## boundary (singular) fit: see help('isSingular')
## ANOVA-like table for random-effects: Single term deletions
## 
## Model:
## cond ~ (1 | gen) + (1 | mun)
##           npar  logLik   AIC    LRT Df Pr(>Chisq)    
## <none>       4 -192.85 393.7                         
## (1 | gen)    3 -192.85 391.7  0.000  1          1    
## (1 | mun)    3 -200.65 407.3 15.597  1  7.837e-05 ***
## ---
## 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
## CNCH13           0
## FBO1             0
## FCHI8            0
## FEAR5            0
## FGI4             0
## FMA7             0
## FSV1             0
#Valor predicho  
fixef(modelo_blup)[1] + blups$gen
##        (Intercept)
## CNCH12    9.313144
## CNCH13    9.313144
## FBO1      9.313144
## FCHI8     9.313144
## FEAR5     9.313144
## FGI4      9.313144
## FMA7      9.313144
## FSV1      9.313144
#Blups Parcela

blups$mun
##     (Intercept)
## Chi  -1.1680593
## Gig   2.0201658
## HtC  -2.5012992
## Jam   2.7589577
## PtR  -2.8545089
## RiN   0.7311893
## SnV  -0.8571742
## Tam  -0.1059897
## ViG   0.9620463
## Yac   1.0146722
fixef(modelo_blup)[1] + blups$mun
##     (Intercept)
## Chi    8.145085
## Gig   11.333310
## HtC    6.811845
## Jam   12.072102
## PtR    6.458636
## RiN   10.044334
## SnV    8.455970
## Tam    9.207155
## ViG   10.275191
## Yac   10.327817
#Tabla blup_gen

blup_gen <- ranef(modelo_blup)$gen %>%
  tibble::rownames_to_column("gen") %>%
  rename(BLUP = `(Intercept)`)
blup_gen
##      gen BLUP
## 1 CNCH12    0
## 2 CNCH13    0
## 3   FBO1    0
## 4  FCHI8    0
## 5  FEAR5    0
## 6   FGI4    0
## 7   FMA7    0
## 8   FSV1    0
#Tabla blup_mun
blup_mun <- ranef(modelo_blup)$mun %>%
  tibble::rownames_to_column("mun") %>%
  rename(BLUP = `(Intercept)`)
blup_mun
##    mun       BLUP
## 1  Chi -1.1680593
## 2  Gig  2.0201658
## 3  HtC -2.5012992
## 4  Jam  2.7589577
## 5  PtR -2.8545089
## 6  RiN  0.7311893
## 7  SnV -0.8571742
## 8  Tam -0.1059897
## 9  ViG  0.9620463
## 10 Yac  1.0146722
#Predichos completos
datos$pred <- predict(modelo_blup)
datos$pred
##  [1] 10.044334 10.044334 10.044334 10.044334 10.044334 10.044334 10.044334
##  [8] 10.044334  8.455970  8.455970  8.455970  8.455970  8.455970  8.455970
## [15]  8.455970  8.455970  8.145085  8.145085  8.145085  8.145085  8.145085
## [22]  8.145085  8.145085  8.145085 10.327817 10.327817 10.327817 10.327817
## [29] 10.327817 10.327817 10.327817 10.327817  6.458636  6.458636  6.458636
## [36]  6.458636  6.458636  6.458636  6.458636 10.275191 10.275191 10.275191
## [43] 10.275191 10.275191 10.275191 10.275191 10.275191 11.333310 11.333310
## [50] 11.333310 11.333310 11.333310 11.333310 11.333310 11.333310  9.207155
## [57]  9.207155  9.207155  9.207155  9.207155  9.207155  9.207155  9.207155
## [64]  6.811845  6.811845  6.811845  6.811845  6.811845  6.811845  6.811845
## [71]  6.811845 12.072102 12.072102 12.072102 12.072102 12.072102 12.072102
#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> 4.182716 2.045169
## 2      gen (Intercept) <NA> 0.000000 0.000000
## 3 Residual        <NA> <NA> 7.239472 2.690627
VarCorr(modelo_blup)
##  Groups   Name        Std.Dev.
##  mun      (Intercept) 2.0452  
##  gen      (Intercept) 0.0000  
##  Residual             2.6906
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
## CNCH13           0
## FBO1             0
## FCHI8            0
## FEAR5            0
## FGI4             0
## FMA7             0
## FSV1             0
##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
## CNCH12           0 9.313144
## CNCH13           0 9.313144
## FBO1             0 9.313144
## FCHI8            0 9.313144
## FEAR5            0 9.313144
## FGI4             0 9.313144
## FMA7             0 9.313144
## FSV1             0 9.313144
# 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, cond)
## Warning: Data imputation used to fill the GxE matrix
modelo_metan
## $cond
## $coordgen
##             [,1]       [,2]       [,3]      [,4]        [,5]       [,6]
## [1,] 10.38908118 -0.7389941 -2.0074417 -2.792865  1.68282321 -0.2925090
## [2,] -4.37254986  4.9426069 -2.4974907 -3.544300  7.01544942 -0.7929734
## [3,] -0.02006140  2.6768724 -5.8671302  7.413993 -0.08252507  2.0626864
## [4,] -2.34149842 -4.7937613 -3.4896284  1.898260 -4.58287016 -4.4554781
## [5,] -3.00057154 -6.7526492 -0.5200041 -4.927063  1.09129918  0.6631124
## [6,]  0.35780775 -2.2257214  8.0578754  5.629876  4.50554037 -1.5454332
## [7,] -0.08798033  6.0058857  3.6443505 -2.495015 -5.71322482 -5.0782426
## [8,] -0.92422738  0.8857609  2.6794691 -1.182885 -3.91649212  9.4388374
##           [,7]      [,8]
## [1,] -1.219032 -4.220909
## [2,] -3.538237 -4.220909
## [3,]  4.888199 -4.220909
## [4,] -6.294036 -4.220909
## [5,]  6.628260 -4.220909
## [6,] -0.563524 -4.220909
## [7,]  3.270819 -4.220909
## [8,] -3.172449 -4.220909
## 
## $coordenv
##             [,1]        [,2]       [,3]       [,4]       [,5]       [,6]
##  [1,] -1.7157433  1.80769688 -2.4319020  1.8226496 -0.8026857  1.5303568
##  [2,] -2.9471917  0.06941256 -1.4462563 -1.4888889  0.7918939 -1.2338486
##  [3,]  0.3068460 -1.18437870 -0.4726929 -1.5365984 -0.8682945 -0.1519053
##  [4,]  0.2132527 -0.75594815  3.6458920  4.7319444  1.2473757 -0.2827476
##  [5,]  4.9952439 -0.29399495 -2.3039564  0.7150383 -0.3027016 -0.3195777
##  [6,]  6.4466424  5.23370255  4.0089425 -2.3134019 -0.4501665  0.2108247
##  [7,] -2.2666267  1.55893234 -0.7884329 -1.1924491  3.9717271  0.4782881
##  [8,]  0.4356654  1.15914238 -0.9456617  1.9679385 -0.6196985 -0.7687876
##  [9,] 13.1957563 -2.80512647 -1.6112098  0.2788092  1.0916076  0.1160521
## [10,] -1.5420674 -7.52483231  2.2805230 -1.4941910 -0.1550086  0.5050909
##              [,7]          [,8]
##  [1,] -0.02242043 -1.024598e-15
##  [2,]  0.19974121 -1.414666e-15
##  [3,] -0.37853845 -1.449546e-15
##  [4,]  0.11433473 -8.617825e-16
##  [5,]  0.56621974  7.107035e-16
##  [6,] -0.01164920 -2.119487e-16
##  [7,] -0.19589509  3.294356e-16
##  [8,] -0.77156288  7.781680e-16
##  [9,] -0.17740514 -5.060902e-16
## [10,] -0.07897742  2.571379e-16
## 
## $eigenvalues
## [1] 1.612806e+01 1.004941e+01 7.225946e+00 6.615092e+00 4.609755e+00
## [6] 2.280866e+00 1.090366e+00 2.735192e-15
## 
## $totalvar
## [1] 484.72
## 
## $varexpl
## [1] 53.66 20.83 10.77  9.03  4.38  1.07  0.25  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
## CNCH12 -1.7707766 -1.8560343  0.6992660 -1.3165785  4.4926077  5.0856576
## CNCH13  0.7777453  2.3408293 -0.4356118 -1.8406996 -2.0060122 -0.6212754
## FBO1    2.9960196 -0.3302184 -1.1632595  0.9663295  1.6807162 -2.2093865
## FCHI8   0.3601186  0.7873459  0.8988068 -0.4851731 -0.1375737 -4.8052824
## FEAR5  -1.2383448  1.4936840  0.9495594 -1.5759124 -1.0149919 -3.8363200
## FGI4   -1.6703031 -1.3303853 -1.1038147  5.3415160 -1.1129303  0.6357393
## FMA7   -0.4742124  0.1269122 -0.0448144 -0.7031829 -0.6015262  4.4151974
## FSV1    1.0197534 -1.2321334  0.1998683 -0.3862989 -1.3002898  1.3356700
##                 SnV         Tam        ViG        Yac
## CNCH12 -1.089288967  0.01627761 12.0316095 -0.9362220
## CNCH13  4.354716476 -0.15050677 -5.0537315 -2.6852360
## FBO1   -0.024728281  1.50157119  0.2536893 -3.6772979
## FCHI8  -1.740416922  0.97000713 -1.3152476  2.3324058
## FEAR5   0.495250883 -2.06383146 -1.7670772  5.1311168
## FGI4   -0.006801001 -0.01122349  0.3677680  2.0711086
## FMA7   -1.348317009  0.29215581 -2.6788784 -2.9280187
## FSV1   -0.640415182 -0.55445001 -1.8381321  0.6921435
## 
## $centering
## [1] "environment"
## 
## $scaling
## [1] "none"
## 
## $svp
## [1] "environment"
## 
## $d
## [1] 0.08376237
## 
## $grand_mean
## [1] 9.307729
## 
## $mean_gen
##    CNCH12    CNCH13      FBO1     FCHI8     FEAR5      FGI4      FMA7      FSV1 
## 10.843381  8.775751  9.307072  8.994228  8.965042  9.625796  8.913260  9.037300 
## 
## $mean_env
##       Chi       Gig       HtC       Jam       PtR       RiN       SnV       Tam 
##  7.892375 11.770375  6.270687 12.722727  5.733182 10.202527  8.270520  9.184224 
##       ViG       Yac 
## 10.483330 10.547342 
## 
## $scale_val
##       Chi       Gig       HtC       Jam       PtR       RiN       SnV       Tam 
## 1.6228080 1.4730231 0.8453049 2.3283877 2.1170313 3.5980455 1.9143510 1.0603802 
##       ViG       Yac 
## 5.1534593 3.0872976 
## 
## 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 9.313144
## CNCH13 CNCH13 9.313144
## FBO1     FBO1 9.313144
## FCHI8   FCHI8 9.313144
## FEAR5   FEAR5 9.313144
## FGI4     FGI4 9.313144
## FMA7     FMA7 9.313144
## FSV1     FSV1 9.313144
##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(cond))

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

#visualización Normas de reacción joint regression env
ggplot(datos, aes(x = env, y = cond,
                  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 = cond,
                  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(cond ~ 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
##  FBO1       0.770 0.386 61 -0.00128     1.54  A    
##  CNCH13     0.829 0.386 61  0.05737     1.60  A    
##  CNCH12     0.855 0.386 61  0.08340     1.63  A    
##  FCHI8      0.886 0.565 61 -0.24399     2.02  A    
##  FMA7       0.977 0.386 61  0.20555     1.75  A    
##  FSV1       0.981 0.459 61  0.06226     1.90  A    
##  FEAR5      1.016 0.386 61  0.24434     1.79  A    
##  FGI4       1.601 0.386 61  0.82953     2.37  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.
# modelo blup  factores aleatorios
modelo_plasticidad <- lmer(cond ~ 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(cond ~ 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
##  FEAR5  -17.514 19.6 61    -56.7     21.6  A    
##  FGI4   -13.897 19.6 61    -53.1     25.3  A    
##  FCHI8   -5.824 20.7 61    -47.2     35.6  A    
##  FSV1    -3.776 19.8 61    -43.5     35.9  A    
##  CNCH13  -0.111 19.6 61    -39.3     39.0  A    
##  FBO1     1.044 19.6 61    -38.1     40.2  A    
##  CNCH12   7.520 19.6 61    -31.6     46.7  A    
##  FMA7     8.840 19.6 61    -30.3     48.0  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.