0

I would like to parallelize the following rollapply in order to make it faster:

Function for rollapply

euclidiansum <- function(x) sum(((standardize(x[,"Passenger.x"]) - standardize(x[,"Passenger.y"]))^2)) 

Calculation

tidyverse_rolling_euclidian <- pairwise_combos %>%
group_by(vehiculetype) %>%
tq_mutate(mutate_fun = rollapply,
          width      = 50,
          FUN        = euclidiansum,
          by.column  = FALSE,
          col_rename = "Euclidiansum")

Example data (very reduced example)

df <- read.table(text = "
vehiculetype,  Passenger.x, Passenger.y
  1,  400,  700
  2, 420, 100
 2, 210, 300
 2,  100,  100
  2,  400, 100
 1,  800, 70
 1,  300, 40
  1,  10, 200
  1, 50,  10
 2, 90,  30",
sep = ",", header = TRUE)

Do you know how I can speed up the calculation?

Thank you very much.

  • Can you provide an example dataset such that we better understand the data that you are working with? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – BrianLang Oct 01 '20 at 11:57
  • I provided a very reduced example as the real data set would not fit – Alex Rohrer Oct 01 '20 at 12:24
  • 1
    Does this answer your question? [Faster alternative to function 'rollapply'](https://stackoverflow.com/questions/25470659/faster-alternative-to-function-rollapply) or [this answer](https://stackoverflow.com/a/12152378/3074947) – BrianLang Oct 01 '20 at 14:10
  • Not really, as far as i know I cannot group a zoo object – Alex Rohrer Oct 01 '20 at 14:21
  • I don't understand where you are grouping, and how that gets in the way of using zoo::rollapply or writing your own code (as in the 2nd example) to give you a faster implementation. One thing that would certainly speed up your stuff would be to standardize your two vectors first and create a new variable which is their difference. In the way you implement it now, you do those steps many times, redundantly on the same rows. You would then run rollapply to take the rolling sum. – BrianLang Oct 01 '20 at 17:49

0 Answers0