Skip to content

Commit c66ce5b

Browse files
committed
plot.deriv
1 parent d884253 commit c66ce5b

3 files changed

Lines changed: 116 additions & 24 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(plot,deriv)
34
S3method(plot,grasps)
45
S3method(plot,pen)
56
export(deriv)

R/plot.deriv.R

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#' Plot for Object of Class "deriv"
2+
#'
3+
#' @description
4+
#' Generate a visualization of penalty derivatives produced by \code{deriv()}.
5+
#' The plot automatically summarizes multiple configurations of penalty type,
6+
#' \eqn{\lambda}, and \eqn{\gamma}. Optional zooming is supported through
7+
#' \code{\link[ggforce]{facet_zoom}}.
8+
#'
9+
#' @param x An object of S3 class "deriv".
10+
#'
11+
#' @param ... Optional arguments passed to \code{\link[ggforce]{facet_zoom}}
12+
#' to zoom in on a subset of the data, while keeping the view of the full
13+
#' dataset as a separate panel.
14+
#'
15+
#' @import ggplot2
16+
#' @import ggforce
17+
#'
18+
#' @return
19+
#' An object of class \code{ggplot}.
20+
#'
21+
#' @export
22+
#'
23+
#' @noRd
24+
25+
plot.deriv <- function(x, ...) {
26+
27+
## identify which configuration columns vary across rows
28+
target <- c("penalty", "lambda", "gamma")
29+
cols_keep <- target[sapply(x[target], function(col) {
30+
col_na_rm <- col[!is.na(col)]
31+
length(col_na_rm) && (anyNA(col) || any(col_na_rm != col_na_rm[1]))
32+
})]
33+
34+
## clean data
35+
df <- x[, (names(x) %in% c("omega", "value", cols_keep)), drop = FALSE]
36+
key <- paste(cols_keep, collapse = "_")
37+
38+
## build facet_zoom() layer if zooming arguments are provided
39+
fz <- if (length(list(...)) > 0L) {
40+
do.call(facet_zoom, list(...))
41+
} else {
42+
NULL
43+
}
44+
45+
## declare
46+
group <- omega <- value <- NULL
47+
48+
## all configurations identical (only one curve)
49+
if (key == "") {
50+
ggplot(df, aes(x = omega, y = value)) +
51+
geom_line() +
52+
fz +
53+
labs(x = expression(italic(omega)),
54+
y = expression("Derivative Function" ~ italic(lambda) * italic(p) ~ "'(" * italic(omega) * ")")) +
55+
theme_bw() +
56+
theme(legend.position = "bottom")
57+
58+
## multiple configurations, use color grouping and legend
59+
} else {
60+
61+
## nicely formatted labels for lambda and gamma
62+
lambda_fmt <- paste0("\u03BB = ", df$lambda)
63+
gamma_fmt <- ifelse(is.na(df$gamma), "", paste0("\u03B3 = ", df$gamma))
64+
65+
## group labels based on which parameter(s) vary
66+
df$group <- switch(
67+
key,
68+
"penalty" = df$penalty,
69+
"lambda" = lambda_fmt,
70+
"gamma" = gamma_fmt,
71+
"penalty_lambda" = paste0(df$penalty, " (", lambda_fmt, ")"),
72+
"penalty_gamma" = paste0(df$penalty, " (", gamma_fmt, ")"),
73+
"lambda_gamma" = paste0(lambda_fmt, ", ", gamma_fmt),
74+
"penalty_lambda_gamma" = paste0(df$penalty, " (", lambda_fmt, ", ", gamma_fmt, ")")
75+
)
76+
77+
## legend label based on which parameter(s) vary
78+
legend_name <- switch(
79+
key,
80+
lambda = "\u03BB",
81+
gamma = "\u03B3",
82+
"Penalty Type"
83+
)
84+
85+
ggplot(df, aes(x = omega, y = value, color = group)) +
86+
geom_line() +
87+
fz +
88+
labs(x = expression(italic(omega)),
89+
y = expression("Derivative Function Function" ~ italic(lambda) * italic(p) ~ "'(" * italic(omega) * ")"),
90+
color = legend_name) +
91+
theme_bw() +
92+
theme(legend.position = "bottom")
93+
}
94+
}

vignettes/pen_est.qmd

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,16 @@ while the inset panel (left) magnifies the region near zero $[-1, 1]$.
223223
library(grasps) ## for penalty computation
224224
library(ggplot2) ## for visualization
225225
226-
omegas <- seq(-4, 4, by = 0.01)
227226
penalties <- c("atan", "exp", "lasso", "lq", "lsp", "mcp", "scad")
228227
229-
df <- grasps::pen(omegas, penalties, lambda = 1)
230-
plot(df, xlim = c(-1, 1), ylim = c(0, 1), zoom.size = 1) +
228+
pen_df <- grasps::pen(seq(-4, 4, by = 0.01), penalties, lambda = 1)
229+
plot(pen_df, xlim = c(-1, 1), ylim = c(0, 1), zoom.size = 1) +
231230
guides(color = guide_legend(nrow = 2, byrow = TRUE))
232231
```
233232

234233

235234
```{r echo=FALSE, fig.show='hide'}
236-
plot <- plot(df, xlim = c(-1, 1), ylim = c(0, 1), zoom.size = 1) +
235+
plot <- plot(pen_df, xlim = c(-1, 1), ylim = c(0, 1), zoom.size = 1) +
237236
guides(color = guide_legend(nrow = 2, byrow = TRUE))
238237
```
239238

@@ -257,28 +256,26 @@ $\vert\omega\vert$ increases, reflecting their tendency to shrink small
257256
$\vert\omega\vert$ strongly while exerting little to no shrinkage on large ones.
258257

259258

260-
::: {#fig-deriv}
261-
```{r fig.cap="Figure 2: Illustrative penalty derivatives."}
262-
df <- grasps::deriv(omegas, penalties, lambda = 1)
263-
ggplot(df, aes(x = omega, y = value, color = penalty)) +
264-
geom_line() +
265-
scale_x_continuous(limits = c(0, 4)) +
259+
```{r fig.show='hide'}
260+
deriv_df <- grasps::deriv(seq(0, 4, by = 0.01), penalties, lambda = 1)
261+
plot(deriv_df) +
262+
scale_y_continuous(limits = c(0, 1.5)) +
263+
guides(color = guide_legend(nrow = 2, byrow = TRUE))
264+
```
265+
266+
267+
```{r echo=FALSE, fig.show='hide'}
268+
plot <- plot(deriv_df) +
266269
scale_y_continuous(limits = c(0, 1.5)) +
267-
xlab(expression(italic(omega))) +
268-
ylab(expression("Derivative Function" ~ italic(p) * "'(" * italic(omega) * ")")) +
269-
scale_color_discrete(name = "Penalty Type",
270-
labels = c(expression(atan ~ "(" * gamma == 0.005 * ")"),
271-
expression(exp ~ "(" * gamma == 0.01 * ")"),
272-
"lasso",
273-
expression(lq ~ "(" * gamma == 0.5 * ")"),
274-
expression(lsp ~ "(" * gamma == 0.1 * ")"),
275-
expression(mcp ~ "(" * gamma == 3 * ")"),
276-
expression(scad ~ "(" * gamma == 3.7 * ")"))) +
277-
guides(color = guide_legend(nrow = 2, byrow = TRUE)) +
278-
theme_bw() +
279-
theme(aspect.ratio = 1,
280-
legend.position = "bottom")
270+
guides(color = guide_legend(nrow = 2, byrow = TRUE))
271+
```
272+
273+
274+
::: {#fig-deriv}
275+
```{r echo=FALSE}
276+
print(plot)
281277
```
278+
Illustrative penalty derivatives.
282279
:::
283280

284281

0 commit comments

Comments
 (0)