Update iteration.qmd (#1071)
Provided another way of getting R squared by using `nest()` from tidyr, which makes the code to be tidyverse consistent.
This commit is contained in:
parent
5ac3dac6bd
commit
f3db882d9d
|
@ -612,6 +612,18 @@ models |>
|
|||
map_dbl("r.squared")
|
||||
```
|
||||
|
||||
Another way to obtain R squared is by using the broom package. Instead of using `split()` from base R, you can use `nest()` from tidyr:
|
||||
|
||||
```{r}
|
||||
mtcars |>
|
||||
nest(data = -cyl) |>
|
||||
arrange(cyl) |>
|
||||
mutate(mod = map(data, ~lm(mpg ~ wt, data = .)),
|
||||
glanced = map(mod, broom::glance)) |>
|
||||
unnest(glanced) %>%
|
||||
pull(r.squared)
|
||||
```
|
||||
|
||||
You can also use an integer to select elements by position:
|
||||
|
||||
```{r}
|
||||
|
@ -705,7 +717,7 @@ str(safe_log("a"))
|
|||
When the function succeeds, the `result` element contains the result and the `error` element is `NULL`.
|
||||
When the function fails, the `result` element is `NULL` and the `error` element contains an error object.
|
||||
|
||||
`safely()` is designed to work with map:
|
||||
`safely()` is designed to work with `map()`:
|
||||
|
||||
```{r}
|
||||
x <- list(1, 10, "a")
|
||||
|
|
Loading…
Reference in New Issue