TidyX Episode 183: Within-group regression using {purrr}

Sometimes you have a number of groups/strata in your data and you want to fit the same model structure to each of them independently. While you could write a for() loop to handle this, the issue becomes more complicated if your data all of a sudden has new groups/or strata (for example, maybe this model is working inside of an on going pipeline, where data is continuously being collected and modeled). Additionally, for() loops can get cumbersome, lead to a lot of copying and pasting, and potentially introducing errors in code (which can sometimes be hard to identify and debug). One way to circumvent these issues is to use the {purrr} package’s suite of map() functions. By splitting the data on the groups/strata one can apply the linear model function (or any model function, for that matter) to each of the groups and quickly obtain results that can be used down stream. This week, Ellis Hughes and I show you how to accomplish this task!

To watch our screen cast, CLICK HERE.

To access our code, CLICK HERE.