My goal is to show multiple plots of residual vs. fitted values for a dataframe of 13 stations each having a separate lm() model. Working from an example in Chapter 25 (Many Models) in the R for Data Science (1st ed) textbook.
When running this line of R code (from Section 25.2.2 in the textbook)
by_stn <- by_stn %>% mutate(model = map(data, allstn_models))
...I receive the following error:
`Error in `mutate()`:ℹ In argument: `model = map(data, allstn_models)`.ℹ In group 1: `station = s04029000`.Caused by error in `map()`:ℹ In index: 1.Caused by error in `pluck_raw()`:! Index 1 must have length 1, not 13.Run `rlang::last_trace()` to see where the error occurred.`
The structure of by_stn
is:
# A tibble: 13 × 2# Groups: station [13] station data <fct> <list> 1 s04029000 <tibble [110 × 2]> 2 s04029990 <tibble [110 × 2]> 3 s04030000 <tibble [110 × 2]> 4 s04062011 <tibble [110 × 2]> 5 s04063000 <tibble [110 × 2]> 6 s04063700 <tibble [110 × 2]> 7 s04064000 <tibble [110 × 2]> 8 s04064500 <tibble [110 × 2]> 9 s04065106 <tibble [110 × 2]>10 s04066000 <tibble [110 × 2]>11 s04066003 <tibble [110 × 2]>12 s04066500 <tibble [110 × 2]>13 s04067500 <tibble [110 × 2]>
The object allstn_models
, a list of 13, is a set of lm() models for 13 different stations.
I went back to the example in the textbook and used the gapminder dataset and followed through with the example code. It worked without issue.
So I then thought to convert my original dataframe to a tibble. But that did not solve the problem.
How can I correct my error?