Barnaby
Barnaby

Reputation: 1480

mapply over a function yields erronoeusly the aplication of another function

I am attempting to create a filter to apply to a time series. For reasons I have been trying but failed to understand when I apply a function (fir1) the package applies another function (fir2) from the same package and makes an error (which is Ok as the fir2 function is uncomplete) - http://cran.r-project.org/web/packages/signal/signal.pdf - . There are no conflicting packages. When the function is applied without applying mapply then it works fine. I believe Mapply is the right overlaping function to apply. I also attempted to use expand.grid as to combine all variables with the same result. My question is if is possible and if so and how could use fir1 with mapply for the expresión below.

library(signal)
N<-list(c(8448, 6034 ,3520,  960,  899,  704), c(6034, 3520, 2485, 1083,  960,  899,  704))

Rup<-list(c(0.03925781, 0.05496094, 0.09421875, 0.34546875, 0.36902344, 0.47109375),c(0.05496094, 0.09421875, 0.13347656, 0.30621094, 0.34546875, 0.36902344, 0.47109375))

Rdown<-list(c(0.03886719, 0.05441406, 0.09328125, 0.34203125, 0.36535156, 0.46640625),c(0.05441406, 0.09328125 ,0.13214844, 0.30316406, 0.34203125, 0.36535156, 0.46640625))

L<- seq(1:length(N))
LL <- function(x) seq(1:length(Rdown[[x]]))
LLL<-lapply(L,LL)

n <- function (x,y) round(3.3/(Rup[[x]][y]-Rdown[[x]][y]))
N <- mapply(n,L,LLL)

F<- function(x,y) fir1(N[[x]][y]-1, c(Rdown[[x]][y], Rup[[x]][y]), "pass", scale = TRUE)
FILTER <- mapply(F,L,LLL)

#Error en fir2(n, f, m, 512, 2, window) : 
#frequency must be nondecreasing starting from 0 and ending at 1
#Además: Mensajes de aviso perdidos
#1: In if (n == 1) c = 1 else { :
#la condición tiene longitud > 1 y sólo el primer elemento será usado
#2: In 0:n :
#expresión numérica tiene 6 elementos: solo el primero es utilizado

#the same error occurs 
LLLL<-expand.grid(L=L,LLL=LLL)
F<-mapply(function(x,y)  fir1(N[[x]][y]-1, c(Rdown[[x]][y], Rup[[x]][y]), "pass", scale = TRUE),LLLL$L,LLLL$LLL)

#when applied directly it works fine. 
fir1(N[[1]][1]-1, c(Rdown[[1]][1], Rup[[1]][1]), "pass", scale = TRUE)

Any help is welcomed

Upvotes: 0

Views: 66

Answers (1)

DatamineR
DatamineR

Reputation: 9618

If you want to apply directly you should not look at N[[1]][1]-1 and c(Rdown[[1]][1], Rup[[1]][1]) but at:

v1 <- N[[L[[1]]]][LLL[[1]]]-1
> v1
[1] 8447 6033 3519  959  898  703

v2 <- c(Rdown[[L[[1]]]][LLL[[1]]], Rup[[L[[1]]]][LLL[[1]]])
> v2
 [1] 0.03886719 0.05441406 0.09328125 0.34203125 0.36535156 0.46640625 0.03925781 0.05496094 0.09421875 0.34546875
[11] 0.36902344 0.47109375

If you look at the arguments of the function fir1 you can read about the first argument n:

n    order of the filter (1 less than the length of the filter)

So, it should be one number, and not a vector as v1.

And about the seccond argument w:

w    band edges, strictly increasing vector in the range [0, 1]...

but as you can see and check on v2, this condition is not fulfilled:

sum(!diff(v2)>=0)
[1] 1

Upvotes: 1

Related Questions