[R] outer function problems

Scott Norton nortonsm at verizon.net
Tue Oct 28 17:19:02 CET 2003


Thanks Spencer and Tom for your help!

      Besides the other errors, I realized last night that I'm making a
fundmental error in my interpretation of the outer function.  The following
short code snippet highlights my confusion.

f<-function(A,B) { sum(A+B) }
outer(1:3,2:4,f)
     [,1] [,2] [,3]
[1,]   45   45   45
[2,]   45   45   45
[3,]   45   45   45

I had *thought* that outer() would give:
     [,1] [,2] [,3]
[1,]   3     4    5
[2,]   4     5    6
[3,]   5     6    7

ie. take each combination from A = 1,2,3; B=2,3,4 such as A=1,B=2 put it in
the sum function, get [1,1]=3 ... 
Then grab A[2]=2,B[1]=2, put them in the sum() function to get [2,1]=4,
etc... That "seems" to be the way the instructions explain "outer", i.e.
element-by-element computation of FUN() 
"Description:

     The outer product of the arrays 'X' and 'Y' is the array 'A' with
     dimension 'c(dim(X), dim(Y))' where element 'A[c(arrayindex.x,
     arrayindex.y)] = FUN(X[arrayindex.x], Y[arrayindex.y], ...)'."

Since my interpretation is *definitely* wrong, could someone put in words
how "OUTER" handles the argument vectors and the functional call with
reference to the preceding example?  
Also, what I need to happen in my code is to actually take each combination
of elements from vectors, A and B, and "feed" them repeatedly into a
function, generating a matrix of results.  How then do I do that?

Thanks in advance!!! 
-Scott

Scott Norton, Ph.D.
Engineering Manager
Nanoplex Technologies, Inc.
2375 Garcia Ave.
Mountain View, CA 94043
www.nanoplextech.com


-----Original Message-----
From: Spencer Graves [mailto:spencer.graves at pdf.com] 
Sent: Tuesday, October 28, 2003 8:13 AM
To: Scott Norton
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] outer function problems

      I don't know that this is your problem, but I see a potential 
scoping issue:  It is not obvious to me where Dk is getting n0 and w.  
I've solved this kind of problem in the past by declaring n0 and w as 
explicit arguments to Dk and then passing them explicitly via "..." in 
"outer".  In general, I prefer to avoid accessing globals from within 
functions.  This may not help you here, but it might help in the future. 

      hope this helps.  spencer graves

Scott Norton wrote:

>I'm pulling my hair (and there's not much left!) on this one. Basically I'm
>not getting the same result t when I "step" through the program and
evaluate
>each element separately than when I use the outer() function in the
>FindLikelihood() function below.
>
> 
>
>Here's the functions:
>
> 
>
>Dk<- function(xk,A,B) 
>
>{
>
>n0 *(A*exp(-0.5*(xk/w)^2) + B)
>
>}
>
> 
>
>FindLikelihood <- function(Nk)
>
>{
>
>A <- seq(0.2,3,by=0.2)
>
>B <- seq(0.2,3,by=0.2)
>
>k <-7
>
>L <- outer(A, B, function(A,B) sum( (Nk*log(Dk(seq(-k,k),A,B))) -
>Dk(seq(-k,k),A,B) ))
>
>return(L)
>
>}
>
> 
>
> 
>
>where Nk <- c(70 , 67 , 75 , 77 , 74 ,102,  75, 104 , 94 , 74 , 78 , 79 ,
83
>, 73 , 76)
>
> 
>
> 
>
>Here's an excerpt from my debug session..
>
> 
>
>  
>
>>Nk
>>    
>>
>
> [1]  70  67  75  77  74 102  75 104  94  74  78  79  83  73  76
>
>  
>
>>debug(FindLikelihood)
>>    
>>
>
>  
>
>>L<-FindLikelihood(Nk)
>>    
>>
>
>debugging in: FindLikelihood(Nk)
>
>debug: {
>
>    A <- seq(0.2, 3, by = 0.2)
>
>    B <- seq(0.2, 3, by = 0.2)
>
>    k <- 7
>
>    L <- outer(A, B, function(A, B) sum((Nk * log(Dk(seq(-k, 
>
>        k), A, B))) - Dk(seq(-k, k), A, B)))
>
>    return(L)
>
>}
>
>Browse[1]> n
>
>debug: A <- seq(0.2, 3, by = 0.2)
>
>Browse[1]> n
>
>debug: B <- seq(0.2, 3, by = 0.2)
>
>Browse[1]> n
>
>debug: k <- 7
>
>Browse[1]> n
>
>debug: L <- outer(A, B, function(A, B) sum((Nk * log(Dk(seq(-k, k), 
>
>    A, B))) - Dk(seq(-k, k), A, B)))
>
>Browse[1]> sum((Nk * log(Dk(seq(-k, k),0.2,0.2))) - Dk(seq(-k, k), 0.2,
>0.2))      # WHY DOES THIS LINE GIVE ME THE CORRECT RESULT WHEN I
SUBSTITUTE
>0.2, 0.2 FOR A AND B
>
>[1] 2495.242
>
>Browse[1]> outer(A, B, function(A, B) sum((Nk * log(Dk(seq(-k, k), 
>
>+     A, B))) - Dk(seq(-k, k), A, B)))
>
>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
>[,8]
>
> [1,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48    # BUT ELEMENT (1,1) WHICH SHOULD ALSO BE (A,B) = (0.2, 0.2),
>GIVES THE INCORRECT RESULT????
>
> [2,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [3,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [4,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [5,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [6,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [7,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [8,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
> [9,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>[10,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>[11,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>[12,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>[13,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>[14,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>[15,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>58389.48
>
>          [,9]    [,10]    [,11]    [,12]    [,13]    [,14]    [,15]
>
> [1,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [2,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [3,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [4,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [5,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [6,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [7,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [8,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
> [9,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>[10,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>[11,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>[12,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>[13,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>[14,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>[15,] 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48 58389.48
>
>Browse[1]>
>
> 
>
>As "commented" above, when I evaluate a single A,B element (i.e. A=0.2,
>B=0.2) I get a different result than when I use OUTER() which should also
be
>evaluating at A=0.2, B=0.2??
>
> 
>
>Any help appreciated.  I know I'm probably doing something overlooking
>something simple, but can anyone point it out???
>
> 
>
>Thanks!
>
>-Scott
>
> 
>
>Scott Norton, Ph.D.
>
>Engineering Manager
>
>Nanoplex Technologies, Inc.
>
>2375 Garcia Ave.
>
>Mountain View, CA 94043
>
>www.nanoplextech.com
>
> 
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>  
>




More information about the R-help mailing list