[R] problems with outer

Bill Venables Bill.Venables at cmis.csiro.au
Sun May 7 01:58:54 CEST 2000


At 07:37 PM 5/6/00 -0400, Faheem Mitha wrote:
>Dear R people,
>
>I have a mysterious problem I have been tearing my hair over for some
>time. I have a function that looks as follows:
>
>covexp <- function(point,pair,theta,a,b)
>{ pointnum <- length(pair$lags)
>  x <- seq(pointnum)
>  y <- x
>  tempexpbinsumsq <- function(x,y) {expbinsumsq(point,pair,x,y,a,b,theta)}
>  tempmatrix <- outer(x,y,tempexpbinsumsq)
>  tempmatrix 
>} 

For this to work your function expbinsumsq has to be *vectorized* with
respect to x and y.  That's something you have to check.  This is the most
common reason (in my experience at least) for outer() and other such
functions not to perform as expected.   

(Note, too, that this would have no show of working in S (as opposed to R)
because of visibility restrictions.  If you were programming for
portability this would be an issue.)

>
>This returns a matrix of dim pointnum times pointnum with
>all entries zero. Now, if I try covexp(topo.point,topo.pair,c(1,1,1),2,3)
>I get a matrix of all zeros, which cannot be correct. However, if I do
>expbinsumsq(topo.point,topo.pair,1,2,2,3,c(1,1,1))  I don't get zero but
>some decimal. So at least the (2,3) element of the matrix should have been
>non-zero.
>
>Note that my function tempexpbinsumsq merely exists for the purpose of
>outer. I could have done 

>tempmatrix <-
>outer(x,y,function(x,y) expbinsumsq(point,pair,x,y,a,b,theta))

>but I don't know if that would be correct usage.

In R, yes, if your work-horse function expbinsumsq is vectorized.  In S you
would also need to do something to make the additional arguments available.

>
>Can someone explain what is going on? I'm at my wits end.

Yes, but it would take quite some space here.  I'd say get to understand as
much as you can about vectorization first, though.  I suspect that is your
problem.


>
>                                 Sincerely, Faheem Mitha.
>
>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
.-.-
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
._._
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list