[R] outer function problems

Spencer Graves spencer.graves at pdf.com
Tue Oct 28 14:12:45 CET 2003


      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