Diseño Kruskal Wallis

Sea \(N\) el número total de observaciones en todos los grupos y \(g\) el número de grupos.

El estadístico de prueba \(H\) del test de Kruskal-Wallis está dado por:

\[ H = \frac{12}{N(N+1)} \left[ \sum_{j=1}^g \frac{R_j^2}{n_j} - 3(N+1) \right] \]

Donde: - \(n_j\) es el tamaño de cada grupo. - \(R_j\) es la suma de los rangos de cada grupo.

La hipótesis nula \(H_0\) es que las muestras provienen de la misma distribución.

library(lawstat)
## Warning: package 'lawstat' was built under R version 4.3.2
library(nortest)
library(BSDA)
## Warning: package 'BSDA' was built under R version 4.3.2
## Loading required package: lattice
## 
## Attaching package: 'BSDA'
## The following object is masked from 'package:datasets':
## 
##     Orange
#install.packages("lawstat")
x<-rbeta(50,1,1)
plot(density(x))

shapiro.test(x)
## 
##  Shapiro-Wilk normality test
## 
## data:  x
## W = 0.96335, p-value = 0.1228
ks.test(x,"pbeta",shape1=1,shape2=1)
## 
##  Exact one-sample Kolmogorov-Smirnov test
## 
## data:  x
## D = 0.10279, p-value = 0.629
## alternative hypothesis: two-sided
lillie.test(x)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  x
## D = 0.096451, p-value = 0.2911
#Simetría
symmetry.test(x)
## 
##  m-out-of-n bootstrap symmetry test by Miao, Gel, and Gastwirth (2006)
## 
## data:  x
## Test statistic = -0.11551, p-value = 0.886
## alternative hypothesis: the distribution is asymmetric.
## sample estimates:
## bootstrap optimal m 
##                  19
y<-rbeta(50,50,1)
plot(density(y))

symmetry.test(y)
## 
##  m-out-of-n bootstrap symmetry test by Miao, Gel, and Gastwirth (2006)
## 
## data:  y
## Test statistic = -0.98366, p-value = 0.328
## alternative hypothesis: the distribution is asymmetric.
## sample estimates:
## bootstrap optimal m 
##                  14
#Test
wilcox.test(x,mu=1/2)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  x
## V = 661, p-value = 0.8243
## alternative hypothesis: true location is not equal to 0.5
#Signo
SIGN.test(x,md=0.5)
## 
##  One-sample Sign-Test
## 
## data:  x
## s = 25, p-value = 1
## alternative hypothesis: true median is not equal to 0.5
## 95 percent confidence interval:
##  0.4042314 0.5954281
## sample estimates:
## median of x 
##   0.5100595 
## 
## Achieved and Interpolated Confidence Intervals: 
## 
##                   Conf.Level L.E.pt U.E.pt
## Lower Achieved CI     0.9351 0.4136 0.5901
## Interpolated CI       0.9500 0.4042 0.5954
## Upper Achieved CI     0.9672 0.3935 0.6015
# Muestra independientos
x1<-rbeta(50,1,1)
x2<-rbeta(30,1,1)
median(x1)
## [1] 0.3987447
median(x2)
## [1] 0.5965458
symmetry.test(x1)
## 
##  m-out-of-n bootstrap symmetry test by Miao, Gel, and Gastwirth (2006)
## 
## data:  x1
## Test statistic = 2.0694, p-value = 0.08
## alternative hypothesis: the distribution is asymmetric.
## sample estimates:
## bootstrap optimal m 
##                  44
symmetry.test(x2)
## 
##  m-out-of-n bootstrap symmetry test by Miao, Gel, and Gastwirth (2006)
## 
## data:  x2
## Test statistic = -0.43361, p-value = 0.626
## alternative hypothesis: the distribution is asymmetric.
## sample estimates:
## bootstrap optimal m 
##                  15
wilcox.test(x1,x2)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  x1 and x2
## W = 580, p-value = 0.09208
## alternative hypothesis: true location shift is not equal to 0
# SIGNO PARA PAREADAS
x1<-rgamma(50,1,8)
x2<-rgamma(50,3,8)
SIGN.test(x1,x2)
## 
##  Dependent-samples Sign-Test
## 
## data:  x1 and x2
## S = 6, p-value = 3.244e-08
## alternative hypothesis: true median difference is not equal to 0
## 95 percent confidence interval:
##  -0.2947534 -0.1827774
## sample estimates:
## median of x-y 
##    -0.2140903 
## 
## Achieved and Interpolated Confidence Intervals: 
## 
##                   Conf.Level  L.E.pt  U.E.pt
## Lower Achieved CI     0.9351 -0.2894 -0.1841
## Interpolated CI       0.9500 -0.2948 -0.1828
## Upper Achieved CI     0.9672 -0.3010 -0.1813
# 3 grupos
x1<-rgamma(50,4,8)
x2<-rgamma(50,6,8)
x3<-rgamma(50,8,8)
# Kuskal Wallis
library(car)
## Loading required package: carData
## 
## Attaching package: 'carData'
## The following objects are masked from 'package:BSDA':
## 
##     Vocab, Wool
## 
## Attaching package: 'car'
## The following object is masked from 'package:lawstat':
## 
##     levene.test
?kruskal.test
## starting httpd help server ...
##  done
kruskal.test(c(x1,x2,x3),g=c(rep(1,50),rep(2,50),rep(3,50)))
## 
##  Kruskal-Wallis rank sum test
## 
## data:  c(x1, x2, x3) and c(rep(1, 50), rep(2, 50), rep(3, 50))
## Kruskal-Wallis chi-squared = 65.737, df = 2, p-value = 5.313e-15
# Detectó las diferencias significativas

# 3 grupos
x1<-rgamma(50,4,8)
x2<-rgamma(50,4,8)
x3<-rgamma(50,4,8)
# Kuskal Wallis
library(car)
?kruskal.test
kruskal.test(c(x1,x2,x3),g=c(rep(1,50),rep(2,50),rep(3,50)))
## 
##  Kruskal-Wallis rank sum test
## 
## data:  c(x1, x2, x3) and c(rep(1, 50), rep(2, 50), rep(3, 50))
## Kruskal-Wallis chi-squared = 2.8059, df = 2, p-value = 0.2459
# Detectó las diferencias significativas


###############################

# PRUEBAS DE COMPARACIÓN  NO PARAMÉTRICAS

# ¿Cuales grupos son significativamente diferentes?
x1<-rgamma(50,4,8)
x2<-rgamma(50,6,8)
x3<-rgamma(50,8,8)
pairwise.wilcox.test(c(x1,x2,x3),g=c(rep(1,50),rep(2,50),rep(3,50)))
## 
##  Pairwise comparisons using Wilcoxon rank sum test with continuity correction 
## 
## data:  c(x1, x2, x3) and c(rep(1, 50), rep(2, 50), rep(3, 50)) 
## 
##   1       2   
## 2 2.1e-05 -   
## 3 3.7e-10 0.03
## 
## P value adjustment method: holm
# 2 diferentes
x1<-rgamma(50,4,8)
x2<-rgamma(50,4,8)
x3<-rgamma(50,8,8)
kruskal.test(c(x1,x2,x3),g=c(rep(1,50),rep(2,50),rep(3,50)))
## 
##  Kruskal-Wallis rank sum test
## 
## data:  c(x1, x2, x3) and c(rep(1, 50), rep(2, 50), rep(3, 50))
## Kruskal-Wallis chi-squared = 58.345, df = 2, p-value = 2.14e-13
pairwise.wilcox.test(c(x1,x2,x3),g=c(rep(1,50),rep(2,50),rep(3,50)))
## 
##  Pairwise comparisons using Wilcoxon rank sum test with continuity correction 
## 
## data:  c(x1, x2, x3) and c(rep(1, 50), rep(2, 50), rep(3, 50)) 
## 
##   1       2      
## 2 0.091   -      
## 3 3.9e-09 1.0e-11
## 
## P value adjustment method: holm