Nothing
#' Poisson lognormal model towards Linear Discriminant Analysis
#'
#' Fit the Poisson lognormal for LDA with a variational algorithm. Use the (g)lm syntax for model specification (covariates, offsets).
#'
#' @inheritParams PLN formula data subset weights
#' @param grouping a factor specifying the class of each observation used for discriminant analysis.
#' @param control a list-like structure for controlling the optimization, with default generated by [PLNLDA_param()]. See the associated documentation.
#'
#' @return an R6 object with class [PLNLDAfit()]
#'
#' @details See [PLNLDA_param()] for a full description of the optimization parameters.
#' Note that unlike [PLN_param()], [PLNLDA_param()] does not expose the `"fixed"` covariance option or the `Omega` parameter, which are not meaningful in the LDA context.
#'
#' @rdname PLNLDA
#' @examples
#' data(trichoptera)
#' trichoptera <- prepare_data(trichoptera$Abundance, trichoptera$Covariate)
#' myPLNLDA <- PLNLDA(Abundance ~ 1, grouping = Group, data = trichoptera)
#' @seealso The class [`PLNLDAfit`]
#' @importFrom stats model.frame model.matrix model.response model.offset
#' @export
PLNLDA <- function(formula, data, subset, weights, grouping, control = PLNLDA_param()) {
## Temporary test for deprecated use of list()
if (!inherits(control, "PLNmodels_param"))
stop("We now use the function PLNLDA_param() to generate the list of parameters that controls the fit:
replace 'list(my_arg = xx)' by PLNLDA_param(my_arg = xx) and see the documentation of PLNLDA_param().")
## look for grouping in the data or the parent frame
if (inherits(try(eval(grouping), silent = TRUE), "try-error")) {
grouping <- try(eval(substitute(grouping), data), silent = TRUE)
if (inherits(grouping, "try-error")) stop("invalid grouping")
}
grouping <- as.factor(grouping)
# force the intercept term if excluded (prevent interference with group means when coding discrete variables)
the_call <- match.call(expand.dots = FALSE)
the_call$formula <- update.formula(formula(the_call), ~ . +1)
## extract the data matrices and weight and remove the intercept
args <- extract_model(the_call, parent.frame())
args$X <- args$X[ , colnames(args$X) != "(Intercept)", drop = FALSE]
## Initialize LDA with appropriate covariance model
myLDA <- switch(control$covariance,
"spherical" = PLNLDAfit_spherical$new(grouping, args$Y, args$X, args$O, args$w, args$formula, control),
"diagonal" = PLNLDAfit_diagonal$new(grouping, args$Y, args$X, args$O, args$w, args$formula, control),
PLNLDAfit$new(grouping, args$Y, args$X, args$O, args$w, args$formula, control))
## Compute the group means
if (control$trace > 0) cat("\n Performing discriminant Analysis...")
myLDA$optimize(grouping, args$Y, args$X, args$O, args$w, control$config_optim)
## Post-treatment: prepare LDA visualization
myLDA$postTreatment(grouping, args$Y, args$X, args$O, args$w, control$config_post, control$config_optim)
if (control$trace > 0) cat("\n DONE!\n")
myLDA
}
#' Control of a PLNLDA fit
#'
#' Helper to define list of parameters to control the PLNLDA fit. All arguments have defaults.
#'
#' @param backend optimization back used, either "nlopt" or "torch". Default is "nlopt"
#' @param covariance character setting the model for the covariance matrix. Either "full", "diagonal" or "spherical". Default is "full".
#' @inheritParams PLN_param trace config_optim config_post inception
#'
#' @return list of parameters configuring the fit.
#' @inherit PLN_param details
#' @export
PLNLDA_param <- function(
backend = c("builtin", "nlopt", "torch"),
trace = 1,
covariance = c("full", "diagonal", "spherical"),
config_post = list(),
config_optim = list(),
inception = NULL # pretrained PLNfit used as initialization
) {
backend <- match.arg(backend)
covariance <- match.arg(covariance)
if (!is.null(inception)) stopifnot(isPLNfit(inception))
## post-treatment config
config_pst <- config_post_default_PLNLDA
config_pst[names(config_post)] <- config_post
config_pst$trace <- trace
## optimization config
config_opt <- make_config_optim(backend, config_optim, trace)
structure(list(
backend = backend ,
trace = trace ,
covariance = covariance,
config_post = config_pst,
config_optim = config_opt,
inception = inception), class = "PLNmodels_param")
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.