Appendix C — Extra exercises

C.1 Creating automatic analysis pipelines

C.1.1 Exercise: Update function to also calculate median and IQR

Time: ~8 minutes.

Let’s make a change to our function and test out how the tar_outdated() and tar_visnetwork() work.

  1. Open up the R/functions.R file.
  2. Add median and interquartile range (IQR) to the summarise() function, by adding it to the end of list(mean = mean, sd = sd), after the second sd. Note, IQR should look like iqr = IQR since we want the output columns to have a lowercase for the column names.
  3. Run tar_outdated() and tar_visnetwork() in the Console (or by using the Command Palette Ctrl-Shift-P, then “targets outdated” or “targets visual”). What does it show?
  4. Style using the Palette (Ctrl-Shift-P, then type “style file”). You might need to force a reformat if the code is too long by highlighting the line and using Ctrl-Shift-P, then “reformat”.
  5. Run tar_make() in the Console or with the Palette (Ctrl-Shift-P, then type “targets run”). Re-check for outdated targets and visualize the network again.
  6. Open up the Git interface and commit the changes to the Git history with Ctrl-Alt-M or with the Palette (Ctrl-Shift-P, then type “commit”).
Click for a potential solution. Only click if you are struggling or are out of time.
#' Calculate descriptive statistics of each metabolite.
#'
#' @param data Lipidomics dataset.
#'
#' @return A data.frame/tibble.
#'
descriptive_stats <- function(data) {
  data |>
    dplyr::group_by(metabolite) |>
    dplyr::summarise(dplyr::across(value, list(
      mean = mean,
      sd = sd,
      median = median,
      iqr = IQR
    ))) |>
    dplyr::mutate(dplyr::across(tidyselect::where(is.numeric), ~round(.x, digits = 1)))
}

C.2 A general approach to doing statistical analyses

C.2.1 Exercise: How would you define a linear regression with parsnip?

Time: ~10 minutes.

Using parsnip’s “Examples” vignette as well as the code we wrote for the logistic regression above as a template, write parsnip code that would define a simple (an engine of"lm") linear regression model. Begin by making a new Markdown header and code chunk at the bottom of the doc/learning.qmd file, like listed below:

## Exercises
### Linear regression model definition

```{r}

```

After writing the code, style the file using the Palette (Ctrl-Shift-P, then type “style file”). We will eventually delete these exercise text in the Quarto file, but for now, commit the changes to the Git history with Ctrl-Alt-M or with the Palette (Ctrl-Shift-P, then type “commit”).

Click for the solution. Only click if you are struggling or are out of time.
linear_reg_specs <- linear_reg() |>
  set_engine("lm")