rfor-loopkolmogorov-smirnov

Kolmogorov-Smirnov test in R - For-loop


I have a problem with comparing two sets of curves by using the Kolmogorow-Smirnow-test.

What I would like the program to do, is to compare each variation of Curve 1 with each variation of Curve 2. To accomplish that, I have tried to build a for-loop that iterates through Curve 1, and within that loop another loop that iterates through Curve 2.

Unfortunately, when executing the code, I get an error message about

"not enough x-Data“

When I try running the test by comparing one variation of each curve manually, it works, so I think the problem is the combination of the two loops and the KS-test.

If anyone has experienced a similar error and was able to solve the issue, I would highly appreciate any advice on how to fix it. Thank you!

Example data.frames:

Kurve1 <- structure(list(Punkte = 1:21,
                         Trial.1 = c(105.5, 85.3, 63.1, 54.9, 42, 34.1, 30.7,
                                     24.2, 20.1, 15.7, 14, 11, 9.3, 7.2, 6.6,
                                     5.3, 4.2, 3.3, 2.6, 1.8, 0.9),
                         Trial.2 = c(103.8, 85.2, 64.3, 54.1, 41.8, 35.9, 29,
                                     23.7, 20.2, 15.9, 13.5, 11, 9.3, 7.3, 6.4,
                                     5.5, 4.3, 3.4, 2.5, 1.9, 0.9),
                         Trial.3 = c(104.8, 87.2, 64.9, 52.8, 40.8, 35.6, 29.1,
                                     24.5, 20.4, 16.2, 13.7, 11.2, 9.2, 7.5,
                                     6.4, 5.5, 4.2, 3.5, 2.5, 1.8, 0.9),
                         Trial.4 = c(106.9, 83.9, 67.1, 55.1, 44.1, 34.1, 29.3,
                                     22.9, 19.4, 16.7, 13.6, 10.8, 9.4, 7.4,
                                     6.1, 5.6, 4.4, 3.5, 2.4, 1.9, 0.9),
                         Trial.5 = c(104.8, 84.3, 68.7, 54.8, 45.3, 35.2, 28.9,
                                     23.1, 20.1, 16.9, 13.3, 11, 9.6, 7.1, 6.3,
                                     5.4, 4.5, 3.4, 2.3, 2, 0.9)),
                    class = "data.frame", row.names = c(NA, -21L))

Kurve2 <- structure(list(Punkte = 1:21,
                         Trial.1 = c(103.5, 81.2, 66.2, 54.5, 45.1, 39.1, 30.9,
                                     27, 21.9, 19.3, 16.6, 14.9, 12.9, 11, 10.1,
                                     9.2, 8, 7.1, 6.3, 6.2, 5),
                         Trial.2 = c(104, 81, 66.9, 55.2, 46, 38.7, 31.2, 27.3,
                                     22.3, 20, 17.2, 15.2, 12.9, 11.1, 10.2,
                                     9.1, 8, 7.1, 6.4, 5.9, 5),
                         Trial.3 = c(103.9, 81.9, 67.2, 53.8, 45.4, 38.5, 31.5,
                                     26.8, 22.2, 19.8, 17.4, 15.1, 13, 10.9,
                                     10.1, 9.2, 8.1, 7.1, 6.4, 6, 4.9),
                         Trial.4 = c(104.2, 84.1, 68.7, 55.4, 45.1, 36.3, 32,
                                     26.9, 22.8, 19.8, 16.8, 14.8, 13.2, 10.9,
                                     10.3, 9.1, 8.2, 7.2, 6.3, 6.1, 5),
                         Trial.5 = c(103.8, 83.2, 69.2, 55.7, 44.8, 36.4, 31.4,
                                     26.7, 22.1, 18.9, 16.9, 14.4, 13, 11.1,
                                     10.2, 9, 7.9, 7, 6.3, 6.1, 5.1)),
                    class = "data.frame", row.names = c(NA, -21L))

The code I used for the loop:

for(i in 1:ncol(Kurve1)){
   for(j in 1:ncol(Kurve2)){
      ks.test(Kurve1$Trial.[i], Kurve2$Trial.[j], alternative = "greater")
   }
   }

Solution

  • This will work:

    for(i in 1:(ncol(Kurve1) - 2)){
      for(j in (i + 1):(ncol(Kurve2) - 1)){
        print(paste0("Trial.", i, " - Trial.", j))
        ks_result <- ks.test(Kurve1[, paste0("Trial.", i)],
                             Kurve2[, paste0("Trial.", j)],
                             alternative="greater")
        print(ks_result)
      }
    }
    

    Explanation:

    1. As it is doesn't make sense to run the KS test for the same column, and also doesn't make sense to run for both Trial.1 ~ Trial.2 and Trial.2 ~ Trial.1, etc., you have to run your outer for loop from 1 to the last but one ((ncol(Kurve1) - 2)) index for Trial.* columns, and you have to run your inner for loop from the next index as the outer loop has (i + 1) to the last index ((ncol(Kurve2) - 1)) for Trial.* columns.

    2. You can not paste strings like Trial.[i], you have to use the paste function for that. As with that the Kurve1$paste0("Trial.", i) notation not working, you have to use the extract operator [ to get the column you need (Kurve1[, paste0("Trial.", i)])

    3. As in a (nested) for loop the ks.test runs silently, a have added a print to be able to see the results. I have also added a line print(paste0("Trial.", i, " - Trial.", j)) to tag the actual result with the columns for which it belongs.