最初の例に配置すると正常に動作する次の関数があります。ただし、任意の形式で 2 つの結果を提供するために、2 つの変数にさらに 2 つのリストをそれぞれ mapply 関数内に持たせたいと考えています。変数 w2 は 2 つのコンポーネントを持つリストで、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()
2 番目の mapply が機能していないように見えるので、おそらく正しく使用していません。これは、2 つの変数が mapply 関数にフィードされる最後の mapply のループとして定式化できるかどうか、またはこれを行うために別の適用ファミリを使用できるかどうか疑問に思っていました。結果は、正しい最初の例を別々の可変データ フィードで 2 回適用した場合と同じになりますが、リストとして結合されます。
編集
フランクからの応答に続いて。MoreArgs=list ( x = ( xx[[i]][[1]] )) が -- MoreArgs=list(x=(xx[[ i ] ][[ j ]])))、新しい変数が関数に導入されたことを意味します - j - 上記のソリューションにこれを追加することにより、どの部分にも含まれません。