customizedGlmnet {customizedTraining}R Documentation

fit glmnet using customized training


Fit a regularized lasso model using customized training


customizedGlmnet(xTrain, yTrain, xTest, groupid = NULL, G = NULL,
    family = c("gaussian", "binomial", "multinomial"), dendrogram = NULL,
    dendrogramTestIndices = NULL)



an n-by-p matrix of training covariates


a length-n vector of training responses. Numeric for family = "gaussian". Factor or character for family = "binomial" or family = "multinomial"


an m-by-p matrix of test covariates


an optional length-m vector of group memberships for the test set. If specified, customized training subsets are identified using the union of nearest neighbor sets for each test group. Either groupid or G must be specified


a positive integer indicating the number of clusters for the joint clustering of the test and training data. Ignored if groupid is specified. Either groupid or G must be specified


response type


optional output from hclust on the joint covariate data. Used by cv.customizedGlmnet so that clustering is not computed redundantly


optional set of indices (corresponding to dendrogram) held out in cross-validation. Used by cv.customizedGlmnet


Identify customized training subsets of the training data through one of two methods: (1) If groupid is specified, grouping the test data, then for each test group find the 10 nearest neighbors of each observation in the group and use the union of these nearest neighbor sets as the customized training set or (2) If G is specified, jointly cluster the test and training data using hierarchical clustering with complete linkage. Within each cluster, the training data are used as the customized training subset for the test data. Once the customized training subsets have been identified, use glmnet to fit an l1-regularized regression model to each.


an object with class customizedGlmnet


the call that produced this object


a list containing the customized training subsets for each test group


a list containing the glmnet fit for each test group


a length-m vector containing the group memberships of the test data


a list containing train (which is the input xTrain) and test (which is the input xTest). Specified in function call


training response vector (specified in function call)


response type (specified in function call)


the fit of glmnet to the entire training set using standard training


Scott Powers, Trevor Hastie, Robert Tibshirani


Scott Powers, Trevor Hastie and Robert Tibshirani (2015) "Customized training with an application to mass specrometric imaging of gastric cancer data." Annals of Applied Statistics 9, 4:1709-1725.

See Also

print.customizedGlmnet, predict.customizedGlmnet, plot.customizedGlmnet, cv.customizedGlmnet



# Simulate synthetic data

n = m = 150
p = 50
q = 5
K = 3
sigmaC = 10
sigmaX = sigmaY = 1

beta = matrix(0, nrow = p, ncol = K)
for (k in 1:K) beta[sample(1:p, q), k] = 1
c = matrix(rnorm(K*p, 0, sigmaC), K, p)
eta = rnorm(K)
pi = (exp(eta)+1)/sum(exp(eta)+1)
z = t(rmultinom(m + n, 1, pi))
x = crossprod(t(z), c) + matrix(rnorm((m + n)*p, 0, sigmaX), m + n, p)
y = rowSums(z*(crossprod(t(x), beta))) + rnorm(m + n, 0, sigmaY)

x.train = x[1:n, ]
y.train = y[1:n]
x.test = x[n + 1:m, ]
y.test = y[n + 1:m]

# Example 1: Use clustering to fit the customized training model to training
# and test data with no predefined test-set blocks

fit1 = customizedGlmnet(x.train, y.train, x.test, G = 3,
    family = "gaussian")

# Print the customized training model fit:

# Extract nonzero regression coefficients for each group:
nonzero(fit1, lambda = 10)

# Compute test error using the predict function:
mean((y.test - predict(fit1, lambda = 10))^2)

# Plot nonzero coefficients by group:
plot(fit1, lambda = 10)

# Example 2: If the test set has predefined blocks, use these blocks to define
# the customized training sets, instead of using clustering. = apply(z == 1, 1, which)[n + 1:m]

fit2 = customizedGlmnet(x.train, y.train, x.test,

# Print the customized training model fit:

# Extract nonzero regression coefficients for each group:
nonzero(fit2, lambda = 10)

# Compute test error using the predict function:
mean((y.test - predict(fit2, lambda = 10))^2)

# Plot nonzero coefficients by group:
plot(fit2, lambda = 10)

[Package customizedTraining version 1.2 Index]