0

While using rsample, tidymodels, I create the resamples with the following code (see below). Though I don't want to assess the model with all horizon positions, in the presented case from 1 to 5. Let's say I want to assess the model only for 4 and 5 horizon positions. My real life problem is somewhat like this. So I don't want my model to be tuned with errors for all horizon positions.

So my option was to use rsample::rolling_origin() and then subset the assessment index. I can do it with a for loop... but I got curious about using another way. I would like to do it with purrr. I know I should probably move one since it is working like it is... but it's killing me.

How do I refer to a third inner level, using purrr's functions?

require(tidyverse)
require(tidymodels)

data(cars)

roll_rs <-rolling_origin(
data=cars,
initial = floor(0.80*(nrow(cars))),
assess = 5,
skip = 0,
cumulative = TRUE )

new_roll_rs<-roll_rs

for (i in 1:13){
new_roll_rs[[1]][[i]][["out_id"]]<-roll_rs[[1]][[i]][["out_id"]][4:5]
}

Thanks

oprick
  • 21
  • 4

1 Answers1

0

When I want to get at something that's a few levels deep, I often map() more than once:

library(tidymodels)
#> ── Attaching packages ──────────────────────────────────────────────── tidymodels 0.1.1 ──
#> ✓ broom     0.7.0      ✓ recipes   0.1.13
#> ✓ dials     0.0.8      ✓ rsample   0.0.7 
#> ✓ dplyr     1.0.0      ✓ tibble    3.0.3 
#> ✓ ggplot2   3.3.2      ✓ tidyr     1.1.0 
#> ✓ infer     0.5.3      ✓ tune      0.1.1 
#> ✓ modeldata 0.0.2      ✓ workflows 0.1.2 
#> ✓ parsnip   0.1.2      ✓ yardstick 0.0.7 
#> ✓ purrr     0.3.4
#> ── Conflicts ─────────────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter()  masks stats::filter()
#> x dplyr::lag()     masks stats::lag()
#> x recipes::step()  masks stats::step()

data(cars)

roll_rs <- rolling_origin(
  data = cars,
  initial = floor(0.80*(nrow(cars))),
  assess = 5,
  skip = 0,
  cumulative = TRUE
)

roll_rs$splits %>% 
  map("out_id") %>% 
  map(magrittr::extract, c(4, 5))
#> [[1]]
#> [1] 44 45
#> 
#> [[2]]
#> [1] 45 46
#> 
#> [[3]]
#> [1] 46 47
#> 
#> [[4]]
#> [1] 47 48
#> 
#> [[5]]
#> [1] 48 49
#> 
#> [[6]]
#> [1] 49 50

Created on 2020-07-21 by the reprex package (v0.3.0.9001)

The first map() pulls out the "out_id", and then the second map() will extract() the fourth and fifth elements of those IDs.

Julia Silge
  • 10,848
  • 2
  • 40
  • 48