Skip to content

Commit

Permalink
- docs!: update @imports #102
Browse files Browse the repository at this point in the history
- style: use comments to structure function. rearrange condition checks, and helper functions. update indentation. #97
  • Loading branch information
egouldo committed Aug 28, 2024
1 parent 330bd6c commit a0d5b14
Showing 1 changed file with 71 additions and 62 deletions.
133 changes: 71 additions & 62 deletions R/make_viz.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
#'
#' @return a nested dataframe grouped by variables `exclusion_set`, `dataset`, `estimate_type`, `publishable_subset`, `expertise_subset`, `collinearity_subset` containing model summaries, tidy model summaries, model fit stats, funnel plots and forest plots
#' @export
#' @family targets-pipeline functions
#' @family Multi-dataset Wrapper Functions
#' @import dplyr
#' @importFrom purrr map_if map2 pmap possibly
#' @importFrom stringr str_detect
#' @importFrom broom.mixed tidy
#' @importFrom broom tidy
#' @importFrom performance performance
#' @importFrom metaviz viz_funnel
#' @importFrom ggplot2 ggplot
#' @importFrom parameters parameters
#' @import metafor
#' @import lme4
#' @importFrom tidyr pivot_longer
#' @importFrom tidyr unnest
#' @importFrom tidyr unnest pivot_longer
#' @importFrom rlang is_na
make_viz <- function(data) {
# targets wrapper function
# define map helper fun

# ---- Define Helper Functions ----

tidy_mod <- function(mod) {
broom.mixed::tidy(mod, conf.int = TRUE)
broom::tidy(mod, conf.int = TRUE)
}
# remove unnecessary inputs, create summary tables and visualisations
# repeat for yi and Zr

viz_funnel_2 <- function(x) {
metaviz::viz_funnel(x, y_axis = "precision")
}

poss_viz_funnel <- possibly(viz_funnel_2, otherwise = NA)

# ---- Prepare Data for Visualisation ----
if (any(str_detect(unique(data$estimate_type), pattern = "Zr"))) {
data_Zr <- data %>%
filter(estimate_type == "Zr") %>%
Expand Down Expand Up @@ -56,7 +61,7 @@ make_viz <- function(data) {
-effects_analysis
)
}

if (any(str_detect(unique(data$estimate_type), "y"))) {
data_yi <- data %>%
filter(estimate_type %in% c("yi", "y25", "y50", "y75")) %>%
Expand All @@ -83,79 +88,83 @@ make_viz <- function(data) {
) %>%
mutate(publishable_subset = NA)
}

if (exists("data_Zr") & exists("data_yi")) {
all_data <- bind_rows(data_Zr, data_yi)
} else if (exists("data_Zr")) {
all_data <- data_Zr
} else {
all_data <- data_yi
}

viz_funnel_2 <- function(x) {
metaviz::viz_funnel(x, y_axis = "precision")
}

poss_viz_funnel <- possibly(viz_funnel_2, otherwise = NA)


# ----- Extract Model Metrics and Apply Visualisation Functions -----

viz_out <-
all_data %>%
mutate(
mod_summary = map_if(
.x = model,
.p = ~ !rlang::is_na(.x),
.f = summary,
.else = ~ return(NA)
),
tidy_mod_summary = map_if(
.x = model,
.p = ~ !rlang::is_na(.x),
.f = purrr::possibly(tidy_mod,
otherwise = NA,
quiet = TRUE
),
.else = ~ return(NA)
),
mod_fit_stats = map_if(
.x = model,
.p = ~ !rlang::is_na(.x),
.f = purrr::possibly(performance::performance, # switch to performance from glance
otherwise = NA,
quiet = FALSE
mod_summary =
map_if(
.x = model,
.p = ~ !rlang::is_na(.x),
.f = summary,
.else = ~ return(NA)
),
.else = ~ return(NA)
),
funnel_plot = purrr::map_if(
.x = model,
.p = ~ any(class(.x) == "rma.uni"),
.f = poss_viz_funnel,
# metafor::funnel(., yaxis = "seinv") #alternative plot fun
.else = ~NA
),
forest_plot = ifelse(!rlang::is_na(model) & model_name == "MA_mod",
purrr::pmap(
.l =
list(model, estimate_type, dataset),
.f = purrr::possibly(gg_forest, otherwise = NA)
tidy_mod_summary =
map_if(
.x = model,
.p = ~ !rlang::is_na(.x),
.f = purrr::possibly(tidy_mod,
otherwise = NA,
quiet = TRUE
),
.else = ~ return(NA)
),
NA
),
MA_fit_stats = ifelse(model_name == "MA_mod" & !rlang::is_na(model),
mod_fit_stats =
map_if(
.x = model,
.p = ~ "rma.mv" %in% class(.x),
.f = purrr::possibly(get_MA_fit_stats, otherwise = NA),
.p = ~ !rlang::is_na(.x),
.f = purrr::possibly(performance::performance, # switch to performance from glance
otherwise = NA,
quiet = FALSE
),
.else = ~ return(NA)
),
NA
),
funnel_plot =
purrr::map_if(
.x = model,
.p = ~ any(class(.x) == "rma.uni"),
.f = poss_viz_funnel,
# metafor::funnel(., yaxis = "seinv") #alternative plot fun
.else = ~NA
),
forest_plot =
ifelse(!rlang::is_na(model) & model_name == "MA_mod",
purrr::pmap(
.l =
list(model, estimate_type, dataset),
.f = purrr::possibly(gg_forest, otherwise = NA)
),
NA
),
MA_fit_stats =
ifelse(model_name == "MA_mod" & !rlang::is_na(model),
map_if(
.x = model,
.p = ~ "rma.mv" %in% class(.x),
.f = purrr::possibly(get_MA_fit_stats, otherwise = NA),
.else = ~ return(NA)
),
NA
),
model_params = map_if(
.x = model,
.p = possibly(\(x) class(x) %in% parameters::supported_models() %>% any(), otherwise = NA),
.p = possibly(\(x) class(x) %in% parameters::supported_models() %>%
any(),
otherwise = NA),
.f = possibly(parameters::parameters, NA),
.else = ~ return(NA),
)
)

return(viz_out)
}

0 comments on commit a0d5b14

Please sign in to comment.