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.