簡體   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