Skip to content
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

Add trend line to plot_trace() #140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions R/plot_trace.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#'
#' @param data An object of class [mice::mids].
#' @param vrb String, vector, or unquoted expression with variable name(s), default is "all".
#' @param trend Logical indicating whether a trend line should be displayed.
#'
#' @return An object of class [ggplot2::ggplot].
#'
#' @examples
#' imp <- mice::mice(mice::nhanes, print = FALSE)
#' plot_trace(imp)
#' @export
plot_trace <- function(data, vrb = "all") {
plot_trace <- function(data, vrb = "all", trend = FALSE) {
verify_data(data, imp = TRUE)
if (is.null(data$chainMean) && is.null(data$chainVar)) {
cli::cli_abort("No convergence diagnostics found", call. = FALSE)
Expand Down Expand Up @@ -60,12 +61,12 @@ plot_trace <- function(data, vrb = "all") {
))

# plot the convergence diagnostics
ggplot2::ggplot(long,
ggplot2::aes(
x = .data$.it,
y = .data$val,
color = as.factor(.data$.m)
)) +
gg <- ggplot2::ggplot(long,
ggplot2::aes(
x = .data$.it,
y = .data$val,
color = as.factor(.data$.m)
)) +
ggplot2::geom_line(linewidth = 0.6) +
ggplot2::geom_hline(yintercept = -Inf) +
ggplot2::facet_wrap(
Expand All @@ -88,4 +89,15 @@ plot_trace <- function(data, vrb = "all") {
strip.placement = "outside",
strip.switch.pad.wrap = ggplot2::unit(0, "cm")
)
if (trend) {
gg <- gg +
ggplot2::geom_smooth(
formula = y ~ x,
method = "loess",
se = FALSE,
color = "black",
linetype = "dashed"
)
}
return(gg)
}
4 changes: 3 additions & 1 deletion man/plot_trace.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading