2

I am trying to determine how to pass additional parameters to the parameter statistic in the function boot() in R, using .... In reading ?boot, it says that "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 sample.... Any further arguments can be passed to statistic through the ... argument. Yet I'm not sure how this will actually look in practice.

Here's some example code -- statFun is a function which takes in some data and returns the mean. If extra parameters are passed through the ... command, it centers the data around those extra parameters and then returns the mean:

statFun <- function(funData, indices, ...)
{
  # Check to see if extra parameters
  extraPar <- list(...)
  if(extraPar[[1]])
  {
    result <- mean(funData[indices] - extraPar[[2]])
  }else
  {
    result <- mean(funData[indices])
  }

  # Return the value
  return(result)
}

# Testing statFun
testData <- 1:20 ; testIndices <- length(testData) 
statFun(testData, testIndices, FALSE) # Returns 10.5
statFun(testData, testIndices, TRUE, mean(testData)) # Returns 0

Now, if I try to apply this function within boot, I run into problems:

boot(testData, statFun, R = 100)

which gives me Error in extraPar[[1]] : subscript out of bounds. If I try passing FALSE into statFun, but still get the same error. I can understand how my scoping is not correct, but how would I overcome this? I've read this and this post to understand how to use ... a little better, but I seem to be missing something glaringly obvious...

This is my first SO post, so please let me know if my question doesn't follow the guidelines. Thanks for any help.

Community
  • 1
  • 1
El-
  • 168
  • 1
  • 10
  • I get results 20 and 9.5 (which I would expect). – Roman Luštrik Feb 13 '17 at 19:43
  • I think the help means you can pass multiple arguments via the `...`, but to actually do this , and to be passed on to boot , you should probably name them : so use `statFun <- function(funData, indices, center=FALSE, val = NULL){` , and change the rest of your function accordingly. You then call boot with `boot(testData, statFun, center=TRUE, val=mean(testData), R = 100)` – user20650 Feb 13 '17 at 19:46
  • 1
    in fact the help for the `...` argumnet says *"Other **named arguments** for statistic which are passed unchanged each time it is called*" (emphasis mine) – user20650 Feb 13 '17 at 19:49
  • Yeah -- I just started working on it and changed my `statFun` to include named parameters, rather than making them ambiguous through `...`, just like you mentioned [user20650](http://stackoverflow.com/users/2250190/user20650). Thanks for the help, got it figured out. – El- Feb 13 '17 at 20:01

1 Answers1

1

Thanks to user20650 and reading ?boot examples a little more carefully, I figured out what was needed -- just to make sure that my parameters within statFun are named and that I refer to these names in my call of boot. Simple stuff:

statFun <- function(funData, indices, addPars, centerMean)
    {
      # Check to see if extra parameters
      if(addPars)
      {
        result <- mean(funData[indices] - centerMean)
      }else
      {
        result <- mean(funData[indices])
      }

      # Return the value
      return(result)
    }

Now, when I apply this within boot and use the names addPars and centerMean, I get what I would expect:

boot(testData, statFun, R = 100, extraPar = TRUE, numCenter = mean(testData)) 
# Returns boot object of data centered around the mean.

Certainly understand ... more now.

Community
  • 1
  • 1
El-
  • 168
  • 1
  • 10