## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## -----------------------------------------------------------------------------
read_epi_file <- function(name) {
  utils::read.csv(
    system.file("extdata", "epi", name, package = "risdr"),
    check.names = FALSE
  )
}

X_train <- read_epi_file("X_old.csv")
X_test <- read_epi_file("X_new.csv")
y_train <- read_epi_file("y_old.csv")[[1L]]
y_test <- read_epi_file("y_new.csv")[[1L]]

stopifnot(
  nrow(X_train) == 180L,
  nrow(X_test) == 180L,
  ncol(X_train) == 70L,
  identical(names(X_train), names(X_test)),
  length(y_train) == nrow(X_train),
  length(y_test) == nrow(X_test)
)

## ----raw-data-protocol, eval=FALSE--------------------------------------------
# library(dplyr)
# library(readr)
# 
# epi_data <- read_csv("data-raw/epi2024results.csv", show_col_types = FALSE)
# 
# epi_old <- epi_data |>
#   select(iso, country, ends_with(".old")) |>
#   rename_with(\(x) sub("[.]old$", "", x))
# 
# epi_new <- epi_data |>
#   select(iso, country, ends_with(".new")) |>
#   rename_with(\(x) sub("[.]new$", "", x))
# 
# X_old_full <- epi_old |> select(-iso, -country)
# X_new_full <- epi_new |> select(-iso, -country)
# 
# stopifnot(identical(names(X_old_full), names(X_new_full)))
# 
# missing_prop_old <- colMeans(is.na(X_old_full))
# keep_names <- names(missing_prop_old[missing_prop_old < 0.40])
# 
# X_old_reduced <- X_old_full[, keep_names, drop = FALSE]
# X_new_reduced <- X_new_full[, keep_names, drop = FALSE]
# 
# training_variance <- vapply(X_old_reduced, var, numeric(1), na.rm = TRUE)
# keep_names <- names(training_variance[training_variance > 1e-6])
# 
# X_old_reduced <- X_old_reduced[, keep_names, drop = FALSE]
# X_new_reduced <- X_new_reduced[, keep_names, drop = FALSE]
# 
# X_old_imputed <- VIM::kNN(X_old_reduced, k = 5, imp_var = FALSE)
# X_new_imputed <- VIM::kNN(X_new_reduced, k = 5, imp_var = FALSE)
# 
# y_train <- X_old_imputed$EPI
# X_train <- X_old_imputed |> select(-EPI)
# y_test <- X_new_imputed$EPI
# X_test <- X_new_imputed |> select(-EPI)

## ----fitting-template, eval=FALSE---------------------------------------------
# fit_epi <- fit_risdr(
#   X = X_train,
#   y = y_train,
#   sdr_method = "phd",
#   cov_method = "oas",
#   nslices = 6,
#   d_max = 10,
#   selector = "cicomp",
#   standardize = TRUE,
#   stabilize = TRUE
# )
# 
# epi_prediction <- predict(fit_epi, X_test)
# evaluate_prediction(y_test, epi_prediction, d = fit_epi$d)

## -----------------------------------------------------------------------------
comparison_path <- system.file(
  "extdata",
  "epi",
  "epi_sdr_covariance_comparison_tidy.csv",
  package = "risdr"
)
comparison <- utils::read.csv(comparison_path)

comparison[
  order(comparison$RMSE),
  c(
    "sdr_method", "cov_method", "selected_d", "condition_number",
    "RMSE", "MAE", "R2", "Adjusted_R2", "Correlation"
  )
]

## -----------------------------------------------------------------------------
cv_path <- system.file(
  "extdata",
  "epi",
  "epi_repeated_cv_summary_tidy.csv",
  package = "risdr"
)
cv_summary <- utils::read.csv(cv_path)

cv_summary[
  order(cv_summary$mean_RMSE),
  c(
    "sdr_method", "cov_method", "n_success", "mean_selected_d",
    "mean_RMSE", "sd_RMSE", "mean_Adjusted_R2", "mean_Correlation"
  )
]

