---
title: "Partial Regression"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{PartialRegression}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(Keng)
data(depress)
```

Aiming to help researchers to understand the role of *PRE* in regression, this vignette will present several ways of examining the unique effect of problem-focused coping(`cope_task1`) on depression(`depr1`) controlling for emotion-focused coping(`cope_emo1`) and avoidance coping(`cope_avo1`) using the first-wave data subset in internal data `depress`.

Four ways will be present in the following: 

- Multiple regression with *t*-test.
- Hierarchical regression with *F*-test.
- The *PRE* of the single parameter, the partial regression coefficient of problem-focused coping.
- One-predictor regression using the residuals.

## Multiple regression with *t*-test

Firstly, examine the unique effect of `cope_task1` using *t*-test. Model C (Compact model) regresses `depr1` on `cope_emo1` and `cope_avo1`. Model A(Augmented model) regresses `depr1` on `cope_task1`, `cope_emo1`, and `cope_avo1`. 

```{r}
# multiple regression
fitC <- lm(depr1 ~ cope_emo1 + cope_avo1, depress)
fitA <- lm(depr1 ~ cope_task1 + cope_emo1 + cope_avo1, depress)
summary(fitA)
```
As shown, the partial regression coefficient of `cope_task1` is -0.16705, *t*(90) = -3.349, *p* = 0.00119.

## Hierarchical regression with *F*-test

Secondly, examine the unique effect of *cope_task1* using hierarchical regression and its *F*-test. In SPSS, this *F*-test is presented as the *F*-test for *R*^2^ change.

```{r}
anova(fitC, fitA)
```
As shown, *F* (1, 90) = 11.217, *p* = 0.001185. This *F*-test is equivalent to the *t*-test above, since they both examine the unique effect of *cope_task1*. In the case that the *df* of *F*'s numerator is 1, *F* = *t*^2^, and *t*'s *df* equals to the *df* of *F*'s denominator.

## The *PRE* of the single parameter

Thirdly, examine the unique effect of *cope_task1* using *PRE*.

```{r}
print(compare_lm(fitC, fitA), digits = 3)
```
As shown, *F* (1, 90) = 11.217, *p* = 0.00119. The *F*-test of *PRE* is equivalent to the *F*-test of anova above.

## One-predictor regression using the residuals

Fourthly, examine the unique effect of `cope_task1` using residuals. Regress `depr1` on `cope_emo1` and `cope_avo1`, and attain the residuals of `depr1`, `dm_res`, which partials out the effect of `cope_emo1` and `cope_avo1` on `depr1`. 

Regress `cope_task1` on `cope_emo1` and `cope_avo1`, and attain the residuals of `cope_task1`, `pm_res`, which partials out the effect of `cope_emo1` and `cope_avo1` on `cope_task1`. 

Correlate `dm_res` with `pm_res`, we attain the partial correlation of `depr1` and `cope_task1`.

```{r}
dm_res <- lm(depr1 ~ cope_emo1 + cope_avo1, depress)$residuals
pm_res <- lm(cope_task1 ~ cope_emo1 + cope_avo1, depress)$residuals
resDat <- data.frame(dm_res, pm_res)
cor(dm_res, pm_res)
```
As shown, the partial correlation of `depr1` and `cope_task1` is -0.3329009.

Regress `dm_res` on `pm_res`, and we attain the unique effect of `cope_task1` on `depr1`.

```{r}
summary(lm(dm_res ~ pm_res, data.frame(dm_res, pm_res)))
```

As shown, the regression coefficient of `pm_res` equals the partial regression coefficients of `cope_task1` in `fitA`. However, their *t*s, as well as *p*s, are different. Why? Let's examine the unique effect of `pm_res` using *PRE*. Note that the *F*-test of one parameter's *PRE* is equivalent to the *t*-test of this parameter. In addition, Model A is relative to Model C. With your statistical purpose changing, the referents of Model C and Model A change.

```{r}
fitC <- lm(dm_res ~ 1, resDat)
fitA <- lm(dm_res ~ pm_res, resDat)
print(compare_lm(fitC, fitA), digits = 3)
```

Compare the *PRE* of `pm_res` with the *PRE* of `cope_task1`. It's shown that two *PRE*s are equivalent. However, *df2*s are different, which make *F*s, as well as *p*s, different. In other words, though the unique effect of `cope_task1` is constant, the compact models and augmented models used to evaluate its significance are different, which lead to different comparison conclusions (i.e., *F*-test and *t*-test results). Rethinking the *F*-test formula of *PRE*, we reach the following conclusion: With *PRE* being equal, the significance of *PRE* is determined by the *df* of Model C and the df-change of Model A against Model C. 

Therefore, given the *PRE* of a specific set of predictor(s), the power of this specific set of predictor(s) are determined by the sample size *n* and the number of parameters [and hence the total number of predictor(s)] in the regression model. Similarly, given the *PRE* of a specific set of predictor(s), the required power for this specific set of predictor(s), and the number of parameters [and hence the total number of predictor(s)] in the regression model, we could compute the required sample size *n*.
