model.adapter-class: Abstraction layer for model functions/objects

model.adapter-classR Documentation

Abstraction layer for model functions/objects

Description

This class encapsulates differences in specifications of statistical/machine learning functions and model objects to provide a standardized way to access data and common methods of models. To add support for a modeling function, new generator object of a model.interface class must be implimented and methods that cannot work well with the model should be overriden.

Methods

model.adapter$new(x, envir = parent.frame(2L), data = NULL, package.name = NULL)

Generate a new object.

Args

x

an object of supported models or a call for a model function.

envir = parent.frame(2L)

an environment in which call in x is evaluated. Default value is the environment where the object of model.adapter was created.

data = NULL

a data.frame used for further manipulations.

package.name = NULL

a character specifying package name of the function/object. If not specified, package.name is automatically determined from x. This is mainly used to control functions with same name in different packages (e.g., gam in gam and mgcv packages).

model.adapter$x.names(specials = NULL, type = c("all", "base"))

Access names of explanatory variables.

Args

specials = NULL

special characters to be passed to terms.

type = c("all", "base")

if "all", this function returns all explanatory variables including interactions, higher order terms, splines, etc. If "base" only basic form of the variables are returned.

Returns

a character vector of names of explanatory variables.

model.adapter$y.names()

Access names of response variables.

model.adapter$predict(newdata = NULL, type = c("response", "link", "prob", "class"), random = ~0, ...)

Call predict method.

For regression models, offset term is adjusted if 1) type is not "link", 2) the model having only one offset term, 3) offset was specified as offset = offset or formula = y ~ x + offset(offset) rather than offset = data$offset or formula = y ~ x + offset(data$offset), 4) the model is not glmmML or ranger.

Also, it is users responsibility to use offset term with appropreate scale. For example if the model is glm with poisson family with log link function, the offset term should be offset = log(offset) or formula = y ~ x + offset(log(offset)).

Args

newdata = NULL

a data.frame containing data used for prediction.

type = c("response", "link", "prob", "class")

the type of prediciton. "response" and "link" can be used for generalized linear (mixed) model and spcify scale of prediction. "response" is on the scale of the response variable. "link" is on the scale of the link function. "prob" and "class" are used for classification models. "prob" calculate probability of being each class of response variable. "class" makes predicted class for each observation.

random = ~0

the random effect to use. Tentatively, ~0 means don't use random effects.

...

other variables passed to predict methods.

Returns

a model.adapter.prediction object.

model.adapter$residuals(type = c("response", "link"))

Calculate residuals of the model.

Args

type = c("response", "link")

a character specifying type of residual (i.e., scale of residual) to be calculated.

Returns

a numeric vector of residuals.

Details

When calling model.adapter$new using model call by do.call, please specify quote = TRUE. Without it, a call of function in x is evaluated and unintended result may be returned.

Active bindings

call

a read-only call object used for initialization of model.adapter or a call object which used for construction of the model object. Note that call in this field is modified to have their full names by match.call() function. Therefore, the call in this field doesn't need to be identical to original call used for initialization of the class. When the original call used for creating the model object could not be recovered from the object, this field becomes NULL.

object

a read-only object of the model. If model.adapter is initialized by a call for a model function, this field returns a object created by evaluating the call.

data

a read-only data.frame used for modeling. At least, all columns used for the modeling are stored in this field. Because some modeling function doesn't keep original data.frame used for modeling or call in resultant object, this field can't have the same data.frame used for modeling in such situation. When all the columns used for the modeling is not available from this field becomes NULL. Also, when the model doesn't store original data and some function such as 'log()' was applied in model construction, the 'data.frame' stored in this field can be different from original values. At least, this is applicable for 'glmer' and 'lmer'.

formula

a read-only formula object specifying structure of the model. '.' in the formula object is expanded so that this field may not be same as the original formula specified in the call for the model function or the model object in such case.

family

a read-only character of family name. If the model does not use family, this field is NULL.

link

a function object of link function of the model. If the model does not have link function, this field has identity function.

linkinv

a function object of inverse link function of the model. If the model does not have inverse link function, this field has identity function.

package.name

a read-only character spcifying package name of the model.

model.type

a character representing model type. The possible values are "regression" and "classification".

x.vars

a data.frame having all explanatory variables.

y.vars

a data.frame having all response variables.

Methods

Public methods


Method new()

Usage
model.adapter$new(
  x,
  envir = parent.frame(2L),
  data = NULL,
  package.name = NULL
)

Method x.names()

Usage
model.adapter$x.names(specials = NULL, type = c("all", "base"))

Method y.names()

Usage
model.adapter$y.names()

Method predict()

Usage
model.adapter$predict(
  newdata = NULL,
  type = c("response", "link", "prob", "class"),
  random = ~0,
  ...
)

Method residuals()

Usage
model.adapter$residuals(type = c("response", "link"))

Method clone()

The objects of this class are cloneable with this method.

Usage
model.adapter$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


Marchen/model.adapter documentation built on July 3, 2023, 11:07 a.m.