Title: | Fitting Deep Conditional Transformation Models |
---|---|
Description: | Allows for the specification of deep conditional transformation models (DCTMs) and ordinal neural network transformation models, as described in Baumann et al (2021) <doi:10.1007/978-3-030-86523-8_1> and Kook et al (2022) <doi:10.1016/j.patcog.2021.108263>. Extensions such as autoregressive DCTMs (Ruegamer et al, 2023, <doi:10.1007/s11222-023-10212-8>) and transformation ensembles (Kook et al, 2022, <doi:10.48550/arXiv.2205.12729>) are implemented. The software package is described in Kook et al (2024, <doi:10.18637/jss.v111.i10>). |
Authors: | Lucas Kook [aut, cre], Philipp Baumann [aut], David Ruegamer [aut] |
Maintainer: | Lucas Kook <[email protected]> |
License: | GPL-3 |
Version: | 1.0-0 |
Built: | 2025-01-03 06:15:31 UTC |
Source: | https://github.com/neural-structured-additive-learning/deeptrafo |
Initializes the Processed Additive Predictor for ATMs
atm_init(atmnr, h1nr)
atm_init(atmnr, h1nr)
atmnr , h1nr
|
positions of the atm and h1 formula |
returns a subnetwork_init function with pre-defined arguments
BoxCox-type neural network transformation models
BoxCoxNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "normal", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
BoxCoxNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "normal", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- BoxCoxNN(y ~ x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- BoxCoxNN(y ~ x, data = df) coef(m) }
S3 methods for deep conditional transformation models
## S3 method for class 'deeptrafo' coef( object, which_param = c("shifting", "interacting", "autoregressive"), type = NULL, ... ) ## S3 method for class 'deeptrafo' predict( object, newdata = NULL, type = c("trafo", "pdf", "cdf", "interaction", "shift", "terms"), batch_size = NULL, K = 100, q = NULL, pred_grid = FALSE, ... ) ## S3 method for class 'deeptrafo' fitted( object, newdata = NULL, batch_size = NULL, convert_fun = as.matrix, call_create_lags = TRUE, ... ) ## S3 method for class 'deeptrafo' logLik( object, newdata = NULL, convert_fun = function(x, ...) -sum(x, ...), ... ) ## S3 method for class 'deeptrafo' residuals(object, newdata = NULL, return_gradients = FALSE, ...) ## S3 method for class 'deeptrafo' simulate(object, nsim = 1, seed = NULL, newdata = NULL, ...) ## S3 method for class 'deeptrafo' print(x, print_model = FALSE, print_coefs = TRUE, with_baseline = FALSE, ...) ## S3 method for class 'deeptrafo' summary(object, ...)
## S3 method for class 'deeptrafo' coef( object, which_param = c("shifting", "interacting", "autoregressive"), type = NULL, ... ) ## S3 method for class 'deeptrafo' predict( object, newdata = NULL, type = c("trafo", "pdf", "cdf", "interaction", "shift", "terms"), batch_size = NULL, K = 100, q = NULL, pred_grid = FALSE, ... ) ## S3 method for class 'deeptrafo' fitted( object, newdata = NULL, batch_size = NULL, convert_fun = as.matrix, call_create_lags = TRUE, ... ) ## S3 method for class 'deeptrafo' logLik( object, newdata = NULL, convert_fun = function(x, ...) -sum(x, ...), ... ) ## S3 method for class 'deeptrafo' residuals(object, newdata = NULL, return_gradients = FALSE, ...) ## S3 method for class 'deeptrafo' simulate(object, nsim = 1, seed = NULL, newdata = NULL, ...) ## S3 method for class 'deeptrafo' print(x, print_model = FALSE, print_coefs = TRUE, with_baseline = FALSE, ...) ## S3 method for class 'deeptrafo' summary(object, ...)
object |
Object of class |
which_param |
Character; either |
type |
Either NULL (all types of coefficients are returned),
"linear" for linear coefficients or "smooth" for coefficients of;
Note that |
... |
Further arguments supplied to |
newdata |
Named |
batch_size |
Integer; optional, useful if data is too large. |
K |
Integer; grid length for the response to evaluate predictions at,
if |
q |
Numeric or factor; user-supplied grid of response values to evaluate
the predictions. Defaults to |
pred_grid |
Logical; set TRUE, if user provides a predefined grid for an atp/atm model through newdata which holds two attributes. The first attribute, rname, should hold the column name (string) of the response variable while the second attribute, y, should hold the grid name. |
convert_fun |
Function; applied to the log-likelihood values of all observations. |
call_create_lags |
Logical; lags may already be computed by a different method (e.g. plot) |
return_gradients |
Return individual gradients instead of the summed
gradients; the residuals are |
nsim |
Integer; number of simulations; defaults to 1. |
seed |
Seed for generating samples; defaults to |
x |
Object of class |
print_model |
Logical; print keras model. |
print_coefs |
Logical; print coefficients. |
with_baseline |
Logical; print baseline coefs. |
If no new data is supplied, predictions are computed on the training
data (i.e. in-sample). If new data is supplied without a response,
predictions are evaluated on a grid of length K
.
Returns vector or matrix of predictions, depending on the supplied
type
.
Returns matrix of fitted values.
Deep continuous outcome logistic regression
ColrNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
ColrNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- ColrNN(y ~ x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- ColrNN(y ~ x, data = df) coef(m) }
Deep distribution-free count regression
cotramNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, ... )
cotramNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { set.seed(1) df <- data.frame(y = as.integer(abs(1 + rnorm(50, sd = 10))), x = rnorm(50)) m <- cotramNN(y ~ 0 + x, data = df, order = 6) optimizer <- optimizer_adam(learning_rate = 0.1, decay = 4e-4) m <- cotramNN(y ~ 0 + x, data = df, optimizer = optimizer, order = 6) library(cotram) fit(m, epochs = 800L, validation_split = 0) logLik(mm <- cotram(y ~ x, data = df, method = "logit")); logLik(m) coef(mm, with_baseline = TRUE); unlist(c(coef(m, which = "interacting"), coef(m, which = "shifting"))) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { set.seed(1) df <- data.frame(y = as.integer(abs(1 + rnorm(50, sd = 10))), x = rnorm(50)) m <- cotramNN(y ~ 0 + x, data = df, order = 6) optimizer <- optimizer_adam(learning_rate = 0.1, decay = 4e-4) m <- cotramNN(y ~ 0 + x, data = df, optimizer = optimizer, order = 6) library(cotram) fit(m, epochs = 800L, validation_split = 0) logLik(mm <- cotram(y ~ x, data = df, method = "logit")); logLik(m) coef(mm, with_baseline = TRUE); unlist(c(coef(m, which = "interacting"), coef(m, which = "shifting"))) }
Cox proportional hazards type neural network transformation models
CoxphNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "gompertz", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
CoxphNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "gompertz", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- CoxphNN(y ~ x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- CoxphNN(y ~ x, data = df) coef(m) }
Deep conditional transformation models with alternative formula interface
dctm( response, intercept = NULL, shift = NULL, shared = NULL, data, response_type = get_response_type(data[[all.vars(response)[1]]]), order = get_order(response_type, data[[all.vars(response)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
dctm( response, intercept = NULL, shift = NULL, shared = NULL, data, response_type = get_response_type(data[[all.vars(response)[1]]]), order = get_order(response_type, data[[all.vars(response)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
response |
Formula for the response; e.g. |
intercept |
Formula for the intercept function; e.g., |
shift |
Formula for the shift part of the model; e.g., |
shared |
Formula for sharing weights between predictors in the intercept and shift part of the model |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- dctm(response = ~ y, shift = ~ 0 + x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- dctm(response = ~ y, shift = ~ 0 + x, data = df) coef(m) }
Deep Conditional Transformation Models
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", ... )
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", ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
loss |
Character; specifies the loss function used. The default is
|
loss_args |
Further additional arguments to |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
return_data |
Include full data in the returned object. Defaults to
|
engine |
Ignored; for compatibility with package |
... |
Additional arguments passed to |
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.
An object of class c("deeptrafo", "deepregression")
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.
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) }
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) }
Deep ensembling for neural network transformation models
## S3 method for class 'deeptrafo' ensemble( x, n_ensemble = 5, reinitialize = TRUE, mylapply = lapply, verbose = FALSE, patience = 20, plot = TRUE, print_members = TRUE, stop_if_nan = TRUE, save_weights = TRUE, callbacks = list(), save_fun = NULL, seed = seq_len(n_ensemble), ... )
## S3 method for class 'deeptrafo' ensemble( x, n_ensemble = 5, reinitialize = TRUE, mylapply = lapply, verbose = FALSE, patience = 20, plot = TRUE, print_members = TRUE, stop_if_nan = TRUE, save_weights = TRUE, callbacks = list(), save_fun = NULL, seed = seq_len(n_ensemble), ... )
x |
Object of class |
n_ensemble |
Numeric; number of ensemble members to fit. |
reinitialize |
Logical; if |
mylapply |
Function; |
verbose |
Logical; whether to print training in each fold. |
patience |
Integer; number of patience for early stopping. |
plot |
Logical; whether to plot the resulting losses in each fold. |
print_members |
Logical; print results for each member. |
stop_if_nan |
Logical; whether to stop ensembling if |
save_weights |
Logical; whether to save the ensemble weights. |
callbacks |
List; callbacks used for fitting. |
save_fun |
Function; function to be applied to each member to be stored in the final result. |
seed |
Numeric vector of length |
... |
Further arguments passed to |
Ensemble of "deeptrafo"
models with list of training histories
and fitted weights included in ensemble_results
. For details see
the return statment in ensemble
.
Define Predictor of Transformation Model
from_preds_to_trafo( atm_toplayer = function(x) layer_dense(x, units = 1L, name = "atm_toplayer"), const_ia = NULL, ... )
from_preds_to_trafo( atm_toplayer = function(x) layer_dense(x, units = 1L, name = "atm_toplayer"), const_ia = NULL, ... )
atm_toplayer |
Function to be applied on top of the transformed lags. |
const_ia |
See |
... |
For compatibility with 'deepregression' |
Not intended to be used directly by the end user.
A function of list_pred_param
returning a list of output tensors
that is passed to model_fun
of deepregression
Initializes the Processed Additive Predictor for TM's Interaction
h1_init(yterms, h1pred, add_const_positiv = 0)
h1_init(yterms, h1pred, add_const_positiv = 0)
yterms |
Terms for the response |
h1pred |
Interacting predictor |
add_const_positiv |
Shift basis for the predictors to be strictly positive |
returns a subnetwork_init function with pre-defined arguments
Lehmann-type neural network transformation models
LehmanNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "gumbel", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
LehmanNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "gumbel", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- LehmanNN(y ~ 0 + x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = rnorm(50), x = rnorm(50)) m <- LehmanNN(y ~ 0 + x, data = df) coef(m) }
Deep normal linear regression
LmNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "normal", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = 1L, response_type = response_type, y_basis_fun = eval_lin, y_basis_fun_lower = .empty_fun(eval_lin), y_basis_fun_prime = eval_lin_prime, basis = "shiftscale"), ... )
LmNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "normal", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = 1L, response_type = response_type, y_basis_fun = eval_lin, y_basis_fun_lower = .empty_fun(eval_lin), y_basis_fun_prime = eval_lin_prime, basis = "shiftscale"), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { set.seed(1) df <- data.frame(y = 10 + rnorm(50), x = rnorm(50)) m <- LmNN(y ~ 0 + x, data = df) optimizer <- optimizer_adam(learning_rate = 0.01, decay = 4e-4) m <- LmNN(y ~ 0 + x, data = df, optimizer = optimizer) library(tram) fit(m, epochs = 900L, validation_split = 0) logLik(mm <- Lm(y ~ x, data = df)); logLik(m) coef(mm, with_baseline = TRUE); unlist(c(coef(m, which = "interacting"), coef(m, which = "shifting"))) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { set.seed(1) df <- data.frame(y = 10 + rnorm(50), x = rnorm(50)) m <- LmNN(y ~ 0 + x, data = df) optimizer <- optimizer_adam(learning_rate = 0.01, decay = 4e-4) m <- LmNN(y ~ 0 + x, data = df, optimizer = optimizer) library(tram) fit(m, epochs = 900L, validation_split = 0) logLik(mm <- Lm(y ~ x, data = df)); logLik(m) coef(mm, with_baseline = TRUE); unlist(c(coef(m, which = "interacting"), coef(m, which = "shifting"))) }
Generic negative log-likelihood for transformation models
nll(latent_distr)
nll(latent_distr)
latent_distr |
Target distribution, character or
|
A function for computing the negative log-likelihood of a neural network transformation model with generic response.
Ordinal neural network transformation models
ontram( response, intercept = NULL, shift = NULL, shared = NULL, data, response_type = "ordered", order = get_order(response_type, data[[all.vars(response)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
ontram( response, intercept = NULL, shift = NULL, shared = NULL, data, response_type = "ordered", order = get_order(response_type, data[[all.vars(response)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
response |
Formula for the response; e.g., |
intercept |
Formula for the intercept function; e.g., |
shift |
Formula for the shift part of the model; e.g., |
shared |
Formula for sharing weights between predictors in the intercept and shift part of the model |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
Kook, L. & Herzog, L., Hothorn, T., Dürr, O., & Sick, B. (2022). Deep and interpretable regression models for ordinal outcomes. Pattern Recognition, 122, 108263. DOI 10.1016/j.patcog.2021.108263
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = ordered(sample.int(6, 50, TRUE)), x = rnorm(50)) m <- ontram(response = ~ y, shift = ~ x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = ordered(sample.int(6, 50, TRUE)), x = rnorm(50)) m <- ontram(response = ~ y, shift = ~ x, data = df) coef(m) }
Plot method for deep conditional transformation models
## S3 method for class 'deeptrafo' plot( x, which = NULL, type = c("smooth", "trafo", "pdf", "cdf"), newdata = NULL, which_param = c("shifting", "interacting"), only_data = FALSE, K = 40, q = NULL, ... )
## S3 method for class 'deeptrafo' plot( x, which = NULL, type = c("smooth", "trafo", "pdf", "cdf"), newdata = NULL, which_param = c("shifting", "interacting"), only_data = FALSE, K = 40, q = NULL, ... )
x |
Object of class |
which |
Which effect to plot, default selects all smooth effects in the shift term. |
type |
Character; One of "smooth", "trafo", "pdf", or "cdf". |
newdata |
Optional new data ( |
which_param |
Character; either |
only_data |
Logical, if |
K |
Integer; If |
q |
Vector of response values to compute predictions at, see |
... |
Further arguments, passed to fit, plot or predict function |
Deep (proportional odds) logistic regression
PolrNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
PolrNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "logistic", monitor_metrics = NULL, trafo_options = trafo_control(order_bsp = order, response_type = response_type), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = ordered(sample.int(5, 50, replace = TRUE)), x = rnorm(50)) m <- PolrNN(y ~ x, data = df) coef(m) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { df <- data.frame(y = ordered(sample.int(5, 50, replace = TRUE)), x = rnorm(50)) m <- PolrNN(y ~ x, data = df) coef(m) }
Deep parametric survival regression
SurvregNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "gompertz", monitor_metrics = NULL, trafo_options = NULL, ... )
SurvregNN( formula, data, response_type = get_response_type(data[[all.vars(formula)[1]]]), order = get_order(response_type, data[[all.vars(formula)[1]]]), addconst_interaction = 0, latent_distr = "gompertz", monitor_metrics = NULL, trafo_options = NULL, ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
response_type |
Character; type of response. One of |
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 |
latent_distr |
A |
monitor_metrics |
See |
trafo_options |
Options for transformation models such as the basis
function used, see |
... |
Additional arguments passed to |
See return statement of deeptrafo
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { set.seed(1) df <- data.frame(y = abs(1 + rnorm(50)), x = rnorm(50)) m <- SurvregNN(y ~ 0 + x, data = df) optimizer <- optimizer_adam(learning_rate = 0.01, decay = 4e-4) m <- SurvregNN(y ~ 0 + x, data = df, optimizer = optimizer) library(tram) fit(m, epochs = 500L, validation_split = 0) logLik(mm <- Survreg(y ~ x, data = df, dist = "loglogistic")); logLik(m) coef(mm, with_baseline = TRUE); unlist(c(coef(m, which = "interacting"), coef(m, which = "shifting"))) }
if (.Platform$OS.type != "windows" && reticulate::py_available() && reticulate::py_module_available("tensorflow") && reticulate::py_module_available("keras") && reticulate::py_module_available("tensorflow_probability")) { set.seed(1) df <- data.frame(y = abs(1 + rnorm(50)), x = rnorm(50)) m <- SurvregNN(y ~ 0 + x, data = df) optimizer <- optimizer_adam(learning_rate = 0.01, decay = 4e-4) m <- SurvregNN(y ~ 0 + x, data = df, optimizer = optimizer) library(tram) fit(m, epochs = 500L, validation_split = 0) logLik(mm <- Survreg(y ~ x, data = df, dist = "loglogistic")); logLik(m) coef(mm, with_baseline = TRUE); unlist(c(coef(m, which = "interacting"), coef(m, which = "shifting"))) }
Options for transformation models
trafo_control( order_bsp = 10L, support = function(y) range(y), y_basis_fun = NULL, y_basis_fun_lower = NULL, y_basis_fun_prime = NULL, penalize_bsp = 0, order_bsp_penalty = 2, tf_bsps = FALSE, response_type = c("continuous", "ordered", "survival", "count"), atm_toplayer = function(x) { layer_dense(x, units = 1L, name = "atm_toplayer", use_bias = FALSE) }, basis = c("bernstein", "ordered", "shiftscale") )
trafo_control( order_bsp = 10L, support = function(y) range(y), y_basis_fun = NULL, y_basis_fun_lower = NULL, y_basis_fun_prime = NULL, penalize_bsp = 0, order_bsp_penalty = 2, tf_bsps = FALSE, response_type = c("continuous", "ordered", "survival", "count"), atm_toplayer = function(x) { layer_dense(x, units = 1L, name = "atm_toplayer", use_bias = FALSE) }, basis = c("bernstein", "ordered", "shiftscale") )
order_bsp |
The order of Bernstein polynomials in case |
support |
A function returning a vector with two elements, namely the support for the basis of y. |
y_basis_fun |
Function; basis function for Y |
y_basis_fun_lower |
Function; basis function for lower bound of interval censored response |
y_basis_fun_prime |
Function; basis function derivative |
penalize_bsp |
Scalar value > 0; controls amount of penalization of Bernstein polynomials. |
order_bsp_penalty |
Integer; order of Bernstein polynomial penalty. 0 results in a penalty based on integrated squared second order derivatives, values >= 1 in difference penalties. |
tf_bsps |
Logical; whether to use a TensorFlow implementation of the Bernstein polynomial functions. |
response_type |
Character; type of response can be continuous, ordered, survival, or count. |
atm_toplayer |
Function; a function specifying the layer on top of ATM lags. |
basis |
Character or function; implemented options are
|
Returns a named list
with all options, basis functions,
support, and penalties.
Transformation ensembles
trafoensemble( formula, data, n_ensemble = 5, verbose = FALSE, print_members = TRUE, stop_if_nan = TRUE, save_weights = TRUE, callbacks = list(), save_fun = NULL, seed = seq_len(n_ensemble), tf_seeds = seq_len(n_ensemble), ... )
trafoensemble( formula, data, n_ensemble = 5, verbose = FALSE, print_members = TRUE, stop_if_nan = TRUE, save_weights = TRUE, callbacks = list(), save_fun = NULL, seed = seq_len(n_ensemble), tf_seeds = seq_len(n_ensemble), ... )
formula |
Formula specifying the response, interaction, shift terms
as |
data |
Named |
n_ensemble |
Numeric; number of ensemble members to fit. |
verbose |
Logical; whether to print training in each fold. |
print_members |
Logical; print results for each member. |
stop_if_nan |
Logical; whether to stop ensembling if |
save_weights |
Logical; whether to save the ensemble weights. |
callbacks |
List; callbacks used for fitting. |
save_fun |
Function; function to be applied to each member to be stored in the final result. |
seed |
Numeric vector of length |
tf_seeds |
Numeric vector of length |
... |
Further arguments passed to |
Ensemble of "deeptrafo"
models with list of training histories
and fitted weights included in ensemble_results
. For details see
the return statment in ensemble
.
Tune and evaluate weighted transformation ensembles
weighted_logLik( object, weights = NULL, newdata = NULL, convert_fun = function(x, ...) mean(x, ...), batch_size = NULL, ... )
weighted_logLik( object, weights = NULL, newdata = NULL, convert_fun = function(x, ...) mean(x, ...), batch_size = NULL, ... )
object |
Object of class |
weights |
Numeric; weight-vector of length |
newdata |
List or data.frame; new data to evaluate or tune the weights on |
convert_fun |
Function; applied to the log-likelihood values of all observations. |
batch_size |
Integer; optional, useful if data is too large. |
... |
Further arguments supplied to |
Returns list of ensemble members, average, and ensemble
log-likelihood converted by convert_fun