1
我有以下的矢量和變量考取多個可變參數的函數與申請家庭
x<-c(1,2,3,4,5,7,6,7,8,9,8,7,6,4,5,3,2,2,3,4,5,6,67,7,7)
wavelet<-c("d2","s2")
n.level<-c(1,2)
schirnkfun <- c("soft", "hard", "mid")
threshfun<-c("universal", "adaptive", "minimax")
threshscale<-c(0.25,1.25)
#I select one variable from above and use lapply which yields results per variable.
library(wmtsa)
ws <- lapply(threshfun, function(k,x)
wavShrink(x,wavelet= "s2",n.level=2,shrink.fun="hard", thresh.fun= k,threshold=NULL,thresh.scale=1, xform="modwt", noise.variance=-1, reflect=TRUE),x=x)
我的問題是,如果它有可能傳遞的變量的所有更多鈔票組合與x向量該使用應用系列的最後一個功能?例如
a)爲不同變量的所有可能組合創建一個數據框,並將該數據框傳遞給函數,以便以類似的方式將該函數作用於數據框的行,該方法如何將應用應用於原始數據在下面的簡單表達式中,確保函數還必須考慮向量x。
A<-expand.grid(c(1,2), c(3,6), c(4,2), c(2,5,3), c(0.25,1.25))
apply(A[,c('Var1' , 'Var5' , 'Var2' , 'Var3' , 'Var4')], 1, function (x) sum(x))
b)通過利用mapply在矢量(不知道的輸出將是每個變量更多鈔票組合和所有組合將被傳遞給函數)
# so here is my attempt to use apply to the combination of variables. First I create a data frame of variables.
A <- expand.grid(c("d2","s2"), c(1,2),c("soft", "hard"),c("universal", "adaptive", "minimax"),c(0.25,1.25))
# and write the function
function(k,l,m,n,e,x) (wavShrink(x,wavelet= k,n.level= l,shrink.fun= m, thresh.fun= n,threshold=NULL,thresh.scale= e, xform="modwt", noise.variance=-1, reflect=TRUE)
#Attempt to pass the data frame for lapply without success possibly because lapply is for vectors or lists
A<-expand.grid(c("d2","s2"), c(1,2),c("soft", "hard"),c("universal", "adaptive", "minimax"),c(0.25,1.25))
ws<-lapply(A[,c('Var1' , 'Var2' , 'Var3' , 'Var4' , 'Var5')],1, function(k,l,m,n,e,x)
wavShrink(x,wavelet= k,n.level= l,shrink.fun= m, thresh.fun= n, threshold=NULL,thresh.scale= e, xform="modwt", noise.variance=-1, reflect=TRUE),x=x)
#Error en match.fun(FUN) : '1' is not a function, character or symbol
#and for mapply which take multiple lists or vectors I pass the variables as vectors, however it fails as it seems I can not pass the x vector to the function
FUN<-function(x,k,l,m,n,e) (wavShrink(x,wavelet= k,n.level= l,shrink.fun= m, thresh.fun= n, threshold=NULL,thresh.scale= e, xform="modwt", noise.variance=-1, reflect=TRUE))
mapply(FUN, c("d2","s2"), c(1,2),c("soft", "hard"),c("universal", "adaptive", "minimax"),c(0.25,1.25),x=x)
#Error en wavShrink(x, wavelet = k, n.level = l, shrink.fun = m, thresh.fun = n, :
#Time series must contain more than one point
我的結果對R進行全新排序並對應用函數新增我可能在嘗試使用應用程序或函數時犯了基本錯誤,但在嘗試完成兩天後,我決定編寫一個可能很簡單的問題,並且歡迎任何幫助。
我建議你先嚐試一個簡單的例子,瞭解它是如何工作的。你好像會遇到各種各樣的錯誤,這些錯誤來自'lapply'中的語法錯誤,'wavShrink'中的錯誤等等。爲什麼不開始用兩個或三個變量的簡單函數替換'wavShrink',並且找出'mapply '首先工作,然後你可以嘗試更復雜的。 – konvas 2014-10-10 14:53:49
我同意一個更簡單的函數將幫助我更好地理解並改進語法問題。讓我試試更多的內容,看看是否有類似的非用戶定義函數的例子,其中在我目前找不到的網絡中使用了一些類似的非用戶定義函數。 – Barnaby 2014-10-10 15:24:14
我認爲這是一個好主意。這裏沒有必要重複已經寫過很多次的'apply'函數的指南(參見http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-應用與vs-tapply-vs-by-vs-aggrega/7141669#7141669) – konvas 2014-10-10 15:29:49