[R]: global and local variables

2003-12-09 Thread allan clark

   Hi all

   I have a problem pertaining to local and global variables.

   Say I have a function defined as follows:

   a-function(x)
   {yx^2}

   i.e
   a(2)
   [1] 4

   function b is now defined to take the value of y and do some
   manipulation with it. As it stands I dont know how to store the
   variable y such that other functions are able to reference its value.

   I know that I can simply put the operations found in b simply into a
   but this is not what I want.

   I would like to have stand alone functions say

   a, b and c which could be run independently as well as have a function
   called say

   control that can run a, b and c.

   i.e.

   control- function( x)
   {
   a(x)
   b(x)
   c(x)
   }

   I hope that you guys understand what I'm trying to do.

   Cheers
   Allan
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R]: global and local variables

2003-12-09 Thread Uwe Ligges
allan clark wrote:

   Hi all

   I have a problem pertaining to local and global variables.

   Say I have a function defined as follows:

   a-function(x)
   {yx^2}
   i.e
   a(2)
   [1] 4
The function a specified above won't return 4!



   function b is now defined to take the value of y and do some
   manipulation with it. As it stands I dont know how to store the
   variable y such that other functions are able to reference its value.
   I know that I can simply put the operations found in b simply into a
   but this is not what I want.
   I would like to have stand alone functions say

   a, b and c which could be run independently as well as have a function
   called say
   control that can run a, b and c.

   i.e.

   control- function( x)
   {
   a(x)
   b(x)
   c(x)
   }
   I hope that you guys understand what I'm trying to do.
You are trying to read An Introduction to R???
If not, please try!
What you are really going to do: using assigments and return() 
statements as in:

a - function(x) return(x^2)

foo - function(x) {
  y - a(x)
  z - b(x)
  return(list(y=y, z=z))
}
foo(.)

Uwe Ligges


   Cheers
   Allan
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


[R]: global and local variables

2003-12-09 Thread allan clark

   Hi

   Thanx for those who responded to my problem. In my previous email I
   tried to ask a general question and probably never explained myself
   correctly.  I wanted to prevent sending this long email. My apologies.

   This is my actual problem.

   I have a regression problem. I am writing some R code in order to
   calculate some collinearity diagnostics. The diagnostics all rely on a
   function named preprocess. I've written the different diagnostics as
   separate functions so that they may be evaluated separately if
   required.

   The two functions are named mci and vif. (I will be writing some
   others later)

   mci calculates the mixed condition index as well as the condition
   indices of a given X matrix while
   vif calculates the variance inflation factors of the X matrix.

   Another function named colldiag has been written. This function will
   calculate all of the collinearity diagnostics by simply calling the
   separate functions defined previously.

   I've attached the code of the different functions as well as a data
   file (say a2) below.

   The functions mci and vif work perfectly.

   i.e.

mci(a2)
   [1] DATA MATRIX CENTERED AND SCALED
   [1] CENTERED AND SCALED MATRIX = $data
   [1] MEANS OF XDATA = $means
   [1] STDS OF XDATA = $stds
   [1] THE CONDITION NUMBER AND THE CONDITION INDICES
   $CN
   [1] 27.34412

   $CI
   [1]  1.00  1.615690 27.344123

   $MCI
 Principal.Component Singular.Values Condition.Index
   1   1   1.47206801.00
   2   2   0.91110781.615690
   3   3   0.0538349   27.344123

vif(a2)
   [1] DATA MATRIX CENTERED AND SCALED
   [1] CENTERED AND SCALED MATRIX = $data
   [1] MEANS OF XDATA = $means
   [1] STDS OF XDATA = $stds
   [1] THE VARIANCE INFLATION FACTORS
   $vif
 x1   x2   x3
   169.3542 175.6667   1.6875

   The output from colldiag is as follows:

colldiag(a2)
   [1] DATA MATRIX CENTERED AND SCALED
   [1] CENTERED AND SCALED MATRIX = $data
   [1] MEANS OF XDATA = $means
   [1] STDS OF XDATA = $stds
   [1] THE CONDITION NUMBER AND THE CONDITION INDICES
   $CN
   [1] 27.34412

   $CI
   [1]  1.00  1.615690 27.344123

   $MCI
 Principal.Component Singular.Values Condition.Index
   1   1   1.47206801.00
   2   2   0.91110781.615690
   3   3   0.0538349   27.344123

   [1] DATA MATRIX CENTERED AND SCALED
   [1] CENTERED AND SCALED MATRIX = $data
   [1] MEANS OF XDATA = $means
   [1] STDS OF XDATA = $stds
   [1] THE VARIANCE INFLATION FACTORS
   $vif
 x1   x2   x3
   169.3542 175.6667   1.6875




   Once you check the colldiag code below you will see that it calls mci
   and vif. In both of these functions they call preprocess. This is
   unnecessary. How can I write the code such that R only calls
   preprocess once?

   ONCE AGAIN I APOLOGIZE FOR THE LENGTH OF THIS EMAIL!!!


   Cheers
   Allan





   The data file:

  x1 x2 x3
   1  20 -4  5
   2  21 -4  4
   3  22 -3  3
   4  23 -2  2
   5  24 -1  1
   6  25  0  2
   7  26  1  3
   8  27  2  4
   9  28  3  5
   10 29  4  6
   11 20 -4  5
   12 21 -4  4
   13 22 -3  3
   14 23 -2  2
   15 24 -1  1
   16 25  0  2
   17 26  1  3
   18 27  2  4
   19 28  3  5
   20 29  4  6

   preprocess-function (xdata,center=1,scale=1)
   {
   if(center==1  scale==1)
   {
   means-apply(xdata,2,mean)
   stds-apply(xdata,2, function(x) sqrt(var(x)))
   scalefactor-((nrow(xdata)-1)^.5)*stds
   data.centsca-sweep(sweep(xdata,2,means,-),2,scalefactor,/)
   print(DATA MATRIX CENTERED AND SCALED)
   print(CENTERED AND SCALED MATRIX = $data)
   print(MEANS OF XDATA = $means)
   print(STDS OF XDATA = $stds)
   list(data=data.centsca,means=means,stds=stds,prep=1)
   }

   else if(center==1  scale==0)
   {
   means-apply(xdata,2,mean)
   data.cen-sweep(xdata,2,means,-)
   print(DATA MATRIX CENTERED)
   list(data=data.cen,means=means,prep=1)
   }

   else if(center==0  scale==1)
   {
   stds-apply(xdata,2, function(x) sqrt(var(x)))
   scalefactor-((nrow(xdata)-1)^.5)*stds
   data.sca-sweep(xdata,2,scalefactor,/)
   print(DATA MATRIX SCALED)
   list(data=data.sca,stds=stds,prep=1)
   }

   else
   {
   print(YOU HAVE TO SPECIFY WHETHER YOU WANT TO SCALE OR CENTER THE
   MATRIX)
   print(THE preprocess FUNCTION HAS THREE ARGUMENTS. i.e.
   preprocess(xdata,center,scale))
   print(xdata IS THE MATRIX TO BE TRANSFORMED)
   print(TO CENTER SPECIFY center=1)
   print(TO SCALE SPECIFY scale=1)
   }

   # A matrix is standardised as follows:
   # X*(i,j) = ( X(i,j)- XBAR(j) ) / (   sqrt(n-1)* STD(j)   )

   }

   mci-function (xdata)
   {
   a-preprocess(xdata)
   b-svd(a$data)
   cn-(b$d)[1]/(b$d)[ncol(a$data)]
   ci-(b$d)[1]/(b$d)[1:ncol(a$data)]

   #paste(THE CONDITION NUMBER = ,cn)

   #the following produces a table in order to output the mci values
   

Re: [R]: global and local variables

2003-12-09 Thread Karl Knoblick
Hi!

I'm no guru in R. But I can think of 2 ways (have to
be tried):

1) As Uwe Ligges said: just save return the stored
variable a-preprocess(xdata) and return it (if you
want to return more than 1 item use list) and give
this variable to the next function. Example:

func1-function(x) 
{
y-x^2
# for testing:
print(paste(func1, x, y))
return(y) 
}

func2-function(x, valuefunc1=NA)
{
if (is.na(valuefunc1)) valuefunc1-func1(x)
# calculate things with valuefunc1
print(paste(func2, x, valuefunc1))
return(list(valuefunc1=valuefunc1))
}

func3-function(x, valuefunc1=NA)
{
if (is.na(valuefunc1)) valuefunc1-func1(x)
# calculate things with valuefunc1
print(paste(func3, x, valuefunc1))
return(list(valuefunc1=valuefunc1))
}

# use this
a-list(NA)
names(a)-c(valuefunc1)

a-func2(x=3, valuefunc1=a$valuefunc1)
a-func3(x=3, valuefunc1=a$valuefunc1) # be careful,
makes only sense if the x is equal to former fuction
call...
a-func3(x=999, valuefunc1=a$valuefunc1) # will be the
same
a-func3(x=999) # will be different 


2) use global variables like
assign(stored.value, x, envir=.GlobalEnv)
example see reply of my posting:
RE: [R] LOCF - Last Observation Carried Forward Simon
Fear (Sat 15 Nov 2003 - 03:28:03 EST) 

HTH,
Karl

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help