# Two ways that correlation and stepwise regression can give different results

**Minding the Brain**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In general, a correlation test is used to test the association between two variables (**y** and **z**). However, if there is a third variable (**x**) that might be related to **z** or **y**, it makes sense to use stepwise regression (or partial correlation). There are two quite different situations where the correlation and stepwise regression will produce different results. Here are some examples using made up data.

Case 1. **z** is not associated with **y**, but is correlated with **x**, which is associated with **y**:

set.seed(1)<br />x <- rnorm(100)<br />z <- x + rnorm(100)<br />y <- x + rnorm(100, sd = 0.1)<br /><br />dat.1 <- data.frame(x = x, y = y, z = z)<br />cor.test(~y + z, data = dat.1)<br />

## <br />## Pearson's product-moment correlation<br />## <br />## data: y and z <br />## t = 9.058, df = 98, p-value = 1.332e-14<br />## alternative hypothesis: true correlation is not equal to 0 <br />## 95 percent confidence interval:<br />## 0.5518 0.7694 <br />## sample estimates:<br />## cor <br />## 0.675 <br />## <br />

anova(lm(y ~ x, data = dat.1), lm(y ~ x + z, data = dat.1))<br />

## Analysis of Variance Table<br />## <br />## Model 1: y ~ x<br />## Model 2: y ~ x + z<br />## Res.Df RSS Df Sum of Sq F Pr(>F)<br />## 1 98 1.06 <br />## 2 97 1.06 1 0.0026 0.24 0.63<br />In this case, the correlation test showed an association between

**z**and

**y**, but that association was really just a by-product of by both variables' association with

**x**. The stepwise regression revealed that

**z**made no independent contribution to

**y**after

**x**was already included in the model.

The second, perhaps less obvious, case is when the relationship between **z** and **y** is masked by variance in **y** due to **x**. In other words, **x** and **z** are completely unrelated and both are related to **y**, but the variance in **y** due to **x** is very large. Here is an example of this situation:

Case 2. **z** and **x** are completely unrelated, both **x** and **z** cotribute to **y**, but the variance in **x** is much larger (10x) than the variance in **z**.

set.seed(1)<br />x <- rnorm(100, sd = 10)<br />z <- rnorm(100)<br />y <- x + z + rnorm(100, sd = 0.1)<br /><br />dat.2 <- data.frame(x = x, z = z, y = y)<br />cor.test(~y + z, data = dat.2)<br />

## <br />## Pearson's product-moment correlation<br />## <br />## data: y and z <br />## t = 1.04, df = 98, p-value = 0.3009<br />## alternative hypothesis: true correlation is not equal to 0 <br />## 95 percent confidence interval:<br />## -0.09387 0.29484 <br />## sample estimates:<br />## cor <br />## 0.1045 <br />## <br />

anova(lm(y ~ x, data = dat.2), lm(y ~ x + z, data = dat.2))<br />

## Analysis of Variance Table<br />## <br />## Model 1: y ~ x<br />## Model 2: y ~ x + z<br />## Res.Df RSS Df Sum of Sq F Pr(>F) <br />## 1 98 90.9 <br />## 2 97 1.1 1 89.9 8254 <2e-16 ***<br />## ---<br />## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 <br />

**y**and

**z**did not reach significance, but their relationship emerged in a stepwise regression once the variance due to

**x**was accounted for (for this simple case, partial correlation would work also). The bottom line is that when you're considering the relationship between an outcome and a predictor, if you know that your outcome variable has an important relationship with some third variable, stepwise regression (or partial correlation) can (1) make sure an observed associaiton is not due to the third variable and (2) reveal an association that could be masked by the third variable.

**leave a comment**for the author, please follow the link and comment on their blog:

**Minding the Brain**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.