Se desea probar 3 tipos de insecticidas para matar zancudos, estos son usados en condiciones similares en las 3 pruebas y se cuenta el número de zancudos muertos. Se pretende saber si hay diferencia en la efectividad de los 3 productos (insecticidas) y en caso de que exista, cuál o cuáles serían los mejores productos.
library(readxl)
Datos_2 <- read_excel("C:/Users/juanc/Downloads/Datos_2.xlsx")
attach(Datos_2)
Datos_2
## # A tibble: 18 x 2
## spray zancudos
## <dbl> <dbl>
## 1 1 72
## 2 1 65
## 3 1 67
## 4 1 75
## 5 1 62
## 6 1 73
## 7 2 45
## 8 2 55
## 9 2 47
## 10 2 65
## 11 2 41
## 12 2 47
## 13 3 64
## 14 3 74
## 15 3 61
## 16 3 58
## 17 3 51
## 18 3 69
datatable(Datos_2, filter = 'top', class = 'cell-border stripe', options = list(pageLength = 6, autoWidth = TRUE))
Se convierte Spray a factor
spray <- factor(spray)
Modelo <- lm(zancudos~spray)
ANOVA <- aov(Modelo)
summary(ANOVA)
## Df Sum Sq Mean Sq F value Pr(>F)
## spray 2 1127.4 563.7 10.13 0.00165 **
## Residuals 15 834.8 55.7
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Hay evidencia estadística en la diferencia de la efectividad de los insecticidas.
library(agricolae)
Grupos <- LSD.test(y = ANOVA, trt = "spray", group = T, console = T)
##
## Study: ANOVA ~ "spray"
##
## LSD t Test for zancudos
##
## Mean Square Error: 55.65556
##
## spray, means and individual ( 95 %) CI
##
## zancudos std r LCL UCL Min Max
## 1 69.00000 5.099020 6 62.50837 75.49163 62 75
## 2 50.00000 8.648699 6 43.50837 56.49163 41 65
## 3 62.83333 8.134290 6 56.34170 69.32496 51 74
##
## Alpha: 0.05 ; DF Error: 15
## Critical Value of t: 2.13145
##
## least Significant Difference: 9.18055
##
## Treatments with the same letter are not significantly different.
##
## zancudos groups
## 1 69.00000 a
## 3 62.83333 a
## 2 50.00000 b
Hay evidencia estadística, aquellos tratamientos con la misma letra no tienen diferencias significativas (1 y 3), el producto 2 es diferente y con menor resultado de promedio.
bar.group(x= Grupos$groups, col= "green", ylim=c(0,80), main= "Prueba de comparaciones múltiples", xlab = "Spray")
qqnorm(rstandard(Modelo))
qqline(rstandard(Modelo))
shapiro.test(rstandard(Modelo))
##
## Shapiro-Wilk normality test
##
## data: rstandard(Modelo)
## W = 0.97246, p-value = 0.8424
Los residuos se comportan de manera normal por lo cual no hay problemas en las observaciones. El P valor es mayor a alfa, entonces, no hay rechazo de la hipótesis.
Con una variable ID
myData2 <- Datos_2
myData2$ID<-seq.int(nrow(myData2))
Como una matriz
myMatrix <- data.matrix(Datos_2)
En un formato largo
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.0.3
myDataLong <- melt (myData2, id.vars = c("ID"))
Usamos librería Stats
library(stats)
friedman.test(myMatrix)
##
## Friedman rank sum test
##
## data: myMatrix
## Friedman chi-squared = 18, df = 1, p-value = 2.209e-05
friedman.test(myDataLong$value, myDataLong$variable, myDataLong$ID)
##
## Friedman rank sum test
##
## data: myDataLong$value, myDataLong$variable and myDataLong$ID
## Friedman chi-squared = 18, df = 1, p-value = 2.209e-05
El test encuentra diferencias significativas entre al menos un grupo.
Usando librería Agricolae
library(agricolae)
friedman(myDataLong$ID, myDataLong$variable, myDataLong$value, console = TRUE)
##
## Study: myDataLong$value ~ myDataLong$ID + myDataLong$variable
##
## myDataLong$variable, Sum of the ranks
##
## myDataLong.value r
## spray 18 18
## zancudos 36 18
##
## Friedman's Test
## ===============
## Adjusted for ties
## Critical Value: 18
## P.Value Chisq: 2.20905e-05
## F Value: Inf
## P.Value F: 0
##
## Post Hoc Analysis
##
## Alpha: 0.05 ; DF Error: 17
## t-Student: 2.109816
## LSD: 0
##
## Treatments with the same letter are not significantly different.
##
## Sum of ranks groups
## zancudos 36 a
## spray 18 b
Usando librería Coin
library(coin)
## Warning: package 'coin' was built under R version 4.0.3
## Loading required package: survival
## Warning: package 'survival' was built under R version 4.0.3
##
## Attaching package: 'survival'
## The following object is masked from 'package:DAAG':
##
## lung
myDataLong[,'ID']<- factor(myDataLong[,'ID'])
friedman_test(value ~ variable | ID, myDataLong)
##
## Asymptotic Friedman Test
##
## data: value by
## variable (spray, zancudos)
## stratified by ID
## chi-squared = 18, df = 1, p-value = 2.209e-05
Con el P valor podemos deducir que se rechaza la hipótesis (P<0.05) por lo cual podemos decir que las variables están asociadas.
zancudos <- Datos_2$zancudos
spray <- Datos_2$spray
pvar1 = var.test(zancudos, spray)
ifelse(pvar1$p.value<0.05, 'Varianzas iguales', 'Varianzas desiguales')
## [1] "Varianzas iguales"
Se realiza la prueba T- Student
pruebat1 = t.test(zancudos,spray, alternative = "t", var.equal = T); pruebat1$p.value
## [1] 2.383245e-22
ifelse (pruebat1$p.value<0.05, 'Rechazo Ho', 'No rechazo Ho')
## [1] "Rechazo Ho"
Se puede concluir que con un nivel de confianza del 95% que las medias de los datos no son estadísticamente iguales, afirmación sustentada con la aplicación de la prueba T-Student.
library(car)
ncvTest(Modelo)
## Non-constant Variance Score Test
## Variance formula: ~ fitted.values
## Chisquare = 0.159408, Df = 1, p = 0.6897
Finalmente, se cumple el supuesto de varianzas iguales. El mejor tratamiento está entre el Spray 3 y 1.