I have the following vector and variables
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)
My question is if it would it be possible to pass all posible combination of variables and the x vector to this last function using the apply family? For example
a) creating a data frame for all posible combinations of different variables and passing this to the function as for the function to act on rows of the data frame in a similar way how apply is applied to a subset of the original data in the simple expression below making sure that also vector x will have to be considered by the function.
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) by using mapply over the vectors (not sure if the output will be a result per posible combination of variables and that all combinations will be passed to the function)
# 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
I am sort new to R and totaly new to the apply function I may have made basic mistakes when attempting to use apply or the function, however after attempting this for two days I decided to write in what is possibly a simple question and any help will be welcomed.