Answers.Body: <p>It's a bit different from usual R, but if you check the help:</p>
  <blockquote>
  <p>data: The input data. A list with components: x- an expression genes
  in the rows, samples in the columns)</p>
  </blockquote>
  <p>So in your case, you need to transpose the matrix:</p>
  <pre><code>mydata &lt;- list(
    x = t(x), 
    y = as.factor(y),
    geneid = as.character(1:nrow(x)),
    genenames = rownames(x)
  )
  </code></pre>
  <p>As I don't have your amazing data, I can only use iris below:</p>
  <pre><code>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    
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-01T23:33:51.343Z
Answers.LastActivityDate: 2020-12-01T23:33:51.343Z
Answers.UserDisplayName: StupidWolf
Body: <p>I have a data file named &quot;geneexp.csv&quot;.</p>
  <p>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).</p>
  <pre><code>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<span class="math-container">$train
  test  &lt;- split$</span>test
  </code></pre>
  <p>then with the use of <code>pamr</code> package I tried to buid the following model and plot :</p>
  <pre><code>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)
  </code></pre>
  <p>but I can't make it work. I get the following error:</p>
  <blockquote>
  <p>Error in <code>contrasts&lt;-</code>(<code>*tmp*</code>, value = contr.funs[1 + isOF[nn]]) :<br />
  contrasts can be applied only to factors with 2 or more levels</p>
  </blockquote>
  <p>I have already transfer y to the factors. Can you help me? How can I fix it?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-12-01T21:35:33.643Z
Comments.Text: ```as.numeric(as.character(y))``` this looks incredibly dangerous. What is in ```y``` ? can you do ```table(y)```
Comments.UserDisplayName: StupidWolf
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-12-01T22:58:25.830Z
Comments.Text: in y there is 3 classes CD4,CD8,CD19. I got this error: NAs introduced by coercion
Comments.UserDisplayName: homa taha
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-12-01T23:07:59.717Z
Comments.Text: table(y)
  CD19  CD4  CD8 
    68   74   68
Comments.UserDisplayName: homa taha
Tags: <r><gene><genome><machine-learning><data-preprocessing>
Title: how to train a gene dataset with a nearest shrunken centroid classifier?
ThreadText: # how to train a gene dataset with a nearest shrunken centroid classifier?
  
  (Asked by: username_0 on 2020-12-01)
  
  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?
  
  **Tags:** r, gene, genome, machine-learning, data-preprocessing
  
  > **Comments:**
  > **username_1**: ```as.numeric(as.character(y))``` this looks incredibly dangerous. What is in ```y``` ? can you do ```table(y)```  
  > **username_0**: in y there is 3 classes CD4,CD8,CD19. I got this error: NAs introduced by coercion  
  > **username_0**: table(y) CD19  CD4  CD8    68   74   68  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-12-01. Score: 0 | Accepted answer
  
  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    
  
  ```
  
  
  ---
  
  