Skip to content

Commit 84e6332

Browse files
plotting method testing & doc
1 parent 568ff9f commit 84e6332

19 files changed

Lines changed: 1208 additions & 68 deletions

DESCRIPTION

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ License: GPL (>= 3)
99
BugReports: https://github.com/marcboschmatas/partialling.out/issues
1010
Suggests:
1111
tinytest,
12-
tinyplot,
12+
tinysnapshot,
1313
knitr,
1414
rmarkdown,
1515
palmerpenguins,
1616
tinytable,
1717
fwlplot,
1818
tsibble,
1919
units,
20-
purrr
20+
purrr,
21+
fontquiver,
22+
rsvg,
23+
svglite
2124
Config/testthat/edition: 3
2225
Encoding: UTF-8
2326
Roxygen:list (markdown = TRUE, roclets = c ("namespace", "rd", "srr::srr_stats_roclet"))
@@ -29,5 +32,6 @@ Imports:
2932
rlang,
3033
fixest,
3134
lfe,
32-
stats
35+
stats,
36+
tinyplot
3337
URL: https://marcboschmatas.github.io/partialling.out/

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ S3method(partialling_out,felm)
44
S3method(partialling_out,fixest)
55
S3method(partialling_out,lm)
66
export(partialling_out)
7+
export(plot_partial_residuals)
78
import(rlang)
89
importFrom(fixest,feols)
910
importFrom(glue,glue)
1011
importFrom(lfe,felm)
1112
importFrom(lifecycle,deprecated)
13+
importFrom(stats,aggregate)
1214
importFrom(stats,as.formula)
1315
importFrom(stats,coef)
1416
importFrom(stats,formula)
1517
importFrom(stats,lm)
1618
importFrom(stats,resid)
1719
importFrom(stats,terms)
20+
importFrom(tinyplot,plt)
21+
importFrom(tinyplot,plt_add)

R/partialling_out.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ partialling_out.lm <- function(
197197
colnames(resdf)[3] <- "weights"
198198
}
199199

200-
# give partial_residual class to resdf to make plot method
200+
# give partial_residuals class to resdf to make plot method
201201

202-
resdf <- structure(resdf, class = c("partial_residual", "data.frame"))
202+
resdf <- structure(resdf, class = c("partial_residuals", "data.frame"))
203203

204204
return(resdf)
205205
}
@@ -330,8 +330,8 @@ partialling_out.fixest <- function(
330330
resdf <- cbind(resdf, weights)
331331
colnames(resdf)[3] <- "weights"
332332
}
333-
# give partial_residual class to resdf to make plot method
334-
resdf <- structure(resdf, class = c("partial_residual", "data.frame"))
333+
# give partial_residuals class to resdf to make plot method
334+
resdf <- structure(resdf, class = c("partial_residuals", "data.frame"))
335335

336336
return(resdf)
337337
}
@@ -466,9 +466,9 @@ partialling_out.felm <- function(
466466
colnames(resdf)[3] <- "weights"
467467
}
468468

469-
# give partial_residual class to resdf to make plot method
469+
# give partial_residuals class to resdf to make plot method
470470

471-
resdf <- structure(resdf, class = c("partial_residual", "data.frame"))
471+
resdf <- structure(resdf, class = c("partial_residuals", "data.frame"))
472472

473473
return(resdf)
474474
}

R/plot_partial_residuals.R

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#' Function for plotting partial residuals
2+
#' Uses `tinyplot` as backend
3+
4+
# nolint start
5+
#' @title plot_partial_residuals: scatterplot of partial residuals
6+
#' @param x a partial_residuals objects from `partialling_out()`
7+
#' @param add_lm if TRUE, a `lm` will be plotted
8+
#' @param quantile if TRUE, will plot only the mean values of the quantiles of the mean explanatory variable specified by `probs`
9+
#' @param probs numeric vector of length one that specifies the number of quantiles to be computed if `quantile` is TRUE.
10+
#' by default, 0.02, which will give 50 quantiles.
11+
#' @param ... Any other `tinyplot::plt()` params
12+
#' @returns invisibly, x
13+
#' @importFrom tinyplot plt
14+
#' @importFrom tinyplot plt_add
15+
#' @importFrom stats aggregate
16+
#' @examples
17+
#' \donttest{library(palmerpenguins)
18+
#' library(fixest)
19+
#' model <- feols(bill_length_mm ~ bill_depth_mm | species + island,
20+
#' data = penguins)
21+
#' partial_df <- partialling_out(model, penguins, both = TRUE)
22+
#' plot_partial_residuals(partial_df)
23+
#' }
24+
#' @export
25+
# nolint end
26+
plot_partial_residuals <- function(
27+
x,
28+
add_lm = TRUE,
29+
quantile = FALSE,
30+
probs = .02,
31+
...
32+
) {
33+
fml <- as.formula(paste(colnames(x)[1], "~", colnames(x)[2]))
34+
if (!quantile) {
35+
tinyplot::plt(fml, data = x, type = "p")
36+
} else {
37+
x$qnt <- cut(
38+
x[[2]],
39+
breaks = quantile(x[[2]], probs = seq(0, 1, probs)),
40+
include.lowest = TRUE
41+
)
42+
43+
fml_agg <- as.formula(paste0(
44+
"cbind(",
45+
colnames(x)[1],
46+
", ",
47+
colnames(x)[2],
48+
")",
49+
" ~ ",
50+
"qnt"
51+
))
52+
53+
res_qnt <- aggregate(
54+
fml_agg,
55+
data = x,
56+
FUN = mean
57+
)
58+
tinyplot::plt(fml, data = res_qnt, type = "p")
59+
}
60+
61+
if (add_lm) {
62+
tinyplot::plt_add(fml, data = x, type = "lm")
63+
}
64+
invisible(x)
65+
}

README.Rmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ The workflow for `partialling.out` is rather simple: first, create a linear or f
7272
```{r}
7373
library(partialling.out)
7474
library(tinytable)
75-
library(tinyplot)
7675
library(palmerpenguins)
7776
7877
model <- lm(bill_length_mm ~ bill_depth_mm + species, data = penguins)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ linear or fixed effects model.
8989
``` r
9090
library(partialling.out)
9191
library(tinytable)
92-
library(tinyplot)
9392
library(palmerpenguins)
9493

9594
model <- lm(bill_length_mm ~ bill_depth_mm + species, data = penguins)

docs/articles/partialling_out.html

Lines changed: 3 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pkgdown: 2.1.3.9000
33
pkgdown_sha: 26cecc444a485bd22cd066612432e94818904b67
44
articles:
55
partialling_out: partialling_out.html
6-
last_built: 2025-08-21T09:13Z
6+
last_built: 2025-08-21T16:52Z
77
urls:
88
reference: https://marcboschmatas.github.io/partialling.out/reference
99
article: https://marcboschmatas.github.io/partialling.out/articles

docs/reference/index.html

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)