<p>Is there a way to output a gene expression matrix corrected for batch (I know I can use ComBat) <em>and</em> surrogate variables from SVA? I would like to visualize the results of a limma differential expression however they are confounded and thus not amenable to visualization.</p>

If you have:

 - `y` as the gene expresion matrix
 - `mod` as the model matrix you sent to sva (the full model)
 - `svs` as svobj$sv where svobj is the output from the sva function

then you can use the function below to get a "cleaned" version of the matrix with the surrogate variables removed.

```
cleanY = function(y, mod, svs) {
    X = cbind(mod, svs)
    Hat = solve(t(X) %*% X) %*% t(X)
    beta = (Hat %*% t(y))
    rm(Hat)
    gc()
    P = ncol(mod)
    return(y - t(as.matrix(X[,-c(1:P)]) %*% beta[-c(1:P),]))
}
```

I modified this from a post by Andrew Jaffe [here][1].

 [1]: http://permalink.gmane.org/gmane.science.biology.informatics.conductor/42857