LoslegenKostenlos loslegen

Catch modeling errors

Life expectancy at birth is a major indicator of population health.

You work as a consultant public health researcher. Your client would like to know the increase in life expectancy for each dollar spent on healthcare. You have sourced data for the life expectancy (in years) for all countries and their corresponding healthcare expenditure per capita, from 2001 to 2021. The consulting statistician has written code to fit regression models to each country's data. However, there are missing values in the data which cause errors in model fitting.

You have a list of data frames in your workspace, ls_df, where each element is the data for a given country. You plan to parallelize with parLapply() and use tryCatch() to catch errors. The parallel package has been loaded for you.

Diese Übung ist Teil des Kurses

Parallel Programming in R

Kurs anzeigen

Anleitung zur Übung

  • Supply a function to the error argument. This function should take one argument, e, and use paste() to put the string "Error!" before e.
  • Edit fit_lm() to check if model is a character string containing the error message.
  • Make a cluster with six cores.
  • Apply fit_lm() to the list ls_df in parallel using parLapply().

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

fit_lm <- function (df) {
  model <- tryCatch(lm(life_expectancy ~ health_exp_per_capita, data = df),
# Supply a function that pastes "Error!" and one argument, e
                    error = ___)                   
# If model is character (error) return model else return model coefficient
  if (___) return(model)
  else return(coef(model)[2])
}
# Make a cluster of six cores
cluster <- ___(___)
# Apply fit_lm in parallel to ls_df using cluster
ls_mod <- parLapply(___, ___, ___)
stopCluster(cluster)
                    
print(paste(sum(sapply(ls_mod, is.numeric)), "models fit and", sum(sapply(ls_mod, is.character)), "errors caught!"))
Code bearbeiten und ausführen