Title: | A Toolbox for Linear Discriminant Analysis with Penalties |
---|---|
Description: | Integrates several popular high-dimensional methods based on Linear Discriminant Analysis (LDA) and provides a comprehensive and user-friendly toolbox for linear, semi-parametric and tensor-variate classification as mentioned in Yuqing Pan, Qing Mai and Xin Zhang (2019) <arXiv:1904.03469>. Functions are included for covariate adjustment, model fitting, cross validation and prediction. |
Authors: | Yuqing Pan <[email protected]>, Qing Mai <[email protected]>, Xin Zhang <[email protected]> |
Maintainer: | Yuqing Pan <[email protected]> |
License: | GPL-2 |
Version: | 1.0.2 |
Built: | 2024-11-23 06:37:40 UTC |
Source: | CRAN |
Adjusts tensor with respect to covariates to achieve a more accurate performance. Tensor depends on the covariates through a linear regression model. The function returns the coefficients of covariates in regression and adjusted tensor list for further classifier modeling. It estimates coefficients based on training data, and then adjusts training tensor. When testing data is provided, the function will automatically adjust testing data by learned coefficients as well.
adjten(x, z, y, testx = NULL, testz = NULL, is.centered = FALSE)
adjten(x, z, y, testx = NULL, testz = NULL, is.centered = FALSE)
x |
Input tensor or matrix list of length |
z |
Input covariate matrix of dimension |
y |
Class label vector of dimention |
testx |
Input testing tensor or matrix list. Each element of the list is a test case. When |
testz |
Input testing covariate matrix with each row being an observation. |
is.centered |
Indicates whether the input tensor and covariates have already been centered by their within class mean or not. If |
The model CATCH assumes the linear relationship bewteen covariates and tensor as
where is the matrix of estimated coefficient of covariates.
The function removes the effects of covariates on response variable through tensor and obtain
as adjusted tensor to fit tensor discriminant analysis model.
In estimating , which is the
alpha
in the package, adjten
first centers both tensor and covariates within their individual classes, then performs tensor response regression which regresses on
.
gamma |
The estimated coefficients of covariates to plug in classifier. |
xres |
Adjusted training tensor list |
testxres |
Adjusted testing tensor list |
Yuqing Pan, Qing Mai, Xin Zhang
Pan, Y., Mai, Q., and Zhang, X. (2018), "Covariate-Adjusted Tensor Classification in High-Dimensions." Journal of the American Statistical Association, accepted.
n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars),nrow=n,ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2),nrow=n,ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,],dim=c(p,p,p)) } obj <- adjten(x, z, y)
n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars),nrow=n,ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2),nrow=n,ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,],dim=c(p,p,p)) } obj <- adjten(x, z, y)
Adjusts vector with respect to covariates. Vector depends on the covariates through a linear regression model. The function returns the coefficients of covariates in regression and adjusted predictor matrix for further classifier modeling. It estimates coefficients based on training data, and then adjusts training tensor. When testing data is provided, the function will automatically adjust testing data by learned coefficients as well.
adjvec(x, z, y, testx = NULL, testz = NULL, is.centered = FALSE)
adjvec(x, z, y, testx = NULL, testz = NULL, is.centered = FALSE)
x |
Input matrix of dimension |
z |
Input covariate matrix of dimension |
y |
Class label vector of dimention |
testx |
Input testing matrix. Each row is a test case. When |
testz |
Input testing covariate matrix with each row being an observation. |
is.centered |
Indicates whether the input vector and covariates have already been centered by their within class mean or not. If |
Similar as CATCH model, assume the linear relationship between vector predictors and covariates as
where is a
matrix and
is the matrix of estimated coefficient of covariates.
The function removes the effects of covariates on response variable through vector and obtain
as adjusted predictors to fit MSDA and DSDA model.
gamma |
The estimated coefficients of covariates to plug in classifier. |
xres |
Adjusted training predictor matrix |
testxres |
Adjusted testing predictor matrix |
Yuqing Pan, Qing Mai, Xin Zhang
Pan, Y., Mai, Q., and Zhang, X. (2018), "Covariate-Adjusted Tensor Classification in High-Dimensions." Journal of the American Statistical Association, accepted.
n <- 50 p <- 200 k <- 2 q <- 2 x <- matrix(rnorm(n*p), n, p) z <- matrix(rnorm(n*q), n, q) x[1:20, ] <- x[1:20, ] + 2 z[1:20, ] <- z[1:20, ] + 0.5 y <- c(rep(1, 20), rep(2, 30)) obj <- adjvec(x, z, y)
n <- 50 p <- 200 k <- 2 q <- 2 x <- matrix(rnorm(n*p), n, p) z <- matrix(rnorm(n*q), n, q) x[1:20, ] <- x[1:20, ] + 2 z[1:20, ] <- z[1:20, ] + 0.5 y <- c(rep(1, 20), rep(2, 30)) obj <- adjvec(x, z, y)
The catch
function solves classification problems and selects variables by fitting a covariate-adjusted tensor classification in high-dimensions (CATCH) model. The input training predictors include two parts: tensor data and low dimensional covariates. The tensor data could be matrix as a special case of tensor. In catch
, tensor data should be stored in a list form. If the dataset contains no covariate, catch
can also fit a classifier only based on the tensor predictors. If covariates are provided, the method will adjust tensor for covariates and then fit a classifier based on the adjusted tensor along with the covariates. If users specify testing data at the same time, predicted response will be obtained as well.
catch(x, z = NULL, y, testx = NULL, testz = NULL, nlambda = 100, lambda.factor = ifelse((nobs - nclass) <= nvars, 0.2, 1E-03), lambda = NULL,dfmax = nobs, pmax = min(dfmax * 2 + 20, nvars), pf = rep(1, nvars), eps = 1e-04, maxit = 1e+05, sml = 1e-06, verbose = FALSE, perturb = NULL)
catch(x, z = NULL, y, testx = NULL, testz = NULL, nlambda = 100, lambda.factor = ifelse((nobs - nclass) <= nvars, 0.2, 1E-03), lambda = NULL,dfmax = nobs, pmax = min(dfmax * 2 + 20, nvars), pf = rep(1, nvars), eps = 1e-04, maxit = 1e+05, sml = 1e-06, verbose = FALSE, perturb = NULL)
x |
Input tensor (or matrix) list of length |
z |
Input covariate matrix of dimension |
y |
Class label. For |
testx |
Input testing tensor or matrix list. Each element of the list is a test case. When |
testz |
Input testing covariate matrix. Can be omitted if covariate is absent. However, training covariates |
nlambda |
The number of tuning values in sequence |
lambda.factor |
When |
lambda |
A sequence of user-specified |
dfmax |
The maximum number of selected variables in the model. Default is the number of observations |
pmax |
The maximum number of potential selected variables during iteration. In middle step, the algorithm can select at most |
pf |
Weight of lasso penalty. Default is a vector of value |
eps |
Convergence threshold for coordinate descent difference between iterations. Default value is |
maxit |
Maximum iteration times for all lambda. Default value is |
sml |
Threshold for ratio of loss function change after each iteration to old loss function value. Default value is |
verbose |
Indicates whether print out lambda during iteration or not. Default value is |
perturb |
Perturbation scaler. If it is specified, the value will be added to diagonal of estimated covariance matrix. A small value can be used to accelerate iteration. Default value is |
The catch
function fits a linear discriminant analysis model as follows:
The categorical response is predicted from the estimated Bayes rule:
where is the tensor,
is the covariates,
,
and
are parameters estimated by CATCH. A detailed explanation can be found in reference. When
Z
is not NULL
, the function will first adjust tensor on covariates by modeling
where is an unobservable tensor normal error independent of
.
Then
catch
fits model on the adjusted training tensor and makes predictions on testing data by using the adjusted tensor list. If
Z
is NULL
, it reduces to a simple tensor discriminant analysis model.
The coefficient of tensor , represented by
beta
in package, is estimated by
When response is multi-class, the group lasso penalty over categories is added to objective function through parameter lambda
, and it reduces to a lasso penalty in binary problems.
The function catch
will predict categorical response when testing data is provided.
If testing data is not provided or if one wishes to perform prediction separately, catch
can be used to only fit model with a catch object outcome. The object outcome can be combined with the adjusted tensor list from adjten
to perform prediction by predict.catch
.
beta |
Output variable coefficients for each |
df |
The number of nonzero variables for each value in sequence |
dim |
Dimension of coefficient array. |
lambda |
The actual |
obj |
Objective function value for each value in sequence |
x |
The tensor list after adjustment in training data. If covariate is absent, this is the original input tensor list. |
y |
Class label in training data. |
npasses |
Total number of iterations. |
jerr |
Error flag. |
sigma |
Estimated covariance matrix on each mode. |
delta |
Estimated delta matrix |
mu |
Estimated mean array of |
prior |
Proportion of samples in each class. |
call |
The call that produces this object. |
pred |
Predicted categorical response for each value in sequence |
Yuqing Pan, Qing Mai, Xin Zhang
Pan, Y., Mai, Q., and Zhang, X. (2018), "Covariate-Adjusted Tensor Classification in High-Dimensions." Journal of the American Statistical Association, accepted.
cv.catch
, predict.catch
, adjten
#without prediction n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars), nrow=n, ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2), nrow=n, ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,],dim=c(p,p,p)) } obj <- catch(x,z,y=y)
#without prediction n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars), nrow=n, ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2), nrow=n, ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,],dim=c(p,p,p)) } obj <- catch(x,z,y=y)
Fits a classifier for matrix data. catch_matrix
is a special case of catch
when each observation is a matrix. Different from
catch
takes list as input, data need to be formed in an array to call the function (see arguments). The function will perform prediction as well.
catch_matrix(x, z = NULL, y, testx = NULL, testz = NULL, ...)
catch_matrix(x, z = NULL, y, testx = NULL, testz = NULL, ...)
x |
Input matrix array. The array should be organized with dimension |
z |
Input covariate matrix of dimension |
y |
Class label. For |
testx |
Input testing matrix array. When |
testz |
Input testing covariate matrix. Can be omitted if there is no covariate. |
... |
Other arguments that can be passed to |
The function fits a matrix classifier as a special case of catch
. The fitted model and predictions should be identical to catch
when matrix data is provided. Input matrix should be organized as three-way array where sample size is the last dimension. If the matrix is organized in a list, users can either reorganize it or use catch
directly to fit model, which takes a matrix or tensor list as input and has the same output as catch_matrix
.
beta |
Output variable coefficients for each |
df |
The number of nonzero variables for each value in sequence |
dim |
Dimension of coefficient array. |
lambda |
The actual |
obj |
Objective function value for each value in sequence |
x |
The matrix list after adjustment in training data. If covariate is absent, this is the original input matrix. |
y |
Class label in training data. |
npasses |
Total number of iterations. |
jerr |
Error flag. |
sigma |
Estimated covariance matrix on each mode. |
delta |
Estimated delta matrix |
mu |
Estimated mean array. |
prior |
Prior proportion of observations in each class. |
call |
The call that produces this object. |
pred |
Predicted categorical response for each value in sequence |
Yuqing Pan, Qing Mai, Xin Zhang
Pan, Y., Mai, Q., and Zhang, X. (2018), "Covariate-Adjusted Tensor Classification in High-Dimensions." Journal of the American Statistical Association, accepted.
#without prediction n <- 20 p <- 4 k <- 2 nvars <- p*p x=array(rnorm(n*nvars),dim=c(p,p,n)) x[,,11:20]=x[,,11:20]+0.3 z <- matrix(rnorm(n*2), nrow=n, ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) obj <- catch_matrix(x,z,y=y)
#without prediction n <- 20 p <- 4 k <- 2 nvars <- p*p x=array(rnorm(n*nvars),dim=c(p,p,n)) x[,,11:20]=x[,,11:20]+0.3 z <- matrix(rnorm(n*2), nrow=n, ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) obj <- catch_matrix(x,z,y=y)
A dataset collected from a series of CSA experiments to identify volatile chemical toxicants (VCT). Chemical dyes were exposed to VCT under different concentration conditions and colors of dyes were recorded to identify the class of VCT. There are two concentration conditions PEL (permissible exposure level) and IDLH (immediately dangerous to life of health).
data(csa)
data(csa)
Two lists, PEL and IDLH, and a numeric vector y. Each list contains 147 matrices of dimension .
PEL
A list of matrices containing the observations after exposure at PEL.
IDLH
A list of matrices containing the observations after exposure at IDLH level.
y
Class label ranging from 1 to 21.
This dataset is provided in the Supplementary matrial of Zhong (2015). In each concentration case, there are 147 observations and 21 classes. We reorganize the data into a list to be directly called by catch
. For matrices in the list, each row represents a dye and the three columns correspond to red, green and blue.
Wenxuan Zhong and Kenneth S. Suslick (2015). "Matrix discriminant analysis with application to colorimetric sensor array data" Technometrics 57(4), 524–534.
Performs k-fold cross validation for CATCH and returns the best tuning parameter in the user-specified or automatically generated choices.
cv.catch(x, z = NULL, y, nfolds = 5, lambda = NULL, lambda.opt = "min",...)
cv.catch(x, z = NULL, y, nfolds = 5, lambda = NULL, lambda.opt = "min",...)
x |
Input tensor or matrix list of length |
z |
Input covariate matrix of dimension |
y |
Class label. For |
nfolds |
Number of folds. Default value is |
lambda |
User-specified |
lambda.opt |
The optimal criteria when multiple elements in |
... |
Other arguments that can be passed to |
The function cv.catch
runs function catch
nfolds+1
times. The first one fits model on all data. If lambda
is specified, it will check if all lambda
satisfies the constraints of dfmax
and pmax
in catch
. If not, a lambda
sequence will be generated according to lambda.factor
in catch
. Then the rest nfolds
many replicates will fit model on nfolds-1
many folds data and predict on the omitted fold, repectively. Return the lambda
with minimum average cross validation error and the largest lambda
within one standard error of the minimum.
lambda |
The actual |
cvm |
The mean of cross validation errors for each |
cvsd |
The standard error of cross validaiton errors for each |
lambda.min |
The |
lambda.1se |
The largest |
catch.fit |
The fitted |
Yuqing Pan, Qing Mai, Xin Zhang
Pan, Y., Mai, Q., and Zhang, X. (2018), "Covariate-Adjusted Tensor Classification in High-Dimensions." Journal of the American Statistical Association, accepted.
n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars), nrow=n, ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2),nrow=n,ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,], dim=c(p,p,p)) } objcv <- cv.catch(x, z, y=y)
n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars), nrow=n, ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2),nrow=n,ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,], dim=c(p,p,p)) } objcv <- cv.catch(x, z, y=y)
Choose the optimal lambda for direct sparse discriminant analysis by cross validation.
cv.dsda(x, y, nfolds = 5, lambda=lambda, lambda.opt="min", standardize=FALSE, alpha=1, eps=1e-7)
cv.dsda(x, y, nfolds = 5, lambda=lambda, lambda.opt="min", standardize=FALSE, alpha=1, eps=1e-7)
x |
An n by p matrix containing the predictors. |
y |
An n-dimensional vector containing the class labels. |
nfolds |
The number of folds to be used in cross validation. Default is 5. |
lambda |
A sequence of lambda's. |
lambda.opt |
Should be either "min" or "max", specifying whether the smallest or the largest lambda with the smallest cross validation error should be used for the final classification rule. |
standardize |
A logic object indicating whether x.matrix should be standardized before performing DSDA. Default is FALSE. |
alpha |
The elasticnet mixing parameter, the same as in glmnet. Default is alpha=1 so that the lasso penalty is used. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
lambda |
The sequence of lambda's used in cross validation. |
cvm |
Cross validation errors. |
cvsd |
The standard error of the cross validation errors. |
lambda.min |
The optimal lambda chosen by cross validation. |
model.fit |
The fitted model. |
Mai, Q., Zou, H. and Yuan, M. (2013). A direct approach to sparse discriminant analysis in ultra-high dimensions. Biometrika, 99, 29-42.
cv.dsda
predict.dsda
dsda
msda
Performs K-fold cross validation for msda
and returns the best tuning parameter in the user-specified or automatically generated choices.
cv.msda(x, y, model = NULL, nfolds = 5, lambda = NULL, lambda.opt = "min", ...)
cv.msda(x, y, model = NULL, nfolds = 5, lambda = NULL, lambda.opt = "min", ...)
x |
Input matrix of predictors. |
y |
Class label. For |
model |
Method type. The |
nfolds |
Number of folds. Default value is 5. Although |
lambda |
User-specified |
lambda.opt |
The optimal criteria when multiple elements in |
... |
other arguments that can be passed to |
The function cv.msda
runs function msda
nfolds+1
times. The first one fits model on all data. If lambda
is specified, it will check if all lambda
satisfies the constraints of dfmax
and pmax
in msda
. If not, a lambda
sequence will be generated according to lambda.factor
in msda
. Then the rest nfolds
many replicates will fit model on nfolds-1
many folds data and predict on the omitted fold, repectively. Return the lambda
with minimum average cross validation error and the largest lambda
within one standard error of the minimum.
Similar as msda
, user can specify which method to use by inputing argument model
. Without specification, the function can automatically decide the method by number of classes and variables.
An object of class cv.dsda
or cv.msda.original
or cv.msda.modified
is returned, which is a
list with the ingredients of the cross-validation fit.
lambda |
The actual |
cvm |
The mean of cross validation errors for each |
cvsd |
The standard error of cross validaiton errors for each |
lambda.min |
The |
lambda.1se |
The largest value of |
model.fit |
A fitted |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q., Zou, H. and Yuan, M. (2012), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrica, 99, 29-42.
Mai, Q., Yang, Y., and Zou, H. (2017), "Multiclass sparse discriminant analysis." Statistica Sinica, in press.
URL: https://github.com/emeryyi/msda
data(GDS1615) x <- GDS1615$x y <- GDS1615$y obj.cv <- cv.msda(x=x, y=y, nfolds=5, lambda.opt="max") lambda.min <- obj.cv$lambda.min obj <- msda(x=x, y=y, lambda=lambda.min) pred <- predict(obj,x)
data(GDS1615) x <- GDS1615$x y <- GDS1615$y obj.cv <- cv.msda(x=x, y=y, nfolds=5, lambda.opt="max") lambda.min <- obj.cv$lambda.min obj <- msda(x=x, y=y, lambda=lambda.min) pred <- predict(obj,x)
Choose the optimal lambda for semiparametric sparse discriminant analysis by cross validation.
cv.SeSDA(x, y, nfolds = 5, lambda=NULL, lambda.opt="min", standardize=FALSE, alpha=1, eps=1e-7)
cv.SeSDA(x, y, nfolds = 5, lambda=NULL, lambda.opt="min", standardize=FALSE, alpha=1, eps=1e-7)
x |
An n by p matrix containing the predictors. |
y |
An n-dimensional vector containing the class labels. |
nfolds |
The number of folds to be used in cross validation. Default is 5. |
lambda |
A sequence of lambda's. |
lambda.opt |
Should be either "min" or "max", specifying whether the smallest or the largest lambda with the smallest cross validation error should be used for the final classification rule. |
standardize |
A logic object indicating whether x.matrix should be standardized before performing DSDA. Default is FALSE. |
alpha |
The elasticnet mixing parameter, the same as in glmnet. Default is alpha=1 so that the lasso penalty is used. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
transform |
The transformation functions. |
objdsda |
The output of cross validation from |
Mai, Q., Zou, H. and Yuan, M. (2013). A direct approach to sparse discriminant analysis in ultra-high dimensions. Biometrika, 99, 29-42.
cv.dsda
SeSDA
Compute the solution path for direct sparse discriminant analysis (DSDA).
dsda(x, z=NULL, y, testx=NULL, testz=NULL, standardize=FALSE, lambda=lambda, alpha=1, eps=1e-7)
dsda(x, z=NULL, y, testx=NULL, testz=NULL, standardize=FALSE, lambda=lambda, alpha=1, eps=1e-7)
x |
Input matrix of predictors. |
z |
Input covariate matrix of dimension |
y |
An n-dimensional vector containing the class labels. The classes have to be labeled as 1 and 2. |
testx |
Input testing matrix. Each row is a test case. When |
testz |
Input testing covariate matrix. Can be omitted if covariate is absent. However, training covariates |
standardize |
A logic object indicating whether x should be standardized before performing DSDA. Default is FALSE. |
lambda |
A sequence of lambda's. If lambda is missed, the function will automatically generates a sequence of lambda's to fit model. |
alpha |
The elasticnet mixing parameter, the same as in glmnet. Default is alpha=1 so that the lasso penalty is used. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
beta |
Output variable coefficients for each lambda. The first element of each solution is the intercept. |
lambda |
The sequence of lambda's used in computing the solution path. |
x |
The predictor matrix in training data. |
y |
The class label in training data. |
pred |
Predicted categorical response for each value in sequence |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q., Zou, H. and Yuan, M. (2013). A direct approach to sparse discriminant analysis in ultra-high dimensions. Biometrika, 99, 29-42.
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- dsda(x, y=y)
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- dsda(x, y=y)
Performs direct sparse discriminant analysis, with the optimal lambda chosen by cross validation. The function can perform prediction on test data as well.
dsda.all(x, y, x.test.matrix=NULL, y.test=NULL, standardize=FALSE, lambda.opt="min", nfolds=10, lambda=lambda, alpha=1, eps=1e-7)
dsda.all(x, y, x.test.matrix=NULL, y.test=NULL, standardize=FALSE, lambda.opt="min", nfolds=10, lambda=lambda, alpha=1, eps=1e-7)
x |
An n by p matrix containing the predictors. |
y |
An n-dimensional vector containing the class labels 1 and 2. |
x.test.matrix |
The predictors of a testing set. (Optional.) |
y.test |
The class labels of the testing set. (Required if x.test.matrix is supplied, but otherwise optional.) |
standardize |
A logic object indicating whether x.matrix should be standardized before performing DSDA. Default is FALSE. |
lambda.opt |
Should be either "min" or "max", specifying whether the smallest or the largest lambda with the smallest cross validation error should be used for the final classification rule. |
nfolds |
The number of folds to be used in cross validation. Default is 10. |
lambda |
A sequence of lambda's. |
alpha |
The elasticnet mixing parameter, the same as in glmnet. Default is alpha=1 so that the lasso penalty is used. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
error |
Testing error if x.test.matrix is supplied. |
beta |
The coefficients of the classification rule corresponding to the optimal lambda chosen by cross validation. |
s |
The optimal lambda chosen by cross validation. |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q., Zou, H. and Yuan, M., (2012), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrika, 99, 29-42.
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] n<-length(y) ##split the original dataset to a training set and a testing set n.test<-round(n/3) set.seed(20120822) id<-sample(n,n.test,replace=FALSE) x.train<-x[-id,] x.test<-x[id,] y.train<-y[-id] y.test<-y[id] set.seed(123) ##perform direct sparse discriminant analysis obj<-dsda.all(x.train,y.train,x.test,y.test) obj$error
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] n<-length(y) ##split the original dataset to a training set and a testing set n.test<-round(n/3) set.seed(20120822) id<-sample(n,n.test,replace=FALSE) x.train<-x[-id,] x.test<-x[id,] y.train<-y[-id] y.test<-y[id] set.seed(123) ##perform direct sparse discriminant analysis obj<-dsda.all(x.train,y.train,x.test,y.test) obj$error
The dataset is a subset of the dataset available on Gene Expression Omnibus with the accession number GDS1615. The original dataset contains 22283 gene expression levels and the disease states of the observed subjects. In Mai, Yang and Zou, the dimension of the original dataset was first reduced to 127 by F-test screening.
data(GDS1615)
data(GDS1615)
This data frame contains the following:
x |
Gene expression levels. |
y |
Disease state that is coded as 1,2,3. 1: normal; 2: ulcerative colitis; 3: Crohn's disease. |
M. E. Burczynski, R. L Peterson, N. C. Twine, K. A. Zuberek, B. J. Brodeur, L. Casciotti, V. Maganti, P. S. Reddy, A. Strahs, F. Immermann, W. Spinelli, U. Schwertschlag, A. M. Slager, M. M. Cotreau, and A. J. Dorner. (2012), "Molecular classification of crohn's disease and ulcerative colitis patients using transcriptional profiles in peripheral blood mononuclear cells". Journal of Molecular Diagnostics, 8:51–61.
Mai, Q., Zou, H. and Yuan, M. (2012), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrica, 99, 29-42.
data(GDS1615)
data(GDS1615)
Transform the predictors to achieve normality.
getnorm(x, y, type="pooled")
getnorm(x, y, type="pooled")
x |
an n dimensional vector containing n observations for one predictor. |
y |
an n-dimensional vector containing the class labels. |
type |
The type of estimator. Two estimators were proposed in Mai & Zou (2015), the naive estimator and the pooled estimator. The function getnorm() uses the naive estimator if type="naive", and it uses the pooled estimator if type="pooled". The default is "pooled". When the naive estimator is used, it is recommended to label the class with more samples as Class 0. |
x.norm |
Transformed x. |
f0 |
The transformation computed based on observations from Class 0. Not applicable if type="naive". |
f1 |
The transformation computed based on observations from Class 1. Not applicable if type="naive". |
mu.hat |
The sample mean for transformed x from Class 1. |
transform |
The transformation that was actually used to transform x. |
Mai, Q., Zou, H. and Yuan, M. (2013). A direct approach to sparse discriminant analysis in ultra-high dimensions. Biometrika, 99, 29-42.
Mai, Q. and Zou, H. (2015). Sparse semiparametric discriminant analysis. Journal of Multivariate Analysis, 135, 175-188.
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x<-exp(x[which(y<3),]) y<-y[which(y<3)] n<-length(y) n1<-sum(y==1) n2<-n-n1 n1.test<-round(n1/2) n2.test<-round(n2/2) n.test<-n1.test+n2.test n.train<-n-n.test id.test<-c(sample(which(y==1),n1.test),sample(which(y==2),n2.test)) p<-ncol(x) x.train<-x[-id.test,] y.train<-y[-id.test] x.test<-x[id.test,] y.test<-y[id.test] x.norm<-matrix(0,n.train,p) x.test.norm<-matrix(0,n.test,p) for(i in 1:p){ obj.norm<-getnorm(x.train[,i],y.train) x.norm[,i]<-obj.norm$x.norm x.test.norm[,i]<-obj.norm$transform(x.test[,i]) } obj<-dsda.all(x.norm,y.train,x.test.norm,y.test)
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x<-exp(x[which(y<3),]) y<-y[which(y<3)] n<-length(y) n1<-sum(y==1) n2<-n-n1 n1.test<-round(n1/2) n2.test<-round(n2/2) n.test<-n1.test+n2.test n.train<-n-n.test id.test<-c(sample(which(y==1),n1.test),sample(which(y==2),n2.test)) p<-ncol(x) x.train<-x[-id.test,] y.train<-y[-id.test] x.test<-x[id.test,] y.test<-y[id.test] x.norm<-matrix(0,n.train,p) x.test.norm<-matrix(0,n.test,p) for(i in 1:p){ obj.norm<-getnorm(x.train[,i],y.train) x.norm[,i]<-obj.norm$x.norm x.test.norm[,i]<-obj.norm$transform(x.test[,i]) } obj<-dsda.all(x.norm,y.train,x.test.norm,y.test)
Fits a regularization path of Sparse Discriminant Analysis at a sequence of regularization parameters lambda. Performs prediction when testing data is provided. The msda
function solves classification problem by fitting a sparse discriminant analysis model. When covariates are provided, the function will first make adjustment on the training data. It provides three models: binary
for fitting DSDA model to solve binary classification problems, multi.original
and multi.modified
for fitting MSDA model to solve multi-class classification problems. multi.original
runs faster for small dimension case but the computation ability is limited to a relatively large dimension. multi.modified
has no such limitation and works in ultra-high dimensions. User can specify method by argument or use the default settings.
msda(x, z=NULL, y, testx=NULL,testz=NULL, model = NULL, lambda = NULL, standardize=FALSE, alpha=1, nlambda = 100, lambda.factor = ifelse((nobs - nclass)<= nvars, 0.2, 1e-03), dfmax = nobs, pmax = min(dfmax * 2 + 20, nvars), pf = rep(1, nvars), eps = 1e-04, maxit = 1e+06, sml = 1e-06, verbose = FALSE, perturb = NULL)
msda(x, z=NULL, y, testx=NULL,testz=NULL, model = NULL, lambda = NULL, standardize=FALSE, alpha=1, nlambda = 100, lambda.factor = ifelse((nobs - nclass)<= nvars, 0.2, 1e-03), dfmax = nobs, pmax = min(dfmax * 2 + 20, nvars), pf = rep(1, nvars), eps = 1e-04, maxit = 1e+06, sml = 1e-06, verbose = FALSE, perturb = NULL)
x |
Input matrix of predictors. |
z |
Input covariate matrix of dimension |
y |
Class labl. This argument should be a factor for classification. For |
testx |
Input testing matrix. Each row is a test case. When |
testz |
Input testing covariate matrix. Can be omitted if covariate is absent. However, training covariates |
model |
Method type. The |
lambda |
A user supplied |
standardize |
A logic object indicating whether x should be standardized before performing DSDA. Default is FALSE. This argument is only valid for |
alpha |
The elasticnet mixing parameter, the same as in glmnet. Default is alpha=1 so that the lasso penalty is used in DSDA. This argument is only valid for |
nlambda |
The number of tuning values in sequence |
lambda.factor |
The factor for getting the minimal lambda in |
dfmax |
The maximum number of selected variables in the model. Default is the number of observations |
pmax |
The maximum number of potential selected variables during iteration. In middle step, the algorithm can select at most |
pf |
L1 penalty factor of length |
eps |
Convergence threshold for coordinate descent. Each inner
coordinate descent loop continues until the relative change in any
coefficient. Defaults value is |
maxit |
Maximum number of outer-loop iterations allowed at fixed lambda value. Default is 1e6. If models do not converge, consider increasing |
sml |
Threshold for ratio of loss function change after each iteration to old loss function value. Default is |
verbose |
Whether to print out computation progress. The default is |
perturb |
A scalar number. If it is specified, the number will be added to each diagonal element of the covariance matrix as perturbation. The default is |
The msda
function fits a linear discriminant analysis model for vector as follows:
The categorical response is predicted from the Bayes rule:
The parameter model
specifies which method to use in estimating . Users can use
binary
for binary problems and binary
and multi.modified
for multi-class problems. In multi.original
, the algorithm first computes and stores , while it doesn't compute or store the entire covariance matrix in
multi.modified
. Since the algorithm is element-wise based, multi.modified
computes each element of covariance matrix when needed. Therefore, multi.original
is faster for low dimension but multi.modified
can fit model for a much higher dimension case.
Note that for computing speed reason, if models are not converging or running slow, consider increasing eps
and sml
, or decreasing
nlambda
, or increasing lambda.factor
before increasing
maxit
. Users can also reduce dfmax
to limit the maximum number of variables in the model.
The arguments list out all parameters in the three models, but not all of them are necessary in applying one of the methods. See the specific explaination of each argument for more detail. Meanwhile, the output of DSDA model only includes beta
and lambda
.
An object with S3 class dsda
or msda.original
and msda.modified
.
beta |
Output variable coefficients for each |
df |
The number of nonzero coefficients for each value of |
obj |
The fitted value of the objective function for each value of |
dim |
Dimension of each coefficient matrix. |
lambda |
The actual |
x |
The input matrix of predictors for training. |
y |
Class label in training data. |
npasses |
Total number of iterations (the most inner loop) summed over all lambda values |
jerr |
Error flag, for warnings and errors, 0 if no error. |
sigma |
Estimated sigma matrix. This argument is only available in object |
delta |
Estimated delta matrix. delta[k] = mu[k]-mu[1]. |
mu |
Estimated mu vector. |
prior |
Prior probability that y belong to class k, estimated by mean(y that belong to k). |
call |
The call that produced this object |
pred |
Predicted categorical response for each value in sequence |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q., Zou, H. and Yuan, M. (2012), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrica, 99, 29-42.
Mai, Q., Yang, Y., and Zou, H. (2017), "Multiclass sparse discriminant analysis." Statistica Sinica, in press.
URL: https://github.com/emeryyi/msda
data(GDS1615) x<-GDS1615$x y<-GDS1615$y obj <- msda(x = x, y = y)
data(GDS1615) x<-GDS1615$x y<-GDS1615$y obj <- msda(x = x, y = y)
Predict categorical responses on new matrix/tensor data given the fitted CATCH model input.
## S3 method for class 'catch' predict(object, newx, z = NULL, ztest = NULL, gamma = NULL,...)
## S3 method for class 'catch' predict(object, newx, z = NULL, ztest = NULL, gamma = NULL,...)
object |
Input |
newx |
Input adjusted testing tensor or matrix list. Each element of the list is a tensor. The tensor should of the same dimension as training data. |
z |
Input training covariates matrix. |
ztest |
Input testing covariates matrix. |
gamma |
Coefficients of covariates obtained from |
... |
Other arguments that can be passed to |
The function fits LDA model on selected discriminant vectors. Call predict
or predict.catch
to perform predictions.
There are two ways to make predictions. One way is to directly predict at the same time as fitting model by catch
since predict.catch
has already been embedded in catch
and it will predicts response when testing data is provided. The other way is to first use adjten
to adjuste tensor and catch
to fit model. predict.catch
will take the input adjusted tensor list newx
, covariate coefficient gamma
from adjten
and the fitted model from catch
to perform prediction. The prediction is identical to providing catch
testing data.
Predicted response of newx
for each lambda
in model object
.
Yuqing Pan, Qing Mai, Xin Zhang
Pan, Y., Mai, Q., and Zhang, X. (2018) Covariate-Adjusted Tensor Classification in High-Dimensions, arXiv:1805.04421.
#generate training data n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars),nrow=n,ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2),nrow=n,ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,],dim=c(p,p,p)) } #generate testing data newx <- array(list(),n) vec_newx <- matrix(rnorm(n*nvars),nrow=n,ncol=nvars) vec_newx[1:10,] <- vec_newx[1:10,]+2 newz <- matrix(rnorm(n*2),nrow=n,ncol=2) newz[1:10,] <- newz[1:10,]+0.5 for (i in 1:n){ newx[[i]] <- array(vec_newx[i,],dim=c(p,p,p)) } #Make adjustment and fit model obj <- adjten(x, z, y, newx, newz) fit <- catch(x, z, y) #Predict pred <- predict(fit, obj$testxres, z, newz, obj$gamma) #The adjusting, fitting model and predicting step can also be completed #by one command. pred <- catch(x, z, y, newx, newz)$pred
#generate training data n <- 20 p <- 4 k <- 2 nvars <- p*p*p x <- array(list(),n) vec_x <- matrix(rnorm(n*nvars),nrow=n,ncol=nvars) vec_x[1:10,] <- vec_x[1:10,]+2 z <- matrix(rnorm(n*2),nrow=n,ncol=2) z[1:10,] <- z[1:10,]+0.5 y <- c(rep(1,10),rep(2,10)) for (i in 1:n){ x[[i]] <- array(vec_x[i,],dim=c(p,p,p)) } #generate testing data newx <- array(list(),n) vec_newx <- matrix(rnorm(n*nvars),nrow=n,ncol=nvars) vec_newx[1:10,] <- vec_newx[1:10,]+2 newz <- matrix(rnorm(n*2),nrow=n,ncol=2) newz[1:10,] <- newz[1:10,]+0.5 for (i in 1:n){ newx[[i]] <- array(vec_newx[i,],dim=c(p,p,p)) } #Make adjustment and fit model obj <- adjten(x, z, y, newx, newz) fit <- catch(x, z, y) #Predict pred <- predict(fit, obj$testxres, z, newz, obj$gamma) #The adjusting, fitting model and predicting step can also be completed #by one command. pred <- catch(x, z, y, newx, newz)$pred
Predict the class labels by direct sparse discriminant analysis.
## S3 method for class 'dsda' predict(object, newx, z=NULL, ztest=NULL, gamma=NULL,...)
## S3 method for class 'dsda' predict(object, newx, z=NULL, ztest=NULL, gamma=NULL,...)
object |
An object returned by |
newx |
An n by p matrix containing the predictors. |
z |
Input training covariates matrix. |
ztest |
Input testing covariates matrix. |
gamma |
Coefficients of covariates obtained from |
... |
Other arguments that can be passed to |
pred |
The the predicted class labels. |
Mai, Q., Zou, H. and Yuan, M. (2013), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrika, 99, 29-42.
Predict categorical responses on new vector data given the fitted DSDA/MSDA model input.
## S3 method for class 'msda' predict(object, newx, z = NULL, ztest = NULL, gamma = NULL,...)
## S3 method for class 'msda' predict(object, newx, z = NULL, ztest = NULL, gamma = NULL,...)
object |
Fitted model object from |
newx |
The matrix of new values for |
z |
Input training covariates matrix. |
ztest |
Input testing covariates matrix. |
gamma |
Coefficients of covariates obtained from |
... |
Other arguments that can be passed to |
The function fits LDA model on selected discriminant vectors. Call predict
or predict.msda
to perform prediction. When covariates exist, users could first call adjvec
to make adjustment and obtain obtain gamma
. The fitted model from msda
should also takes adjusted vector as input. The newx
in predict.msda
shoudl be adjusted vector as well.
Predicted class label(s) at the entire sequence of the penalty parameter lambda
used to create the model.
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q., Zou, H. and Yuan, M. (2012), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrica, 99, 29-42.
Mai, Q., Yang, Y., and Zou, H. (2017), "Multiclass sparse discriminant analysis." Statistica Sinica, in press.
Pan, Y., Mai, Q., and Zhang, X. (2018), "Covariate-Adjusted Tensor Classification in High-Dimensions." Journal of the American Statistical Association, accepted.
data(GDS1615) x<-GDS1615$x y<-GDS1615$y obj <- msda(x = x, y = y) pred<-predict(obj,x)
data(GDS1615) x<-GDS1615$x y<-GDS1615$y obj <- msda(x = x, y = y) pred<-predict(obj,x)
Predict the class labels by semiparametric sparse discriminant analysis.
## S3 method for class 'SeSDA' predict(object, x.test,...)
## S3 method for class 'SeSDA' predict(object, x.test,...)
object |
An object returned by |
x.test |
An n by p matrix containing the predictors. |
... |
Other arguments that can be passed to |
pred |
The the predicted class labels. |
Mai, Q., Zou, H. and Yuan, M. (2013), "A direct approach to sparse discriminant analysis in ultra-high dimensions." Biometrika, 99, 29-42.
Compute the solution path for regularized optimal affine discriminant (ROAD).
ROAD(x,y,standardize=FALSE,lambda=NULL,eps=1e-7)
ROAD(x,y,standardize=FALSE,lambda=NULL,eps=1e-7)
x |
Input matrix of predictors. |
y |
An n-dimensional vector containing the class labels. The classes have to be labeled as 1 and 2. |
standardize |
A logic object indicating whether x should be standardized before performing ROAD. Default is FALSE. |
lambda |
A sequence of lambda's. If lambda is missed, the function will automatically generates a sequence of lambda's to fit model. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
The function obtains the solution path of ROAD through dsda
.
beta |
Output variable coefficients for each lambda. |
lambda |
The sequence of lambda's used in computing the solution path. |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q. and Zou, H. (2013), "A note on the connection and equivalence of three sparse linear discriminant analysis methods." Technometrics, 55, 243-246.
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- ROAD(x, y)
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- ROAD(x, y)
Compute the solution path for semiparametric sparse discriminant analysis.
SeSDA(x,y,standardize=FALSE,lambda=NULL,alpha=1,eps=1e-7)
SeSDA(x,y,standardize=FALSE,lambda=NULL,alpha=1,eps=1e-7)
x |
Input matrix of predictors. |
y |
An n-dimensional vector containing the class labels. The classes have to be labeled as 1 and 2. |
standardize |
A logic object indicating whether x should be standardized after transformation but before fitting classifier. Default is FALSE. |
lambda |
A sequence of lambda's. If lambda is missed or NULL, the function will automatically generates a sequence of lambda's to fit model. |
alpha |
The elasticnet mixing parameter, the same as in glmnet. Default is alpha=1 so that the lasso penalty is used. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
transform |
The tranformation functions. |
objdsda |
A DSDA object fitted on transformed data. |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q., Zou, H. and Yuan, M. (2013). A direct approach to sparse discriminant analysis in ultra-high dimensions. Biometrika, 99, 29-42.
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- SeSDA(x,y)
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- SeSDA(x,y)
Simulate a binary data set with vector predictor.
sim.bi.vector(tesize = 100)
sim.bi.vector(tesize = 100)
tesize |
Number of observations in testing data. |
The function simulates a data set with . Response are binary.
x |
Simulated vector predictor. |
testx |
Simulated testing vector predictor. |
y |
Response corresponding to |
testy |
Response corresponding to |
Yuqing Pan, Qing Mai, Xin Zhang
Simulate a data set with tensor predictor and covariates.
sim.tensor.cov(tesize = 100)
sim.tensor.cov(tesize = 100)
tesize |
Number of observations in testing data. |
The function simulates a data set with tensor and covariate being a two-dimensional vector. Response are binary.
x |
Simulated tensor predictor. |
z |
Simulated covariate. |
testx |
Simulated testing tensor predictor. |
testz |
Simualted testing covariate. |
vec_x |
Vectorization of |
vec_testx |
Vectorization of |
y |
Response corresponding to |
testy |
Response corresponding to |
Yuqing Pan, Qing Mai, Xin Zhang
Compute the solution path for sparse optimal scoring (SOS).
SOS(x,y,standardize=FALSE,lambda=NULL,eps=1e-7)
SOS(x,y,standardize=FALSE,lambda=NULL,eps=1e-7)
x |
Input matrix of predictors. |
y |
An n-dimensional vector containing the class labels. The classes have to be labeled as 1 and 2. |
standardize |
A logic object indicating whether x should be standardized before performing SOS. Default is FALSE. |
lambda |
A sequence of lambda's. If lambda is missed, the function will automatically generates a sequence of lambda's to fit model. |
eps |
Convergence threshold for coordinate descent, the same as in glmnet. Default is 1e-7. |
The function obtains the solution path of sparse optimal scoring model through dsda
.
beta |
Output variable coefficients for each lambda. |
lambda |
The sequence of lambda's used in computing the solution path. |
Yuqing Pan, Qing Mai, Xin Zhang
Mai, Q. and Zou, H. (2013), "A note on the connection and equivalence of three sparse linear discriminant analysis methods." Technometrics, 55, 243-246.
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- SOS(x, y)
data(GDS1615) ##load the prostate data x<-GDS1615$x y<-GDS1615$y x=x[which(y<3),] y=y[which(y<3)] obj.path <- SOS(x, y)