setwd("/Volumes/GoogleDrive/Mi unidad/Agrosavia/Colaboraciones/Laura")
datos6<-read.table("mirafloresflo.csv", header=T, sep=',')
datos6$gen<-as.factor(datos6$gen)
datos6$bloque<-as.factor(datos6$bloque)
attach(datos6)
library(ggplot2)
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
library(emmeans)
#Gráfica cojines por planta
ggplot(datos6, aes(y=cojplanta, x=gen, fill= gen, colour=gen)) + 
  geom_bar(position="dodge", stat="identity") +
  labs(x="Genotipo", y="Cojines por planta")

# Gráfica frutos por planta
ggplot(datos6, aes(fill=gen, y=frutos, x=gen, colour=gen)) + 
  geom_bar(position="dodge", stat="identity") +
  labs(x="Genotipo", y="Frutos por planta")

#modelos
gfit1<-glm(cojplanta~gen, data=datos6, family = poisson (link=log))
gfit2<-glm(frutos~gen, data=datos6, family = poisson (link=log))
# resultado cojines por planta
summary(gfit1)
## 
## Call:
## glm(formula = cojplanta ~ gen, family = poisson(link = log), 
##     data = datos6)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -10.303   -4.407   -1.711    1.796   34.751  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  2.459905   0.056254  43.728  < 2e-16 ***
## genTCS 02    1.165916   0.064426  18.097  < 2e-16 ***
## genTCS 03    0.541367   0.070754   7.651 1.99e-14 ***
## genTCS 04   -0.038715   0.080337  -0.482    0.630    
## genTCS 05   -1.465283   0.129858 -11.284  < 2e-16 ***
## genTCS 08   -1.730390   0.144918 -11.940  < 2e-16 ***
## genTCS 10   -0.048632   0.080541  -0.604    0.546    
## genTCS 11   -0.009539   0.079746  -0.120    0.905    
## genTCS 12    1.369541   0.063000  21.739  < 2e-16 ***
## genTCS 20    0.432522   0.072235   5.988 2.13e-09 ***
## genTCS 40    0.545044   0.070707   7.709 1.27e-14 ***
## genTCS 41   -0.035430   0.080270  -0.441    0.659    
## genTCS 42    0.689978   0.068934  10.009  < 2e-16 ***
## genTCS 43   -1.010810   0.108893  -9.283  < 2e-16 ***
## genTCS 44   -0.389766   0.088529  -4.403 1.07e-05 ***
## genTCS 45    1.511783   0.062148  24.325  < 2e-16 ***
## genTCS 46    1.396527   0.062830  22.227  < 2e-16 ***
## genTCS 47    0.351281   0.073428   4.784 1.72e-06 ***
## genTCS 48   -0.092782   0.081466  -1.139    0.255    
## genTCS 49    0.337828   0.073634   4.588 4.48e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 16336  on 539  degrees of freedom
## Residual deviance: 10728  on 520  degrees of freedom
## AIC: 12526
## 
## Number of Fisher Scoring iterations: 6
anova(gfit1, test = "Chisq")
## Analysis of Deviance Table
## 
## Model: poisson, link: log
## 
## Response: cojplanta
## 
## Terms added sequentially (first to last)
## 
## 
##      Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                   539      16336              
## gen  19   5607.6       520      10728 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# resultado frutos por planta
summary(gfit2)
## 
## Call:
## glm(formula = frutos ~ gen, family = poisson(link = log), data = datos6)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -5.3817  -1.8053  -1.0541   0.1155  10.0882  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  1.32914    0.09901  13.424  < 2e-16 ***
## genTCS 02   -1.36688    0.21969  -6.222 4.92e-10 ***
## genTCS 03   -0.98739    0.19005  -5.195 2.04e-07 ***
## genTCS 04   -1.44692    0.22687  -6.378 1.80e-10 ***
## genTCS 05   -1.98592    0.28501  -6.968 3.21e-12 ***
## genTCS 08   -2.83321    0.42007  -6.745 1.53e-11 ***
## genTCS 10   -0.98739    0.19005  -5.195 2.04e-07 ***
## genTCS 11   -3.23868    0.50971  -6.354 2.10e-10 ***
## genTCS 12    0.20334    0.13343   1.524 0.127524    
## genTCS 20   -0.10318    0.14378  -0.718 0.472979    
## genTCS 40   -0.32091    0.15270  -2.102 0.035592 *  
## genTCS 41   -0.15906    0.14594  -1.090 0.275740    
## genTCS 42   -0.59962    0.16632  -3.605 0.000312 ***
## genTCS 43   -1.91692    0.27649  -6.933 4.12e-12 ***
## genTCS 44   -0.67373    0.17039  -3.954 7.69e-05 ***
## genTCS 45   -0.84078    0.18036  -4.662 3.14e-06 ***
## genTCS 46    1.34373    0.11118  12.086  < 2e-16 ***
## genTCS 47   -0.53063    0.16269  -3.262 0.001108 ** 
## genTCS 48   -1.06962    0.19590  -5.460 4.76e-08 ***
## genTCS 49   -0.88730    0.18334  -4.840 1.30e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 3776.7  on 539  degrees of freedom
## Residual deviance: 2513.6  on 520  degrees of freedom
## AIC: 3241.6
## 
## Number of Fisher Scoring iterations: 6
anova(gfit2, test = "Chisq")
## Analysis of Deviance Table
## 
## Model: poisson, link: log
## 
## Response: frutos
## 
## Terms added sequentially (first to last)
## 
## 
##      Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                   539     3776.7              
## gen  19   1263.1       520     2513.6 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Contrastes para Cojines
#Gen
contrast <- emmeans(gfit1, ~ gen)
plot(contrast, comparisons = TRUE, xlab ="LN(Cojines por planta)")

cld_gen <-multcomp::cld(contrast, alpha = 0.05, Letters = LETTERS, reversed=T)
cld_gen
##  gen    emmean     SE  df asymp.LCL asymp.UCL .group    
##  TCS 45  3.972 0.0264 Inf     3.920     4.023  A        
##  TCS 46  3.856 0.0280 Inf     3.802     3.911  AB       
##  TCS 12  3.829 0.0284 Inf     3.774     3.885   B       
##  TCS 02  3.626 0.0314 Inf     3.564     3.687    C      
##  TCS 42  3.150 0.0398 Inf     3.072     3.228     D     
##  TCS 40  3.005 0.0428 Inf     2.921     3.089     DE    
##  TCS 03  3.001 0.0429 Inf     2.917     3.085     DE    
##  TCS 20  2.892 0.0453 Inf     2.804     2.981      E    
##  TCS 47  2.811 0.0472 Inf     2.719     2.904      E    
##  TCS 49  2.798 0.0475 Inf     2.705     2.891      E    
##  TCS 01  2.460 0.0563 Inf     2.350     2.570       F   
##  TCS 11  2.450 0.0565 Inf     2.340     2.561       F   
##  TCS 41  2.424 0.0573 Inf     2.312     2.537       F   
##  TCS 04  2.421 0.0574 Inf     2.309     2.534       F   
##  TCS 10  2.411 0.0576 Inf     2.298     2.524       F   
##  TCS 48  2.367 0.0589 Inf     2.252     2.483       FG  
##  TCS 44  2.070 0.0684 Inf     1.936     2.204        G  
##  TCS 43  1.449 0.0932 Inf     1.266     1.632         H 
##  TCS 05  0.995 0.1170 Inf     0.765     1.224         HI
##  TCS 08  0.730 0.1336 Inf     0.468     0.991          I
## 
## Results are given on the log (not the response) scale. 
## Confidence level used: 0.95 
## P value adjustment: tukey method for comparing a family of 20 estimates 
## significance level used: alpha = 0.05 
## NOTE: Compact letter displays can be misleading
##       because they show NON-findings rather than findings.
##       Consider using 'pairs()', 'pwpp()', or 'pwpm()' instead.
#Contrastes para frutos
#Gen
contrast2 <- emmeans(gfit2, ~ gen)
plot(contrast2, comparisons = TRUE, xlab = "LN(Frutos por planta)")

cld_gen <-multcomp::cld(contrast2, alpha = 0.05, Letters = LETTERS, reversed=T)
cld_gen
##  gen     emmean     SE  df asymp.LCL asymp.UCL .group     
##  TCS 46  2.6729 0.0506 Inf    2.5738    2.7720  A         
##  TCS 12  1.5325 0.0894 Inf    1.3572    1.7078   B        
##  TCS 01  1.3291 0.0990 Inf    1.1351    1.5232   BC       
##  TCS 20  1.2260 0.1043 Inf    1.0216    1.4303   BCD      
##  TCS 41  1.1701 0.1072 Inf    0.9599    1.3802   BCD      
##  TCS 40  1.0082 0.1162 Inf    0.7804    1.2361    CDE     
##  TCS 47  0.7985 0.1291 Inf    0.5455    1.0515    CDEF    
##  TCS 42  0.7295 0.1336 Inf    0.4676    0.9914     DEFG   
##  TCS 44  0.6554 0.1387 Inf    0.3836    0.9272     DEFG   
##  TCS 45  0.4884 0.1508 Inf    0.1929    0.7838      EFG   
##  TCS 49  0.4418 0.1543 Inf    0.1394    0.7443      EFGH  
##  TCS 10  0.3417 0.1622 Inf    0.0238    0.6597      EFGHI 
##  TCS 03  0.3417 0.1622 Inf    0.0238    0.6597      EFGHI 
##  TCS 48  0.2595 0.1690 Inf   -0.0718    0.5908       FGHI 
##  TCS 02 -0.0377 0.1961 Inf   -0.4221    0.3466        GHIJ
##  TCS 04 -0.1178 0.2041 Inf   -0.5179    0.2823        GHIJ
##  TCS 43 -0.5878 0.2582 Inf   -1.0938   -0.0818         HIJ
##  TCS 05 -0.6568 0.2673 Inf   -1.1806   -0.1330          IJ
##  TCS 08 -1.5041 0.4082 Inf   -2.3042   -0.7039           J
##  TCS 11 -1.9095 0.5000 Inf   -2.8895   -0.9296           J
## 
## Results are given on the log (not the response) scale. 
## Confidence level used: 0.95 
## P value adjustment: tukey method for comparing a family of 20 estimates 
## significance level used: alpha = 0.05 
## NOTE: Compact letter displays can be misleading
##       because they show NON-findings rather than findings.
##       Consider using 'pairs()', 'pwpp()', or 'pwpm()' instead.
detach(datos6)