-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5827c98
commit 4dfe058
Showing
1 changed file
with
41 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,42 +1,42 @@ | ||
#' Plot the posterior predictive pdf (interactions) | ||
#' #' Plot the posterior predictive pdf (interactions) | ||
#' #' | ||
#' #' Plot the posterior predictive pdf for each combination of 2 factors, | ||
#' #' fixing the others at the reference level. | ||
#' #' @param fit An object of class anova_bnp_model. | ||
#' #' @param d1 The 1st factor id. | ||
#' #' @param d2 The 2nd factor id. | ||
#' #' @return A ggplot2 plot. | ||
#' #' @importFrom dplyr mutate filter left_join | ||
#' #' @importFrom dplyr c_across across pull rowwise dense_rank | ||
#' #' @importFrom ggplot2 ggplot aes_string geom_line | ||
#' #' @importFrom rlang := .data | ||
#' #' @export | ||
#' predictive_plot_interaction <- function(fit, d1, d2) { | ||
#' # Get the relevant groups | ||
#' var1 <- paste0("x", d1) | ||
#' var2 <- paste0("x", d2) | ||
#' target_groups <- | ||
#' group_codes(fit) |> | ||
#' mutate(across(-c("group"), ~ dense_rank(.x))) |> | ||
#' mutate(x0 = 1) |> | ||
#' rowwise() |> | ||
#' mutate( | ||
#' touse = max(c_across(-c("group", var1, var2))), | ||
#' touse = .data$touse == 1, | ||
#' touse = .data$touse & (.data[[var1]] == 1 | .data[[var2]] != 1), | ||
#' touse = .data$touse & (.data[[var1]] != 1 | .data[[var2]] == 1) | ||
#' ) |> | ||
#' filter(.data$touse) |> | ||
#' pull("group") | ||
#' | ||
#' Plot the posterior predictive pdf for each combination of 2 factors, | ||
#' fixing the others at the reference level. | ||
#' @param fit An object of class anova_bnp_model. | ||
#' @param d1 The 1st factor id. | ||
#' @param d2 The 2nd factor id. | ||
#' @return A ggplot2 plot. | ||
#' @importFrom dplyr mutate filter left_join | ||
#' @importFrom dplyr c_across across pull rowwise dense_rank | ||
#' @importFrom ggplot2 ggplot aes_string geom_line | ||
#' @importFrom rlang := .data | ||
#' @export | ||
predictive_plot_interaction <- function(fit, d1, d2) { | ||
# Get the relevant groups | ||
var1 <- paste0("x", d1) | ||
var2 <- paste0("x", d2) | ||
target_groups <- | ||
group_codes(fit) |> | ||
mutate(across(-c("group"), ~ dense_rank(.x))) |> | ||
mutate(x0 = 1) |> | ||
rowwise() |> | ||
mutate( | ||
touse = max(c_across(-c("group", var1, var2))), | ||
touse = .data$touse == 1, | ||
touse = .data$touse & (.data[[var1]] == 1 | .data[[var2]] != 1), | ||
touse = .data$touse & (.data[[var1]] != 1 | .data[[var2]] == 1) | ||
) |> | ||
filter(.data$touse) |> | ||
pull("group") | ||
|
||
# Plot the posterior predictive pdf | ||
group_codes(fit) |> | ||
filter(.data$group %in% target_groups) |> | ||
left_join(f_post(fit)) |> | ||
mutate( | ||
{{ var1 }} := factor(.data[[var1]]), | ||
{{ var2 }} := factor(.data[[var2]]) | ||
) |> | ||
ggplot(aes_string(x = "y",y = "f", color = paste0(var1, ":", var2))) + | ||
geom_line() | ||
} | ||
#' # Plot the posterior predictive pdf | ||
#' group_codes(fit) |> | ||
#' filter(.data$group %in% target_groups) |> | ||
#' left_join(f_post(fit)) |> | ||
#' mutate( | ||
#' {{ var1 }} := factor(.data[[var1]]), | ||
#' {{ var2 }} := factor(.data[[var2]]) | ||
#' ) |> | ||
#' ggplot(aes_string(x = "y",y = "f", color = paste0(var1, ":", var2))) + | ||
#' geom_line() | ||
#' } |