[R] [R/S] strange

2004-09-01 Thread Erin Hodgess
Dear R and S People:

I have run across something very strange.  Here is a function that I wrote
for R:

boot1 - function(y,method=f,p=1) {
n1 - length(y)
n2 - n1*p
n3 - n2 - 1
a - 0.5*(outer(1:n3,1:n3,function(x,y){n2 - pmax(x,y)}))
return(a)
}

and here is the R output:
 y1
[1] 9 8 7 3 6
 source(boot1.R)
 boot1(y=y1,p=4)
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] 
[,16] [,17] [,18] [,19]
 [1,]  9.5  9.0  8.5  8.0  7.5  7.0  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [2,]  9.0  9.0  8.5  8.0  7.5  7.0  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [3,]  8.5  8.5  8.5  8.0  7.5  7.0  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [4,]  8.0  8.0  8.0  8.0  7.5  7.0  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [5,]  7.5  7.5  7.5  7.5  7.5  7.0  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [6,]  7.0  7.0  7.0  7.0  7.0  7.0  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [7,]  6.5  6.5  6.5  6.5  6.5  6.5  6.5  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [8,]  6.0  6.0  6.0  6.0  6.0  6.0  6.0  6.0  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
 [9,]  5.5  5.5  5.5  5.5  5.5  5.5  5.5  5.5  5.5   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
[10,]  5.0  5.0  5.0  5.0  5.0  5.0  5.0  5.0  5.0   5.0   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
[11,]  4.5  4.5  4.5  4.5  4.5  4.5  4.5  4.5  4.5   4.5   4.5   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
[12,]  4.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0   4.0   4.0   4.0   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
[13,]  3.5  3.5  3.5  3.5  3.5  3.5  3.5  3.5  3.5   3.5   3.5   3.5   3.5   3.0   2.5 
  2.0   1.5   1.0   0.5
[14,]  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0  3.0   3.0   3.0   3.0   3.0   3.0   2.5 
  2.0   1.5   1.0   0.5
[15,]  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5   2.5   2.5   2.5   2.5   2.5   2.5 
  2.0   1.5   1.0   0.5
[16,]  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0   2.0   2.0   2.0   2.0   2.0   2.0 
  2.0   1.5   1.0   0.5
[17,]  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5   1.5   1.5   1.5   1.5   1.5   1.5 
  1.5   1.5   1.0   0.5
[18,]  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0   1.0   1.0   1.0   1.0   1.0   1.0 
  1.0   1.0   1.0   0.5
[19,]  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5   0.5   0.5   0.5   0.5   0.5   0.5 
  0.5   0.5   0.5   0.5
 
which is as it should be.

Now I tried the same thing in S+
 y1
[1] 9 8 7 3 6
 source(boot1.R)
 boot1(y=y1,p=4)
Problem in FUN(rep(X, length(Y)), rep(Y, each = length(X))): Object n2 not found 
Use traceback() to see the call stack
 

I have no idea why it works in R but not in S+.

R 1.9.1 for Windows
S+ Version 6.2 for Windows.
Any help is appreciated!

Is this part of the difference in scoping, please?

Sincerely,
Erin M. Hodgess
Associate Professor
Dept. of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: [EMAIL PROTECTED]

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] [R/S] strange solution

2004-09-01 Thread Erin Hodgess
Dear R and S People:

I ended up using the assign command, and things work in S+.

boot1 - function(y,method=f,p=1) {
n1 - length(y)
#n2 - n1*p
assign(n2,n1*p)
n3 - n2 - 1
a - 0.5*(outer(1:n3,1:n3,function(x,y){n2 - pmax(x,y)}))
return(a)
}

thanks for listening!


Sincerely,
Erin H
mailto: [EMAIL PROTECTED]

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] [R/S] strange

2004-09-01 Thread Peter Dalgaard
Erin Hodgess [EMAIL PROTECTED] writes:

 Dear R and S People:
 
 I have run across something very strange.  Here is a function that I wrote
 for R:
 
 boot1 - function(y,method=f,p=1) {
   n1 - length(y)
   n2 - n1*p
   n3 - n2 - 1
   a - 0.5*(outer(1:n3,1:n3,function(x,y){n2 - pmax(x,y)}))
   return(a)
 }
[and it doesn't work in S]

Well, if you didn't understand what lexical scoping was about before,
now is your chance... 

n2 is in the lexical scope of the (unnamed) function that is getting
passed to outer(), because the function is defined in (the environment
of) the call to boot1. Therefor the value of n2 is available inside
that function.

S doesn't have lexical scoping. Instead it has frame 0 and frame
1, which can be used as scratchpads for routines to scribble pieces
of information on. So a not-quite-so-dirty variant of your solution is
to assign n2 to frame 1, rather than as permanent data. Not the only
solution, obviously.

-- 
   O__   Peter Dalgaard Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html