Package {midnight}


Type: Package
Title: A 'tidymodels' Engine and Other Extensions for the 'midr' Package
Version: 0.3.0
Description: Provides a 'parsnip' engine for the 'midr' package, enabling users to fit, tune, and evaluate Maximum Interpretation Decomposition (MID) models within the 'tidymodels' framework. Developed through research by the Moonlight Seminar 2025, a study group of actuaries from the Institute of Actuaries of Japan, to enhance practical applications of interpretable modeling. Detailed methodology is available in Asashiba et al. (2025) <doi:10.48550/arXiv.2506.08338>.
License: MIT + file LICENSE
Encoding: UTF-8
Imports: Rcpp, stats, utils
Suggests: midr (≥ 0.6.0), parsnip, rlang, shapviz, testthat (≥ 3.0.0)
RoxygenNote: 7.3.2
URL: https://github.com/ryo-asashi/midnight, https://ryo-asashi.github.io/midnight/
BugReports: https://github.com/ryo-asashi/midnight/issues
LinkingTo: Rcpp, RcppEigen
Config/Needs/website: rmarkdown
Config/testthat/edition: 3
NeedsCompilation: yes
Packaged: 2026-11-29 03:08:48 UTC; daysb
Author: Ryoichi Asashiba ORCID iD [aut, cre]
Maintainer: Ryoichi Asashiba <ryoichi.asashiba@gmail.com>
Repository: CRAN
Date/Publication: 2026-11-29 04:00:02 UTC

midnight: A 'tidymodels' Engine and Other Extensions for the 'midr' Package

Description

logo

Provides a 'parsnip' engine for the 'midr' package, enabling users to fit, tune, and evaluate Maximum Interpretation Decomposition (MID) models within the 'tidymodels' framework. Developed through research by the Moonlight Seminar 2025, a study group of actuaries from the Institute of Actuaries of Japan, to enhance practical applications of interpretable modeling. Detailed methodology is available in Asashiba et al. (2025) doi:10.48550/arXiv.2506.08338.

Author(s)

Maintainer: Ryoichi Asashiba ryoichi.asashiba@gmail.com (ORCID)

See Also

Useful links:


Fit Multivariate Linear Models

Description

fastLmMatrix() estimates the linear model for multivariate response using one of several methods implemented using the Eigen linear algebra library.

Usage

fastLmMatrix(x, ...)

## Default S3 method:
fastLmMatrix(x, y, tol = 1e-07, method = 0L, ...)

## S3 method for class 'formula'
fastLmMatrix(formula, data = list(), method = 0L, ...)

Arguments

x

a model matrix X.

...

optional parameters passed to methods.

y

the response matrix Y.

tol

tolerance for the rank calculation.

method

an integer with value 0 for the column-pivoted QR decomposition, 1 for the unpivoted QR decomposition, 2 for the LLT Cholesky, 3 for the LDLT Cholesky, and 4 for the Jacobi singular value decomposition (SVD). Default is zero.

formula

an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted.

data

an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model.

Details

fastLmMatrix() is a performance-optimized version of the standard lm.fit() function. Unlike RcppEigen::fastLm(), it is specifically designed to handle multivariate responses (Y as a matrix). It leverages the Eigen C++ template library for high-performance linear algebra, providing several decomposition methods with different trade-offs between speed and numerical stability.

Value

fastLmMatrix() returns a list with the following components:

coefficients

p \times k matrix of coefficients.

fitted.values

n \times k matrix of fitted values.

residuals

n \times k matrix of residuals.

rank

an integer giving the numeric rank of the model matrix X.

See Also

lm.fit, fastLm

Examples

# Matrix interface with a multivariate response matrix Y
data(iris, package = "datasets")
X <- model.matrix(~ Petal.Length + Petal.Width, data = iris)
Y <- as.matrix(iris[, c("Sepal.Length", "Sepal.Width")])
fit <- fastLmMatrix(X, Y)
coef(fit)

# Formula interface using SVD (method = 4L)
fit_svd <- fastLmMatrix(
  cbind(Sepal.Length, Sepal.Width) ~ Petal.Length + Petal.Width,
  data = iris,
  method = 4L
)
coef(fit_svd)

Internal Fitting Function for MID Regression

Description

fit_mid_reg() is an internal helper function called by parsnip to fit a mid_reg() model using midr::interpret().

Usage

fit_mid_reg(
  formula,
  data,
  weights = NULL,
  model = NULL,
  params_main = NULL,
  params_inter = NULL,
  penalty = NULL,
  terms = NULL,
  ...
)

Arguments

formula

an object of class formula: a symbolic description of the model to be fitted.

data

a data frame containing the variables in the model.

weights

an optional vector of case weights.

model

an optional fitted model object (black-box model) to be interpreted. Default is NULL.

params_main

an integer specifying the maximum number of sample points (knots) to model main effects. Corresponds to the argument k[1] in midr::interpret().

params_inter

an integer specifying the maximum number of sample points (knots) to model interaction effects. Corresponds to the argument k[2] or k2 in midr::interpret().

penalty

a non-negative number representing the total amount of regularization. Corresponds to the argument lambda in midr::interpret().

terms

a character vector or formula of term labels to be included in the fitting process.

...

other arguments to be passed on to midr::interpret().

Value

A fitted "mid" object returned by midr::interpret().


MID Regression

Description

mid_reg() defines a model that can predict numeric values from predictors using a set of functions each with up to two predictors. This function fits a regression model based on the Maximum Interpretation Decomposition.

Usage

mid_reg(
  mode = "regression",
  engine = "midr",
  model = NULL,
  params_main = NULL,
  params_inter = NULL,
  penalty = NULL,
  terms = NULL
)

Arguments

mode

a single character string for the type of model. Currently, only "regression" is supported.

engine

a single character string specifying the computational engine to use. The default is "midr".

model

an optional fitted model object (black-box model) to be interpreted. Default is NULL.

params_main

an integer specifying the maximum number of sample points (knots) to model main effects. Corresponds to the argument k[1] in midr::interpret().

params_inter

an integer specifying the maximum number of sample points (knots) to model interaction effects. Corresponds to the argument k[2] or k2 in midr::interpret().

penalty

a non-negative number representing the total amount of regularization. Corresponds to the argument lambda in midr::interpret().

terms

a character vector or formula of term labels to be included in the fitting process.

Details

This function is the main specification for the parsnip model.

Value

a "model_spec" object with class "mid_reg".

See Also

interpret


Calculate MID-Derived Shapley Values

Description

shapviz.mid() is an S3 method for the shapviz::shapviz() generic, which calculates MID-derived Shapley values from a fitted MID model. This method is dynamically registered when the shapviz package is loaded.

Usage

## S3 method for class 'mid'
shapviz(object, data = NULL)

## S3 method for class 'mids'
shapviz(object, data = NULL)

Arguments

object

a "mid" object.

data

a data frame containing the observations for which to calculate MID-derived Shapley values.

Details

The function calculates MID-derived Shapley values by attributing the contribution of each component function to its respective variables as follows: first, each main effect is fully attributed to its corresponding variable; and then, each second-order interaction effect is split equally between the two variables involved.

Value

An object of class "shapviz".