-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME.Rmd
More file actions
133 lines (85 loc) · 4.76 KB
/
Copy pathREADME.Rmd
File metadata and controls
133 lines (85 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
warning = FALSE,
message = FALSE
)
```
# partialling.out
<!-- badges: start -->
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=partialling.out)
[](https://app.codecov.io/gh/ropensci/partialling.out)
[](https://github.com/ropensci/partialling.out/actions/workflows/R-CMD-check.yaml)
[](https://github.com/ropensci/software-review/issues/703)
<!-- badges: end -->
Partialling out is a package that allows to generate residualised variables of already existing linear or fixed effects models. So far it works with `lm`, `felm` (`lfe`package) and `feols` (`fixest` package) for applications of the Frisch-Waugh-Lovell theorem, as explained in Lovell ([2008](doi:10.3200/JECE.39.1.88-91)). Whereas this algorithm has already been implemented in [`fwlplot`](https://github.com/kylebutts/fwlplot), this package offers three new characteristics.
- It uses an already existing model instead of a formula.
- Works with `lm` and `felm` objects alongside `feols`.
- Returns a data.frame with residualised variables instead of a plot, thus offering more freedom of what to do with the results.
## Using the Frisch-Waugh-Lovell theorem
The Frisch-Waugh-Lovell theorem states that for a linear model
``` math
Y = X_1 \beta_1 + X_2 \beta_2 + u
```
The coefficient $\hat{\beta}_1$ will be equivalent to the coefficient, $\tilde{\beta}_1$, from the regression of
``` math
M_{X_2} Y = M_{X_2} X_1 \beta_1 + M_{X_1} u
```
Where $M_{X_2} Y$ are the residuals of the regression of $Y$ on $X_2$ and
$M_{X_2}X_1$ are the residuals of the regression of $X_1$ on $X_2$.
<br/>
This theorem is designed to help simplifying linear and, particularly, fixed effects models for easier visualisation and interpretation, transforming a multiple regression into a simple one that can be easily visualised via a scatterplot or further used in other models.
## Installation
The package can be installed with:
```r
install.packages("partialling.out")
```
You can install the development version of partialling.out from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("marcboschmatas/partialling.out")
```
Or, from R-Universe with
```r
install.packages("partialling.out", repos = c('https://ropensci.r-universe.dev', 'https://cloud.r-project.org'))
```
## Examples
The workflow for `partialling.out` is rather simple: first, create a linear or fixed effects model.
```{r}
library(partialling.out)
library(tinytable)
library(palmerpenguins)
model <- lm(bill_length_mm ~ bill_depth_mm + species, data = penguins)
summary(model)
```
Using the `partialling_out` function, you can get the residualised variable of interest (bill length) and of the first explanatory variable (bill_length), i.e. it would return the residuals of the following two regressions.
```{r eval = FALSE}
modely <- lm(bill_length_mm ~ species, data = penguins)
modelx <- lm(bill_depth_mm ~ species, data = penguins)
```
```{r}
res <- partialling_out(model, data = penguins)
tt(head(res)) |>
format_tt(digits = 2) |>
style_tt(align = "c")
```
## Checking the results
As stated above, if we follow following the Frisch-Waugh-Lovell theorem the coefficient of `res_bill_depth_mm` in the model `lm(res_bill_length_mm ~ res_bill_depth_mm)` will be the same of the coefficient of `bill_depth_mm` in the original model.
```{r}
resmodel <- lm(res_bill_length_mm ~ res_bill_depth_mm, data = res)
print(c(model$coefficients[2], resmodel$coefficients[2]))
```
## Contributing
Contributing instructions can be found [here](https://github.com/ropensci/partialling.out/blob/main/.github/contributing.md)
Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/). By contributing to this project, you agree to abide by its terms.
## Acknowledgements
To the authors of the [fwlplot](https://github.com/kylebutts/fwlplot) package, Kyle Butts and Grant McDermott, which has provided inspiration and ideas for this project.
To my colleague Andreu Arenas-Jal for his insight and guiding.
To the ROpensci editors Mark Padgham and Nima Hejazi and to the reviewers Christian Testa, Kyle Butts, and Adam Loy.