diff --git a/chapters/evaluation_data_input.qmd b/chapters/evaluation_data_input.qmd index c361abf..9439b40 100644 --- a/chapters/evaluation_data_input.qmd +++ b/chapters/evaluation_data_input.qmd @@ -15,7 +15,7 @@ library(formattable) # others ---- library(here) -library(aggreCAT) +# library(aggreCAT) library(DescTools) select <- dplyr::select diff --git a/code/DistAggModified.R b/code/DistAggModified.R index 6ef7458..1c2e0c9 100644 --- a/code/DistAggModified.R +++ b/code/DistAggModified.R @@ -1,124 +1,138 @@ -# aggreCAT::DistributionWAgg Modified to also include 90% upper and lower bounds of the aggregated distribution + +# aggreCAT::DistributionWAgg modified to also include 90% upper and lower bounds +# of the aggregated distribution. +# preprocess_judgements() is taken from aggreCAT with some modifications + +preprocess_judgements <- function(expert_judgements, + round_2_filter = TRUE, + three_point_filter = TRUE, + percent_toggle = FALSE){ + + if(any(is.na(expert_judgements$value))) stop("NAs Found in Values") + + # Variables of focus + expert_judgements <- expert_judgements %>% + dplyr::select(round, + paper_id, + user_name, + element, + value) + + filter_round <- function(expert_judgements, round_2_filter){ + output_df <- if(isTRUE(round_2_filter)){ + expert_judgements %>% + dplyr::filter(round %in% "round_2") + } else { + expert_judgements + } + } + + filter_element <- function(expert_judgements, three_point_filter){ + output_df <- if(isTRUE(three_point_filter)){ + expert_judgements %>% + dplyr::group_by(round, paper_id, user_name) %>% + dplyr::filter(element %in% c("three_point_best", + "three_point_lower", + "three_point_upper")) + } else { + expert_judgements %>% + dplyr::filter(element != "binary_question") + } + } + + change_value <- function(expert_judgements, percent_toggle){ + # Converts values to 0,1 + output_df <- if(isTRUE(percent_toggle)){ + expert_judgements %>% + dplyr::mutate(value = + dplyr::case_when( + element %in% c("three_point_best", + "three_point_lower", + "three_point_upper") ~ value / 100, + TRUE ~ value + )) + } else { + expert_judgements + } + + } + + check_values <- function(expert_judgements, round_2_filter, three_point_filter, percent_toggle){ + if(isTRUE(percent_toggle)){ + if(isTRUE(max(expert_judgements$value) > 1)) { + warning("Non probability value outside 0,1 ") + } + } + + return(expert_judgements) + } + + method_out <- expert_judgements %>% + filter_round(round_2_filter) %>% + filter_element(three_point_filter) %>% + change_value(percent_toggle) %>% + dplyr::bind_rows() %>% + check_values(round_2_filter, three_point_filter, percent_toggle) %>% + dplyr::ungroup() + + return(method_out) +} DistributionWAggMOD <- function(expert_judgements, - type = "DistribArMean", name = NULL, placeholder = FALSE, percent_toggle = FALSE, round_2_filter = TRUE) { - - if(!(type %in% c("DistribArMean", - "TriDistribArMean"))){ - - stop('`type` must be one of "DistribArMean" or "TriDistribArMean"') - - } - + ## Set name argument name <- ifelse(is.null(name), - type, + "DistributionWAggMOD", name) - - cli::cli_h1(sprintf("DistributionWAgg: %s", - name)) - - if(isTRUE(placeholder)){ - - method_placeholder(expert_judgements, - name) - - } else { - + df <- expert_judgements %>% - preprocess_judgements(percent_toggle = {{percent_toggle}}, - round_2_filter = {{round_2_filter}}) %>% + preprocess_judgements(percent_toggle = percent_toggle, + round_2_filter = round_2_filter) %>% dplyr::group_by(paper_id) - # create different Fx_fun and avdist_fun based on "type" - switch(type, - "DistribArMean" = { - - Fx_fun <- function(x, lower, best, upper) { - dplyr::case_when( - x < 0 ~ 0, - x >= 0 & x < lower ~ 0.05 / lower * x, - x >= lower & - x < best ~ 0.45 / (best - lower) * (x - lower) + 0.05, - x >= best & - x < upper ~ 0.45 / (upper - best) * (x - best) + 0.5, - x >= upper & - x < 1 ~ 0.05 / (1 - upper) * (x - upper) + 0.95, - x > 1 ~ 1 - ) - } - - avdist_fun <- function(dq, claim_input) { - claim_input %>% - dplyr::mutate(Fx = purrr::pmap( - list(three_point_lower, - three_point_best, - three_point_upper), - .f = function(l, b, u) { - function(x) { - Fx_fun(x, - - lower = l, - best = b, - upper = u) - } - } - )) %>% - purrr::pluck("Fx") %>% - purrr::map_dbl( - .f = function(Fx_fun) { - Fx_fun(dq) - } - ) %>% - mean() - } - - }, - "TriDistribArMean" = { - - Fx_fun <- function(x, lower, best, upper) { - dplyr::case_when( - x < lower ~ 0, - x >= lower & - x < best ~ ((x-lower) ^ 2) / ((upper - lower) * (best - lower)), - x >= best & - x < upper ~ 1 - (((upper - x) ^ 2) / ((upper - lower) * (upper - best))), - x >= upper ~ 1 - ) - } - - avdist_fun <- function(dq, claim_input) { - claim_input %>% - dplyr::mutate(Fx = purrr::pmap( - list(three_point_lower, - three_point_best, - three_point_upper), - .f = function(l, b, u) { - function(x) { - Fx_fun(x, - - lower = l, - best = b, - upper = u) - } - } - )) %>% - purrr::pluck("Fx") %>% - purrr::map_dbl( - .f = function(Fx_fun) { - Fx_fun(dq) - } - ) %>% - mean() - } - - }) - + Fx_fun <- function(x, lower, best, upper) { + dplyr::case_when( + x < 0 ~ 0, + x >= 0 & x < lower ~ 0.05 / lower * x, + x >= lower & + x < best ~ 0.45 / (best - lower) * (x - lower) + 0.05, + x >= best & + x < upper ~ 0.45 / (upper - best) * (x - best) + 0.5, + x >= upper & + x < 1 ~ 0.05 / (1 - upper) * (x - upper) + 0.95, + x > 1 ~ 1 + ) + } + + avdist_fun <- function(dq, claim_input) { + claim_input %>% + dplyr::mutate(Fx = purrr::pmap( + list(three_point_lower, + three_point_best, + three_point_upper), + .f = function(l, b, u) { + function(x) { + Fx_fun(x, + + lower = l, + best = b, + upper = u) + } + } + )) %>% + purrr::pluck("Fx") %>% + purrr::map_dbl( + .f = function(Fx_fun) { + Fx_fun(dq) + } + ) %>% + mean() + } agg_judge_df <- df %>% tidyr::pivot_wider(names_from = element, values_from = value) %>% dplyr::mutate( @@ -208,7 +222,9 @@ DistributionWAggMOD <- function(expert_judgements, x <- quantiles %>% dplyr::left_join(n_experts, by = "paper_id") %>% - dplyr::select(paper_id, aggregated_judgement, aggregated_judgement_90ci_lb, aggregated_judgement_90ci_ub, n_experts) %>% + dplyr::select(paper_id, aggregated_judgement, + aggregated_judgement_90ci_lb, aggregated_judgement_90ci_ub, + n_experts) %>% dplyr::ungroup() x %>% @@ -226,5 +242,4 @@ DistributionWAggMOD <- function(expert_judgements, agg_90ci_lb = aggregated_judgement_90ci_lb, agg_90ci_ub = aggregated_judgement_90ci_ub ) - } } diff --git a/renv.lock b/renv.lock index a9064fa..4ba11ca 100644 --- a/renv.lock +++ b/renv.lock @@ -145,42 +145,6 @@ ], "Hash": "999d7960fe5a5fd98267d0b68fd10065" }, - "R2WinBUGS": { - "Package": "R2WinBUGS", - "Version": "2.1-22.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "boot", - "coda", - "graphics", - "stats", - "utils" - ], - "Hash": "5f50ae507977ff05af153a3b76a775a7" - }, - "R2jags": { - "Package": "R2jags", - "Version": "0.8-5", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "R2WinBUGS", - "abind", - "coda", - "grDevices", - "graphics", - "methods", - "parallel", - "rjags", - "stats", - "stringr", - "utils" - ], - "Hash": "1d783d1b22f80a173631a45b505f3eb8" - }, "R6": { "Package": "R6", "Version": "2.5.1", @@ -212,65 +176,6 @@ ], "Hash": "5ea2700d21e038ace58269ecdbeb9ec0" }, - "VGAM": { - "Package": "VGAM", - "Version": "1.1-11", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "methods", - "splines", - "stats", - "stats4" - ], - "Hash": "0a347b0c06e87ad505699b7a6290abd2" - }, - "abind": { - "Package": "abind", - "Version": "1.4-5", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "methods", - "utils" - ], - "Hash": "4f57884290cc75ab22f4af9e9d4ca862" - }, - "aggreCAT": { - "Package": "aggreCAT", - "Version": "0.0.0.9004", - "Source": "GitHub", - "RemoteType": "github", - "RemoteHost": "api.github.com", - "RemoteUsername": "unjournal", - "RemoteRepo": "aggreCAT", - "RemoteRef": "master", - "RemoteSha": "cd221069bfbc624c28dcc9ad040d61db2be76a0c", - "Remotes": "softloud/neet, unjournal/rfUtilities@f64894e", - "Requirements": [ - "GoFKernel", - "R", - "R2jags", - "VGAM", - "cli", - "coda", - "crayon", - "dplyr", - "ggplot2", - "insight", - "magrittr", - "mathjaxr", - "precrec", - "purrr", - "rfUtilities", - "stringr", - "tibble", - "tidyr" - ], - "Hash": "8375c42308d4931b23a97b17cdcd00c2" - }, "airtabler": { "Package": "airtabler", "Version": "0.1.6", @@ -298,16 +203,6 @@ ], "Hash": "cad6cf7f1d5f6e906700b9d3e718c796" }, - "assertthat": { - "Package": "assertthat", - "Version": "0.2.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "tools" - ], - "Hash": "50c838a310445e954bc13f26f26a6ecf" - }, "backports": { "Package": "backports", "Version": "1.4.1", @@ -506,31 +401,6 @@ ], "Hash": "3f038e5ac7f41d4ac41ce658c85e3042" }, - "cluster": { - "Package": "cluster", - "Version": "2.1.6", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "grDevices", - "graphics", - "stats", - "utils" - ], - "Hash": "0aaa05204035dc43ea0004b9c76611dd" - }, - "coda": { - "Package": "coda", - "Version": "0.19-4.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "lattice" - ], - "Hash": "af436915c590afc6fffc3ce3a5be1569" - }, "colorspace": { "Package": "colorspace", "Version": "2.1-0", @@ -992,20 +862,6 @@ ], "Hash": "d6db1667059d027da730decdc214b959" }, - "gridExtra": { - "Package": "gridExtra", - "Version": "2.3", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "grDevices", - "graphics", - "grid", - "gtable", - "utils" - ], - "Hash": "7d7f283939f563670a697165b2cf5560" - }, "gtable": { "Package": "gtable", "Version": "0.3.5", @@ -1149,19 +1005,6 @@ ], "Hash": "99df65cfef20e525ed38c3d2577f7190" }, - "insight": { - "Package": "insight", - "Version": "0.19.10", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "methods", - "stats", - "utils" - ], - "Hash": "c15a38c9655cba66f5f5537a14c1bef4" - }, "irr": { "Package": "irr", "Version": "0.84.1", @@ -1322,13 +1165,6 @@ ], "Hash": "7ce2733a9826b3aeb1775d56fd305472" }, - "mathjaxr": { - "Package": "mathjaxr", - "Version": "1.6-0", - "Source": "Repository", - "Repository": "CRAN", - "Hash": "87da6ccdcee6077a7d5719406bf3ae45" - }, "memoise": { "Package": "memoise", "Version": "2.0.1", @@ -1502,26 +1338,6 @@ ], "Hash": "a1ac5c03ad5ad12b9d1597e00e23c3dd" }, - "precrec": { - "Package": "precrec", - "Version": "0.14.4", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "Rcpp", - "assertthat", - "data.table", - "ggplot2", - "graphics", - "grid", - "gridExtra", - "methods", - "rlang", - "withr" - ], - "Hash": "e5a70cbe83260613031dfdc9a91496e0" - }, "prettyunits": { "Package": "prettyunits", "Version": "1.2.0", @@ -1644,17 +1460,6 @@ ], "Hash": "082e1a198e3329d571f4448ef0ede4bc" }, - "randomForest": { - "Package": "randomForest", - "Version": "4.7-1.1", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "stats" - ], - "Hash": "b52825075358b1ebd159e262bf40649d" - }, "rappdirs": { "Package": "rappdirs", "Version": "0.3.3", @@ -1752,35 +1557,6 @@ ], "Hash": "1425f91b4d5d9a8f25352c44a3d914ed" }, - "rfUtilities": { - "Package": "rfUtilities", - "Version": "2.1-4", - "Source": "GitHub", - "Repository": "CRAN", - "RemoteType": "github", - "RemoteHost": "api.github.com", - "RemoteUsername": "unjournal", - "RemoteRepo": "rfUtilities", - "RemoteRef": "f64894efe08d569cdb5cd74e625b72f09019caaa", - "RemoteSha": "f64894efe08d569cdb5cd74e625b72f09019caaa", - "Requirements": [ - "R", - "cluster", - "randomForest" - ], - "Hash": "327e1ced41e7f356564d67d8379d6d48" - }, - "rjags": { - "Package": "rjags", - "Version": "4-15", - "Source": "Repository", - "Repository": "CRAN", - "Requirements": [ - "R", - "coda" - ], - "Hash": "dedea2fb969477a705de3b1a04e1aa25" - }, "rlang": { "Package": "rlang", "Version": "1.1.3",