简体   繁体   中英

Formulating a function to supply variables to a mapply function

I have the following function which when placed in the first example works fine. However I would like to have two of the variables to have additionally two lists each within the mapply function as to provide two results in any form. variable w2 is a list with two components and xx is a list with 2 vectors.

library(wmtsa)

# data feed to function
wavelet <-  c("d2","s2","d4","s4","d6")
schrinkfun <- c("soft","hard") 
threshfun <- c("universal", "adaptive")
threshscale <- c(0.05,0.1,0.15,0.2)
xx <- c(1,2,3,4,5,6,7,5,4,3,2,4,3,2,3,5,4,3,2,3,4,5,6,3,2,1,2,3,5,4,3,3)
nlevel<-seq(1: as.integer (floor (logb ((length(xx)),base=2))))   
w2 <- expand.grid(wavelet=wavelet,nlevel=nlevel,schrinkfun=schrinkfun, threshfun= threshfun, threshscale= threshscale, stringsAsFactors=FALSE) 

# Original function: To which I apply a unique list of values (w2) and a single vector for x. This function works fine.  

result <-  mapply(function(m,k,p,u,l,x)  (wavShrink(x, wavelet= m, n.level =k, shrink.fun = p, thresh.fun =u, threshold=NULL, thresh.scale = l, xform="modwt", noise.variance=-1, reflect=TRUE)), w2$wavelet, w2$nlevel, w2$schrinkfun, w2$threshfun, w2$threshscale, MoreArgs=list(x=(xx)))

# Attempt to use (1) the index of w2 which is now a list of two (index z) - (2) the index of the list of xx which is a list of tow (index g). Again data feed with new levels and xx now formed each by a list of two elements. 

wavelet <-  c("d2","s2","d4","s4","d6")
schrinkfun <- c("soft","hard") 
threshfun <- c("universal", "adaptive")
threshscale <- c(0.05,0.1,0.15,0.2)
xx <- list(c(1,2,3,4,5,6,7,5,4,3,2,4,3,2,3,5,4,3,2,3,4,5,6,3,2,1,2,3,5,4,3,3),c(0,3,1,4,1,2,7,5,4,1,3,4,9,2,7,5,1,3,2,2,4,7,6,4,2,1,1,1,5,1,3,1))
g <- seq(1:length(xx))
fun <- function (x) seq(1: as.integer (floor (logb ((length(xx[[x]])),base=2))))   
nlevel <- lapply( g,fun)
fun <-  function(x) expand.grid(wavelet=wavelet,nlevel=nlevel[[x]], schrinkfun=schrinkfun, threshfun= threshfun, threshscale= threshscale, stringsAsFactors=FALSE) 
w2 <- lapply(g,fun)
z <- seq(1:length(w2))

# Attempt 1  
result <-  mapply(function(m,k,p,u,l,x)  (wavShrink(x, wavelet= m, n.level =k, shrink.fun = p, thresh.fun =u, threshold=NULL, thresh.scale = l, xform="modwt", noise.variance=-1, reflect=TRUE)), w2[[z]]$wavelet, w2[[z]]$nlevel, w2[[z]]$schrinkfun, w2[[z]]$threshfun, w2[[z]]$threshscale, MoreArgs=list(x=(xx[[g]])))
Error in w2[[z]]$wavelet : $ operator is invalid for atomic vectors

# Attempt 2  
result <-  mapply ( function(z,g) ( mapply ( function(m,k,p,u,l,x)  (wavShrink(x, wavelet= m, n.level =k, shrink.fun = p, thresh.fun =u, threshold=NULL, thresh.scale = l, xform="modwt", noise.variance=-1, reflect=TRUE)), w2[[z]]$wavelet, w2[[z]]$nlevel, w2[[z]]$schrinkfun, w2[[z]]$threshfun, w2[[z]]$threshscale, MoreArgs=list(x=(xx[[g]])))))
result
list()

I am probably not using correctly the second mapply as it seems not to work. I was wondering if this could be formulated as a loop for the last mapply where the two variables should feed into the mapply function or maybe I could use another apply family to do this. The outcome should be the same as applying the correct first example twice in separated variable data feeds but joined as a list.

EDIT

Following the reponse from frank. A question that has promted is how would you reformulate the expression response function if MoreArgs=list ( x = ( xx[[i]][[1]] )) was -- MoreArgs=list(x=(xx[[ i ]][[ j ]]))), meaning a new variable would have been introduced into the function - j - which is not included in any of the parts as per adding this to your solution above.

Not being sure about the problem you're trying to solve all I can say about the result of the code below is that it runs and that the answer is consistent with the result obtained from your first attempt.

I created a function called mapply2, shown below.

mapply2 <- function(i){
    w3 <- w2[[i]]
    mapply(function(m,k,p,u,l,x) wavShrink(x, wavelet= m, n.level =k, shrink.fun = p, 
                                          thresh.fun =u, threshold=NULL, 
                                          thresh.scale = l, xform="modwt", 
                                          noise.variance=-1, reflect=TRUE), 
          w3$wavelet, w3$nlevel, w3$schrinkfun, w3$threshfun, 
          w3$threshscale, MoreArgs=list(x=(xx[[i]])))
}

Note that I indexed the list xx by the same variable as the rest of the input, purely because w2 and xx were lists of the same length. (is this acceptable?)

The function is then called for each w2 and xx by using lapply,

result <- lapply(z, mapply2)

Also note that the function input (or lack there of) requires that mapply2 is called from the same environment that contains w2 and xx.

Edit: Without having access to the full example I can only take a guess about how to modify my answer. But my best guess is that something along the lines of

mapply2 <- function(xxi, w){

    mapply(function(m,k,p,u,l,x) wavShrink(x, wavelet = m, n.level = k,         shrink.fun = p, 
                                       thresh.fun = u, threshold = NULL, 
                                       thresh.scale = l, xform = "modwt", 
                                       noise.variance = -1, reflect = TRUE), 
       w$wavelet, w$nlevel, w$schrinkfun, w$threshfun, 
       w$threshscale, MoreArgs = list(x = xxi))
}

mapply3 <- function(i, w2, xx){
    xxi <- xx[[i]]
    w3  <- w2[[i]]
    z2  <- seq(1, length(xxi), 1)
    lapply(xxi, mapply2, w3)
}

This is called as follows result <- lapply(z, mapply3, w2, xx) . For the purposes of checking that the code could even run I used the following form of xx (whether or not this is structurally similar to the full version I do not know).

xx  <- list(list(c(1,2,3,4,5,6,7,5,4,3,2,4,3,2,3,5,4,3,2,3,4,5,6,3,2,1,2,3,5,4,3,3),
             c(0,3,1,4,1,2,7,5,4,1,3,4,9,2,7,5,1,3,2,2,4,7,6,4,2,1,1,1,5,1,3,1)),
        list(c(0,3,1,4,1,2,7,5,4,1,3,4,9,2,7,5,1,3,2,2,4,7,6,4,2,1,1,1,5,1,3,1),
             c(1,2,3,4,5,6,7,5,4,3,2,4,3,2,3,5,4,3,2,3,4,5,6,3,2,1,2,3,5,4,3,3)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM