library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
library(ape)
datos<-read_excel("C:/Users/S340/Downloads/cosechaVARIEDADxy.xlsx")
datos
## # A tibble: 54 × 4
##    cosecha variedad     x     y
##      <dbl> <chr>    <dbl> <dbl>
##  1    2660 A            1     1
##  2    1459 A            1     2
##  3    1831 A            1     3
##  4    1193 A            1     4
##  5    1803 A            1     5
##  6    1685 A            1     6
##  7    2318 A            2     1
##  8    2245 A            2     2
##  9    1928 A            2     3
## 10    1229 A            2     4
## # … with 44 more rows

Tecnica de analisis de varianza

mod1=aov(cosecha ~ variedad, data=datos)
s_mod1=summary(mod1)
p_valor = s_mod1[[1]][1,5]
ifelse(p_valor<0.05, "Rechazo H0","NO rechazo H0") #se cumple con varias repeticiones
## [1] "Rechazo H0"
#Prueba de Tukey de comparacion entre medias
TukeyHSD(mod1,"variedad")
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = cosecha ~ variedad, data = datos)
## 
## $variedad
##          diff       lwr       upr     p adj
## B-A 2181.8889  1394.042 2969.7355 0.0000001
## C-A 1897.7778  1109.931 2685.6244 0.0000012
## C-B -284.1111 -1071.958  503.7355 0.6611661
#Podemos concluir que todos los genotipos tienen un contenido diferente de canabinoides
#Supuestos del modelo

#Normalidad de residuales - Shapiro si p_valor>0,05 son normales
shapiro.test(mod1$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  mod1$residuals
## W = 0.97959, p-value = 0.4832
#Igualdad de varianzas - Bartlett si p_valor>0,05 son normales
bartlett.test(mod1$residuals, datos$variedad)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  mod1$residuals and datos$variedad
## Bartlett's K-squared = 10.116, df = 2, p-value = 0.00636
#Independecia - dependencia espacial con Indice de Moran

#Dependencia espacial en los residuales usando R y Phyton

##Matriz de distancia

dist_matrix <- as.matrix(dist(cbind(datos$x,datos$y)))

##Inversa de la distancia

dist_matrix_inv <- 1 / dist_matrix
diag(dist_matrix_inv) <- 0

Indice de Moran

Moran.I(mod1$residuals, dist_matrix_inv) 
## $observed
## [1] -0.006222189
## 
## $expected
## [1] -0.01886792
## 
## $sd
## [1] 0.01712343
## 
## $p.value
## [1] 0.4602078

#Hay autocorrelacion espacial entre las variables, sin embargo, es minima y puede ser despreciable