[R] Arguments in functions

Phil Spector spector at stat.berkeley.edu
Thu Dec 16 21:50:33 CET 2010

Paul -
    Please take a look at the help file for boot, i.e. type


    at the R prompt.  There you will see, along with other

statistic:  A function which when applied to data returns a vector
           containing the statistic(s) of interest.  When
           ‘sim="parametric"’, the first argument to ‘statistic’ must be
           the data.  For each replicate a simulated dataset returned by
           ‘ran.gen’ will be passed.  In all other cases ‘statistic’
           must take at least two arguments.  The first argument passed
           will always be the original data. The second will be a vector
           of indices, frequencies or weights which define the bootstrap

Since the default for sim is "ordinary", the boot function will automatically 
pass the entire data set as the first argument, and the indices of the bootstrap
sample as the second argument.

Hope this helps.
 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu

On Thu, 16 Dec 2010, Prew, Paul wrote:

> Hello,  I'm not much of a programmer, and am trying to understand the workings of the function below called "RStatFctn" within this bootstrap procedure.
> RStatFctn is defined to have two arguments: "x", intended to be a data vector; and "d" intended to be an index (or so it looks to me).
> Later, "rnormdat" is created to be the data vector.   However, when RStatFctn is called within the bootstrap function, I don't see where "rnormdat" is explicitly passed to RStatFctn as the data vector "x".  And I don't see where any values are ever passed to the index variable "d", or where any index is ever referenced or used in conjunction with RStatFctn.
> Any help you can offer is appreciated.
> Thanks, Paul
> library(boot)
> RStatFctn <- function(x,d) {return(mean(x[d]))}
> b.basic = matrix(data=NA, nrow=1000, ncol=2) b.normal = matrix(data=NA, nrow=1000, ncol=2) b.percent =matrix(data=NA, nrow=1000, ncol=2) b.bca =matrix(data=NA, nrow=1000, ncol=2)
> for(i in 1:1000){
> rnormdat = rnorm(30,0,1)
> b <- boot(rnormdat, RStatFctn, R = 1000) b.ci=boot.ci(b, conf =0.95,type=c("basic","norm","perc","bca"))
> b.basic[i,] = b.ci$basic[,4:5]
> b.normal[i,] = b.ci$normal[,2:3]
> b.percent[i,] = b.ci$percent[,4:5]
> b.bca[i,] = b.ci$bca[,4:5]
> }
> Paul Prew   ▪  Statistician
> 651-795-5942   ▪   fax 651-204-7504
> Ecolab Research Center   ▪  Mail Stop ESC-F4412-A
> 655 Lone Oak Drive   ▪   Eagan, MN 55121-1560
> ______________________________________________
> 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.

More information about the R-help mailing list