Kruskal-wallis in R

4.1k views Asked by At

I have tried to perform the Kruskal-Wallis test, but I am not sure if the data is ordered correctly, therefore, I do not know if the result is correct.

I still do not know how to verify that it is correct, I do not know if you can help me identify the error.

Thank you.

Data:
There are 13 samples and 138 observation, divided by months.

             ï..meses fajerae
            1     ene-15       0
            2     ene-15       1
            3     ene-15       0
            4     ene-15       0
            5     ene-15       0
            6     ene-15       0
            7     ene-15       1
            8     ene-15       1
            9     ene-15       0
            10    ene-15       1
            11    feb-15       0
            12    feb-15       0
            13    feb-15       0
            14    feb-15       1
            15    feb-15       0
            16    feb-15       1
            17    feb-15       1
            18    feb-15       0
            19    feb-15       0
            20    feb-15       3
            21    feb-15       2
            22    feb-15       2
            23    feb-15       0
            24    feb-15       0
            25    feb-15       1
            26    mar-15       1
            27    mar-15       0
            28    mar-15       3
            29    mar-15       3
            30    mar-15       1
            31    mar-15       4
            32    mar-15       2
            33    mar-15       5
            34    mar-15       0
            35    mar-15       1

Here I do the Kruskal-Wallis test.

#kruskal-wallis test
  
setwd("~/")

datos <- read.csv(file.choose(), header = TRUE)
datos


#estructura de los datos
str(datos)
attach(datos)
names(datos)
class(datos)
factor(datos)

Here it does not recognize the levels, the results are null, I expected it to show me the months, ene-15, Feb-15, Mar-15, Nov-15, etc.

levels(datos$ï..meses)

But in the table, it's correct.

table(datos$ï..meses)

    abr-17 dic-15 dic-16 dic-17 ene-15 ene-16 ene-17 ene-18 feb-15 feb-16 mar-15 
        10     10     10     12     10     10     13     20     15      2     10 
    mar-17 nov-15 
         4     12

I did the homocedasticity test

# Homocedasticidad: la varianza debe de ser constante entre todos los grupos.
 
 leveneTest(fajerae ~ ï..meses, data = matriz, center = "median")
 
 #Results#
 Levene's Test for Homogeneity of Variance (center = "median")
       Df F value  Pr(>F)  
  group  12  1.8447 0.04784 *
        125                  
  ---
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  Warning message:
  In leveneTest.default(y = y, group = group, ...) : group coerced to factor.

And the Kruskal-Wallis test, which gave me a result, but I don't know how to check if it is correct.

    kruskal.test(fajerae ~ ï..meses, data = matriz)

#Results# 
Kruskal-Wallis rank sum test
data:  fajerae by ï..meses 
Kruskal-Wallis chi-squared = 24.493, df = 12, p-value = 0.01742
1

There are 1 answers

0
Rui Barradas On

The code in the question can be simplified.

1. Load the non-base package(s) you are going to use.

library(car)

2. Check your data structure with str.

str(datos)
#'data.frame':  35 obs. of  2 variables:
# $ ï..meses  : chr  "ene-15" "ene-15" "ene-15" "ene-15" ...
# $ fajerae   : int  0 1 0 0 0 0 1 1 0 1 ...

That first name, ï..meses, is rather ugly with its accents and nonsense dots, make it prettier.

names(datos)[1] <- "meses"

From the output of str you know that meses is of class "character", so the next instruction is expected to return NULL.

levels(datos$meses)
#NULL

But if you want a factor, it's simply

datos$meses <- factor(datos$meses)

3 The distribution of meses:

table(datos$meses)
#
#ene-15 feb-15 mar-15 
#    10     15     10 

4. Determine homocedasticity by groups of meses.

leveneTest(fajerae ~ meses, data = datos, center = "median")
#Levene's Test for Homogeneity of Variance (center = "median")
#      Df F value  Pr(>F)  
#group  2  3.4584 0.04367 *
#      32                  
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

5. Finally, the Kuskall-Wallis test.

If you are trying to determine whether the groups have the same median, why not check a box-and-whiskers plot first? See your data.

boxplot(fajerae ~ meses, data = datos)

enter image description here

The groups seem to have different locations, confirm with a non parametric test.

kruskal.test(fajerae ~ meses, data = datos)
#
#   Kruskal-Wallis rank sum test
#
#data:  fajerae by meses
#Kruskal-Wallis chi-squared = 6.8633, df = 2, p-value = 0.03233
#

You ask if the test is right. Yes, it is, in the sense that base R functions are well coded and tested countless times by countless users along the years. Whether this is expected is a different question, and only you can answer to it.

Data in dput format.

datos <-
structure(list(meses = c("ene-15", "ene-15", "ene-15", "ene-15", 
"ene-15", "ene-15", "ene-15", "ene-15", "ene-15", "ene-15", "feb-15", 
"feb-15", "feb-15", "feb-15", "feb-15", "feb-15", "feb-15", "feb-15", 
"feb-15", "feb-15", "feb-15", "feb-15", "feb-15", "feb-15", "feb-15", 
"mar-15", "mar-15", "mar-15", "mar-15", "mar-15", "mar-15", "mar-15", 
"mar-15", "mar-15", "mar-15"), fajerae = c(0L, 1L, 0L, 0L, 0L, 
0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 3L, 2L, 
2L, 0L, 0L, 1L, 1L, 0L, 3L, 3L, 1L, 4L, 2L, 5L, 0L, 1L)), 
class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
"29", "30", "31", "32", "33", "34", "35"))