custom_pipe_component: Custom pipeline component

Description Usage Arguments Details Value Examples

Description

Used to define a custom pipeline component. Users can define functions fit, transform, predict, or incfit with desired behavior.

Usage

1
2
3
custom_pipe_component(classname = "custom", fit = NULL, transform = NULL,
  predict = NULL, incr_fit = NULL, inv_transform = NULL,
  predict_proba = NULL, initialize = NULL, ..., as_private = character(0))

Arguments

classname

string of class name

fit

fit function

transform

transform function

predict

predict function

incr_fit

incremantal fit function

inv_transform

inverse-transform function

predict_proba

probability predict function

initialize

intialize function

...

additional class attributes, they must be given with names and there must be no name conflict

as_private

names of additional parameters to be stored as private fields

Details

This function is used to define a custom pipeline component. Users can define functions with desired behavior, together with addional public and private fields.

To be properly incorporated to a pipeline framework, the functions should satisfy the following properties:

Typically, self$object is used to store the fitted model object, and is updated by fit and incr_fit functions. Alternatively, one may also define additional class attributes to store relevant information.

Value

an R6ClassGenerator

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
32
33
34
35
36
37
38
39
40
41
OLSPipe <- custom_pipe_component(
  fit = function(x, y) {
    x <- cbind(as.matrix(x), 1)
    self$object <- solve(crossprod(x), crossprod(x,y))
    invisible(self)
   },
  predict = function(x, y=NULL) {
    cbind(as.matrix(x), 1) %*% self$object
  }
)
o <- OLSPipe$new()
data(mtcars)
x <- mtcars[, c('wt', 'am')]
y <- mtcars[['mpg']]
o$fit(x, y)
o$predict(x)

MeanCalculator <- custom_pipe_component(
  fit = function(x, y=NULL) {
    self$sum <- sum(x)
    self$n <- length(x)
    invisible(self)
  },
  incr_fit = function(x, y=NULL) {
    self$n <- self$n + length(x)
    self$sum <- self$sum + sum(x)
    invisible(self)
  },
  predict = function(x=NULL, y=NULL) {
    self$sum / self$n
  },
  initialize = function(...) {
    invisible(self)
  },
  sum=0, n=0
)
m <- MeanCalculator$new()
m$fit(1:9)
m$predict()
m$incr_fit(10)
m$predict()

kota7/MLPipe documentation built on May 5, 2019, 5:53 p.m.