Provides standardized estimation and prediction methods

See also

predictor_sl

Author

Klaus Kähler Holst

Public fields

info

Optional information/name of the model

formals

List with formal arguments of estimation and prediction functions

formula

Formula specifying response and design matrix

args

additional arguments specified during initialization

optional

description field

Active bindings

optional

description field

fit

Active binding returning estimated model object

Methods


Method new()

Create a new prediction model object

Usage

ml_model$new(
  formula = NULL,
  estimate,
  predict = stats::predict,
  predict.args = NULL,
  info = NULL,
  specials = c(),
  response.arg = "y",
  x.arg = "x",
  ...
)

Arguments

formula

formula specifying outcome and design matrix

estimate

function for fitting the model (must be a function response, 'y', and design matrix, 'x'. Alternatively, a function with a single 'formula' argument)

predict

prediction function (must be a function of model object, 'object', and new design matrix, 'newdata')

predict.args

optional arguments to prediction function

info

optional description of the model

specials

optional additional terms (weights, offset, id, subset, ...) passed to 'estimate'

response.arg

name of response argument

x.arg

name of design matrix argument

...

optional arguments to fitting function


Method estimate()

Estimation method

Usage

ml_model$estimate(data, ..., store = TRUE)

Arguments

data

data.frame

...

Additional arguments to estimation method

store

Logical determining if estimated model should be stored inside the class.


Method predict()

Prediction method

Usage

ml_model$predict(newdata, ..., object = NULL)

Arguments

newdata

data.frame

...

Additional arguments to prediction method

object

Optional model fit object


Method update()

Update formula

Usage

ml_model$update(formula, ...)

Arguments

formula

formula or character which defines the new response

...

Additional arguments to lower level functions


Method print()

Print method

Usage

ml_model$print(...)

Arguments

...

Additional arguments to lower level functions


Method response()

Extract response from data

Usage

ml_model$response(data, ...)

Arguments

data

data.frame

...

additional arguments to 'design'


Method design()

Extract design matrix (features) from data

Usage

ml_model$design(data, ...)

Arguments

data

data.frame

...

additional arguments to 'design'


Method opt()

Get options

Usage

ml_model$opt(arg, ...)

Arguments

arg

name of option to get value of

...

additional arguments to lower level functions


Method clone()

The objects of this class are cloneable with this method.

Usage

ml_model$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

data(iris)
rf <- function(formula, ...)
ml_model$new(formula, info="grf::probability_forest",
  estimate=function(x,y, ...) grf::probability_forest(X=x, Y=y, ...),
  predict=function(object, newdata)
             predict(object, newdata)$predictions, ...)

args <- expand.list(num.trees=c(100,200), mtry=1:3,
          formula=c(Species ~ ., Species ~ Sepal.Length + Sepal.Width))
models <- lapply(args, function(par) do.call(rf, par))

x <- models[[1]]$clone()
x$estimate(iris)
predict(x, newdata=head(iris))
#>         setosa  versicolor   virginica
#> [1,] 0.9913974 0.007491453 0.001111111
#> [2,] 0.9454808 0.054519231 0.000000000
#> [3,] 0.9921474 0.007852564 0.000000000
#> [4,] 0.9721474 0.027852564 0.000000000
#> [5,] 0.9968697 0.002019231 0.001111111
#> [6,] 0.9612937 0.032595238 0.006111111

 # Reduce Ex. timing
a <- targeted::cv(models, data=iris)
cbind(coef(a), attr(args, "table"))
#>              brier -logscore num.trees mtry
#> model1  0.09615551 0.2097763       100    1
#> model2  0.09675938 0.2184058       200    1
#> model3  0.08210013 0.1772280       100    2
#> model4  0.08439038 0.1828726       200    2
#> model5  0.08112753 0.1647543       100    3
#> model6  0.08003185 0.1666948       200    3
#> model7  0.34752831 0.5697157       100    1
#> model8  0.34206584 0.5609870       200    1
#> model9  0.34192105 0.5549436       100    2
#> model10 0.33810713 0.5501649       200    2
#> model11 0.32801397 0.5328124       100    3
#> model12 0.33355260 0.5465439       200    3
#>                                      formula
#> model1                           Species ~ .
#> model2                           Species ~ .
#> model3                           Species ~ .
#> model4                           Species ~ .
#> model5                           Species ~ .
#> model6                           Species ~ .
#> model7  Species ~ Sepal.Length + Sepal.Width
#> model8  Species ~ Sepal.Length + Sepal.Width
#> model9  Species ~ Sepal.Length + Sepal.Width
#> model10 Species ~ Sepal.Length + Sepal.Width
#> model11 Species ~ Sepal.Length + Sepal.Width
#> model12 Species ~ Sepal.Length + Sepal.Width


ff <- ml_model$new(estimate=function(y,x) lm.fit(x=x, y=y),
        predict=function(object, newdata) newdata%*%object$coefficients)
## tmp <- ff$estimate(y, x=x)
## ff$predict(x)