IniziaInizia gratis

The bootstrap error

You work as an R programmer for a clinical laboratory. The statistician at the lab is conducting a large scale analysis to establish ranges for the average weights of different species of mice. She has written a a function, bootstrap(), that is applied to every element of weight_list in parallel using a futures backend. However the code throws the following error:

Error in checkForRemoteErrors(val) : 
  one node produced an error: missing values and NaN's not allowed if 'na.rm' is FALSE

She cannot find the element of weight_list which causes the error. She also loses all results of the time-consuming calculation. You have been asked to fix the code. The furrr package has been loaded for you and the multisession for the futures has been planned.

Questo esercizio fa parte del corso

Parallel Programming in R

Visualizza il corso

Istruzioni dell'esercizio

  • Supply the code in the curly braces to a function that can catch the error.
  • To the error argument, supply a function that takes one argument, e, and returns the string "Error here!".
  • Map bootstrap() to all elements of the weight_list using a furrr function.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

bootstrap <- function (x) {
  # Supply code to a function for catching errors
       ___({est <- rep(0, 1e3)
            for (i in 1:1e3) {
              b <- sample(x, replace = T)
              est[i] <- mean(b)
              }
    return(quantile(est, c(0.25, 0.75)))
     # Supply a function that returns a string
  }, error = ___
 )
}
# Map bootstrap() to every element of weight_list
___(___, ___)
plan(sequential)
Modifica ed esegui il codice