繁体   English   中英

制定函数以将变量提供给mapply函数

[英]Formulating a function to supply variables to a mapply function

我有以下函数,将其放在第一个示例中时效果很好。 但是,我想让两个变量在mapply函数中分别具有两个列表,以便以任何形式提供两个结果。 变量w2是具有两个分量的列表,而xx是具有2个向量的列表。

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()

我可能没有正确使用第二个mapply,因为它似乎无法正常工作。 我想知道这是否可以表示为最后一个mapply的循环,其中两个变量应输入mapply函数,或者我可以使用另一个Apply系列来做到这一点。 结果应该与将正确的第一个示例两次应用于分离的变量数据提要中相同,但以列表形式结合在一起。

编辑

坦率的回应之后。 引发的一个问题是,如果MoreArgs = list(x =(xx [[i]] [[1]])--MoreArgs = list(x =(xx [[i] ] [[j]]))),这意味着将在函数j中引入一个新变量,因为将其添加到上述解决方案中没有包含在任何部分中。

对于以下代码的结果,我不确定是否能解决所有问题,我只能说它可以运行并且答案与您第一次尝试获得的结果一致。

我创建了一个名为mapply2的函数,如下所示。

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]])))
}

请注意,我通过与输入的其余部分相同的变量来索引列表xx,这完全是因为w2和xx是长度相同的列表。 (可以接受吗?)

然后使用lapply为每个w2和xx调用该函数,

result <- lapply(z, mapply2)

还要注意,函数输入(或缺少函数输入)要求从包含w2和xx的同一环境中调用mapply2。

编辑:在无法访问完整示例的情况下,我只能猜测如何修改答案。 但我最大的猜想是

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)
}

这称为result <- lapply(z, mapply3, w2, xx) 为了检查代码是否可以运行,我使用了以下形式的xx(无论结构上是否类似于我不知道的完整版本)。

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)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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