lundi 23 avril 2018

Faster nested loops with if/else

Colleagues, I'm trying to speed up an if/else query within a nested loop.

Context:

Calculating "common-language effect size" (CLE) (i.e. probability that a randomly-selected observation from one group will have a higher (or lower) score than another randomly-selected observation from the other group). Existing estimation methods assume a normal distribution, but much of my data are skewed. I'd like to get a more accurate estimate of CLE, by comparing each observation in one group with each other observation in the other group.

A working example of the project so far appears in this Shiny app: https://winzar.shinyapps.io/Inter_ocular_tests_applied_to_WV6_Shiny/

Working Code:

The following code works...

## Common Language Effect Size function
## one-to-one comparison to overcome skewness problem
##
CLE.function <- function(dimension) {
  x <- c(FirstCountry[[dimension]])
  y <- c(SecondCountry[[dimension]])
  bigger <- 0
  smaller <- 0
  equal <- 0

  for (i in x) {
    for (j in y) {
      if (i > j) {bigger <- bigger + 1}
      else
        if (i < j) {smaller <- smaller + 1}
      else
      {equal <- equal + 0.5}
    }
  }
  (bigger + equal) / (length(x) * length(y))
}   # end Common Language Effect Size function


## Selected country from a list
country_1 <- "Germany"
country_2 <- "Netherlands"

## Extract Country Data
FirstCountry <- subset(WVS_Schwartz, Country == country_1)
SecondCountry <- subset(WVS_Schwartz, Country == country_2)
MergeFile <- merge(FirstCountry, SecondCountry, all = TRUE)

## Common-Language calculation
system.time(
  CLE_Alt <- CLE.function("Altruism")
)

Question:

While the above code works, but I'm pretty sure that the loop and if/else routines could be a lot quicker.

I've attempted most of the suggestions in this StackOverflow discussion: Speed up the loop operation in R but I've failed with vectorising, and I get no appreciable difference in speed when placing anything more outside of the loop or using ifelse instead of if... else....

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire