deeptrafo {deeptrafo}R Documentation

Deep Conditional Transformation Models

Description

Deep Conditional Transformation Models

Usage

deeptrafo(
  formula,
  data,
  response_type = get_response_type(data[[all.vars(fml)[1]]]),
  order = get_order(response_type, data[[all.vars(fml)[1]]]),
  addconst_interaction = 0,
  latent_distr = "logistic",
  loss = "nll",
  loss_args = NULL,
  monitor_metrics = NULL,
  trafo_options = trafo_control(order_bsp = order, response_type = response_type),
  return_data = FALSE,
  engine = "tf",
  ...
)

Arguments

formula

Formula specifying the response, interaction, shift terms as response | interacting ~ shifting. auto-regressive transformation models (ATMs).

data

Named list or data.frame which may contain both structured and unstructured data.

response_type

Character; type of response. One of "continuous", "survival", "count", or "ordered". If not supplied manually it is determined by the first entry in data[[response]].

order

Integer; order of the response basis. Default 10 for Bernstein basis or number of levels minus one for ordinal responses.

addconst_interaction

Positive constant; a constant added to the additive predictor of the interaction term. If NULL, terms are left unchanged. If 0 and predictors have negative values in their design matrix, the minimum value of all predictors is added to ensure positivity. If > 0, the minimum value plus the addconst_interaction is added to each predictor in the interaction term. This ensures a monotone non-decreasing transformation function in the response when using (tensor product) spline bases in the interacting term.

latent_distr

A tfd_distribution or character; the base distribution for transformation models. If character, can be "normal", "logistic", "gumbel" or "gompertz".

loss

Character; specifies the loss function used. The default is "nll", an internal function which takes latent_distr as an argument and returns a function with arguments y_true and y_pred to be given to the underlying 'keras' model. Custom loss functions can be supplied with the same structure, either as a character or function.

loss_args

Further additional arguments to loss.

monitor_metrics

See deepregression

trafo_options

Options for transformation models such as the basis function used, see trafo_control for more details.

return_data

Include full data in the returned object. Defaults to FALSE. Set to TRUE if inteded to use simulate afterwards.

engine

Ignored; for compatibility with package deepregression.

...

Additional arguments passed to deepregression

Details

deeptrafo is the main function for setting up neural network transformation models and is called by all aliases for the more special cases (see e.g. ColrNN). The naming convention of the aliases follow the 'tram' package (see e.g. Colr) and add the suffix "NN" to the function name.

Value

An object of class c("deeptrafo", "deepregression")

References

Kook, L., Baumann, P. F., Dürr, O., Sick, B., & Rügamer, D. (2024). Estimating conditional distributions with neural networks using R package deeptrafo. Journal of Statistical Software. doi:10.18637/jss.v111.i10.

Examples

if (.Platform$OS.type != "windows" &&
  reticulate::py_available() &&
  reticulate::py_module_available("tensorflow") &&
  reticulate::py_module_available("keras") &&
  reticulate::py_module_available("tensorflow_probability")) {
  data("wine", package = "ordinal")
  wine$z <- rnorm(nrow(wine))
  wine$x <- rnorm(nrow(wine))

  nn <- \(x) x |>
    layer_dense(input_shape = 1L, units = 2L, activation = "relu") |>
    layer_dense(1L)

  fml <- rating ~ 0 + temp + contact + s(z, df = 3) + nn(x)

  m <- deeptrafo(fml, wine,
    latent_distr = "logistic", monitor_metric = NULL,
    return_data = TRUE, list_of_deep_models = list(nn = nn)
  )

  print(m)

  m %>% fit(epochs = 10, batch_size = nrow(wine))
  coef(m, which_param = "interacting")
  coef(m, which_param = "shifting")
  fitted(m)
  predict(m, type = "pdf")
  predict(m, type = "pdf", newdata = wine[, -2])
  logLik(m)
  logLik(m, newdata = wine[1:10, ])
  plot(m)
  mcv <- cv(m, cv_folds = 3)
  ens <- ensemble(m, n_ensemble = 3)
  coef(ens)
}


[Package deeptrafo version 1.0-0 Index]