Skip to content

vector manipulations

6 messages · Pete Dorothy, Erik Iverson, Duncan Murdoch +1 more

#
Did you read the help page for 'outer'?

?outer

It says

      '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.


I don't think your logl function is vectorized according to your summary 
of what you did.
Pete Dorothy 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.  See

?Vectorize 


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 at csiro.au
http://www.cmis.csiro.au/bill.venables/ 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Pete Dorothy
Sent: Wednesday, 5 March 2008 3:38 AM
To: r-help at r-project.org
Subject: [R] vector manipulations

Hello,

I have simulated a set of data which i called "nir" (a vector).

I have created a function "logl" which calculates the log-likelihood.

logl is a function of 2 real parameters : "beta" and "zeta" (of length
1).

This function works perfectly well when I try for example
"logl(0.1,0.2)"

Now if I try :

x <- seq(0.1, 0.5, by = 0.1)
y <- seq(0.1, 0.5, by = 0.1)
z <- outer(x, y, logl)"

I get an error.

The problem seems to be that inside "logl", the following expression is
calculated : "sum( log( beta+(nir-1)*zeta )  )". So it is a vector
manipulation. The error tells me that "nir" is not the size of "zeta".
Yet
usually it is no problem since "length(zeta)=1".

When I replace "sum( log( beta+(nir-1)*zeta ) )" by a loop, I get no
mistake. But I think it slows down the program.

Do you have an idea where the problem is ?

Thank you very much.


______________________________________________
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.
#
On 3/4/2008 5:41 PM, Bill.Venables at csiro.au wrote:
What problems have you seen?

Duncan Murdoch
#
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 at csiro.au
http://www.cmis.csiro.au/bill.venables/ 

-----Original Message-----
From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] 
Sent: Wednesday, 5 March 2008 9:36 AM
To: Venables, Bill (CMIS, Cleveland)
Cc: r-help at r-project.org
Subject: Re: [R] vector manipulations
On 3/4/2008 5:41 PM, Bill.Venables at csiro.au wrote:
must
Therefore,
a
in
What problems have you seen?

Duncan Murdoch