Search code examples
rmatrixstatisticsmultiple-resultsets

mk.test() results to tabble/matrix R


I want to apply mk.test() to the large dataset and get results in a table/matrix.

My data look something like this:

Column A Column B ... ColumnXn
1 2 ... 5
... ... ... ...
3 4 ... 7

So far I managed to perform mk.test() for all columns and print the results:

for(i in 1:ncol(data)) {      
  print(mk.test(as.numeric(unlist(data[ , i]))))
}

I got all the results printed:

.....

Mann-Kendall trend test

data: as.numeric(unlist(data[, i])) z = 4.002, n = 71, p-value = 6.28e-05 alternative hypothesis: true S is not equal to 0 sample estimates: S varS tau 7.640000e+02 3.634867e+04 3.503154e-01

Mann-Kendall trend test

data: as.numeric(unlist(data[, i])) z = 3.7884, n = 71, p-value = 0.0001516 alternative hypothesis: true S is not equal to 0 sample estimates: S varS tau 7.240000e+02 3.642200e+04 3.283908e-01

....

However, I was wondering if it is possible to get results in a table/matrix format that I could save as excel. Something like this:

Column z p-value S varS tau
Column A 4.002 0.0001516 7.640000e+02 3.642200e+04 3.283908e-01
... ... ... ... ... ...
ColumnXn 3.7884 6.28e-05 7.240000e+02 3.642200e+04 3.283908e-01

Is it possible to do so? I would really appreciate your help.


Solution

  • Instead of printing the test results you can store them in a variable. This variable holds the various test statistics and values. To find the names of the properties you can perform the test on the first row and find the property names using a string conversion:

    testres = mk.test(as.numeric(unlist(data[ , 1])))
    str(testres)
    List of 9
     $ data.name  : chr "as.numeric(unlist(data[, 1]))"
     $ p.value    : num 0.296
     $ statistic  : Named num 1.04
      ..- attr(*, "names")= chr "z"
     $ null.value : Named num 0
      ..- attr(*, "names")= chr "S"
     $ parameter  : Named int 3
      ..- attr(*, "names")= chr "n"
     $ estimates  : Named num [1:3] 3 3.67 1
      ..- attr(*, "names")= chr [1:3] "S" "varS" "tau"
     $ alternative: chr "two.sided"
     $ method     : chr "Mann-Kendall trend test"
     $ pvalg      : num 0.296
     - attr(*, "class")= chr "htest"
    

    Here you see that for example the z-value is called testres$statistic and similar for the other properties. The values of S, varS and tau are not separate properties but they are grouped together in the list testres$estimates.

    In the code you can create an empty dataframe, and in the loop add the results of that run to this dataframe. Then at the end you can convert to csv using write.csv().

    library(trend)
    # sample data
    mydata = data.frame(ColumnA = c(1,3,5), ColumnB = c(2,4,1), ColumnXn = c(5,7,7))
    # empty dataframe to store results
    results = data.frame(matrix(ncol=6, nrow=0))
    colnames(results) <- c("Column", "z", "p-value", "S", "varS", "tau")
    
    for(i in 1:ncol(mydata)) {      
      # store test results in variable
      testres = mk.test(as.numeric(unlist(mydata[ , i])))
      # extract elements of result
      testvars = c(colnames(mydata)[i],    # column
                   testres$statistic,      # z
                   testres$p.value,        # p-value
                   testres$estimates[1],   # S
                   testres$estimates[2],   # varS
                   testres$estimates[3])   # tau
      # add to results dataframe
      results[nrow(results)+1,] <- testvars
    }
    write.csv(results, "mannkendall.csv", row.names=FALSE)
    

    The resulting csv file can be opened in Excel.