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.
Diese Übung ist Teil des Kurses
Parallel Programming in R
Anleitung zur Übung
- Create a cluster of four cores.
- Specify a file called
state_log.txt
where messages from the cluster will be logged. - Within the loop body, log the maximum value of column
weight_gain_pounds
in data framedf
. - Read
state_log.txt
to check print messages.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# 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 = ";")