# Causally Remove Negative Confound Effects of Size Metric for Software Defect Prediction
## 3 Method
### Method of Causally Removing Negative Confounding Effect  
The prerequisite for the confounding effects of the size metric is that it is related to other software metrics. In Figure 1, Z can explain part of X. If there is no this related part, then there will be no edge between X and Z, so Z will not confound the correlation between X and Y. We denote the unrelated part in X as X\({}^{\prime}\). Hence, X\({}^{\prime}\) is the part of X that has nothing to do with Z, and the correlation between X\({}^{\prime}\) and Y is not affected by the confounder Z. Based on the above analysis, we quantify the confounding effects of size metric. Then, we use the correlation-based method to analyze the direction of the confounding effects. We know that high correlation usually means the great predictive ability of one software metric for the software defect. We compare CorXY (the correlation between X and Y) with CorX\({}^{\prime}\)Y (the correlation between X\({}^{\prime}\) and Y). If CorXY is the larger one, it means that Z hurts the predictive ability of X; in other words, confounding has a negative effect. This confounding effect should be removed. On the contrary, if CorX\({}^{\prime}\)Y is the larger one, it means that Z enhances the predictive ability of X, and the confounding has a positive effect. This confounding effect should be retained. Based on the above, we propose the CRNCEM, and Algorithm 1 presents the pseudocode. The CRNCEM is generally divided into two steps. The first step is to quantify the value of the confounding effect; the second step is to analyze the confounding effect, and then remove the negative confounding effect, which is not conducive to defect prediction. For each metric except the size metric:  
1. Quantify the confounding effect  
Since there are complex nonlinear relationships between the size metric and other metrics, we choose a generalized additive model (GAM) to quantify the confounding effects. GAM is a data-driven model with a strong ability to analyze the nonlinear relationship and does not require any assumptions between variables. The mathematical expression of GAM is as shown:  
\[B=\beta_{0}+\sum_{i=1}^{k}f_{i}(A_{i}) \tag{1}\]  
Figure 1: Path diagram illustrating confounding effect. in which coefficients \(\beta_{0}\) is a constant, function \(f_{i}(A_{i})\) is a smoothing function determined by the explained variables themselves [19], and K is the number of independent variables. First, we use the GAM to fit a metric by size metric, as shown in formula (2). \[X=\beta_{0}+f_{i}(Z) \tag{2}\]  
Then, we use the size metric to predict that metric. As shown in formula (3), \(\hat{X}\) means the prediction value. Let\(\hat{\beta_{0}}\) and \(\hat{f}\) be the sample estimates for \(\beta_{0}\) and \(f\), respectively. \(\hat{X}\) is the explained part of X.  
\[\hat{X}=\hat{\beta_{0}}+\hat{f_{i}}(Z) \tag{3}\]  
Therefore, X\({}^{\prime}\) can be expressed as X minus the explained part of X, as shown in formula (4). Indeed, X\({}^{\prime}\) is equal to the prediction error [33]. \[X^{\prime}=X-\hat{X} \tag{4}\]  
* Remove the negative confounding effect Pearson coefficient is used to analyze the confounding effect. Pearson coefficient is the most commonly used statistics indicator for the correlation between variables. The formula is as follows:  
\[p(X,Y)=\frac{Cov(X,Y)}{\sqrt{Var[X]Var[Y]}} \tag{5}\]  
in which Cov[] represents covariance, Var[] representative variance. We use formula (5) to calculate the Pearson coefficients between the original metric X and defect-proneness Y, signed as \(p_{xy}\); Pearson coefficients between the nonconfounding metric X\({}^{\prime}\) and defect-proneness Y, signed as \(p_{x^{\prime}y}\). Compare \(p_{xy}\) and \(p_{x^{\prime}y}\), and if \(p_{xy}\) is larger, we keep the original X; otherwise, replace X with X\({}^{\prime}\).