|
| 1 | +# TODO: update the plot and the text |
| 2 | + |
| 3 | +`%||%` <- function(a, b) if (!is.null(a)) a else b |
| 4 | + |
| 5 | +kde4d_intern <- function(df, n_samp = 20000) { |
| 6 | + stopifnot(ncol(df) == 4) |
| 7 | + colnames(df) <- colnames(df) %||% paste0("V", seq_len(ncol(df))) |
| 8 | + H <- ks::Hpi(as.matrix(df)) |
| 9 | + fit <- ks::kde(as.matrix(df), H = H) |
| 10 | + draws <- as.data.frame(ks::rkde(n = n_samp, fhat = fit)) |
| 11 | + colnames(draws) <- colnames(df) |
| 12 | + marginals <- lapply(seq_len(ncol(draws)), function(j) { |
| 13 | + d <- density(draws[[j]], n = 512) |
| 14 | + data.frame(x = d$x, y = d$y) |
| 15 | + }) |
| 16 | + names(marginals) <- colnames(draws) |
| 17 | + list(fit = fit, draws = draws, marginals = marginals) |
| 18 | +} |
| 19 | +kde4d_with_smr <- function(df, prob = 0.95, n_samp = 20000) { |
| 20 | + stopifnot(ncol(df) == 4) |
| 21 | + ki <- kde4d_intern(df, n_samp = n_samp) |
| 22 | + fit <- ki$fit |
| 23 | + Xs <- ki$draws |
| 24 | + grid <- expand.grid(fit$eval.points) |
| 25 | + dens <- as.vector(fit$estimate) |
| 26 | + mode <- as.numeric(grid[which.max(dens), ]) |
| 27 | + names(mode) <- colnames(df) |
| 28 | + alpha <- (1 - prob) / 2 |
| 29 | + qs <- t(apply(Xs, 2, quantile, probs = c(alpha, 1 - alpha), na.rm = TRUE)) |
| 30 | + colnames(qs) <- c("lower", "upper") |
| 31 | + rownames(qs) <- colnames(df) |
| 32 | + list( |
| 33 | + mode = mode, |
| 34 | + lower_ci = qs[, "lower"], |
| 35 | + upper_ci = qs[, "upper"], |
| 36 | + df = ki$marginals |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +# # NOTE: Using Contour Levels for Significant Model Regions |
| 41 | +# # Significant Model Regions (SMR) |
| 42 | +# kde4d_intern <- function(df) { |
| 43 | +# mins <- apply(df, 2, min) |
| 44 | +# maxs <- apply(df, 2, max) |
| 45 | +# res <- ks::kde(df, xmin = mins, xmax = maxs) |
| 46 | +# grid_points <- expand.grid(res$eval.points) |
| 47 | +# joint_densities <- as.vector(res$estimate) |
| 48 | +# density_data <- cbind(grid_points, joint_density = joint_densities) |
| 49 | +# return(density_data) |
| 50 | +# } |
| 51 | + |
| 52 | +# kde4d_with_smr <- function(df, prob = 0.95) { |
| 53 | +# df <- df[, 1:4] |
| 54 | +# res <- ks::kde(df) |
| 55 | +# level <- paste0(prob * 100, "%") |
| 56 | +# density_threshold <- res$cont[level] |
| 57 | +# grid_points <- expand.grid(res$eval.points) |
| 58 | +# densities <- as.vector(res$estimate) |
| 59 | +# significant_points <- grid_points[densities >= density_threshold, ] |
| 60 | +# mode_index <- which.max(densities) |
| 61 | +# mode <- grid_points[mode_index, ] |
| 62 | +# mode <- ifelse(mode < 0, 0, mode) |> as.numeric() |
| 63 | +# CIs <- apply(significant_points, 2, range) |
| 64 | +# lc <- CIs[1, ] |
| 65 | +# lc <- ifelse(lc < 0, 0, lc) |
| 66 | +# uc <- CIs[2, ] |
| 67 | +# uc <- ifelse(uc < 0, 0, uc) |
| 68 | +# res <- kde4d_intern(df) |
| 69 | +# df <- lapply(1:4, function(x) { |
| 70 | +# i <- parent.frame()$i[] |
| 71 | +# data.frame(x = res[, i], y = res[, 5]) |
| 72 | +# }) |
| 73 | +# return(list( |
| 74 | +# mode = mode, |
| 75 | +# lower_ci = lc, |
| 76 | +# upper_ci = uc, |
| 77 | +# df = df |
| 78 | +# )) |
| 79 | +# } |
| 80 | + |
| 81 | +jkd <- function(df) { |
| 82 | + res <- kde4d_with_smr(df) |
| 83 | + res$mode |
| 84 | + res$lower_ci |
| 85 | + res$upper_ci |
| 86 | + res <- lapply(1:4, function(idx) { |
| 87 | + mode <- res$mode[idx] |
| 88 | + l <- res$lower_ci[idx] |
| 89 | + u <- res$upper_ci[idx] |
| 90 | + df_temp <- data.frame( |
| 91 | + values = c(mode, l, u), |
| 92 | + type = c("mode", "lower", "upper") |
| 93 | + ) |
| 94 | + names(df_temp)[1] <- names(df)[idx] |
| 95 | + return(df_temp) |
| 96 | + }) |
| 97 | + res <- lapply(res, function(x) { |
| 98 | + x[, 1] |
| 99 | + }) |
| 100 | + res <- Reduce(rbind, res) |> as.data.frame() |
| 101 | + res <- cbind(names(df)[1:4], res) |
| 102 | + names(res) <- c("Parameter", "mode", "lower", "upper") |
| 103 | + row.names(res) <- NULL |
| 104 | + return(res) |
| 105 | +} |
| 106 | + |
| 107 | +make_joint_sampler_kde <- function(df, lb, ub) { |
| 108 | + eps <- 1e-6 |
| 109 | + p <- ncol(df) |
| 110 | + to_unit <- function(X) { |
| 111 | + U <- sweep(X, 2, lb, "-") |
| 112 | + U <- sweep(U, 2, (ub - lb), "/") |
| 113 | + pmin(pmax(U, eps), 1 - eps) |
| 114 | + } |
| 115 | + to_param <- function(U) { |
| 116 | + U <- sweep(U, 2, (ub - lb), "*") |
| 117 | + sweep(U, 2, lb, "+") |
| 118 | + } |
| 119 | + U <- to_unit(as.matrix(df)) |
| 120 | + Z <- qlogis(U) |
| 121 | + kde_obj <- ks::kde(Z) |
| 122 | + function(n) { |
| 123 | + Znew <- ks::rkde(n = n, fhat = kde_obj) |
| 124 | + Unew <- plogis(Znew) |
| 125 | + Xnew <- to_param(Unew) |
| 126 | + Xdf <- as.data.frame(Xnew) |
| 127 | + colnames(Xdf) <- colnames(df) |
| 128 | + Xdf |
| 129 | + } |
| 130 | +} |
| 131 | +sobolVariance_dep <- function(parameter_df, lossFct, env, lb, ub, parameterNames, runAsShiny) { |
| 132 | + n <- 1000 |
| 133 | + nboot <- 100 |
| 134 | + joint_sampler <- make_joint_sampler_kde(parameter_df, lb, ub) |
| 135 | + X <- joint_sampler(n) |
| 136 | + names(X) <- parameterNames |
| 137 | + sobolFun <- function(X) { |
| 138 | + p <- NULL |
| 139 | + if (is.data.frame(X) || is.matrix(X)) { |
| 140 | + return(sapply(1:nrow(X), function(x) { |
| 141 | + lossFct(as.numeric(X[x, ]), env, FALSE) |
| 142 | + })) |
| 143 | + } else { |
| 144 | + p <- as.numeric(X) |
| 145 | + } |
| 146 | + lossFct(p, env, FALSE) |
| 147 | + } |
| 148 | + sh <- shapleysobol_knn(model = sobolFun, X = X, nboot = nboot) |
| 149 | + ggplot(sh) + |
| 150 | + theme(axis.text.x = element_text(size = 8, angle = 90, hjust = 1), |
| 151 | + axis.text.y = element_text(size = 8)) + |
| 152 | + ylab("Explained fraction of variance (Shapley effects)") |
| 153 | +} |
| 154 | + |
1 | 155 | # Monte Carlo Estimation of Sobol’ Indices |
2 | 156 | sobolVariance <- function(lossFct, env, lb, ub, parameterNames, runAsShiny) { |
3 | 157 | n <- 1000 |
@@ -38,8 +192,6 @@ sobolVariance <- function(lossFct, env, lb, ub, parameterNames, runAsShiny) { |
38 | 192 | ylab("Explained fraction of variance") |
39 | 193 | } |
40 | 194 |
|
41 | | - |
42 | | - |
43 | 195 | #' Optimize algebraic systems which describe thermodynamic binding systems |
44 | 196 | #' |
45 | 197 | #' @export |
|
0 commit comments