predict_glmnetcr_wrapper: Predict wrappers for 'glmnetcr'

View source: R/ordinal_reg-glmnetcr.R

predict_glmnetcr_wrapperR Documentation

Predict wrappers for glmnetcr

Description

Select predictions at a specific penalty value or a vector of penalty values. For penalties between path values, linearly interpolate between the probability matrices at the neighboring steps. For penalties outside the path range, use the path endpoints.

Usage

predict_glmnetcr_wrapper(
  object,
  newx,
  type,
  lambda,
  criteria = c("bic", "aic")
)

multi_predict_glmnetcr_wrapper(object, newx, type, lambda)

## S3 method for class ''_glmnetcr''
predict(
  object,
  new_data,
  type = NULL,
  opts = list(),
  penalty = NULL,
  multi = FALSE,
  ...
)

## S3 method for class ''_glmnetcr''
predict_class(object, new_data, ...)

## S3 method for class ''_glmnetcr''
predict_classprob(object, new_data, ...)

## S3 method for class ''_glmnetcr''
multi_predict(
  object,
  new_data,
  type = NULL,
  opts = list(),
  penalty = NULL,
  ...
)

Arguments

object

A glmnetcr object.

newx

A predictor matrix.

type

Either "class" or "prob".

lambda

A penalty value or vector at which to predict. If NULL, the step minimizing criteria is used (single penalty only).

criteria

The criterion by which to select lambda within the path sequence. Defaults to "bic" for consistency with glmnetcr::predict.glmnetcr(). (NB: This contrasts with predict_ordinalNet_wrapper().)

penalty

A numeric vector of penalty values. Overrides the default penalty. If NULL, the regularization path stored in the model fit is used.

Value

predict_glmnetcr_wrapper() returns a character vector of class predictions or a matrix of class probabilities. multi_predict_glmnetcr_wrapper() returns a numeric array with one slice per penalty value for class probabilities, or a character matrix with one column per penalty value for class predictions. Both outputs mimic those of glmnetcr::predict.glmnetcr().

Examples


house_data <-
  MASS::housing[rep(seq(nrow(MASS::housing)), MASS::housing$Freq), -5]
house_matrix <- model.matrix(
  Sat ~ Type + Infl + Cont + 0,
  data = house_data,
  contrasts.arg = lapply(house_data[, 2:4], contrasts, contrasts = FALSE)
)
pen_vec <- 10 ^ seq(-1, -2, -.25)
fit <- glmnetcr::glmnetcr(
  house_matrix, y = house_data$Sat,
  lambda = pen_vec
)
# predictions (may disagree if `lambda` is not on the path `fit$lambda`)
set.seed(34091)
house_submatrix <- house_matrix[sample(nrow(house_matrix), 6), ]
# single, probability
predict(
  fit,
  newx = house_submatrix
)$probs[, , which.min(abs(fit$lambda - .02))]
predict_glmnetcr_wrapper(
  fit,
  newx = house_submatrix,
  type = "prob",
  lambda = .02
)
# single, class
predict(
  fit,
  newx = house_submatrix
)$class[, which.min(abs(fit$lambda - .02))]
predict_glmnetcr_wrapper(
  fit,
  newx = house_submatrix,
  type = "class",
  lambda = .02
)
# multiple, probabilities
predict(
  fit,
  newx = house_submatrix
)$probs
multi_predict_glmnetcr_wrapper(
  fit,
  newx = house_submatrix,
  type = "prob",
  lambda = c(.2, .02, .002)
)
# multiple, class
predict(
  fit,
  newx = house_submatrix
)$class
multi_predict_glmnetcr_wrapper(
  fit,
  newx = house_submatrix,
  type = "class",
  lambda = c(.2, .02, .002)
)


ordered documentation built on Nov. 23, 2026, 9:08 a.m.