[R] Odp: Problem with outer()
Petr PIKAL
petr.pikal at precheza.cz
Wed Sep 22 16:57:21 CEST 2010
Hi
It is difficult to give some help as you did not provide any clue what the
result shall be.
basically outer takes 2 vectors and evaluate function for each combination
of elements in both vectors. However the function has to be vectorised and
your function is not.
fff=function(x,y) {
+ AA <- sign(x)
+ BB <- sign(y)
+ CC <- abs(y)
+ DD1 <- mat1[,2]-mat1[1,1]
+ DD2 <- mat2[,2]-mat2[1,1]
+ EE <- (DD1 - 0) *AA + DD2*BB
+ res1 <- mean(EE)/mat1[1,1]
+ res2 <- ifelse(quantile(EE/mat1[1,1], 0.05) >
+ -0.65, quantile(EE/mat1[1,1], 0.05), paste("< -0.65"))
+ return(paste(res1, res2, sep="--"))
+ }
> fff(1:5, 3:8)
[1] "-2.22113283836956--< -0.65"
Warning messages:
1: In DD2 * BB :
longer object length is not a multiple of shorter object length
2: In (DD1 - 0) * AA + DD2 * BB :
longer object length is not a multiple of shorter object length
Therefore you need to redefine DD1, DD2 and EE computation.
Regards
Petr
r-help-bounces at r-project.org napsal dne 22.09.2010 10:18:07:
> Dear all, I have following piece of codes:
>
> xx <- seq(-2,2, length.out=11)
> mat1 <- cbind(rep(43, 5), rnorm(5))
> mat2 <- cbind(rep(53, 5), rnorm(5))
> outer(c(1,-1), xx, function(x,y) {
> AA <- sign(x)
> BB <- sign(y)
> CC <- abs(y)
> DD1 <- mat1[,2]-mat1[1,1]
> DD2 <- mat2[,2]-mat2[1,1]
> EE <- (DD1 - 0) *AA + DD2*BB
> res1 <- mean(EE)/mat1[1,1]
> res2 <- ifelse(quantile(EE/mat1[1,1], 0.05) >
> -0.65, quantile(EE/mat1[1,1], 0.05), paste("< -0.65"))
> return(paste(res1, res2, sep="--"))
> }
> )
>
> While running this code I am getting warnings as well as error:
>
> > outer(c(1,-1), xx, function(x,y) {
> + AA <- sign(x)
> + BB <- sign(y)
> + CC <- abs(y)
> + DD1 <- mat1[,2]-mat1[1,1]
> + DD2 <- mat2[,2]-mat2[1,1]
> + EE <- (DD1 - 0) *AA + DD2*BB
> + res1 <- mean(EE)/mat1[1,1]
> + res2 <- ifelse(quantile(EE/mat1[1,1], 0.05) >
> -0.65, quantile(EE/mat1[1,1], 0.05), paste("< -0.65"))
> + return(paste(res1, res2, sep="--"))
> + }
> + )
> Error in dim(robj) <- c(dX, dY) :
> dims [product 22] do not match the length of object [1]
> In addition: Warning messages:
> 1: In (DD1 - 0) * AA :
> longer object length is not a multiple of shorter object length
> 2: In DD2 * BB :
> longer object length is not a multiple of shorter object length
>
> I am able to trace the warning, which comes from multiplication with
> AA & BB. However could not find the correct way to tackle this
> warning. Neither the error. Can somebody help me where I was wrong?
>
> Thanks
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list