grpregOverlap
grpregOverlap copied to clipboard
Update grpregOverlap.R
Hi Yaohui, Thank you for your interesting package. While working with it, I had an error with the ExpandX function with the dimnames in the Overlap matrix. I propose this:
expandX <- function(X, group) {
incidence.mat <- incidenceMatrix(X, group) # group membership incidence matrix
over.mat <- Matrix(incidence.mat %*% t(incidence.mat), sparse = TRUE)
# dimnames = dimnames(incidence.mat)) # overlap matrix
grp.vec <- rep(1:nrow(over.mat), times = diag(over.mat)) # group index vector
# expand X to X.latent
X.latent <- NULL
names <- NULL
## the following code will automatically remove variables not included in 'group'
for(i in 1:nrow(incidence.mat)) {
idx <- incidence.mat[i,]==1
X.latent <- cbind(X.latent, X[, idx, drop=FALSE])
names <- c(names, colnames(incidence.mat)[idx])
# colnames(X.latent) <- c(colnames(X.latent), colnames(X)[incidence.mat[i,]==1])
}
colnames(X.latent) <- paste('grp', grp.vec, '_', names, sep = "")
X.latent
}
Because when creating the overlap matrix, the dimnames are not the same with the incidence matrix. Thanks you, Best