Search code examples
rfunctionloopstidyrkolmogorov-smirnov

How can I create a loop or function to cycle through a list of two vectors containing variables to run KS tests on the response


For ease I have simplified and generalized the code for the question.

So my problem (R one that is) is that I am trying to cycle through a set of Kolmogorov Smirnov ks.boot tests for multiple levels across two factors. I need to subset the data for each level of the vector df.test$names (e.g. W, X, Y, and Z representing species names) then cycle through comparing the length distributions between each level of df.test$TSM.FACT (e.g. A,B,C and so on representing time periods).

So for each level in df.test$names (e.g. W, X,Y,Z)I will need to compare their lengths distributions from different time periods A versus B; then A versus C, then B versus C and save out each result in a dataframe; recording where the comparison took place.

#for ease create the data##
  df.fact <- data.frame("A"=abs(rnorm(1000, mean = 350, sd=160)),"B"= abs(rpois(n = 1000, lambda = 50)), "C"=abs(rnorm(1000, mean = 200, sd=80)), names=rep(factor(LETTERS[23:26]), 1000))
library(reshape2)
df.test<-melt(df.fact, id.vars = "names", value.name = "Length2")
names(df.test)[names(df.test) =="variable"] <- "TSM.FACT"
names(df.test)[names(df.test) =="value"] <- "length2"

dfX <-subset(df.test, names == c("X"))
A <-subset(dfX , TSM.FACT  == c("A"))
B <-subset(dfX , TSM.FACT  == c("B"))
C <-subset(dfX , TSM.FACT  == c("C"))
KS.XAB <- ks.boot(A$length2,B$length2, nboots=5000)
KS.XAC <- ks.boot(A$length2,C$length2, nboots=5000)
KS.XBC <- ks.boot(B$length2,C$length2, nboots=5000)
summary(KS.XAB)
summary(KS.XAC)
summary(KS.XBC)

dfY<-subset(df.test, names == c("Y"))
A <-subset(dfY , TSM.FACT  == c("A"))
B <-subset(dfY , TSM.FACT  == c("B"))
C <-subset(dfY , TSM.FACT  == c("C"))
KS.YAB <- ks.boot(A$length2,B$length2, nboots=5000)
KS.YAC <- ks.boot(A$length2,C$length2, nboots=5000)
KS.YBC <- ks.boot(B$length2,C$length2, nboots=5000)
summary(KS.YAB)
summary(KS.YAC)
summary(KS.YBC)
#AND REPEAT FOR Z#

Solution

  • Preparing all patterns of names, TSM.FACT1, TSM.FACT2, you can do all test easily by loop like methods.

    Here is my example:

    library(tibble); library(tidyr); library(dplyr); library(purrr)
    
    # preparing all pattern
    comb_d <- df.test %>% 
      as_tibble() %>%       # conv to tibble
      group_by(names) %>% 
      summarize(TSM.FACT = list(unique(as.character(TSM.FACT)))) %>%   # get unique TSM.FACT as vector
      mutate(comb_ = map(TSM.FACT,
                         ~ {
                           .x %>%           # calculate all combination by combn()
                             combn(2) %>%   # output is row:2 x col:n matrix
                             t() %>% 
                             as_tibble()   # conv to row:n x col:2 tibble
                         })) %>% 
      dplyr::select(names, comb_) %>% 
      # unnest(names(.)) %>%   # for tidyr v1.0.0
      unnest() %>%             # for tidyr under v1.0.0
      set_names("names_", "TSM.FACT1", "TSM.FACT2") # chage colnames
    
    
    # making data and do ks.test line by line
    comb_d <- comb_d %>%               # I typo the below line, sorry.
      mutate(ks_res = pmap(list(names_, TSM.FACT1, TSM.FACT2),    # making data sets using 3rows
                           function(names_, TSM.FACT1, TSM.FACT2){
    
                             temp <- df.test %>% 
                               filter(names == names_)  # LIKE subset(df.test, names == c("X"))
    
                             d1 <- temp %>% 
                                      filter(TSM.FACT == TSM.FACT1) %>%   # LIKE subset(dfX , TSM.FACT  == c("A"))
                               pull(Length2)  # pull the col as vector
    
                             d2 <- temp %>% 
                               filter(TSM.FACT == TSM.FACT2) %>% 
                               pull(Length2)
    
                             Matching::ks.boot(d1, d2, nboots = 5000)  # do ks.boot
    
                           }))
    
    # you can access the result such like
    comb_d$ks_res[[1]] %>% 
      summary()
    # or
    comb_d %>% 
      filter(names == "X", TSM.FACT1 == "A", TSM.FACT2 == "B") %>% 
      pluck("ks_res")