Debugging correlations
You work for the US Department of Health. Your team is investigating whether having multiple pregnancies (twins, triplets, etc.) is correlated with more weight gain during pregnancy. You calculated the correlation but your values differ significantly from the estimates of another analyst. She has pointed out that your data does not record values for weight gain greater than 99 pounds. You have decided to run the calculation again, and this time you want to log the maximum value for weight gained.
In your workspace, you have a list of data frames, ls_df. Each element of ls_df contains birth data for each state. You have written a foreach loop to run the calculation in parallel.
foreach and doParallel have been loaded for you.
Este ejercicio forma parte del curso
Parallel Programming in R
Instrucciones del ejercicio
- Create a cluster of four cores.
- Specify a file called
state_log.txtwhere messages from the cluster will be logged. - Within the loop body, log the maximum value of column
weight_gain_poundsin data framedf. - Read
state_log.txtto check print messages.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# Create a cluster of four cores
cl <- ___(___,
# Specify a file to log print statements
___ = "___")
registerDoParallel(cl)
res <- foreach(df = ls_df) %dopar% {
print(paste(unique(df$state), ":",
# Calculate maximum of weight_gain_pounds in df
___(___)))
cor(df$plurality, df$weight_gain_pounds)
}
stopCluster(cl)
# Read log file to check print messages
read.delim("___", sep = ";")