Answers.Body: <p>You should be able to remove any one of the following strains to end up with a rank-sufficient model matrix: 5, 10, 12, 13, 14, 15, 19, 26, 28, 3, 30, 32, 36, 39, 41, 45, 46, 49, 5, 50, 52, 53, 58, 59, 60, 69, 8. As an aside you can figure this sort of thing out as follows (I read your dataframe in a the <code>d</code> object):</p>
  
  <pre><code>&gt; m = model.matrix(~batch+strain, d)
  &gt; dim(m) # 142 row, 77 columns, so minimum rank is 77
  &gt; qr(m)$rank # 76, so just barely rank insufficient
  &gt; #see if we can remove a single column and still get rank 76
  &gt; colnames(m)[which(sapply(1:77, function(x) qr(m[,-x])$rank) == 76)]
  </code></pre>
  
  <p>You obviously don't want to remove the batch columns or the intercept. The normal tricks that you can sometimes use to get around this issue with case-control studies don't appear to help here, which is why I would just drop a strain and call it done. Keep in mind that your power is still likely terrible. I generally recommend at least 6 replicates per group (scale down the number of groups to fit your budget).</p>
  
  <p>EDIT:</p>
  
  <p>Once the desired strain is removed from the model, it can be fit directly into the <code>sleuth_fit</code> function to obtain the full model:</p>
  
  <pre><code>&gt; m = m[, -9] # whatever column to drop to get the appropriate rank
  &gt; so &lt;- sleuth_fit(so, m, 'full')
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-20T10:48:26.183Z
Answers.Text: That's just briliant; I removed a strain and I can now run the full model to take into account batch effects. I will update your answer to include a snippet on how to feed the "chopped" model into sleuth
Answers.UserDisplayName: mgalardini
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-19T19:55:37.043Z
Answers.LastActivityDate: 2017-06-20T11:00:23.183Z
Answers.LastEditDate: 2017-06-20T11:00:23.183Z
Answers.UserDisplayName: Devon Ryan
Answers.Body: <p>This happen when the variables (strain +batch) create a design matrix like this:</p>
  
  <pre><code>batch strain
  1 1 #
  1 1 #
  1 2
  2 2
  3 3
  4 3
  ...
  16 72
  </code></pre>
  
  <p>Which means that some of the covariates are not linearly independent (ie batch 1 and strain 1), all the strain 1 is in batch 1. </p>
  
  <p>You can correct for batch effects, but not in this design of the linear model (if you want to take into account the strain). You could do one batch more with those 15 strains that are in a single batch (if they are in different batch between them) that way you would get an independent design.</p>
  
  <p>Many strains in a single batch are in the same batch. You need to increase the number of samples (recommended anyway due to the low number of samples per strain) to avoid this problem. </p>
  
  <hr>
  
  <p><sub>There are a lot of related question in Bioconductor <a href="https://www.google.es/search?q=site%3Asupport.bioconductor.org%20%22system%20is%20computationally%20singular%22&amp;rlz=1C1WPZB_enES727ES727&amp;oq=site%3Asupport.bioconductor.org%20%22system%20is%20computationally%20singular%22&amp;ie=UTF-8" rel="nofollow noreferrer">support forum</a>, from where I expanded an <a href="https://support.bioconductor.org/p/62058/#62059" rel="nofollow noreferrer">answer</a>.</sub></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-16T15:30:53.273Z
Answers.Text: Yes, this is how my design matrix looks like. I guess that means that I will have no way of correcting for batch effects?
Answers.UserDisplayName: mgalardini
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-16T15:39:34.347Z
Answers.Text: You can correct for batch effects, but not in this design of the linear model (if you want to take into account the strain). 
  I am unsure (at the moment) if you could use combat or other batch effect removal tool to incorporate the batch effect, other option could be to try to use surrogate variables (which will detect the batch effect AFAIK).Or you could do one batch more with those 15 strains that are in a single batch (if they are in different batch between them) that way you would get an independent design
Answers.UserDisplayName: llrs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-16T17:34:51.783Z
Answers.Text: @mgalardini As I said above, please post the entire design dataframe somewhere. Then we can actually look an tell you what your options are rather than speculating.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-16T15:10:27.567Z
Answers.LastActivityDate: 2017-06-20T07:33:09.990Z
Answers.LastEditDate: 2017-06-20T07:33:09.990Z
Answers.UserDisplayName: llrs
Body: <p>I am analysing 142 samples belonging to 6 batches. Additionally, those samples belong to 72 strains, which means that for most of the strains there are two samples.</p>
  
  <p>I could fit simple models (for strain and batches for instance), but when I get to the "full" model (~batch+strain), I get the following error:</p>
  
  <pre><code>so &lt;- sleuth_fit(so, ~strain+batch, 'full')
  Error in solve.default(t(X) %*% X) :
    system is computationally singular: reciprocal condition number = 5.2412e-19
  </code></pre>
  
  <p>I should point out that of the 72 strains, only 15 have samples in distinct batches. This means that most strains (57) have both samples in the same batch.</p>
  
  <p>Is the error due to an unknown bug or rather to the experimental design? Does it mean that the information on batches cannot be used?</p>
  
  <p>Thanks</p>
  
  <p>EDIT I've posted the experimental design in a <a href="https://gist.github.com/mgalardini/7553e14ead89a7f020f2b0a610086805" rel="nofollow noreferrer">gist</a></p>
  
  <pre><code>batch   strain  replica
  batch_1     strain_41   1
  batch_4     strain_41   2
  batch_1     strain_28   1
  batch_4     strain_28   2
  batch_1     strain_26   1
  [...]
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-16T13:53:25.203Z
Comments.Text: You have a strain completely in a batch or something along those lines. You'll have to post the entire design (the samples and their strain and batch association) somewhere.
Comments.UserDisplayName: Devon Ryan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-16T13:56:58.297Z
Comments.Text: In fact, I did mention that: "I should point out that of the 72 strains, only 15 have samples in distinct batches.". Is it correct to say that that is the root of the problem?
Comments.UserDisplayName: mgalardini
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-16T14:06:49.097Z
Comments.Text: It depends on what you really mean by that. Post the data frame on pastebin or something.
Comments.UserDisplayName: Devon Ryan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-19T13:44:53.770Z
Comments.Text: Hi, I just edited the question with a link to the experimental design matrix
Comments.UserDisplayName: mgalardini
Tags: <rna-seq><r><differential-expression>
Title: Getting a "system is computationally singular" error in sleuth
ThreadText: # Getting a "system is computationally singular" error in sleuth
  
  (Asked by: username_0 on 2017-06-16)
  
  I am analysing 142 samples belonging to 6 batches. Additionally, those samples belong to 72 strains, which means that for most of the strains there are two samples.
  
  I could fit simple models (for strain and batches for instance), but when I get to the "full" model (~batch+strain), I get the following error:
  
  ```
  so <- sleuth_fit(so, ~strain+batch, 'full')
  Error in solve.default(t(X) %*% X) :
    system is computationally singular: reciprocal condition number = 5.2412e-19
  
  ```
  
  I should point out that of the 72 strains, only 15 have samples in distinct batches. This means that most strains (57) have both samples in the same batch.
  
  Is the error due to an unknown bug or rather to the experimental design? Does it mean that the information on batches cannot be used?
  
  Thanks
  
  EDIT I've posted the experimental design in a [gist](https://gist.github.com/username_0/7553e14ead89a7f020f2b0a610086805)
  
  ```
  batch   strain  replica
  batch_1     strain_41   1
  batch_4     strain_41   2
  batch_1     strain_28   1
  batch_4     strain_28   2
  batch_1     strain_26   1
  [...]
  
  ```
  
  **Tags:** rna-seq, r, differential-expression
  
  > **Comments:**
  > **username_1**: You have a strain completely in a batch or something along those lines. You'll have to post the entire design (the samples and their strain and batch association) somewhere.  
  > **username_0**: In fact, I did mention that: "I should point out that of the 72 strains, only 15 have samples in distinct batches.". Is it correct to say that that is the root of the problem?  
  > **username_1**: It depends on what you really mean by that. Post the data frame on pastebin or something.  
  > **username_0**: Hi, I just edited the question with a link to the experimental design matrix  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-06-19. Score: 2 | Accepted answer
  
  You should be able to remove any one of the following strains to end up with a rank-sufficient model matrix: 5, 10, 12, 13, 14, 15, 19, 26, 28, 3, 30, 32, 36, 39, 41, 45, 46, 49, 5, 50, 52, 53, 58, 59, 60, 69, 8. As an aside you can figure this sort of thing out as follows (I read your dataframe in a the `d` object):
  
  ```
  > m = model.matrix(~batch+strain, d)
  > dim(m) # 142 row, 77 columns, so minimum rank is 77
  > qr(m)$rank # 76, so just barely rank insufficient
  > #see if we can remove a single column and still get rank 76
  > colnames(m)[which(sapply(1:77, function(x) qr(m[,-x])$rank) == 76)]
  
  ```
  
  You obviously don't want to remove the batch columns or the intercept. The normal tricks that you can sometimes use to get around this issue with case-control studies don't appear to help here, which is why I would just drop a strain and call it done. Keep in mind that your power is still likely terrible. I generally recommend at least 6 replicates per group (scale down the number of groups to fit your budget).
  
  EDIT:
  
  Once the desired strain is removed from the model, it can be fit directly into the `sleuth_fit` function to obtain the full model:
  
  ```
  > m = m[, -9] # whatever column to drop to get the appropriate rank
  > so <- sleuth_fit(so, m, 'full')
  
  ```
  
  > **Comments:**
  > **username_0**: That's just briliant; I removed a strain and I can now run the full model to take into account batch effects. I will update your answer to include a snippet on how to feed the "chopped" model into sleuth  
  
  ---
  
  ### Answer by username_2 on 2017-06-16. Score: 5
  
  This happen when the variables (strain +batch) create a design matrix like this:
  
  ```
  batch strain
  1 1 #
  1 1 #
  1 2
  2 2
  3 3
  4 3
  ...
  16 72
  
  ```
  
  Which means that some of the covariates are not linearly independent (ie batch 1 and strain 1), all the strain 1 is in batch 1.
  
  You can correct for batch effects, but not in this design of the linear model (if you want to take into account the strain). You could do one batch more with those 15 strains that are in a single batch (if they are in different batch between them) that way you would get an independent design.
  
  Many strains in a single batch are in the same batch. You need to increase the number of samples (recommended anyway due to the low number of samples per strain) to avoid this problem.
  
  ---
  
  There are a lot of related question in Bioconductor [support forum](https://www.google.es/search?q=site%3Asupport.bioconductor.org%20%22system%20is%20computationally%20singular%22&rlz=1C1WPZB_enES727ES727&oq=site%3Asupport.bioconductor.org%20%22system%20is%20computationally%20singular%22&ie=UTF-8), from where I expanded an [answer](https://support.bioconductor.org/p/62058/#62059).
  
  > **Comments:**
  > **username_0**: Yes, this is how my design matrix looks like. I guess that means that I will have no way of correcting for batch effects?  
  > **username_2**: You can correct for batch effects, but not in this design of the linear model (if you want to take into account the strain).  I am unsure (at the moment) if you could use combat or other batch effect removal tool to incorporate the batch effect, other option could be to try to use surrogate variables (which will detect the batch effect AFAIK).Or you could do one batch more with those 15 strains that are in a single batch (if they are in different batch between them) that way you would get an independent design  
  > **username_1**: @username_0 As I said above, please post the entire design dataframe somewhere. Then we can actually look an tell you what your options are rather than speculating.  
  
  ---
  
  