Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active October 20, 2021 05:48
Show Gist options
  • Select an option

  • Save brshallo/1a7e42e2f2d518ebaf688ba430fc8d3f to your computer and use it in GitHub Desktop.

Select an option

Save brshallo/1a7e42e2f2d518ebaf688ba430fc8d3f to your computer and use it in GitHub Desktop.
"off-label" use of {modeltime} funs to produce pred intervals on cross-sectional problem
library(tidyverse)
library(lubridate)
library(tidymodels)
library(modeltime)

set.seed(1234)
iris2 <- iris %>% 
  as_tibble() %>% 
  mutate(date = ymd(19800101) + days(1:n()))

split <- rsample::initial_split(iris2)
train <- rsample::training(split)
test <- rsample::testing(split)

earth_recipe <- 
  recipe(formula = Sepal.Length ~ ., data = train) %>% 
  update_role(date, new_role = "date") %>% 
  step_novel(all_nominal(), -all_outcomes()) %>% 
  step_dummy(all_nominal(), -all_outcomes()) %>% 
  step_zv(all_predictors()) 

earth_spec <- 
  mars() %>% 
  set_mode("regression") %>% 
  set_engine("earth") 

earth_workflow <- 
  workflow() %>% 
  add_recipe(earth_recipe) %>% 
  add_model(earth_spec) 

earth_workflow %>% 
  fit(data = train) %>% 
  modeltime::modeltime_table() %>% 
  modeltime::modeltime_calibrate(
    new_data = test
  ) %>% 
  modeltime::modeltime_forecast(
    new_data = test
  ) %>% 
  select(-.index)
#> # A tibble: 37 x 6
#>    .model_id .model_desc .key       .value .conf_lo .conf_hi
#>        <int> <chr>       <fct>       <dbl>    <dbl>    <dbl>
#>  1         1 EARTH       prediction   4.79     4.15     5.43
#>  2         1 EARTH       prediction   5.38     4.74     6.02
#>  3         1 EARTH       prediction   4.97     4.33     5.61
#>  4         1 EARTH       prediction   4.93     4.29     5.57
#>  5         1 EARTH       prediction   5.17     4.53     5.81
#>  6         1 EARTH       prediction   4.99     4.35     5.63
#>  7         1 EARTH       prediction   4.96     4.32     5.60
#>  8         1 EARTH       prediction   4.98     4.34     5.62
#>  9         1 EARTH       prediction   4.82     4.18     5.46
#> 10         1 EARTH       prediction   4.89     4.25     5.53
#> # ... with 27 more rows

Created on 2021-10-19 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment