| Title: | Discretization and Grouping for Logistic Regression |
|---|---|
| Description: | A Stochastic-Expectation-Maximization (SEM) algorithm (Celeux et al. (1995) <https://hal.inria.fr/inria-00074164>) associated with a Gibbs sampler which purpose is to learn a constrained representation for logistic regression that is called quantization (Ehrhardt et al. (2019) <arXiv:1903.08920>). Continuous features are discretized and categorical features' values are grouped to produce a better logistic regression model. Pairwise interactions between quantized features are dynamically added to the model through a Metropolis-Hastings algorithm (Hastings, W. K. (1970) <doi:10.1093/biomet/57.1.97>). |
| Authors: | Adrien Ehrhardt [aut, cre], Vincent Vandewalle [aut], Christophe Biernacki [ctb], Philippe Heinrich [ctb] |
| Maintainer: | Adrien Ehrhardt <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 0.6 |
| Built: | 2026-11-14 10:31:28 UTC |
| Source: | https://github.com/cran/glmdisc |
The glmdisc package provides two important functions: glmdisc and its associate method discretize.
glmdisc functionThe glmdisc function discretizes a training set using an SEM-Gibbs based method.
discretize functionThe discretize function will discretize a new input dataset given a discretization scheme of S4 class glmdisc.
cutpoints functionThe cutpoints function will provide the cutpoints / groupings of a discretization scheme of S4 class glmdisc in a list.
predict functionThe predict function will discretize a raw test set, given a provided discretization scheme of S4 class glmdisc, using the discretize function and return the predicted probabilities.
We provide as well the classical show, print, summary functions, as well as normalizedGini that is used to calculate the Gini index (a classical Credit Scoring goodness-of-fit indicator).
Adrien Ehrhardt.
This defines the generic method "cutpoints" which will provide the cutpoints of a discretization scheme of S4 class glmdisc.
This defines the method to provide the cutpoints of a trained glmdisc.
cutpoints(object) ## S4 method for signature 'glmdisc' cutpoints(object)cutpoints(object) ## S4 method for signature 'glmdisc' cutpoints(object)
object |
generic glmdisc object |
glmdisc |
The trained glmdisc S4 object. |
Adrien Ehrhardt.
# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) cutpoints(sem_disc)# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) cutpoints(sem_disc)
This function discretizes a user-provided test dataset given a discretization scheme provided by an S4 glmdisc object.
It then applies the learnt logistic regression model and outputs its prediction (see predict.glm).
This defines the method "discretize" which will discretize a new input dataset given a discretization scheme of S4 class glmdisc
discretize(object, data) ## S4 method for signature 'glmdisc' discretize(object, data)discretize(object, data) ## S4 method for signature 'glmdisc' discretize(object, data)
object |
glmdisc object |
data |
the data to discretize according to the provided discretization scheme |
Adrien Ehrhardt.
# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) discretize(sem_disc, data.frame(x))# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) discretize(sem_disc, data.frame(x))
This function discretizes a training set using an SEM-Gibbs based method (see References section).
It detects numerical features of the dataset and discretizes them ; values of categorical features (of type factor) are regrouped. This is done in a multivariate supervised way. Assessment of the correct model is done via AIC, BIC or test set error (see parameter criterion).
Second-order interactions can be searched through the optional interaction parameter using a Metropolis-Hastings algorithm (see References section).
glmdisc( predictors, labels, interact = TRUE, validation = TRUE, test = TRUE, criterion = "gini", iter = 1000, m_start = 20, reg_type = "poly", proportions = c(0.2, 0.2) )glmdisc( predictors, labels, interact = TRUE, validation = TRUE, test = TRUE, criterion = "gini", iter = 1000, m_start = 20, reg_type = "poly", proportions = c(0.2, 0.2) )
predictors |
The matrix array containing the numerical or factor attributes to discretize. |
labels |
The actual labels of the provided predictors (0/1). |
interact |
Boolean : True (default) if interaction detection is wanted (Warning: may be very memory/time-consuming). |
validation |
Boolean : True if the algorithm should use predictors to construct a validation set on which to search for the best discretization scheme using the provided criterion (default: TRUE). |
test |
Boolean : True if the algorithm should use predictors to construct a test set on which to calculate the provided criterion using the best discretization scheme (chosen thanks to the provided criterion on either the test set (if true) or the training set (otherwise)) (default: TRUE). |
criterion |
The criterion ('gini','aic','bic') to use to choose the best discretization scheme among the generated ones (default: 'gini'). Nota Bene: it is best to use 'gini' only when test is set to TRUE and 'aic' or 'bic' when it is not. When using 'aic' or 'bic' with a test set, the likelihood is returned as there is no need to penalize for generalization purposes. |
iter |
The number of iterations to do in the SEM protocole (default: 1000). |
m_start |
The maximum number of resulting categories for each variable wanted (default: 20). |
reg_type |
The model to use between discretized and continuous features (currently, only multinomial logistic regression ('poly') and ordered logistic regression ('polr') are supported ; default: 'poly'). WARNING: 'poly' requires the |
proportions |
The list of the proportions wanted for test and validation set. Not used when both test and validation are false. Only the first is used when there is only one of either test or validation that is set to TRUE. Produces an error when the sum is greater to one. Default: list(0.2,0.2) so that the training set has 0.6 of the input observations. |
This function finds the most appropriate discretization scheme for logistic regression. When provided with a continuous variable , it tries to convert it to a categorical variable which values uniquely correspond to intervals of the continuous variable .
When provided with a categorical variable , it tries to find the best regroupement of its values and subsequently creates categorical variable . The goal is to perform supervised learning with logistic regression so that you have to specify a target variable denoted by labels.
The ‘‘discretization” process, i.e. the transformation of to is done so as to achieve the best logistic regression model . It can be interpreted as a special case feature engineering algorithm.
Subsequently, its outputs are: the optimal discretization scheme and the logistic regression model associated with it. We also provide the parameters that were provided to the function and the evolution of the criterion with respect to the algorithm's iterations.
Adrien Ehrhardt.
Celeux, G., Chauveau, D., Diebolt, J. (1995), On Stochastic Versions of the EM Algorithm. [Research Report] RR-2514, INRIA. 1995. <inria-00074164>
Agresti, A. (2002) Categorical Data. Second edition. Wiley.
# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) print(sem_disc)# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) print(sem_disc)
Class glmdisc represents a discretization scheme associated with its optimal logistic regression model.
parametersThe parameters associated with the method.
best.discThe best discretization scheme found by the method given its parameters.
performanceThe performance obtained with the method given its parameters.
disc.dataThe discretized data: test set if test is TRUE; if test is FALSE and validation is TRUE, then it provides the discretized validation set. Otherwise, it provides the discretized training set.
disc.dataThe continuous data: test set if test is TRUE; if test is FALSE and validation is TRUE, then it provides the discretized validation set. Otherwise, it provides the discretized training set.
This function calculates the Gini index of a classification rule outputting probabilities. It is a classical metric in the context of Credit Scoring. It is equal to 2 times the AUC (Area Under ROC Curve) minus 1.
normalizedGini(actual, predicted)normalizedGini(actual, predicted)
actual |
The numeric binary vector of the actual labels observed. |
predicted |
The vector of the probabilities predicted by the classification rule. |
Adrien Ehrhardt
normalizedGini(c(1, 1, 1, 0, 0), c(0.7, 0.9, 0.5, 0.6, 0.3))normalizedGini(c(1, 1, 1, 0, 0), c(0.7, 0.9, 0.5, 0.6, 0.3))
This defines the plot method which will plot some useful graphs for the discretization scheme of S4 class glmdisc
plot.glmdisc(x) ## S4 method for signature 'glmdisc,missing' plot(x)plot.glmdisc(x) ## S4 method for signature 'glmdisc,missing' plot(x)
x |
The S4 |
# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) plot(sem_disc)# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) plot(sem_disc)
This defines the method "discretize" which will discretize a new input dataset given a discretization scheme of S4 class glmdisc
This defines the method "predict" which will predict the discretization of a new input dataset given a discretization scheme of S4 class glmdisc
predict(object, ...) predict.glmdisc(object, predictors) ## S4 method for signature 'glmdisc' predict(object, predictors)predict(object, ...) predict.glmdisc(object, predictors) ## S4 method for signature 'glmdisc' predict(object, predictors)
object |
The S4 discretization object. |
... |
Essai |
predictors |
The test dataframe to discretize and for which we wish to have predictions. |
This function discretizes a user-provided test dataset given a discretization scheme provided by an S4 "glmdisc" object.
It then applies the learnt logistic regression model and outputs its prediction (see predict.glm).
This function discretizes a user-provided test dataset given a discretization scheme provided by an S4 "glmdisc" object.
It then applies the learnt logistic regression model and outputs its prediction (see predict.glm).
# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) predict(sem_disc, data.frame(x))# Simulation of a discretized logit model set.seed(1) x <- matrix(runif(300), nrow = 100, ncol = 3) cuts <- seq(0, 1, length.out = 4) xd <- apply(x, 2, function(col) as.numeric(cut(col, cuts))) theta <- t(matrix(c(0, 0, 0, 2, 2, 2, -2, -2, -2), ncol = 3, nrow = 3)) log_odd <- rowSums(t(sapply(seq_along(xd[, 1]), function(row_id) { sapply( seq_along(xd[row_id, ]), function(element) theta[xd[row_id, element], element] ) }))) y <- rbinom(100, 1, 1 / (1 + exp(-log_odd))) sem_disc <- glmdisc(x, y, iter = 50, m_start = 4, test = FALSE, validation = FALSE, criterion = "aic" ) predict(sem_disc, data.frame(x))
This function returns a numeric vector containing the probability of each observation of being of class 1 given a vector of logistic regression parameters (usually estimated through RCpp::fast_LR).
predictlogisticRegression(test, parameters)predictlogisticRegression(test, parameters)
test |
A matrix containing test data |
parameters |
A vector containing the logistic regression parameters |