gen_additive_mod: Interface for Generalized Additive Models (GAM)

Description Usage Arguments Details Value Engine Details Fit Details Examples

View source: R/parsnip-gam_mod.R

Description

Interface for Generalized Additive Models (GAM)

Usage

1
2
3
4
5
gen_additive_mod(
  mode = "regression",
  select_features = NULL,
  adjust_deg_free = NULL
)

Arguments

mode

A single character string for the type of model.

select_features

TRUE or FALSE. If this is TRUE then can add an extra penalty to each term so that it can be penalized to zero. This means that the smoothing parameter estimation that is part of fitting can completely remove terms from the model. If the corresponding smoothing parameter is estimated as zero then the extra penalty has no effect. Use adjust_deg_free to increase level of penalization.

adjust_deg_free

If select_features = TRUE, then acts as a multiplier for smoothness. Increase this beyond 1 to produce smoother models.

Details

Available Engines:

Parameter Mapping:

modelgam mgcv::gam
select_features select (FALSE)
adjust_deg_free gamma (1)

Value

A parsnip model specification

Engine Details

gam

This engine uses mgcv::gam() and has the following parameters, which can be modified through the parsnip::set_engine() function.

1
2
3
4
5
6
## function (formula, family = gaussian(), data = list(), weights = NULL, 
##     subset = NULL, na.action, offset = NULL, method = "GCV.Cp", optimizer = c("outer", 
##         "newton"), control = list(), scale = 0, select = FALSE, knots = NULL, 
##     sp = NULL, min.sp = NULL, H = NULL, gamma = 1, fit = TRUE, paraPen = NULL, 
##     G = NULL, in.out = NULL, drop.unused.levels = TRUE, drop.intercept = NULL, 
##     discrete = FALSE, ...)

Fit Details

MGCV Formula Interface

Fitting GAMs is accomplished using parameters including:

These are applied in the fit() function:

fit(value ~ s(date_mon, k = 12) + s(date_num), data = df)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library(tidymodels)
library(modelgam)
library(modeltime)
library(tidyverse)
library(timetk)
library(lubridate)

m750_extended <- m750 %>%
    group_by(id) %>%
    future_frame(.length_out = 24, .bind_data = TRUE) %>%
    mutate(lag_24 = lag(value, 24)) %>%
    ungroup() %>%
    mutate(date_num = as.numeric(date)) %>%
    mutate(date_month = month(date))

m750_train  <- m750_extended %>% drop_na()
m750_future <- m750_extended %>% filter(is.na(value))

model_fit_gam <- gen_additive_mod(mode = "regression") %>%
    set_engine("gam", family=Gamma(link="log"), method = "REML") %>%
    fit(value ~ s(date_month, k = 12) 
        + s(date_num) 
        + s(lag_24) 
        + s(date_num, date_month), 
        data = m750_train)

model_fit_gam %>% predict(m750_future, type = "numeric") 

model_fit_gam %>% predict(m750_future, type = "raw") 

 

business-science/gammodels documentation built on Dec. 19, 2021, 12:46 p.m.