# traditional `contr.sum` does not name levels, so use function from `car` package
library(car)
library(tidyverse)
param <- getOption("contrasts")
go_deviance <- param
go_deviance["unordered"] <- "contr.Sum"
options(contrasts = go_deviance)library(tidyverse)
library(recipes)
tib_a <- tibble(x = c(rep("a", 9), "b")) %>%
mutate(rand = rnorm(n()))
tib_b <- tibble(x = c(rep("a", 90), "b")) %>%
mutate(rand = rnorm(n()))library(dplyr)
library(tibble)
library(recipes)
library(car)
param <- getOption("contrasts")
go_deviance <- param
# traditional `contr.sum` does not name levels, so use function from `car` package
go_deviance["unordered"] <- "contr.Sum"library(tidyverse)
library(broom)
library(car)
param <- getOption("contrasts")
go_deviance <- param
# traditional `contr.sum` does not name levels, so use function from `car` package
go_deviance["unordered"] <- "contr.Sum"library(recipes)
library(tidyverse)
diamonds_nested <- diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(recipes = map(data, ~recipe(price ~ clarity + color + carat, data = .x)))
diamonds_nested %>%
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| # this is difficult to read, but I was proud of myself using `lobstr::ast` to figure it out so wanted to save it. | |
| assign_in_index <- function(start_vec, new_vals, num_index = 1:3) { | |
| `<-`(`[`(start_vec[num_index]), new_vals) | |
| start_vec | |
| } | |
| # this is the more sensible/readable version | |
| assign_in_index <- function(start_vec, new_vals, num_index = 1:3) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| title: "Many models" | |
| date: "`r paste('Last updated: ', format(Sys.time(), '%Y-%m-%d'))`" | |
| author: "Bryan Shalloway" | |
| output: | |
| github_document: | |
| toc: true | |
| toc_depth: 3 | |
| html_document: | |
| toc: true |
library(forecast)
library(tidyverse)
# hyjack `forecast.stlm` and simulate rather than forecast `ets` component
# (Insufficient -- likely only partially capture variability)
devtools::source_gist("https://gist.github.com/brshallo/4b93d0cf48937da6de06ffcffaed2b57")
set.seed(1234)
ts_test <- ts(rnorm(9, 0, 3),
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| simulate_stlm <- function (object, h = 2 * object$m, | |
| lambda = object$lambda, biasadj = NULL, newxreg = NULL, | |
| allow.multiplicative.trend = FALSE, ...) | |
| { | |
| if (!is.null(newxreg)) { | |
| if (nrow(as.matrix(newxreg)) != h) { | |
| stop("newxreg should have the same number of rows as the forecast horizon h") | |
| } | |
| } | |
| seasonal.periods <- attributes(object$stl)$seasonal.periods |
General approach to pseudo simulation:
Take bootstrap sample --> build model --> refit model to original data --> forecast refitted model though use simulate() on ets/trend component of forecast.
library(tidyverse)
library(forecast)
# Function takes bootstrapped sample --> fits model --> refits model to original `y`
boot_mod_refit <- function(ts, fun = stlm, ...){
bld.mbb.bootstrap(ts, 2)[[2]] %>%