Skip to content

help

5 messages · Dominique Katshunga, (Ted Harding), Adaikalavan Ramasamy +2 more

#
Dear helpeRs,
I seem to be a little bit confused on the result I am getting from the
few codes below:
[1] 0.00000000 0.03448276 0.06896552 0.10344828 0.13793103 0.17241379
 [7] 0.20689655 0.24137931 0.27586207 0.31034483 0.34482759 0.37931034
[13] 0.41379310 0.44827586 0.48275862 0.51724138 0.55172414 0.58620690
[19] 0.62068966 0.65517241 0.68965517 0.72413793 0.75862069 0.79310345
[25] 0.82758621 0.86206897 0.89655172 0.93103448 0.96551724 1.00000000
[1] 0.00000000 0.03448276 0.06896552 0.10344828 0.13793103 0.17241379
 [7] 0.20689655 0.24137931 0.27586207 0.31034483 0.34482759 0.37931034
[13] 0.41379310 0.44827586 0.48275862 0.51724138 0.55172414 0.58620690
[19] 0.62068966 0.65517241 0.68965517 0.72413793 0.75862069 0.79310345
[25] 0.82758621 0.86206897 0.89655172 0.93103448 0.96551724 1.00000000
z is a 30x30 matrix which is fine, but why all its entries are equal to
1? for example, the maximum between u[22]+v[22]-1 and 0 is not 1?? I
don't really know where I went wrong! 
thanks,
Dominique
#
On 05-Sep-05 Dominique Katshunga wrote:
A trap easy to fall into!

First, note from "?outer" that "FUN" must be a function which
"operates elementwise on arrays". In other words, given two
array arguments (x,y), for each element of x and the corresponding
element of y, it returns a value.

However, 'max' is not such a function (despite intuitive expectations).

Specifically:

  max(u+v-1,0)
  [1] 1

In other words, 'max' is like 'sum': it evaluates a single result
for the whole array. However, if you look at "?max" and read far
enough, you will find 'pmax' -- "parallel maximum" -- which does
it element by element.

Look at

  pmax(u+v-1,0)

So try

  f<-function(x,y){pmax(x+y-1,0)}
  outer(u,v,f)

The reason this is a trap is that without looking at the code for
'outer' one might perhaps expect that 'outer' picks in turn each
possible pair of values (u[i],v[j]) and passes this pair as a
set of two numbers (x,y) to f(x,y). This is not so: it passes the
entire array of pairs all at once. When "FUN" is 'max' it returns the
maximum of this entire array for each position in the array!

Look at the code for 'outer' to see how this happens: just enter

  outer

The clue is in the line

  robj <- array(FUN(X, Y, ...), c(dX, dY))

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 05-Sep-05                                       Time: 16:11:22
------------------------------ XFMail ------------------------------
#
[[ Please use a meaningful subject line. Thank you. ]]

Dominique,

I am unable to reproduce your example in R-2.1.1. Can you reproduce this
in a fresh session of R and state which version are you using ?

 u <- v <- seq(0, 1, length=30)
 f <- function(x, y){ cp=max(x+y-1,0) }
 z <- outer(u, v, f)
Error in outer(u, v, f) : dim<- : dims [product 900] do not match the
length ofobject [1]



As shown in the R FAQ 7.17 (http://tinyurl.com/amofj), you will need to
write a wrapper function. This works for me.

 wrapper <- function(x, y, my.fun, ...) {
      sapply(seq(along = x), FUN = function(i) f(x[i], y[i], ...))
  }
 z <- outer(u, v, wrapper)

dim(z)
[1] 30 30

range(z)
[1] 0 1


Regards, Adai
On Mon, 2005-09-05 at 14:06 +0200, Dominique Katshunga wrote:
#
On Mon, 5 Sep 2005, Ted.Harding at nessie.mcc.ac.uk wrote:
So easy, in fact, that it's a FAQ.

 	-thomas
#
howzit dom

i just saw your mail now!

i tried the following and got an error:
[1] 1 2 3
[1] 1 2 3
Error in outer(u, v, f) : dim<- : dims [product 9] do not match the
length of object [1]

it seems s if r is not performing elementwise maximisation!!! 

all you need is:

f=function(x,y){pmax(x+y-1,0)}
z=outer(u,v,f)

ok lets try it!!! i used length =5 to save space

u=v=seq(0,1,length=5)

f=function(x,y){pmax(x+y-1,0)}
z=outer(u,v,f)
z
     [,1] [,2] [,3] [,4] [,5]
[1,]    0 0.00 0.00 0.00 0.00
[2,]    0 0.00 0.00 0.00 0.25
[3,]    0 0.00 0.00 0.25 0.50
[4,]    0 0.00 0.25 0.50 0.75
[5,]    0 0.25 0.50 0.75 1.00

think this works!

check ?pmax

see ya
allan
Dominique Katshunga wrote: