surrogate: Surrogate GLM

View source: R/surrogate.R

surrogateR Documentation

Surrogate GLM

Description

Fit a surrogate generalized linear model (GLM) to the segmented data.

Usage

surrogate(data, par_list)

Arguments

data

Data frame containing the segmented training data.

par_list

Named list, constructed via alist, containing additional arguments to be passed on to glm. Examples are: formula, family, weights or offset.

Value

GLM (i.e., a "glm" object) which is fit to the segmented data.

See Also

Extra info on arguments can be found at glm.

Examples

## Not run: 
data('mtpl_be')
features <- setdiff(names(mtpl_be), c('id', 'nclaims', 'expo', 'long', 'lat'))
set.seed(12345)
gbm_fit <- gbm::gbm(as.formula(paste('nclaims ~',
                               paste(features, collapse = ' + '))),
                    distribution = 'poisson',
                    data = mtpl_be,
                    n.trees = 50,
                    interaction.depth = 3,
                    shrinkage = 0.1)
gbm_fun <- function(object, newdata) mean(predict(object, newdata, n.trees = object$n.trees, type = 'response'))
gbm_fit %>% insights(vars = c('ageph', 'bm', 'coverage', 'fuel', 'bm_fuel'),
                     data = mtpl_be,
                     interactions = 'user',
                     pred_fun = gbm_fun) %>%
            segmentation(data = mtpl_be,
                         type = 'ngroups',
                         values = setNames(c(7, 8, 2, 2, 3), c('ageph', 'bm', 'coverage', 'fuel', 'bm_fuel'))) %>%
            surrogate(par_list = alist(formula = nclaims ~ ageph_ + bm_ + coverage_ + fuel_ + bm_fuel_,
                                       family =  poisson(link = 'log'),
                                       offset = log(expo)))

## End(Not run)

henckr/maidrr documentation built on July 27, 2023, 3:17 p.m.