-
-
Notifications
You must be signed in to change notification settings - Fork 87
add fun_avg to ppc_avg functions #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c96c7d6
add fun_avg to ppc_avg functions
tjmahr ffe723f
forward "stat" to axis labels in ppc
tjmahr c647d69
fix docs
tjmahr 23865df
simplify as_tagged_function()
tjmahr 27c17d6
use "stat" for anon functions. support formulas
tjmahr 2deeca9
use x expression as axis label
tjmahr 1acd544
bump r version to support native pipe (added 2021)
tjmahr 4293eb8
- fixed roxygen warning
tjmahr 0993aa3
merge changes from main tree
tjmahr 9cb807c
final clean up
tjmahr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
#' @template args-group | ||
#' @template args-facet_args | ||
#' @param ... Currently unused. | ||
#' @param fun_avg Function to apply to compute the posterior average. | ||
#' Defaults to `"mean"`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. |
||
#' @param size,alpha Arguments passed to [ggplot2::geom_point()] to control the | ||
#' appearance of the points. | ||
#' @param ref_line If `TRUE` (the default) a dashed line with intercept 0 and | ||
|
@@ -31,10 +33,10 @@ | |
#' } | ||
#' \item{`ppc_scatter_avg()`}{ | ||
#' A single scatterplot of `y` against the average values of `yrep`, i.e., | ||
#' the points `(x,y) = (mean(yrep[, n]), y[n])`, where each `yrep[, n]` is | ||
#' a vector of length equal to the number of posterior draws. Unlike | ||
#' for `ppc_scatter()`, for `ppc_scatter_avg()` `yrep` should contain many | ||
#' draws (rows). | ||
#' the points `(x,y) = (average(yrep[, n]), y[n])`, where each `yrep[, n]` is | ||
#' a vector of length equal to the number of posterior draws and `average()` | ||
#' is summary statistic. Unlike for `ppc_scatter()`, for `ppc_scatter_avg()` | ||
tjmahr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' `yrep` should contain many draws (rows). | ||
#' } | ||
#' \item{`ppc_scatter_avg_grouped()`}{ | ||
#' The same as `ppc_scatter_avg()`, but a separate plot is generated for | ||
|
@@ -59,6 +61,9 @@ | |
#' p1 + lims | ||
#' p2 + lims | ||
#' | ||
#' # "average" function is customizable | ||
#' ppc_scatter_avg(y, yrep, fun_avg = "median", ref_line = FALSE) | ||
#' | ||
#' # for ppc_scatter_avg_grouped the default is to allow the facets | ||
#' # to have different x and y axes | ||
#' group <- example_group_data() | ||
|
@@ -116,6 +121,7 @@ ppc_scatter_avg <- | |
function(y, | ||
yrep, | ||
..., | ||
fun_avg = "mean", | ||
size = 2.5, | ||
alpha = 0.8, | ||
ref_line = TRUE) { | ||
|
@@ -125,7 +131,7 @@ ppc_scatter_avg <- | |
dots$group <- NULL | ||
} | ||
|
||
data <- ppc_scatter_avg_data(y, yrep, group = dots$group) | ||
data <- ppc_scatter_avg_data(y, yrep, group = dots$group, fun_avg = fun_avg) | ||
if (is.null(dots$group) && nrow(yrep) == 1) { | ||
inform( | ||
"With only 1 row in 'yrep' ppc_scatter_avg is the same as ppc_scatter." | ||
|
@@ -155,6 +161,7 @@ ppc_scatter_avg_grouped <- | |
yrep, | ||
group, | ||
..., | ||
fun_avg = "mean", | ||
facet_args = list(), | ||
size = 2.5, | ||
alpha = 0.8, | ||
|
@@ -184,16 +191,20 @@ ppc_scatter_data <- function(y, yrep) { | |
|
||
#' @rdname PPC-scatterplots | ||
#' @export | ||
ppc_scatter_avg_data <- function(y, yrep, group = NULL) { | ||
ppc_scatter_avg_data <- function(y, yrep, group = NULL, fun_avg = "mean") { | ||
y <- validate_y(y) | ||
yrep <- validate_predictions(yrep, length(y)) | ||
if (!is.null(group)) { | ||
group <- validate_group(group, length(y)) | ||
} | ||
|
||
data <- ppc_scatter_data(y = y, yrep = t(colMeans(yrep))) | ||
data <- ppc_scatter_data(y = y, yrep = t(apply(yrep, 2, FUN = fun_avg))) | ||
data$rep_id <- NA_integer_ | ||
levels(data$rep_label) <- "mean(italic(y)[rep]))" | ||
if (is.character(fun_avg)) { | ||
levels(data$rep_label) <- sprintf("%s(italic(y)[rep]))", fun_avg) | ||
} else { | ||
levels(data$rep_label) <- "Average(italic(y)[rep]))" | ||
} | ||
|
||
if (!is.null(group)) { | ||
data <- tibble::add_column(data, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
ppc_stat
functions, we have a similar argument, by the namestat
, which does a very similar job:We could align this doc to read, for example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Average y - y_rep
axis label is not affected. I didn't want to makeyrep_avg_label()
anderror_avg_label()
depend onfun_avg
or change the default "Average y - y_rep" labels.It does affect the
$rep_label
inppc_scatter_avg_data(y, yrep)
when fun_avg is a string.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait, i'm just noticing Aki's comment. I'll switch to stat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tjmahr. The code looks good. And I agree with changing to
stat