0

I am getting an error "Error in statistic(data, original, ...) : unused argument (original)" when trying to execute the code below. Specifically, I am trying to apply a function and bootstrap those results for each plot#. Am I missing something straightforward here?

    library(codyn) # has sample dataset called pplots
    library(boot)

    str(pplots)

    stability <- function(x){
  mean(multivariate_change(x,
                           species.var = "species",
                           time.var = "year",
                           abundance.var = "relative_cover",
                           replicate.var = "plot")$composition_change)
}

boot_obj <- lapply(splitspplots,boot,statistic = stability,R = 20)
boot_obj <- lapply(splitspplots,myBootFun)

myBootFun <- function(x,i) {
  lapply(x[i],boot, statistic=stability, R = 10)
}
myBootFun <- function(x,i) {
  boot(x[i], statistic=stability, R = 10)
}
splitspplots <- split(pplots,pplots$plot)
lapply(splitspplots, print(myBootFun))
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
ecology
  • 606
  • 3
  • 9
  • 29
  • 1
    I don't know package codyn, but if you are using package boot (maybe indirectly) then you need to study the description of the `statistic` parameter in `help("boot")`. – Roland Mar 13 '20 at 12:36
  • 2
    Thanks! ended up gettting the code to run but it doesn't generate bias or std error stats. any ideas? – ecology Mar 13 '20 at 12:59
  • 1
    Bias and standard error are calculated by the `print` method. Either call `print` or calculate them manually: https://stackoverflow.com/a/19963673/1412059 – Roland Mar 13 '20 at 13:12
  • 1
    Also, if you don't use `i` inside the `stability` function, you are not doing bootstrapping. – Roland Mar 13 '20 at 13:13
  • 2
    Is that i instead of the x inside the function? That does not work for me. – ecology Mar 13 '20 at 13:27
  • 1
    Please stop a moment and *think* about how bootstrap works. The `boot` function creates a random sample of indices and you need to use these indices for subsetting your data. Compare with the example in the linked question above. – Roland Mar 13 '20 at 13:33
  • 2
    I see that in the stability function I use then x[i], however I am unsure how the boot works. Does the 'mybootfunction' have to by lapply'd? – ecology Mar 13 '20 at 13:47

1 Answers1

0

I figured a different way to do this that works as well.

I used a for loop to loop across different groups/sites, but I do not show that here. Here is the bootstrap code.

data_sampled <- data_grouped %>%
    group_by(SITE_ID) %>%
    crossing(id = seq(100)) %>% # create replicate dataframes
    sample_n(size = n(), replace = T) # sample dataframes
ecology
  • 606
  • 3
  • 9
  • 29