LoslegenKostenlos loslegen

Modeling on nested data frames

You'll be working on the US Army ANSUR II body measurement dataset, which has been pre-loaded as ansur_df. The goal is to nest the data for both sexes so that you can simultaneously train two linear models, one for each sex. These models will derive a person's weight from their stature (height) and waist circumference. You'll then unnest the data to inspect the model's statistics produced by the glance() function from the broom package.

The dplyr, broom, and purrr packages have been pre-loaded for you.

Side note: In the provided code, the purrr package's map() function applies functions on each nested data frame. Check out this package if you like using functions inside pipes!

Diese Übung ist Teil des Kurses

Reshaping Data with tidyr

Kurs anzeigen

Anleitung zur Übung

  • Group the data by sex.
  • Nest the data.
  • Unnest the glanced column.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

ansur_df %>%
  # Group the data by sex
  ___ %>% 
  # Nest the data
  ___ %>% 
  mutate(
    fit = map(data, function(df) lm(weight_kg ~ waist_circum_m + stature_m, data = df)),
    glanced = map(fit, glance)
  ) %>% 
  # Unnest the glanced column
  ___
Code bearbeiten und ausführen