0

I'm working on an assignment with the following prompt:

1b) Create a function which will take a vector of numeric values, and an integer n as arguments. The function will perform nonparametric bootstrapping on this data, n times, calculating the most-probable value of the data set. The function should return the output of the boot command.

This is the function I have written:

non.p.bootstrap <- function(vector, n) {
    mode.stat <- function(my.data,i) return(my.mode(my.data[i]))
    b <- boot(data = vector, statistic = mode.stat, R = n)
    return(b)
}

The function my.mode is one I have written which outputs the mode of a vector of continuous data.

Edit: Here is the my mode function I created as well

my.mode <- function(vector) {
    hist <-hist(vector, breaks = length(vector), plot = FALSE)
    densest <- max(hist$density)
    mode <- hist$mid[hist$density == densest]
    return(mode)
}

However, when I try to run my bootstrapping function on a vector of 1000 data points, I get an error message.

  • Do you get the same error on a nice small example of, say, 10 values? What does `my.mode` look like? If you provide the definition, we can try to run your code, but without it we cannot. What is the point of `i` in `mode.stat`? It seems like you don't use it when you call `boot`. – Gregor Thomas Nov 10 '22 at 03:40
  • I guess I"m confused about the point of `mode.stat` at all. Looking at the `boot::boot` documentation, it seems like `boot(vector, my.mode, R = n)` should be sufficient. (It is the `boot` package you're using, right?) – Gregor Thomas Nov 10 '22 at 03:45
  • Thanks for the help, I'll edit the question to include my.mode stat. From what I understand (what I was taught in class) the statistic argument of the boot function (from the boot packages) should take 2 arguments, the original data and a vector of indices – Micaela Wiseman Nov 10 '22 at 13:30

0 Answers0