# principal component analysis adjusting for confounder

Suppose I have a dataset of $n$ samples and $p$ variables ($V_1,V_2, ...,V_p$). The samples are from two groups ($G$) and $Z$ is a confounder. I would like to perform principal component analysis (PCA) controlling the $Z$. I did some search regarding how to control the confounder before PCA but didn't find much, although people asked about this, e.g. here. So I come up with following thought and I would like to validate if this is logically acceptable.

In univariate analysis, if I would like to find out which variables in the $p$ variables are significantly correlated with the $G$ controlling the confounder $Z$, I can run $p$ linear regressions $V_i \sim g_iG + z_iZ$, and report the p value of $G$ ($g_i$ and $z_i$ is the regression coefficient for $i^{th}$ variable).

Then, I regress out the $Z$ for each variable by $V_i-z_iZ$ and get a 'residual' matrix. Can I perform PCA on this residual matrix? I hope this residual matrix has all the confounder $Z$ regressed out.

Is this logically correct?

• 1) What do the confounder do? - which variable effect on which variable does it facilitate or hamper? 2) Do the groups differ in means? Do you wish to remove the difference when doing PCA? – ttnphns May 26 '18 at 7:30
• I don’t understand your first question. Confounder is just a confounder confounding on each $V$ and $G$. 2) Some $V_i$ have different mean of $G$. I want to visualize the effect of $G$ (controlling the confounder). I don’t think the PCA as a unsupervised method can possibly remove the difference cause by $G$. – WCMC May 26 '18 at 7:41
• Doing PCA on residuals after ANOVA by G is doing it on the group clouds superposed towards the dame mean, so the group differences were removed. – ttnphns May 26 '18 at 8:17
• @ttnphns no, not on residual of ANOVA by G. It is ANOVA by Z and G, and take the residual only by Z, as what is written from my question. – WCMC May 26 '18 at 8:19
• What do you mean exactly by "perform PCA"? Do you just mean that you want to make a PCA plot (usually a scatterplot of the first 2 components) of $V_1,\ldots,V_p$ to see if they separate by group? Or do mean something else? Do you have some downstream statistical analysis in mind? Assessing what is logically correct as far as adjusting for a confounder depends on what you are trying to achieve. – Gordon Smyth May 27 '18 at 2:51