Answers.Body: <p>If you want to plot the "corrected" expression, you will need to remove the variation introduced by these surrogate variables. Removing the expression affected can introduce some bias too and it is usually not recommended (despite comBat doing so). You should apply linear algebra, you can look at <a href="http://llrs.github.io/IEO/IEO_report.html#removing_batch_effect_with_svd" rel="nofollow noreferrer">here</a> is an example how to do it:</p>
  
  <pre><code>library("corpcor")
  s &lt;- corpcor::fast.svd(t(scale(t(dat), center = TRUE, scale = TRUE)))
  pcSds &lt;- s<span class="math-container">$d
  pcSds[1] &lt;- 0
  svdexp &lt;- s$</span>u %*% diag(pcSds) %*% t(s$v)
  colnames(svdexp) &lt;- colnames(dat)
  </code></pre>
  
  <p>But if your surrogate variables do have a big enough influence on your MDS plot to be observable (IMHO) there would be too much batch effect to work with the data. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-30T14:15:29.760Z
Answers.Text: But batch effect often appears as grouping of points, and that is how often I can see it. So, I am a bit confused regarding it. I would expect this grouping to disappear and get a different clustering of points. Are you talking only about MDS or about pca too?
Answers.UserDisplayName: Nikita Vlasenko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-30T14:22:08.117Z
Answers.Text: Well, without knowing the data is difficult to know if there is really a batch effect or other variables that could be adjusted for. I'm talking about both PCA and MDS, usually there isn't such a big difference between them (unless you use some other metric for distance in the MDS that is not the eucledian distance).
Answers.UserDisplayName: llrs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-30T08:09:21.397Z
Answers.LastActivityDate: 2018-10-30T08:09:21.397Z
Answers.UserDisplayName: llrs
Body: <p>I ran <code>SVA</code> to remove batch effects for my <code>bulk RNAseq</code> experiments, but now I need to somehow correct my data matrix in order to run <code>pca</code>, <code>mds</code>. I am using <code>DESeq2</code> for the analysis. Here is the code that I got now:</p>
  
  <pre><code>dds &lt;- estimateSizeFactors(dds)
  dat = counts(dds, normalized = TRUE)
  idx = rowMeans(dat) &gt; 1
  dat = dat[idx,]
  mod = model.matrix(~Group, colData(dds))
  mod0 = model.matrix(~1, colData(dds))
  
  nsv = num.sv(dat, mod)
  svseq = svaseq(dat, mod, mod0, n.sv = nsv)
  
  dds_sva = dds
  dds_sva<span class="math-container">$SV1 = svseq$</span>sv[,1]
  design(dds_sva) = ~ SV1 + Group
  dds_sva = DESeq(dds_sva)
  </code></pre>
  
  <p>So, I am wondering now, how to use <code>svseq</code> object to plot, say, the corrected version of <code>MDS plot</code>.  </p>
  
Tags: <r><rna-seq><deseq2><batch-effects><sva>
Title: How to get the corrected matrix after SVA batch effect correction
ThreadText: # How to get the corrected matrix after SVA batch effect correction
  
  (Asked by: username_0 on 2018-10-29)
  
  I ran `SVA` to remove batch effects for my `bulk RNAseq` experiments, but now I need to somehow correct my data matrix in order to run `pca`, `mds`. I am using `DESeq2` for the analysis. Here is the code that I got now:
  
  ```
  dds <- estimateSizeFactors(dds)
  dat = counts(dds, normalized = TRUE)
  idx = rowMeans(dat) > 1
  dat = dat[idx,]
  mod = model.matrix(~Group, colData(dds))
  mod0 = model.matrix(~1, colData(dds))
  
  nsv = num.sv(dat, mod)
  svseq = svaseq(dat, mod, mod0, n.sv = nsv)
  
  dds_sva = dds
  dds_sva$SV1 = svseq$sv[,1]
  design(dds_sva) = ~ SV1 + Group
  dds_sva = DESeq(dds_sva)
  
  ```
  
  So, I am wondering now, how to use `svseq` object to plot, say, the corrected version of `MDS plot`.
  
  **Tags:** r, rna-seq, deseq2, batch-effects, sva
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-30. Score: 2 | Accepted answer
  
  If you want to plot the "corrected" expression, you will need to remove the variation introduced by these surrogate variables. Removing the expression affected can introduce some bias too and it is usually not recommended (despite comBat doing so). You should apply linear algebra, you can look at [here](http://username_1.github.io/IEO/IEO_report.html#removing_batch_effect_with_svd) is an example how to do it:
  
  ```
  library("corpcor")
  s <- corpcor::fast.svd(t(scale(t(dat), center = TRUE, scale = TRUE)))
  pcSds <- s$d
  pcSds[1] <- 0
  svdexp <- s$u %*% diag(pcSds) %*% t(s$v)
  colnames(svdexp) <- colnames(dat)
  
  ```
  
  But if your surrogate variables do have a big enough influence on your MDS plot to be observable (IMHO) there would be too much batch effect to work with the data.
  
  > **Comments:**
  > **username_0**: But batch effect often appears as grouping of points, and that is how often I can see it. So, I am a bit confused regarding it. I would expect this grouping to disappear and get a different clustering of points. Are you talking only about MDS or about pca too?  
  > **username_1**: Well, without knowing the data is difficult to know if there is really a batch effect or other variables that could be adjusted for. I'm talking about both PCA and MDS, usually there isn't such a big difference between them (unless you use some other metric for distance in the MDS that is not the eucledian distance).  
  
  ---
  
  