# how to train a gene dataset with a nearest shrunken centroid classifier?

I have a data file named "geneexp.csv".

the data contains information about gene expression of three different cell types (CD4 and CD8, CD19) I want to classify cells by performing the nearest shrunken centroid classification of training data in which the threshold is chosen by cross-validation. I split the data (70% train and 30% test).

data = read.csv("geneexp.csv")

splitData <- function(data, trainRate) {
n <- dim(data)[1]
idxs <- sample(1:n, floor(trainRate*n))
train <- data[idxs,]
test <- data[-idxs,]
return (list(train = train, test = test))
}

split <- splitData(data, .7)
train <- split$$train test <- split$$test


then with the use of pamr package I tried to buid the following model and plot :

y <- train[[ncol(train)]]
x <- t(train[,-ncol(train)])
mydata <- list(
x = x,
y = as.factor(as.factor(y)),
geneid = as.character(1:nrow(x)),
genenames = rownames(x)
)

# Training and cross-validating threshold
model <- pamr.train(mydata)
cvmodel <- pamr.cv(model, mydata)
pamr.plotcv(cvmodel)


but I can't make it work. I get the following error:

Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels

I have already transfer y to the factors. Can you help me? How can I fix it?

• as.numeric(as.character(y)) this looks incredibly dangerous. What is in y ? can you do table(y) Dec 1 '20 at 21:35
• in y there is 3 classes CD4,CD8,CD19. I got this error: NAs introduced by coercion Dec 1 '20 at 22:58
• table(y) CD19 CD4 CD8 68 74 68 Dec 1 '20 at 23:07

It's a bit different from usual R, but if you check the help:

data: The input data. A list with components: x- an expression genes in the rows, samples in the columns)

So in your case, you need to transpose the matrix:

mydata <- list(
x = t(x),
y = as.factor(y),
geneid = as.character(1:nrow(x)),
genenames = rownames(x)
)


As I don't have your amazing data, I can only use iris below:

pamr.train(list(x=t(iris[,1:4]),y=iris[,5]))

pamr.train(data = list(x = t(iris[, 1:4]), y = iris[, 5]))
threshold nonzero errors
1   0.000    4       6
2   0.841    4       7
3   1.682    4       10
4   2.523    4       11
5   3.364    4       13
6   4.205    4       18
7   5.046    3       22
8   5.887    3       23