How to write this function to have as output a vector when the input is a vector?(UPDATE)

Viewed 43

enter image description here

h = max{dk(l), hmin}, where dk(l) is the distance from l to the 70th nearest li and hmin=1000. Hello, I want to have in output a vector when the entry is vector for this function. I want l,li and yi in entries, so I can calcultef1(L,li=position,yi=y) with differents vectors for li and yi. Let's consider only this vectors for yi and li for example:

y <- seq(1, 100, length.out = 23710)
position <- seq(10351673, 12422082, length.out=23710)
L <- seq(11190000, 11460000, by=10)

With help of @jblood94 I got this

library(parallel)

f1 <- function(l) {
  dkAll <- abs(outer(position, l, "-"))
  Knorm <- dnorm(outer(position, l, "-")/pmax(dkAll[order(col(dkAll), dkAll)[seq(70, by = length(position), length.out = length(l))]], 1000))
  colSums(Knorm*y)/colSums(Knorm)
}

y <- seq(1, 100, length.out = 23710)
position <- seq(10351673, 12422082, length.out=23710)
l <- seq(11190000, 11460000, by=10)
# ysmoothed <- f1(l) # memory allocation error
cl <- makeCluster(detectCores())
clusterExport(cl, list("y", "position", "l", 'f1'))
system.time(ysmoothed <- parLapply(cl, l, f1))
#>    user  system elapsed 
#>    0.02    0.00   20.13

This function works correctly. The purpose is to rewrite with three entries and avoid error Error in get(name, envir = envir) : object 'y' not found by using the command clusterExport(cl, list("y", "position", "l", 'f1')) . I hope @jblood94 will see my message!

1 Answers

Is this what you're looking for?

I've modified the function and added a second set of data so you could see how you can change values in the input vectors.

# modified function
fl = function(l, li, yi){
  dkAll <- abs(outer(li, l, "-"))
  Knorm <- dnorm(outer(li, 
                       l, 
                       "-")/pmax(
                         dkAll[order(col(dkAll), dkAll)[
                           seq(70, by = length(li), 
                               length.out = length(l))]], 1000))
  colSums(Knorm * yi)/colSums(Knorm)
}

First run (with data from the question)

# original values
y <- seq(1, 100, length.out = 23710)
position <- seq(10351673, 12422082, length.out=23710)
l <- seq(11190000, 11460000, by=10)

# call function
cl <- makeCluster(detectCores())
system.time(ysmoothed <- parLapply(cl, l, fl, position, y))
#    user  system elapsed 
#   0.032   0.034   7.487  
stopCluster(cl)
ysmoothed[1:8] %>% unlist()
#  [1] 41.08598 41.08646 41.08694 41.08741 41.08789 41.08837 41.08885 41.08933

Second run (changed input vectors)

# new values (appended numbers to original values in position and L)
yx <- seq(1, 100, length.out = 23710)
positionX <- seq(103516730, 124220820, length.out=23710)
LX <- seq(1119000, 1146000, by=10)

# call function
cl <- makeCluster(detectCores())
system.time(ysmoothed <- parLapply(cl, LX, fl, positionX, yx))
# user  system elapsed 
# 0.017   0.006   0.621 

stopCluster(cl)
ysmoothed[1:8] %>% unlist()
#  [1] 48.66928 48.66928 48.66928 48.66928 48.66928 48.66928 48.66928 48.66928

If this isn't what you were looking for let me know.

Related