Skip to content

Commit

Permalink
added ks in Description as package requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Jan 23, 2025
1 parent fd0caca commit c5b292b
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 11 deletions.
41 changes: 31 additions & 10 deletions Paper/MeasurementVariance/DistributionFitting.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
setwd("/home/konrad/Documents/GitHub/RProjects/Thermosimfit/Paper/MeasurementVariance/")
library(ggplot2)
library(ks)
library(cowplot)
Expand Down Expand Up @@ -187,6 +188,12 @@ fit_distri <- function(data, distri) {
)
}

# Median IQR
# ========================================
median_iqr <- function(x) {
c(median(x), quantile(x, 0.25), quantile(x, 0.75))
}

# Bootstrap method for calculating CI for the median
# ========================================
calc_location_ci_bootstrap <- function(
Expand Down Expand Up @@ -352,6 +359,7 @@ plotting <- function(data, idx, distri, density_data, kd4_m_ci) {
data[, idx] <- transform(data[, idx], distri)
bins <- hist(data[, idx], plot = FALSE)$breaks
median_ci <- calc_location_ci_bootstrap(median, data[, idx])
median_iqr <- median_iqr(data[, idx])
mean_ci <- calc_location_ci_bootstrap(mean, data[, idx])
kde_ci <- kde(data[, idx])
fd <- fit_distri(data[, idx], distri)
Expand All @@ -363,18 +371,18 @@ plotting <- function(data, idx, distri, density_data, kd4_m_ci) {
y <- c(3, 3, 1.4, 2.1)[idx]
location_error <- data.frame(
x = c(
mean_ci$location, median_ci$location, kde_ci$mode, kd4_m_ci[1]
mean_ci$location, median_ci$location, kde_ci$mode, kd4_m_ci[1], median_iqr[1]
),
xmin = c(
mean_ci$lower_ci, median_ci$lower_ci, kde_ci$lower_ci, kd4_m_ci[2]
mean_ci$lower_ci, median_ci$lower_ci, kde_ci$lower_ci, kd4_m_ci[2], median_iqr[2]
),
xmax = c(
mean_ci$upper_ci, median_ci$upper_ci, kde_ci$upper_ci, kd4_m_ci[3]
mean_ci$upper_ci, median_ci$upper_ci, kde_ci$upper_ci, kd4_m_ci[3], median_iqr[3]
),
type = c(
"Mean", "Median", "Mode (Kernel density)", "Mode (joint Kernel density)"
"Mean", "Median", "Mode (Kernel density)", "Mode (joint Kernel density)", "Median IQR"
),
y = c(0.5, 0.6, 0.7, 0.8)
y = c(0.5, 0.6, 0.7, 0.8, 0.9)
)

p <- ggplot() +
Expand Down Expand Up @@ -407,7 +415,7 @@ plotting <- function(data, idx, distri, density_data, kd4_m_ci) {
) +
labs(x = names(data)[idx], y = "Density")

colors <- RColorBrewer::brewer.pal(4, "Dark2")
colors <- RColorBrewer::brewer.pal(5, "Dark2")
p <- p +
geom_errorbarh(
data = location_error,
Expand All @@ -425,7 +433,8 @@ plotting <- function(data, idx, distri, density_data, kd4_m_ci) {
"Mean" = colors[1],
"Median" = colors[2],
"Mode (Kernel density)" = colors[3],
"Mode (joint Kernel density)" = colors[4]
"Mode (joint Kernel density)" = colors[4],
"Median IQR" = colors[5]
)
) +
scale_linetype_manual(
Expand Down Expand Up @@ -502,7 +511,7 @@ calc_values <- function(df, distris) {
l <- back_transform(res$lower_ci[idx], distris[idx], min, max)
u <- back_transform(res$upper_ci[idx], distris[idx], min, max)
df_temp <- data.frame(
values = c(mode, l, u),
values = sprintf("%.3e", c(mode, l, u)),
type = c("mode", "lower", "upper")
)
names(df_temp)[1] <- names(df)[idx]
Expand All @@ -517,6 +526,18 @@ calc_values <- function(df, distris) {
row.names(res) <- NULL
return(res)
}
calc_values(p_dba, distris)
calc_values(p_ida, distris)
dba_res <- calc_values(p_dba, distris)
dba_res
ida_res <- calc_values(p_ida, distris)
ida_res
calc_values(p_gda, distris)


# Calc IQR
calc_iqr <- function(df) {
df <- df[, 1:4]
lapply(1:4, function(x) {
median_iqr(df[, x])
})
}
calc_iqr(p_dba)
139 changes: 139 additions & 0 deletions Paper/MeasurementVariance/DistributionFittingTables.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
format:
html:
code-fold: true
---

```{r}
#| warning: false
#| echo: false
setwd("/home/konrad/Documents/GitHub/RProjects/Thermosimfit/Paper/MeasurementVariance/")
library(ks)
# Load the results
# ========================================
extract_best_runs <- function(res) {
states <- res$states
params <- res$params
errors <- res$metrices
errors <- Reduce(rbind, errors)
states <- Reduce(rbind, states)
params <- Reduce(rbind, params)
params <- lapply(unique(errors$dataset), function(x) {
params_subset <- params[params$dataset == x, ]
errors_subset <- errors[errors$dataset == x, ]
errors_subset <- errors_subset[order(errors_subset$MeanSquareError), ][1:50, ]
res <- params_subset[params_subset$repetition %in% errors_subset$repetition, ]
res <- res[, 1:4]
res$error <- errors_subset$MeanSquareError
return(res)
})
params <- Reduce(rbind, params)
return(params)
}
load_params <- function(path) {
load(path)
extract_best_runs(res[[1]])
}
p_dba <- load_params("dba_100Runs.RData")
p_ida <- load_params("ida_100.RData")
p_gda <- load_params("gda_100.RData")
# Transform the data
# ========================================
transform <- function(data, distri) {
data <- (data - min(data)) / (max(data) - min(data))
data <- ifelse(data < 1e-6, 1e-6, data) # Avoid log(0)
if (distri != "norm" && distri != "exp") {
if (min(data) < 1e-2) {
data <- data + 0.01
}
}
return(data)
}
kde4d_intern <- function(df) {
mins <- apply(df, 2, min)
maxs <- apply(df, 2, max)
res <- ks::kde(df, xmin = mins, xmax = maxs)
grid_points <- expand.grid(res$eval.points)
joint_densities <- as.vector(res$estimate)
density_data <- cbind(grid_points, joint_density = joint_densities)
return(density_data)
}
kde4d_with_smr <- function(df, distris, prob = 0.95) {
df <- df[, 1:4]
df <- mapply(transform, df, distris, SIMPLIFY = FALSE)
df <- as.data.frame(df)
res <- ks::kde(df)
level <- paste0(prob * 100, "%")
density_threshold <- res$cont[level]
grid_points <- expand.grid(res$eval.points)
densities <- as.vector(res$estimate)
significant_points <- grid_points[densities >= density_threshold, ]
mode_index <- which.max(densities)
mode <- grid_points[mode_index, ]
mode <- ifelse(mode < 0, 0, mode) |> as.numeric()
CIs <- apply(significant_points, 2, range)
lc <- CIs[1, ]
lc <- ifelse(lc < 0, 0, lc)
uc <- CIs[2, ]
uc <- ifelse(uc < 0, 0, uc)
res <- kde4d_intern(df)
res[, 5] <- transform(res[, 5], "norm")
df <- lapply(1:4, function(x) {
i <- parent.frame()$i[]
data.frame(x = res[, i], y = res[, 5])
})
return(list(
mode = mode,
lower_ci = lc,
upper_ci = uc,
df = df
))
}
# Calculate results
distis <- rep("norm", 4)
back_transform <- function(transformed_data, distri,
original_min, original_max) {
original_data <- transformed_data *
(original_max - original_min) + original_min
return(original_data)
}
calc_values <- function(df, distris) {
res <- kde4d_with_smr(df, distris)
res$mode
res$lower_ci
res$upper_ci
res <- lapply(1:4, function(idx) {
max <- max(df[, idx])
min <- min(df[, idx])
mode <- back_transform(res$mode[idx], distris[idx], min, max)
l <- back_transform(res$lower_ci[idx], distris[idx], min, max)
u <- back_transform(res$upper_ci[idx], distris[idx], min, max)
df_temp <- data.frame(
values = c(mode, l, u),
type = c("mode", "lower", "upper")
)
names(df_temp)[1] <- names(df)[idx]
return(df_temp)
})
res <- lapply(res, function(x) {
x[, 1]
})
res <- Reduce(rbind, res) |> as.data.frame()
res <- cbind(names(df)[1:4], res)
names(res) <- c("Parameter", "mode", "lower", "upper")
row.names(res) <- NULL
return(res)
}
dba_res <- calc_values(p_dba, distris)
ida_res <- calc_values(p_ida, distris)
gda_res <- calc_values(p_gda, distris)
knitr::kable(dba_res, format = "latex")
```

Binary file modified Paper/MeasurementVariance/LocationEstimation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion tsf/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Imports:
callr,
cowplot,
RColorBrewer,
plotly
plotly,
ks
Suggests: knitr, rmarkdown, tinytest, shinytest2
VignetteBuilder: knitr
RoxygenNote: 7.3.2
Expand Down

0 comments on commit c5b292b

Please sign in to comment.