<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>

CC BY-SA 3.0
2017-06-20T10:48:26.183Z
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
mgalardini
CC BY-SA 3.0
2017-06-19T19:55:37.043Z
2017-06-20T11:00:23.183Z
2017-06-20T11:00:23.183Z
Devon Ryan
<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>

CC BY-SA 3.0
2017-06-16T15:30:53.273Z
Yes, this is how my design matrix looks like. I guess that means that I will have no way of correcting for batch effects?
mgalardini
CC BY-SA 3.0
2017-06-16T15:39:34.347Z
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
llrs
CC BY-SA 3.0
2017-06-16T17:34:51.783Z
@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.
Devon Ryan
CC BY-SA 3.0
2017-06-16T15:10:27.567Z
2017-06-20T07:33:09.990Z
2017-06-20T07:33:09.990Z
llrs
<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>

CC BY-SA 3.0
2017-06-16T13:53:25.203Z
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.
Devon Ryan
CC BY-SA 3.0
2017-06-16T13:56:58.297Z
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?
mgalardini
CC BY-SA 3.0
2017-06-16T14:06:49.097Z
It depends on what you really mean by that. Post the data frame on pastebin or something.
Devon Ryan
CC BY-SA 3.0
2017-06-19T13:44:53.770Z
Hi, I just edited the question with a link to the experimental design matrix
mgalardini
<rna-seq><r><differential-expression>
Getting a "system is computationally singular" error in sleuth
# 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
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, from where I expanded an answer.

> **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**: @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.  

---

