26 Exercises for ‘Mapping over functions’ chapter ✎ Rough draft
These exercises accompany the mapping chapter.
You can complete these exercises in your local version of the .qmd file. Either download a copy of the whole book from github (see introduction), or download this .qmd using the download button on the top right of the page.
Write code for each of the following. Remember that you can write pseudocode first if it helps.
26.1 Warm-up (brief)
26.1.1 Repeat a simulation with map()
Use the generate_data() and analyse_data() helper functions defined above.
Create iterations <- 1:20, then use map() to run the same simulation 20 times with:
n_per_condition = 50mean_control = 0mean_intervention = 0.5sd = 1
Inspect the output. What object type did map() return?
26.1.2 Return a numeric vector with map_dbl()
Repeat the previous exercise using map_dbl() to extract only the p values into a numeric vector. Then calculate the proportion of p-values less than 0.05.
26.2 Parameter grids + pmap() (our main focus)
26.2.1 Create a parameter grid with expand_grid()
Use expand_grid() to create a parameter grid where:
iteration = 1:200n_per_condition = c(20, 60)mean_intervention = c(0, 0.4, 0.8)mean_control = 0sd = c(0.5, 1)
After creating the grid:
- Check the total number of rows.
- Use
distinct()to verify the unique combinations of design parameters (excludingiteration).
26.2.2 Generate data row-wise with pmap()
Using your parameter grid, create a new object where each row has a list-column called generated_data, produced with pmap() and generate_data().
26.2.3 Analyse each generated dataset and unnest
Starting from your object with the generated_data list-column:
- Use
map()to applyanalyse_data()to each generated dataset. - Store the output in a
resultslist-column. - Use
unnest()to create one tidy simulation-results tibble.
26.3 Write reusable wrappers
26.3.1 Write a simulation wrapper using expand_grid() + pmap()
Write a function called run_simulation_pmap() with arguments:
n_iterationsn_per_conditionmean_interventionmean_control = 0sd = 1keep_data = FALSE
Function requirements:
- Build a parameter grid with
expand_grid(). - Generate data using
pmap(). - Analyse data using
map()+analyse_data(). - Return an unnested tibble.
- If
keep_data = FALSE, drop thegenerated_datalist-column before returning.
26.4 A whole new simulation, from scratch
26.4.1 Build a new simulation with binary outcomes using expand_grid() + pmap()
In this final exercise, write a full simulation workflow for a different data-generating process (binary outcomes instead of continuous scores), covering steps 1 through 4 of a simulation:
- Design the experiment.
- Write a
generate_data()function. - Write an
analyse_data()function. - Run the workflow many times using mapping.
Use the following specification:
Build a parameter grid called
experiment_parameters_binaryusingexpand_grid()withiteration = 1:300,n_per_condition = c(40, 120),prob_control = c(0.20, 0.35), andrisk_difference = c(0, 0.10).Add
prob_interventionwithmutate(prob_intervention = prob_control + risk_difference).Write
generate_data_binary(n_per_condition, prob_control, prob_intervention). The function should return a tibble with columnscondition("control"/"intervention") andoutcome(0/1, generated withrbinom()).Write
analyse_data_binary(data)that returns a one-row tibble with columnsp(fromprop.test()comparing intervention vs control) andrisk_difference_observed(observed mean outcome in intervention minus control).Run the simulation by using
pmap()to generate a data for each row ofexperiment_parameters_binary(making sure the generated data are stored in the output), thenmap()to applyanalyse_data_binary(), thenunnest()into one tibble.Do some basic checks: confirm the number of output rows equals the number of rows in the parameter grid, and verify all
pvalues are between 0 and 1.
26.5 Optional extension: parallel mapping with {furrr}
26.5.1 Use future_pmap() and future_map()
Rewrite your pipeline using future_pmap() and future_map(). Use plan(multisession) and furrr_options(seed = TRUE) to keep results reproducible.