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.
This exercise is part of the course
Parallel Programming in R
Exercise instructions
- Supply a function to the error argument. This function should take one argument,
e
, and usepaste()
to put the string"Error!"
beforee
. - Edit
fit_lm()
to check ifmodel
is a character string containing the error message. - Make a cluster with six cores.
- Apply
fit_lm()
to the listls_df
in parallel usingparLapply()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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!"))