Thank you everybody.

Phil, your expand.grid works very nicely and I will use it for
non-vectorized functions.

Yet I am a bit confused about "vectorization". For me it is synonymous of
"no loop". :-(

I wrote a toy example (with a function which is not my log-likelihood).

FIRST PART

nir=1:10
logl=function(x,y,nir) sum(log(x*nir+y))

x=seq(0.1,0.3,by=10^(-1))
y=seq(0.1,0.3,by=10^(-1))
z=outer(x,y,logl,nir=nir)

This does not work. Can you explain me why it is not "vectorised" ?

SECOND PART

nir=1:10
logl2=function(x,y,nir) {
    a=0
    for (i in 1:10) {
        a=a+log(x*nir[i]+y)
        }
    return(a)
    }

x=seq(0.1,0.3,by=10^(-1))
y=seq(0.1,0.3,by=10^(-1))
z2=outer(x,y,logl2,nir=nir)

This seems to work, though the function does not seem to be vectorized.

I am sorry for being such a noob. I'm ok in maths but I am bad at
programming. I bought a book on R (Introductory Statistics with R by
Dalgaard) ** on Amazon last week . I will read it when I receive it. Do you
know other good books ?

2008/3/5, Bill.Venables@csiro.au <Bill.Venables@csiro.au>:
>
> No problems with it working.  The main problem I have observed is
> unrealistic expectations.  People write an *essentially* non-vectorized
> function and expect Vectorize to produce a version of it which will
> out-perform explicit loops every time.  No magic bullets in this game.
>
> Bill.
>
>
>
> Bill Venables
> CSIRO Laboratories
> PO Box 120, Cleveland, 4163
> AUSTRALIA
> Office Phone (email preferred): +61 7 3826 7251
> Fax (if absolutely necessary):  +61 7 3826 7304
> Mobile:                         +61 4 8819 4402
> Home Phone:                     +61 7 3286 7700
> mailto:Bill.Venables@csiro.au
> http://www.cmis.csiro.au/bill.venables/
>
> -----Original Message-----
>
> From: Duncan Murdoch [mailto:murdoch@stats.uwo.ca]
> Sent: Wednesday, 5 March 2008 9:36 AM
> To: Venables, Bill (CMIS, Cleveland)
> Cc: r-help@r-project.org
> Subject: Re: [R] vector manipulations
>
> On 3/4/2008 5:41 PM, Bill.Venables@csiro.au wrote:
> > Your problem is that your function log1( , ) is not vectorized with
> > respect to its arguments.  For a function to work in outer(...) it
> must
> > accept vectors for its first two arguments and it must produce a
> > parallel vector of responses.
> >
> > To quote the help information for outer:
> >
> > "FUN is called with these two extended vectors as arguments.
> Therefore,
> > it must be a vectorized function (or the name of one), expecting at
> > least two arguments."
> >
> > Sometimes Vectorize can be used to make a non-vectorized function into
> a
> > vectorized one, but the results are not always entirely satisfactory
> in
> > my experience.
>
> What problems have you seen?
>
> Duncan Murdoch
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

