[R] Help with functions

From: Pryseley Assam <assampryseley_at_yahoo.com>
Date: Thu 02 Feb 2006 - 03:52:58 EST


Dear R-users    

  I intend to create a function which calls some smaller other functions in return. Some of these smaller functions all call some functions. I do not know a good way to do this. I tried using the source() function to include the smaller functions within the main functions before they are called. This does not work, or maybe i am not doing the right thing.   For example:    

  the following quantities are computed in the main function (only a part of the main function is shown below)    

    ###############################################################
  # quantities for bootstrap confidence intervals
###############################################################
  r.value<- cor(x,y)
  npb.B <- mean(r)-r.value
  npb.mean <- mean(r)
  npb.var <- var(r)

  r.star <- sort(r)
  z.star <- (r.star - mean(r.star))/sqrt(var(r.star))    

  after which the main function calls the function (i.s.conf.int) shown below, which is saved in a different script file    

###################################################
    # Confidence Interval
###################################################
  i.s.conf.int <- function(type) {
  switch(type,

  a = nci(),
  b = inci(),
  c = bbi(),
  d = pi(),
  e = si())

  }    

  Also, this 'sub' function (i.s.conf.int) may call one of 5 other smaller functions saved in yet another file, or the same file as the function "i.s.conf.int".    

  However, these 5 functions need the quantities calculated in the main function. One of these functions is shown below:    

###############################################################
  # nonparametric bootstrap normal confidence interval
###############################################################
  nci <-function () {

  npb.n.L <- r.value - 1.96*sqrt(npb.var)
  npb.n.U <- r.value + 1.96*sqrt(npb.var)
  npb.n <- array(data = c( npb.n.L, npb.n.U), 
  dim = c(1,2),
  dimnames = list("Estimates" ,c("LowerBound","UpperBound")))   

  npb.n.CI <- list( Type = "Normal Confidence Interval",   ConfidenceInterval= npb.n)
  npb.n.CI
  }    

  I use the source() function to include these "secondary/sub" functions, just before they are called the main function. I get an Error message when any of the 5 small functions is executing : that it can not find the quantities calculated in the main function.    

  So my question boils down to how can i call functions from different files in the main function, so that the recognise quantities already calculated in the main function or passed as arguements to the main function ?    

  Thanks
  Pryseley.                     



Bring words and photos together (easily) with

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Feb 02 04:02:58 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:16 EST