1

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.

Barnaby
  • 1,472
  • 4
  • 20
  • 34
  • 1
    I suggest you try this on a simpler example first, to understand how it works. You seem to be getting all sorts of errors, coming from syntax error in `lapply`, error within `wavShrink`, etc. Why not start by replacing `wavShrink` by a simple function of two or three variables and figure out how `mapply` works first, then you can try with the more complicated one. – konvas Oct 10 '14 at 14:53
  • I agree that a simpler function will help me better undertand and improve the syntax issues. Let me try some more and see if there are examples of similar non user defined functions where apply is used somehwere in the net which I could not find so far. – Barnaby Oct 10 '14 at 15:24
  • I think that's a good idea. There is no point here in repeating a guide to `*apply` functions that has been written many times (see e.g. http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega/7141669#7141669) – konvas Oct 10 '14 at 15:29
  • 1
    When the phrase: "pass all posible combination of variable" is uttered, it generally means the speaker wants to use `expand.grid`. It would produce a result that would be appropriate for an `mapply` operation. I would have expected it to be `expand.grid( wavelet, n.level, schirnkfun, threshfun, threshscale)` – IRTFM Oct 10 '14 at 16:09

1 Answers1

0

I made a little progress in undertanding mapply so I believe I can answer my own question

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)
x<-c(1,2,3,4,5,7,6,7,8,9,8,7,6,4,5,3,2,2,3,4,5,6,6,7,7,7,5,6,7,7,8,8,9,0,9,0,8,7,5,4,3,3,4,4,4,4,3,2,2,1,2,3,4,5,6,5,7,8,8,9,9,0,2,3,4,2,3,5,5,2,4,6,7)

w1<- expand.grid(wavelet=wavelet,n.level=n.level,schirnkfun= schirnkfun,threshfun= threshfun,threshscale= threshscale, stringsAsFactors=FALSE)

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)), w1$wavelet,  w1$n.level , w1$schirnkfun,   w1$ threshfun, w1$threshscale ,MoreArgs=list(x=x))

colnames(result)=c(rownames(w1))

The number of colums is equal to the number of rows of w1, every row from expand,grid is evaluated by the function.

Barnaby
  • 1,472
  • 4
  • 20
  • 34