how to train a gene dataset with a nearest shrunken centroid classifier? I have a data file named &quot;geneexp.csv&quot;.
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(&quot;geneexp.csv&quot;)

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

split &lt;- splitData(data, .7)
train &lt;- split$train
test  &lt;- split$test

then with the use of pamr package I tried to buid the following model and plot :
y &lt;- train[[ncol(train)]]
x &lt;- t(train[,-ncol(train)]) 
mydata &lt;- list(
  x = x, 
  y = as.factor(as.factor(y)),
  geneid = as.character(1:nrow(x)),
  genenames = rownames(x)
)

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

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

Error in contrasts&lt;-(*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?
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 &lt;- 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