setwd("~/Google Drive/Agrosavia/colaboraciones/Laura")
datos6<-read.table("sanluisflo.csv", header=T, sep=',')
datos6$gen<-as.factor(datos6$gen)
datos6$bloque<-as.factor(datos6$bloque)
datos6$forestal<-as.factor(datos6$forestal)
attach(datos6)
library(ggplot2)
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
#Gráfica cojines por planta
ggplot(datos6, aes(fill=gen, y=cojplanta, x=forestal)) + 
  geom_bar(position="dodge", stat="identity") +
  labs(x="Forestal", y="Cojines por planta")

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

#modelos
gfit1<-glm(cojplanta~forestal*gen, data=datos6, family = poisson (link=log))
gfit2<-glm(frutos~forestal*gen, data=datos6, family = poisson (link=log))
# resultado cojines por planta
summary(gfit1)
## 
## Call:
## glm(formula = cojplanta ~ forestal * gen, family = poisson(link = log), 
##     data = datos6)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -9.428  -5.720  -1.716   2.256  15.621  
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                   3.44113    0.04218  81.577  < 2e-16 ***
## forestalRoble                -0.42614    0.06711  -6.350 2.16e-10 ***
## forestalTerminalia            0.09499    0.05829   1.630 0.103184    
## genTCS 01                    -0.93334    0.07940 -11.755  < 2e-16 ***
## genTCS 06                    -0.38870    0.06636  -5.857 4.71e-09 ***
## genTCS13                     -0.66508    0.07238  -9.188  < 2e-16 ***
## genTCS19                     -0.06241    0.06061  -1.030 0.303168    
## forestalRoble:genTCS 01       0.79317    0.11029   7.192 6.39e-13 ***
## forestalTerminalia:genTCS 01  0.64458    0.10041   6.420 1.37e-10 ***
## forestalRoble:genTCS 06       0.09281    0.10388   0.893 0.371629    
## forestalTerminalia:genTCS 06  0.64683    0.08528   7.585 3.32e-14 ***
## forestalRoble:genTCS13        0.66508    0.10339   6.433 1.25e-10 ***
## forestalTerminalia:genTCS13   0.33666    0.09542   3.528 0.000418 ***
## forestalRoble:genTCS19       -0.03481    0.09696  -0.359 0.719553    
## forestalTerminalia:genTCS19  -0.67711    0.09317  -7.267 3.67e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 8665.4  on 269  degrees of freedom
## Residual deviance: 7914.4  on 255  degrees of freedom
## AIC: 8861.2
## 
## 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                           269     8665.4              
## forestal      2   226.23       267     8439.2 < 2.2e-16 ***
## gen           4   175.31       263     8263.9 < 2.2e-16 ***
## forestal:gen  8   349.44       255     7914.4 < 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 ~ forestal * gen, family = poisson(link = log), 
##     data = datos6)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4157  -2.0276  -1.4907   0.4377  15.5293  
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                    1.6314     0.1043  15.648  < 2e-16 ***
## forestalRoble                 -0.6716     0.1793  -3.746 0.000180 ***
## forestalTerminalia             0.1322     0.1428   0.926 0.354686    
## genTCS 01                     -1.1896     0.2158  -5.512 3.56e-08 ***
## genTCS 06                     -1.3029     0.2255  -5.777 7.61e-09 ***
## genTCS13                      -1.1545     0.2130  -5.421 5.92e-08 ***
## genTCS19                      -0.4443     0.1668  -2.664 0.007732 ** 
## forestalRoble:genTCS 01        1.6702     0.2846   5.868 4.42e-09 ***
## forestalTerminalia:genTCS 01   1.1408     0.2571   4.437 9.13e-06 ***
## forestalRoble:genTCS 06        0.4485     0.3495   1.283 0.199395    
## forestalTerminalia:genTCS 06   0.2599     0.2957   0.879 0.379444    
## forestalRoble:genTCS13        -0.2978     0.3969  -0.750 0.453138    
## forestalTerminalia:genTCS13   -3.4995     1.0271  -3.407 0.000656 ***
## forestalRoble:genTCS19         0.4008     0.2671   1.501 0.133434    
## forestalTerminalia:genTCS19   -0.5988     0.2537  -2.360 0.018269 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 2072.0  on 269  degrees of freedom
## Residual deviance: 1744.5  on 255  degrees of freedom
## AIC: 2114.1
## 
## Number of Fisher Scoring iterations: 7
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                           269     2072.0              
## forestal      2   13.882       267     2058.1 0.0009671 ***
## gen           4  214.073       263     1844.0 < 2.2e-16 ***
## forestal:gen  8   99.524       255     1744.5 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Contrastes para Cojines
library(emmeans)
contrast <- emmeans(gfit1, ~ forestal * gen)
plot(contrast, comparisons = TRUE, xlab ="LN(Cojines por planta)")

#Contrastes para frutos
contrast2 <- emmeans(gfit2, ~ forestal * gen)
plot(contrast2, comparisons = TRUE, xlab = "LN(Frutos por planta)")

detach(datos6)