[R] Problem with outer()

Christofer Bogaso bogaso.christofer at gmail.com
Thu Sep 23 06:31:57 CEST 2010


Thanks Petr for your input. As you correctly said to make the function
vectorize, I have done it and it is working fine:

> fff=Vectorize(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="--"))
+
+                      } , SIMPLIFY=T)
>
> 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, fff)
     [,1]                                   [,2]
            [,3]
[1,] "0.233925416547453--0.200552670788527"
"0.233925416547453--0.200552670788527"
"0.233925416547453--0.200552670788527"
[2,] "2.22952930395195--2.18449964755786"
"2.22952930395195--2.18449964755786"
"2.22952930395195--2.18449964755786"
     [,4]                                   [,5]
            [,6]
[1,] "0.233925416547453--0.200552670788527"
"0.233925416547453--0.200552670788527" "-0.997801943702248--< -0.65"
[2,] "2.22952930395195--2.18449964755786"
"2.22952930395195--2.18449964755786"
"0.997801943702248--0.973240910381122"
     [,7]                                     [,8]
                [,9]
[1,] "-2.22952930395195--< -0.65"             "-2.22952930395195--<
-0.65"             "-2.22952930395195--< -0.65"
[2,] "-0.233925416547453---0.295820647090963"
"-0.233925416547453---0.295820647090963"
"-0.233925416547453---0.295820647090963"
     [,10]                                    [,11]
[1,] "-2.22952930395195--< -0.65"             "-2.22952930395195--<
-0.65"
[2,] "-0.233925416547453---0.295820647090963"
"-0.233925416547453---0.295820647090963"


On Wed, Sep 22, 2010 at 8:27 PM, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> 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