Search code examples
rfunctiondefault

R function - default value not inerpreted by the body


My problem concerns the following R function:

    but <- function(x, p = 0.95, FUN = mean, B = 1e4, smooth=FALSE, sd_smooth =1/sqrt(length(x)), ...)
{
  n <- length(x)
  bootmat <- replicate(B, sample(x, n, replace = TRUE))
  bootstat <- apply(bootmat, 2, FUN, ...)
  summary(bootstat); hist(bootstat)
  if (smooth) bootstat <- bootstat +rnorm(B, 0, sd_smooth)
  lo <- (1 - p)/2
  hi <- 1 - lo
  quantile(bootstat, c(lo, hi))
}

when I run

but(1:10)

it returns confidence intervals for a mean, as expected. When I try:

but(1:10, median)

an error occurs:

Error in 1 - p : non-numeric argument to binary operator

It seems that the default p (0.95) is not passed to the function body. I would be grateful for any hint about what is going wrong in my reasoning.


Solution

  • As there are many default parameters, and some of them are ..., call them by name

    but(1:10, FUN = median)
     2.5% 97.5% 
        3     8 
    

    -output

    enter image description here