|
| 1 | +# gen_additive_mod() - General Interface to Linear GAM Models |
| 2 | +# - backend: gam |
| 3 | +# - prediction: |
| 4 | +# - mode = "regression" (default) uses |
| 5 | +# - mode = "classification" |
| 6 | + |
| 7 | +#' Interface for Generalized Additive Models (GAM) |
| 8 | +#' |
| 9 | +#' @param mode A single character string for the type of model. |
| 10 | +#' @param select_features TRUE or FALSE. If this is TRUE then can add an |
| 11 | +#' extra penalty to each term so that it can be penalized to zero. |
| 12 | +#' This means that the smoothing parameter estimation that is part of |
| 13 | +#' fitting can completely remove terms from the model. If the corresponding |
| 14 | +#' smoothing parameter is estimated as zero then the extra penalty has no effect. |
| 15 | +#' Use `adjust_deg_free` to increase level of penalization. |
| 16 | +#' @param adjust_deg_free If `select_features = TRUE`, then acts as a multiplier for smoothness. |
| 17 | +#' Increase this beyond 1 to produce smoother models. |
| 18 | +#' |
| 19 | +#' |
| 20 | +#' @return |
| 21 | +#' A `parsnip` model specification |
| 22 | +#' |
| 23 | +#' @details |
| 24 | +#' |
| 25 | +#' __Available Engines:__ |
| 26 | +#' - __gam__: Connects to `mgcv::gam()` |
| 27 | +#' |
| 28 | +#' __Parameter Mapping:__ |
| 29 | +#' |
| 30 | +#' ```{r echo = FALSE} |
| 31 | +#' tibble::tribble( |
| 32 | +#' ~ "modelgam", ~ "mgcv::gam", |
| 33 | +#' "select_features", "select (FALSE)", |
| 34 | +#' "adjust_deg_free", "gamma (1)" |
| 35 | +#' ) %>% knitr::kable() |
| 36 | +#' ``` |
| 37 | +#' |
| 38 | +#' @section Engine Details: |
| 39 | +#' |
| 40 | +#' __gam__ |
| 41 | +#' |
| 42 | +#' This engine uses [mgcv::gam()] and has the following parameters, |
| 43 | +#' which can be modified through the [set_engine()] function. |
| 44 | +#' |
| 45 | +#' ``` {r echo=F} |
| 46 | +#' str(mgcv::gam) |
| 47 | +#' ``` |
| 48 | +#' |
| 49 | +#' @section Fit Details: |
| 50 | +#' |
| 51 | +#' __MGCV Formula Interface__ |
| 52 | +#' |
| 53 | +#' Fitting GAMs is accomplished using parameters including: |
| 54 | +#' |
| 55 | +#' - [mgcv::s()]: GAM spline smooths |
| 56 | +#' - [mgcv::te()]: GAM tensor product smooths |
| 57 | +#' |
| 58 | +#' These are applied in the `fit()` function: |
| 59 | +#' |
| 60 | +#' ``` r |
| 61 | +#' fit(value ~ s(date_mon, k = 12) + s(date_num), data = df) |
| 62 | +#' ``` |
| 63 | +#' |
| 64 | +#' |
| 65 | +#' @examples |
| 66 | +#' |
| 67 | +#' show_engines("gen_additive_mod") |
| 68 | +#' |
| 69 | +#' gen_additive_mod() |
| 70 | +#' |
| 71 | +#' |
| 72 | +#' @export |
| 73 | +gen_additive_mod <- function(mode = "regression", |
| 74 | + select_features = NULL, |
| 75 | + adjust_deg_free = NULL) { |
| 76 | + |
| 77 | + args <- list( |
| 78 | + select_features = rlang::enquo(select_features), |
| 79 | + adjust_deg_free = rlang::enquo(adjust_deg_free) |
| 80 | + ) |
| 81 | + |
| 82 | + new_model_spec( |
| 83 | + "gen_additive_mod", |
| 84 | + args = args, |
| 85 | + eng_args = NULL, |
| 86 | + mode = mode, |
| 87 | + method = NULL, |
| 88 | + engine = NULL |
| 89 | + ) |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | +#' @export |
| 94 | +print.gen_additive_mod <- function(x, ...) { |
| 95 | + cat("GAM Model Specification (", x$mode, ")\n\n", sep = "") |
| 96 | + model_printer(x, ...) |
| 97 | + |
| 98 | + if(!is.null(x$method$fit$args)) { |
| 99 | + cat("Model fit template:\n") |
| 100 | + print(show_call(x)) |
| 101 | + } |
| 102 | + |
| 103 | + invisible(x) |
| 104 | +} |
| 105 | + |
| 106 | +#' @export |
| 107 | +#' @importFrom stats update |
| 108 | +update.gen_additive_mod <- function(object, |
| 109 | + select_features = NULL, |
| 110 | + adjust_deg_free = NULL, |
| 111 | + parameters = NULL, |
| 112 | + fresh = FALSE, ...) { |
| 113 | + |
| 114 | + update_dot_check(...) |
| 115 | + |
| 116 | + if (!is.null(parameters)) { |
| 117 | + parameters <- check_final_param(parameters) |
| 118 | + } |
| 119 | + |
| 120 | + args <- list( |
| 121 | + select_features = rlang::enquo(select_features), |
| 122 | + adjust_deg_free = rlang::enquo(adjust_deg_free) |
| 123 | + ) |
| 124 | + |
| 125 | + args <- update_main_parameters(args, parameters) |
| 126 | + |
| 127 | + if (fresh) { |
| 128 | + object$args <- args |
| 129 | + } else { |
| 130 | + null_args <- purrr::map_lgl(args, null_value) |
| 131 | + if (any(null_args)) |
| 132 | + args <- args[!null_args] |
| 133 | + if (length(args) > 0) |
| 134 | + object$args[names(args)] <- args |
| 135 | + } |
| 136 | + |
| 137 | + new_model_spec( |
| 138 | + "gen_additive_mod", |
| 139 | + args = object$args, |
| 140 | + eng_args = object$eng_args, |
| 141 | + mode = object$mode, |
| 142 | + method = NULL, |
| 143 | + engine = object$engine |
| 144 | + ) |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +#' @export |
| 149 | +#' @importFrom parsnip translate |
| 150 | +translate.gen_additive_mod <- function(x, engine = x$engine, ...) { |
| 151 | + if (is.null(engine)) { |
| 152 | + message("Used `engine = 'gam'` for translation.") |
| 153 | + engine <- "gam" |
| 154 | + } |
| 155 | + x <- translate.default(x, engine, ...) |
| 156 | + |
| 157 | + x |
| 158 | +} |
0 commit comments