R/graph_inference.R

Defines functions posterior_crossvalidation_loo Efnorm Exy Exx LS SCRPS CRPS posterior_crossvalidation_covariance posterior_crossvalidation.graph_lme posterior_crossvalidation_manual posterior_crossvalidation_covariance_manual posterior_mean_covariance

Documented in posterior_crossvalidation.graph_lme posterior_crossvalidation_loo

#' Posterior mean for Gaussian random field models on metric graphs assuming
#' observations at vertices
#'
#' @param theta Estimated model parameters (sigma_e, tau, kappa).
#' @param graph A `metric_graph`  object.
#' @param model Type of model: "alpha1" gives SPDE with alpha=1, "GL1" gives
#' the model based on the graph Laplacian with smoothness 1, "GL2" gives the
#' model based on the graph Laplacian with smoothness 2, and "isoExp" gives a
#' model with isotropic exponential covariance
#' @details This function does not use sparsity for any model.
#'
#' @return Vector with the posterior mean evaluated at the observation locations
#' @noRd
posterior_mean_covariance <- function(theta, graph, model = "alpha1")
{
  check <- check_graph(graph)

  if(is.null(graph$PtV) && (model != "isoExp")){
    stop("You must run graph$observation_to_vertex() first.")
  }

  n.o <- length(graph$y)
  n.v <- dim(graph$V)[1]

  sigma_e <- theta[1]
  tau <- theta[2]
  kappa <- theta[3]

  #build covariance matrix
  if (model == "alpha1") {
    Q <- Qalpha1(c(tau, kappa), graph)
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "alpha2") {
    n_const <- length(graph$CoB$S)
    if (n_const > 0) {
      Tc <- graph$CoB$T[-seq_len(n_const), , drop = FALSE]
    } else {
      Tc <- graph$CoB$T
    }
    Q <- Qalpha2(c(tau, kappa), graph, BC = 1)
    Qtilde <- Tc %*% Q %*% t(Tc)
    Sigma.overdetermined = t(Tc) %*% solve(Qtilde) %*% Tc
    index.obs <- 4*(graph$PtE[, 1] - 1) + (1 * (graph$PtE[, 2] == 0)) +
      (3 * (graph$PtE[, 2] != 0))
    Sigma <-  as.matrix(Sigma.overdetermined[index.obs, index.obs])
  } else if (model == "GL1"){
    Q <- (kappa^2 * Matrix::Diagonal(graph$nV, 1) + graph$Laplacian[[1]]) * tau^2
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "GL2"){
    K <- kappa^2*Matrix::Diagonal(graph$nV, 1) + graph$Laplacian[[1]]
    Q <- K %*% K * tau^2
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "isoExp"){
    Sigma <- as.matrix(tau^(-2) * exp(-kappa*graph$res_dist))
  } else {
    stop("wrong model choice")
  }

  Sigma.o <- Sigma

  diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2

  return(as.vector(Sigma %*% solve(Sigma.o, graph$y)))
}

#' Prediction for models assuming observations at vertices
#'
#' @param theta Estimated model parameters (sigma_e, tau, kappa).
#' @param graph A `metric_graph` object.
#' @param model Type of model: "alpha1" gives SPDE with alpha=1, "GL1" gives
#' the model based on the graph Laplacian with smoothness 1, "GL2" gives the
#' model based on the graph Laplacian with smoothness 2, and "isoExp" gives a
#' model with isotropic exponential covariance
#' @param data_name Name of the column of the response variable.
#' @param ind Indices for cross validation. It should be a vector of the same
#' length as the data, with integer values representing each group in the
#' cross-validation. If NULL, leave-one-out cross validation is performed.
#' @param BC Which boundary condition to use for the Whittle-Matern models (0,1).
#' Here 0 denotes Neumann boundary conditions and 1 stationary conditions.
#' @details This function does not use sparsity for any model.
#'
#' @return Vector with all predictions
#' @noRd
posterior_crossvalidation_covariance_manual <- function(theta,
                                                 graph,
                                                 data_name,
                                                 model = "alpha1",
                                                 ind = NULL,
                                                 BC=1)
{
  check <- check_graph(graph)

  if(is.null(graph$PtV)){
    stop("You must run graph$observation_to_vertex() first.")
  }

  sigma_e <- theta[1]
  tau <- theta[2]
  kappa <- theta[3]

  #build covariance matrix
  if (model == "alpha1") {
    Q <- Qalpha1(c(tau, kappa), graph)
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "alpha2") {

    graph$buildC(2,BC==0)
    n_const <- length(graph$CoB$S)
    if (n_const > 0) {
      Tc <- graph$CoB$T[-seq_len(n_const), , drop = FALSE]
    } else {
      Tc <- graph$CoB$T
    }
    Q <- Qalpha2(c(tau, kappa), graph, BC = BC)
    Qtilde <- Tc %*% Q %*% t(Tc)
    Sigma.overdetermined = t(Tc) %*% solve(Qtilde) %*% Tc
    PtE = graph$get_PtE()
    index.obs <- 4*(PtE[, 1] - 1) + (1 * (PtE[, 2] == 0)) +
      (3 * (PtE[, 2] != 0))
    Sigma <-  as.matrix(Sigma.overdetermined[index.obs, index.obs])
  } else if (model == "GL1"){
    Q <- (kappa^2 * Matrix::Diagonal(graph$nV, 1) + graph$Laplacian[[1]]) * tau^2 # DOES NOT WORK FOR REPLICATES
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "GL2"){
    K <- kappa^2*Matrix::Diagonal(graph$nV, 1) + graph$Laplacian[[1]] # DOES NOT WORK FOR REPLICATES
    Q <- K %*% K * tau^2
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "isoExp"){
    Sigma <- as.matrix(tau^(-2) * exp(-kappa*graph$res_dist))
  } else {
    stop("wrong model choice")
  }
  Sigma.o <- Sigma

  diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2

  if(is.null(ind)){
    ind <- 1:length(graph$.__enclos_env__$private$data[[data_name]])
  }
  mu.p <- var.p <- logscore <- crps <- scrps <- rep(0, length(graph$.__enclos_env__$private$data[[data_name]]))
  mae <- rmse <- rep(0, length(graph$.__enclos_env__$private$data[[data_name]]))
  for (j in 1:length(unique(ind))) {
    i <- which(ind == j)
    mu.p[i] <-Sigma[i, -i] %*% solve(Sigma.o[-i, -i], graph$.__enclos_env__$private$data[[data_name]][-i])
    Sigma.p <- Sigma.o[i, i] - Sigma.o[i, -i] %*% solve(Sigma.o[-i, -i],
                                                        Sigma.o[-i, i])
    var.p[i] <- diag(Sigma.p)
    logscore[i] <- LS(graph$.__enclos_env__$private$data[[data_name]][i], mu.p[i], sqrt(var.p[i]))
    crps[i] <- CRPS(graph$.__enclos_env__$private$data[[data_name]][i], mu.p[i], sqrt(var.p[i]))
    scrps[i] <- SCRPS(graph$.__enclos_env__$private$data[[data_name]][i], mu.p[i], sqrt(var.p[i]))
    mae[i] <- abs(graph$.__enclos_env__$private$data[[data_name]][i] - mu.p[i])
    rmse[i] <- (graph$.__enclos_env__$private$data[[data_name]][i] - mu.p[i])^2
  }
  return(list(mu = mu.p,
              var = var.p,
              logscore = -mean(logscore),
              crps = -mean(crps),
              scrps = -mean(scrps),
              mae = mean(mae),
              rmse = sqrt(mean(rmse))))
}


#' Prediction for models assuming observations at vertices
#'
#' @param theta Estimated model parameters (sigma_e, tau, kappa).
#' @param graph A `metric_graph` object.
#' @param data_name Name of the data.
#' @param model Type of model: "alpha1" gives SPDE with alpha=1, "GL1" gives
#' the model based on the graph Laplacian with smoothness 1, "GL2" gives the
#' model based on the graph Laplacian with smoothness 2, and "isoExp" gives a
#' model with isotropic exponential covariance.
#' @param ind Indices for cross validation. It should be a vector of the same
#' length as the data, with integer values representing each group in the
#' cross-validation. If NULL, leave-one-out cross validation is performed.
#' @param BC Which boundary condition to use for the Whittle-Matern models (0,1).
#' Here 0 denotes Neumann boundary conditions and 1 stationary conditions.
#' @return Vector with the posterior expectations and variances as well as
#' mean absolute error (MAE), root mean squared errors (RMSE), and three
#' negatively oriented proper scoring rules: log-score, CRPS, and scaled
#' CRPS.
#' @noRd
posterior_crossvalidation_manual <- function(theta,
                                      graph,
                                      data_name,
                                      model = "alpha1",
                                      ind = NULL,
                                      BC = 1)
{
  check <- check_graph(graph)
  if(is.null(graph$PtV)){
    stop("You must run graph$observation_to_vertex() first.")
  }
  sigma_e <- theta[1]
  tau <- theta[2]
  kappa <- theta[3]

  #setup matrices for prediction
  if(model == "isoExp"){
    graph$compute_resdist()
    Sigma <- as.matrix(tau^(-2) * exp(-kappa*graph$res_dist[[".complete"]]))
    Sigma.o <- Sigma
    diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2
  } else if(model == "alpha2"){
    n_const <- length(graph$CoB$S)
    if (n_const > 0) {
      Tc <- graph$CoB$T[-seq_len(n_const), , drop = FALSE]
    } else {
      Tc <- graph$CoB$T
    }
    Q <- Qalpha2(c(tau, kappa), graph, BC = 1)
    Qtilde <- Tc %*% Q %*% t(Tc)
    Sigma.overdetermined = t(Tc) %*% solve(Qtilde) %*% Tc
    PtE = graph$get_PtE()
    index.obs <- 4*(PtE[, 1] - 1) + (1 * (PtE[, 2] == 0)) +
      (3 * (PtE[, 2] != 0))
    Sigma <-  as.matrix(Sigma.overdetermined[index.obs, index.obs])
    Sigma.o <- Sigma
    diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2
  } else if (model %in% c("alpha1", "GL1", "GL2")) {
    if(model == "alpha1"){
      Q <- Qalpha1(c(tau, kappa), graph, BC = BC)
    } else if(model == "GL1"){
      Q <- (kappa^2*Matrix::Diagonal(graph$nV,1) + graph$Laplacian[[1]]) * tau^2
    } else if (model == "GL2") {
      K <- (kappa^2*Matrix::Diagonal(graph$nV,1) + graph$Laplacian[[1]])
      Q <- K %*% K * tau^2
    }
  } else {
    stop("Wrong model choice.")
  }

  if(is.null(ind)){
    ind <- 1:length(graph$.__enclos_env__$private$data[[data_name]])
  }

  mu.p <- var.p <- logscore <- crps <- scrps <- rep(0, length(graph$.__enclos_env__$private$data[[data_name]]))
  mae <- rmse <- rep(0, length(graph$.__enclos_env__$private$data[[data_name]]))

  for (j in 1:length(unique(ind))) {
    i <- which(ind == j)
    if(model == "isoExp" || model == "alpha2"){
      mu.p[i] <-Sigma[i,-i] %*% solve(Sigma.o[-i,-i], graph$.__enclos_env__$private$data[[data_name]][-i])
      Sigma.p <- Sigma.o[i, i] - Sigma.o[i, -i] %*% solve(Sigma.o[-i, -i],
                                                          Sigma.o[-i, i])
      var.p[i] <- diag(Sigma.p)
    } else {
      .row_idx <- graph$PtV[-i]
      A <- Matrix::sparseMatrix(i = seq_along(.row_idx), j = .row_idx,
                                x = 1, dims = c(length(.row_idx), graph$nV))
      Q.p <- Q + t(A) %*% A / sigma_e^2
      mu.p[i] <- solve(Q.p,
                       as.vector(t(A) %*% graph$.__enclos_env__$private$data[[data_name]][-i] / sigma_e^2))[graph$PtV[i]]
      v <- rep(0,dim(Q.p)[1])
      v[graph$PtV[i]] <- 1
      var.p[i] <- solve(Q.p, v)[graph$PtV[i]] + sigma_e^2
    }
    logscore[i] <- LS(graph$.__enclos_env__$private$data[[data_name]][i], mu.p[i], sqrt(var.p[i]))
    crps[i] <- CRPS(graph$.__enclos_env__$private$data[[data_name]][i], mu.p[i], sqrt(var.p[i]))
    scrps[i] <- SCRPS(graph$.__enclos_env__$private$data[[data_name]][i], mu.p[i], sqrt(var.p[i]))
    mae[i] <- abs(graph$.__enclos_env__$private$data[[data_name]][i] - mu.p[i])
    rmse[i] <- (graph$.__enclos_env__$private$data[[data_name]][i] - mu.p[i])^2
  }
  return(list(mu = mu.p,
              var = var.p,
              logscore = -mean(logscore),
              crps = -mean(crps),
              scrps = -mean(scrps),
              mae = mean(mae),
              rmse = sqrt(mean(rmse))))
}

#' Cross-validation for `graph_lme` models assuming observations at
#' the vertices of metric graphs
#'
#' This function performs cross-validation by computing predictions for test data
#' using either the posterior distribution from a fitted model (pseudo-CV) or by
#' refitting the model for each fold (true CV).
#'
#' @param object A fitted model using the `graph_lme()` function. Lists of
#' fitted models are handled by the list method of the generic
#' [rSPDE::posterior_crossvalidation()], which can also mix `graph_lme` and
#' `rspde_lme` fits.
#' @param scores A vector of scores to compute. The options are "logscore", "crps", "scrps", "mae", and "rmse". By default, all scores are computed.
#' @param mode Cross-validation mode. Options are "k-fold", "loo" (leave-one-out), or "lpo" (leave-percentage-out). Default is "k-fold".
#' @param k Number of folds for k-fold cross-validation. Default is 10.
#' @param percentage The percentage (from 1 to 99) of the data to be used to train the model. Will only be used if `mode` is "lpo". Default is 80.
#' @param number_folds Number of folds to be done if `mode` is "lpo". Default is 10.
#' @param train_test_indices Optional list containing train and test indices for each fold. If provided, k, mode, and percentage are ignored.
#' @param true_CV Logical indicating whether to refit the model for each fold (TRUE) or use the posterior distribution from the fitted model (FALSE). Default is FALSE.
#' @param factor Which factor to multiply the scores. The default is 1.
#' @param tibble Return the scores as a `tidyr::tibble()`
#' @param parallel_folds Logical indicating whether to run computations in parallel across folds. Default is FALSE.
#' @param parallel_fitting Logical indicating whether to run model fitting in parallel. Default is FALSE.
#' @param n_cores Number of cores to use for parallel computation. Default is parallel::detectCores() - 1.
#' @param print Logical indicating whether to print progress of which fold is being processed. Default is FALSE.
#' @param seed Random seed for reproducibility in fold creation. Default is NULL.
#' @param return_indices Logical indicating whether to return the train/test indices used. Default is FALSE.
#' @param use_precomputed Logical indicating whether to use precomputation for faster CV. Default is TRUE.
#' @param ... Currently not used.
#' @return Vector with the posterior expectations and variances as well as
#' mean absolute error (MAE), root mean squared errors (RMSE), and three
#' negatively oriented proper scoring rules: log-score, CRPS, and scaled
#' CRPS.
#' @aliases posterior_crossvalidation
#' @method posterior_crossvalidation graph_lme
#' @export
posterior_crossvalidation.graph_lme <- function(object, scores = c("logscore", "crps", "scrps", "mae", "rmse"), mode = "k-fold", k = 10, percentage = 20, number_folds = 10,
                                     train_test_indices = NULL, true_CV = FALSE, factor = 1, tibble = TRUE,
                                     parallel_folds = FALSE, parallel_fitting = FALSE, n_cores = parallel::detectCores() - 1,
                                     print = FALSE, seed = NULL, return_indices = FALSE, use_precomputed = TRUE, ...)
{

  # Convert scores to lowercase for robustness
  scores <- tolower(scores)
  
  # Check if scores are valid
  valid_scores <- c("logscore", "crps", "scrps", "mae", "rmse")
  invalid_scores <- setdiff(scores, valid_scores)

  # Pre-clean the data:
  #   * drop NA response rows (otherwise downstream indexing pulls in
  #     rows whose y is missing — see existing behaviour);
  #   * drop rows whose replicate is NOT in object$which_repl. When the
  #     fit was restricted (graph_lme(..., which_repl = 2)), object$nobs
  #     and object$A_list are sized for that replicate subset only.
  #     Indexing the graph's full data vector by 1..nobs would otherwise
  #     pull rows from replicates the fit never saw, and predict() would
  #     ask for an A_list entry that doesn't exist (symptom: "argument
  #     is not a matrix" inside t(A_repl)).
  # We clone the graph first so that we don't mutate the user's
  # fit$graph (R6 reference) as a side effect.
  if(inherits(object, "graph_lme")) {
    response_var <- as.character(object$response_var)
    data <- object$graph$.__enclos_env__$private$data
    n_data <- length(data[[".group"]])
    keep <- rep(TRUE, n_data)

    if(response_var %in% names(data)) {
      keep <- keep & !is.na(data[[response_var]])
    } else {
      warning(paste("Response variable", response_var, "not found in the data."))
    }

    if(!is.null(object$which_repl) && ".group" %in% names(data)) {
      keep <- keep & (as.character(data[[".group"]]) %in%
                        as.character(object$which_repl))
    }

    cv_graph <- object$graph$clone()
    cv_graph$.__enclos_env__$private$data <-
      lapply(data, function(col) col[keep])
    object$graph <- cv_graph
    object$nobs  <- sum(keep)
  }
  
  if(length(invalid_scores) > 0) {
    warning(paste("Invalid scores:", paste(invalid_scores, collapse=", "), 
                  "- will be ignored. Valid options are:", paste(valid_scores, collapse=", ")))
    scores <- intersect(scores, valid_scores)
  }

  if(parallel_folds && parallel_fitting){
    stop("parallel_folds and parallel_fitting cannot both be TRUE.")
  }

  # Create or use provided train/test indices
  if(is.null(train_test_indices)) {
    if(!is.null(seed)) {
      set.seed(seed)
    }
    
    # Get number of observations
    n_obs <- object$nobs
    
    # Create indices for cross-validation
    if(mode == "loo") {
      # Leave-one-out: each observation is a fold
      train_test_indices <- lapply(1:n_obs, function(i) {
        list(train = setdiff(1:n_obs, i), test = i)
      })
      k <- n_obs
    } else if(mode == "k-fold") {
      # k-fold cross-validation
      fold_indices <- sample(rep(1:k, length.out = n_obs))
      train_test_indices <- lapply(1:k, function(i) {
        test <- which(fold_indices == i)
        train <- setdiff(1:n_obs, test)
        list(train = train, test = test)
      })
    } else if(mode == "lpo") {
      # Leave-percentage-out cross-validation
      if(percentage <= 0 || percentage >= 100) {
        stop("percentage must be a number between 1 and 99!")
      }
      
      # Calculate number of observations for training
      n_train <- round(n_obs * percentage / 100)
      
      # Create multiple folds
      train_test_indices <- lapply(1:number_folds, function(i) {
        # Randomly select indices for training
        train <- sample(1:n_obs, n_train)
        test <- setdiff(1:n_obs, train)
        list(train = train, test = test)
      })
      k <- number_folds
    } else {
      stop("Invalid mode. Choose 'k-fold', 'loo', or 'lpo'")
    }
  } else {
    # Use provided indices
    k <- length(train_test_indices)
  }

  # Initialize result vectors
  n_obs <- object$nobs
  mu.p <- var.p <- rep(NA, n_obs)
  logscore <- crps <- scrps <- mae <- rmse <- rep(NA, n_obs)
  
  # Different precomputation strategy based on CV type
  need_variances <- any(scores %in% c("logscore", "crps", "scrps"))
  precomputed_data <- NULL
  precomputed_graph <- NULL

  # For rspde_lme fits the precomputed structures live in rSPDE
  # (predict.rspde_lme): new_rspde_obj + Q + mu_corr. These are
  # parameter-dependent only, so they can be built once and reused
  # across folds. The cv-time NA mask is plumbed through as
  # advanced_options$na_test_idx. We use a separate code path below
  # (predict.rspde_lme expects only na_test_idx, not the
  # test_idx / which_repl / PtV-aware machinery the native models use).
  is_rspde_obj <- inherits(object, "rspde_lme")

  # The precomputed prediction path treats test_idx as a WITHIN-replicate
  # index (it picks rows of PtV / PtE_full / A, all of which are per-
  # unique-location). For multi-replicate models we still allow the
  # precomputed path on a per-fold basis, but only for folds whose test
  # points all live in a single replicate (always true for LOO; usually
  # false for k-fold). The per-fold decision is made in process_fold.
  n_repl_obj <- length(unique(object$graph$.__enclos_env__$private$data[[".group"]]))

  if(print) {
    cat("Pre-computing data structures for faster cross-validation...\n")
  }

  if(use_precomputed) {
    if(!true_CV) {
      # For pseudo-CV: precompute parameter-dependent structures (Q, Sigma, etc.)
      # Get a dummy test index to create precomputed structures
      dummy_test_idx <- train_test_indices[[1]]$test[1]
      
      # Create dummy prediction data for a single point
      dummy_data <- object$graph$.__enclos_env__$private$data
      dummy_data <- lapply(dummy_data, function(x){x[dummy_test_idx]})
      
      # Make initial prediction to get precomputed data
      precomp_pred <- predict(object, 
                            newdata = dummy_data, 
                            edge_number = ".edge_number", 
                            distance_on_edge = ".distance_on_edge", 
                            normalized = TRUE, 
                            compute_variances = need_variances,
                            advanced_options = list(precompute_data = TRUE, precompute_type = "full"))
                            
      # Extract precomputed data
      precomputed_data <- precomp_pred$precomputed_data

      precomputed_graph <- precomputed_data$graph_bkp
      
      if(is.null(precomputed_data)) {
        warning("Precomputation for pseudo-CV failed, falling back to standard prediction")
      } else if(print) {
        cat("Precomputation for pseudo-CV successful.\n")
      }
    } else {
      # For true-CV: just precompute the graph structure
      # Create a dummy test index to create precomputed structures
      dummy_test_idx <- train_test_indices[[1]]$test[1]
      
      # Create dummy prediction data for a single point
      dummy_data <- object$graph$.__enclos_env__$private$data
      dummy_data <- lapply(dummy_data, function(x){x[dummy_test_idx]})
      
      # Make initial prediction to get structure-only precomputed data
      precomp_pred <- predict(object, 
                            newdata = dummy_data, 
                            edge_number = ".edge_number", 
                            distance_on_edge = ".distance_on_edge", 
                            normalized = TRUE, 
                            compute_variances = need_variances,
                            advanced_options = list(precompute_data = TRUE, precompute_type = "structure"))
                            
      # Extract precomputed data
      precomputed_data <- precomp_pred$precomputed_data
      
      if(is.null(precomputed_data)) {
        warning("Precomputation for true-CV failed, falling back to standard prediction")
      } else if(print) {
        cat("Precomputation for true-CV successful.\n")
      }
      
      # Extract just the graph for use
      precomputed_graph <- precomputed_data$graph_bkp
    }
  # Reorder train and test indices based on the ordering from precomputed_graph
  if (!is.null(precomputed_graph) && !is.null(precomputed_graph$.__enclos_env__$private$data[[".dummy_order_var"]])) {
    # Get the ordering vector
    order_var <- precomputed_graph$.__enclos_env__$private$data[[".dummy_order_var"]]

    # For multi-replicate models the precomputed predict path indexes
    # PtV / PtE_full per-location (one row per unique observation
    # location), so test_idx passed to predict must be a WITHIN-replicate
    # position. Build a mapping from precomputed-graph positions to
    # (replicate, within-replicate index).
    pre_group <- precomputed_graph$.__enclos_env__$private$data[[".group"]]
    within_repl_idx <- integer(length(pre_group))
    if(!is.null(pre_group)) {
      for(r in unique(pre_group)) {
        pos_r <- which(pre_group == r)
        within_repl_idx[pos_r] <- seq_along(pos_r)
      }
    }

    # Apply the reordering to train_test_indices
    for (i in 1:length(train_test_indices)) {
      train_orig <- train_test_indices[[i]]$train
      test_orig  <- train_test_indices[[i]]$test

      train_in_graph <- match(train_orig, order_var)
      test_in_graph  <- match(test_orig,  order_var)

      # Backwards-compatible names: in single-replicate models these
      # equal the global positions, matching the original code path.
      train_test_indices[[i]]$reordered_train <- train_in_graph
      train_test_indices[[i]]$reordered_test  <- test_in_graph

      # New: within-replicate index + replicate id of the test (and
      # train) points, used by the multi-replicate precomputed path.
      if(!is.null(pre_group)) {
        train_test_indices[[i]]$reordered_train_within <- within_repl_idx[train_in_graph]
        train_test_indices[[i]]$reordered_test_within  <- within_repl_idx[test_in_graph]
        train_test_indices[[i]]$test_repl  <- pre_group[test_in_graph]
        train_test_indices[[i]]$train_repl <- pre_group[train_in_graph]
      }
    }

    if (print) {
        cat("Train and test indices reordered according to graph structure.\n")
      }
    }
  }

  # Function to process a single fold
  process_fold <- function(fold_idx) {
    if(print && !parallel_folds) {
      cat("Processing fold", fold_idx, "of", k, "\n")
    }
    
    train_indices <- train_test_indices[[fold_idx]]$train
    test_indices <- train_test_indices[[fold_idx]]$test

    # Decide whether THIS fold can use the precomputed path. The path
    # supports one replicate per predict() call (test_idx is within-
    # replicate for native models, na_test_idx is global for rspde).
    # For rspde, multi-replicate folds split into per-replicate
    # sub-predicts below (rspde_multi_rep_split). For native models,
    # multi-replicate folds fall back to a single non-precomputed
    # predict. For LOO every fold is single-replicate.
    fold_use_precomputed <- use_precomputed
    fold_test_repl <- NULL
    rspde_multi_rep_split <- FALSE
    test_repls_in_fold <- NULL
    # For multi-replicate rspde fits the split path is needed for
    # correctness regardless of `use_precomputed`: predict.rspde_lme
    # does not emit $edge_number / $distance_on_edge, so a single
    # multi-replicate predict() returns $mean rows the downstream key
    # match cannot disambiguate by (replicate, location) — it silently
    # falls through to position-based indexing and reports the wrong
    # replicate's prediction for most test points. Splitting by
    # replicate avoids this AND lets the cached precompute be reused.
    if(is_rspde_obj && n_repl_obj > 1){
      test_repls_in_fold <-
        object$graph$.__enclos_env__$private$data[[".group"]][test_indices]
      if(length(unique(test_repls_in_fold)) != 1L){
        rspde_multi_rep_split <- TRUE
      } else {
        fold_test_repl <- test_repls_in_fold[1]
      }
    }
    if(fold_use_precomputed && !rspde_multi_rep_split){
      reordered_train_indices <- train_test_indices[[fold_idx]]$reordered_train
      reordered_test_indices  <- train_test_indices[[fold_idx]]$reordered_test
      if(n_repl_obj > 1 && !is_rspde_obj){
        # Native model: per-fold precompute path needs the test points to
        # live in a single replicate. Multi-replicate folds fall back.
        test_repls_in_fold <- train_test_indices[[fold_idx]]$test_repl
        if(is.null(test_repls_in_fold)){
          test_repls_in_fold <-
            object$graph$.__enclos_env__$private$data[[".group"]][test_indices]
        }
        if(length(unique(test_repls_in_fold)) != 1L){
          fold_use_precomputed <- FALSE
        } else {
          fold_test_repl <- test_repls_in_fold[1]
          # Native precompute path: predict idx_prd is WITHIN-replicate.
          reordered_test_indices  <- train_test_indices[[fold_idx]]$reordered_test_within
          reordered_train_indices <- train_test_indices[[fold_idx]]$reordered_train_within
        }
      }
    }
    # rspde precompute always uses newdata for the prediction location,
    # so build new_data unconditionally for that case (the split path
    # builds per-replicate newdata below).
    if(!rspde_multi_rep_split &&
       (!fold_use_precomputed || is.null(precomputed_data) || is_rspde_obj)){
      new_data <- object$graph$.__enclos_env__$private$data
      new_data <- lapply(new_data, function(x){x[test_indices]})
    }
    
    local_results <- list(
      test_indices = test_indices,
      mu.p = rep(NA, length(test_indices)),
      var.p = rep(NA, length(test_indices)),
      logscore = rep(NA, length(test_indices)),
      crps = rep(NA, length(test_indices)),
      scrps = rep(NA, length(test_indices)),
      mae = rep(NA, length(test_indices)),
      rmse = rep(NA, length(test_indices))
    )

    response_name <- as.character(object$response_var)
    
    if(true_CV) {
      # True cross-validation: refit the model for each fold
      # Create a copy of the graph with NA values for test indices
      cv_graph <- NULL
      
      cv_graph <- object$graph$clone()
      cv_graph$.__enclos_env__$private$data[[response_name]][test_indices] <- NA
      
      model_options <- object$options_model
      # For rSPDE-backed (FEM) fits we let graph_lme/rspde_lme do the
      # name-based extraction of starting values via `previous_fit = object`
      # — the positional kappa/tau extraction below would silently pick the
      # wrong elements (e.g. "nu (fixed)" or "theta1") for nonstationary fits.
      refit_previous_fit <- NULL
      if(inherits(object, "rspde_lme")){
        refit_previous_fit <- object
      } else if(tolower(object$latent_model$type) %in% c("whittlematern", "graphlaplacian")) {
        if(!is.null(object$coeff$random_effects)) {
          model_options$start_kappa <- object$coeff$random_effects[2]
          model_options$start_tau <- object$coeff$random_effects[1]
        }
        if(!is.null(object$coeff$measurement_error)) {
          model_options$start_sigma_e <- object$coeff$measurement_error
        }
      } else if(tolower(object$latent_model$type) == "isocov") {
        # For isoCov models
        if(!is.null(object$coeff$random_effects)) {
          model_options$start_par_vec <- object$coeff$random_effects
        }
        if(!is.null(object$coeff$measurement_error)) {
          model_options$start_sigma_e <- object$coeff$measurement_error
        }
      }

      # Refit the model using the same optimization method.
      # Prefer `model_arg` (original user input) over `latent_model`: the
      # latter is the rSPDE operator for FEM fits and does not carry the
      # B.sigma/B.range or fem=TRUE flags graph_lme needs to re-dispatch.
      refit_model <- if(!is.null(object$model_arg)) object$model_arg else object$latent_model
      refit_is_fem <- isTRUE(refit_model$fem)
      refit_optim_method <- if(!is.null(object$optim_method)) object$optim_method else "L-BFGS-B"
      refit_optim_controls <- if(!is.null(object$optim_controls)) object$optim_controls else list()
      refit_args <- list(
        formula = object$formula,
        graph = cv_graph,
        model = refit_model,
        optim_method = refit_optim_method,
        optim_controls = refit_optim_controls,
        model_options = model_options,
        parallel = parallel_fitting,
        n_cores = n_cores
      )
      if(!is.null(refit_previous_fit)){
        refit_args$previous_fit <- refit_previous_fit
      }
      # Only pass BC when it is meaningful for the model (i.e. non-FEM
      # WhittleMatern). Passing it for FEM fits would trigger a spurious
      # "BC has no effect" warning on every fold.
      if(!refit_is_fem && !is.null(object$BC)){
        refit_args$BC <- object$BC
      }
      cv_model <- do.call(graph_lme, refit_args)

    } else {
      # Pseudo cross-validation: use the original model parameters
      # Create a copy of the object with NA values for test indices
      cv_model <- update_graph_lme_with_na(object, test_indices)
    }

    # Make predictions for test indices
    if(rspde_multi_rep_split){
      # rspde multi-replicate fold: split into per-replicate
      # sub-predicts so each one has unique prediction locations (no
      # duplicate-loc warning), only iterates kriging over the one
      # relevant replicate, and — critically — assembles predictions
      # in test_indices order so the downstream key match can't pick
      # the wrong replicate's row. When `use_precomputed` is on, the
      # cached new_rspde_obj / Q are reused across sub-predicts.
      gdata <- object$graph$.__enclos_env__$private$data
      n_test <- length(test_indices)
      mu_vec <- rep(NA_real_, n_test)
      var_vec <- rep(NA_real_, n_test)
      base_adv_opts <- list(precompute_data = precomputed_data)
      for(r in unique(test_repls_in_fold)){
        pos_r <- which(test_repls_in_fold == r)
        ti_r  <- test_indices[pos_r]
        nd_r  <- lapply(gdata, function(x){x[ti_r]})
        adv_opts_r <- base_adv_opts
        adv_opts_r$na_test_idx <- ti_r
        pred_r <- predict(cv_model,
                          newdata = nd_r,
                          edge_number = ".edge_number",
                          distance_on_edge = ".distance_on_edge",
                          normalized = TRUE,
                          compute_variances = need_variances,
                          which_repl = r,
                          advanced_options = adv_opts_r)
        mu_vec[pos_r]  <- as.numeric(pred_r$mean)
        if(need_variances){
          var_vec[pos_r] <- as.numeric(pred_r$variance)
        }
      }
      # Build a `pred` object in test_indices order. The downstream
      # post-processing detects length(pred$mean) == n_test and skips
      # the key-based lookup.
      pred <- list(mean = mu_vec, variance = var_vec)
    } else if(fold_use_precomputed && !is.null(precomputed_data) && is_rspde_obj) {
      # rspde precompute path: predict.rspde_lme reuses the cached
      # (new_rspde_obj, Q, mu_corr) and masks the held-out points via
      # na_test_idx. The location for the prediction itself still has
      # to come from newdata.
      adv_opts <- list(precompute_data = precomputed_data,
                       na_test_idx     = test_indices)
      pred <- predict(cv_model,
                    newdata = new_data,
                    edge_number = ".edge_number",
                    distance_on_edge = ".distance_on_edge",
                    normalized = TRUE,
                    compute_variances = need_variances,
                    which_repl = fold_test_repl,
                    advanced_options = adv_opts)
    } else if(fold_use_precomputed && !is.null(precomputed_data)) {
      # Native (non-rspde) precompute path. For multi-replicate folds we
      # pass the within-replicate test_idx (for predict's PtV /
      # PtE_full lookup) together with na_test_idx (global, for masking
      # the response vector). For single-replicate folds these collapse
      # to the same value and the call is equivalent to the original
      # code path.
      adv_opts <- list(precompute_data = precomputed_data,
                       precompute_type = "full",
                       test_idx = reordered_test_indices,
                       train_idx = reordered_train_indices)
      if(n_repl_obj > 1L){
        adv_opts$na_test_idx <- train_test_indices[[fold_idx]]$reordered_test
      }
      pred <- predict(cv_model,
                    edge_number = ".edge_number",
                    distance_on_edge = ".distance_on_edge",
                    normalized = TRUE,
                    compute_variances = need_variances,
                    which_repl = fold_test_repl,
                    advanced_options = adv_opts)
    } else {
      # Fallback path (precomputation skipped, failed, or the fold spans
      # multiple replicates). When the fold is confined to a single
      # replicate (single-rep models, or LOO/per-fold on multi-rep), pass
      # `which_repl` so predict() does not iterate over every replicate
      # and return rows the downstream key match cannot disambiguate.
      pred <- predict(cv_model,
                    newdata = new_data,
                    edge_number = ".edge_number",
                    distance_on_edge = ".distance_on_edge",
                    normalized = TRUE,
                    compute_variances = need_variances,
                    which_repl = fold_test_repl)
    }
      
    # predict() returns one entry per (newdata row x replicate). In the
    # precompute path with `which_repl` set and in the single-replicate
    # fallback (and the rspde multi-rep split path above), the output
    # already has exactly one entry per test point in `test_indices`
    # order; we can index directly. The match-based lookup is only
    # needed for the multi-replicate native fallback, where predict
    # produces predictions across all replicates.
    n_test <- length(test_indices)
    full_data <- object$graph$.__enclos_env__$private$data
    if(length(pred$mean) == n_test){
      pred_idx_vec <- seq_len(n_test)
    } else {
      test_repl <- full_data[[".group"]][test_indices]
      test_edge <- full_data[[".edge_number"]][test_indices]
      test_dist <- full_data[[".distance_on_edge"]][test_indices]
      pred_repl <- pred$repl
      if(is.null(pred_repl)){
        # Older / single-replicate code paths may omit $repl; assume
        # one entry per test point in input order.
        pred_repl <- rep(test_repl[1], length(pred$mean))
      }
      pred_keys <- paste(as.character(pred_repl),
                         pred$edge_number,
                         formatC(pred$distance_on_edge, digits = 12, format = "g"),
                         sep = "")
      test_keys <- paste(as.character(test_repl),
                         test_edge,
                         formatC(test_dist, digits = 12, format = "g"),
                         sep = "")
      pred_idx_vec <- match(test_keys, pred_keys)
      na_pos <- is.na(pred_idx_vec)
      if(any(na_pos)){
        pred_idx_vec[na_pos] <- which(na_pos)
      }
    }

    mu_vec <- pred$mean[pred_idx_vec]
    local_results$mu.p <- mu_vec
    if(need_variances){
      var_vec <- pred$variance[pred_idx_vec] + object$coeff$measurement_error^2
      local_results$var.p <- var_vec
      sd_vec <- sqrt(var_vec)
    }
    y_test_vec <- full_data[[response_name]][test_indices]
    resid_vec  <- y_test_vec - mu_vec

    if("logscore" %in% scores){
      local_results$logscore <- LS(y_test_vec, mu_vec, sd_vec)
    }
    if("crps" %in% scores){
      local_results$crps <- CRPS(y_test_vec, mu_vec, sd_vec)
    }
    if("scrps" %in% scores){
      local_results$scrps <- SCRPS(y_test_vec, mu_vec, sd_vec)
    }
    if("mae" %in% scores){
      local_results$mae <- abs(resid_vec)
    }
    if("rmse" %in% scores){
      local_results$rmse <- resid_vec^2
    }

    return(local_results)
  }

  # Process folds in parallel or sequentially
  if(parallel_folds) {
    if(!requireNamespace("parallel", quietly = TRUE)) {
      warning("Package 'parallel' is not available. Using sequential processing instead.")
      parallel_folds <- FALSE
    }
  }
  
  if(parallel_folds) {
    cl <- parallel::makeCluster(n_cores)
    
    # Load necessary packages and functions in each worker
    parallel::clusterEvalQ(cl, {
      suppressPackageStartupMessages(library(Matrix))
      
      # Define helper functions inside each worker
      LS <- function(y, mu, sigma) {
        return(dnorm(y, mean = mu, sd = sigma, log = TRUE))
      }
      
      CRPS <- function(y, mu, sigma) {
        return(-Exy(mu, sigma, y) + 0.5 * Exx(mu, sigma))
      }
      
      SCRPS <- function(y, mu, sigma) {
        return(-Exy(mu, sigma, y) / Exx(mu, sigma) - 0.5 * log(Exx(mu, sigma)))
      }
      
      Exx <- function(mu, sigma) {
        return(Efnorm(0, sqrt(2) * sigma))
      }
      
      Exy <- function(mu, sigma, y) {
        return(Efnorm(mu - y, sigma))
      }
      
      Efnorm <- function(mu, sigma) {
        return(sigma * sqrt(2 / pi) * exp(-(mu ^ 2) / (2 * sigma ^ 2)) + mu * (1 - 2 * pnorm(-mu / sigma)))
      }
      
      NULL
    })
    
    # Export necessary objects to the workers
    parallel::clusterExport(cl, 
                          varlist = c("object", "train_test_indices", 
                                     "update_graph_lme_with_na", "graph_lme", "predict", 
                                     "print", "true_CV", "precomputed_data", "precomputed_graph",
                                     "need_variances", "scores", "use_precomputed"),
                          envir = environment())
    
    if(print) {
      cat("Starting parallel processing with", n_cores, "cores\n")
    }
    
    results <- parallel::parLapply(cl, 1:k, process_fold)
    parallel::stopCluster(cl)
  } else {
    results <- lapply(1:k, process_fold)
  }
  
  # Combine results from all folds
  for(i in 1:k) {
    test_indices <- results[[i]]$test_indices
    for(j in seq_along(test_indices)) {
      idx <- test_indices[j]
      mu.p[idx] <- results[[i]]$mu.p[j]
      var.p[idx] <- results[[i]]$var.p[j]
      logscore[idx] <- results[[i]]$logscore[j]
      crps[idx] <- results[[i]]$crps[j]
      scrps[idx] <- results[[i]]$scrps[j]
      mae[idx] <- results[[i]]$mae[j]
      rmse[idx] <- results[[i]]$rmse[j]
    }
  }
  
  # Compile final results
  res <- list(
    mu = mu.p,
    var = var.p
  )
  
  # Create a data frame with only the requested scores
  # Initialize scores data frame with at least one row to avoid the error
  # "replacement has 1 row, data has 0"
  res[["scores"]] <- data.frame(dummy = NA)
  
  if("logscore" %in% scores) {
    res[["scores"]]$logscore <- -factor * mean(logscore, na.rm = TRUE)
  }
  if("crps" %in% scores) {
    res[["scores"]]$crps <- -factor * mean(crps, na.rm = TRUE)
  }
  if("scrps" %in% scores) {
    res[["scores"]]$scrps <- -factor * mean(scrps, na.rm = TRUE)
  }
  if("mae" %in% scores) {
    res[["scores"]]$mae <- factor * mean(mae, na.rm = TRUE)
  }
  if("rmse" %in% scores) {
    res[["scores"]]$rmse <- factor * sqrt(mean(rmse, na.rm = TRUE))
  }
  
  # Remove the dummy column after adding the real scores
  res[["scores"]]$dummy <- NULL
  
  attr(res[["scores"]], "factor") <- factor
  
  if(return_indices) {
    res[["indices"]] <- train_test_indices
  }
  
  if(tibble){
    res[["scores"]] <- tidyr::as_tibble(res[["scores"]])
  }
  
  return(res)
}

#' Leave-one-out crossvalidation for `graph_lme` models assuming observations at
#' the vertices of metric graphs
#'
#' @param object A fitted model using the `graph_lme()` function.
#' @details This function does not use sparsity for any model.
#'
#' @return Vector with all predictions
#' @noRd
posterior_crossvalidation_covariance <- function(object)
{

  if(!inherits(object,"graph_lme")){
    stop("object should be of class graph_lme.")
  }

  graph <- object$graph$clone()

  graph$observation_to_vertex()

  check <- check_graph(graph)

  beta_cov <- object$coeff$fixed_effects
  sigma_e <- object$coeff$measurement_error
  tau <- object$coeff$random_effects[1]
  kappa <- object$coeff$random_effects[2]

  # if(tolower(object$latent_model$type) == "whittlematern"){
  #   if(object$parameterization_latent == "matern"){
  #     kappa <- ifelse(object$latent_model$alpha == 1,
  #                 sqrt(8 * 0.5) / kappa, sqrt(8 * (1.5)) / kappa)
  #   }
  # }

  if(tolower(object$latent_model$type) == "isocov"){
    if(object$latent_model$cov_function_name == "other"){
      stop("Currently the cross-validation is only implemented for the exponential covariance function.")
    }
    model <- "isoExp"
    sigma <- object$coeff$random_effects[1]
  } else if(tolower(object$latent_model$type) == "whittlematern"){
    if(object$latent_model$alpha == 1){
      model <- "alpha1"
    } else {
      model <- "WM alpha2"
    }
  } else{
    graph$compute_laplacian(full=TRUE)
    if(object$latent_model$alpha == 1){
      model <- "GL1"
    } else {
      model <- "GL2"
    }
  }

  BC <- object$BC

  if(!is.matrix(object$model_matrix)){
    object$model_matrix <- matrix(object$model_matrix, ncol=1)
  }

  y_graph <- object$model_matrix[,1]

  ind <- 1:length(y_graph)

  if(ncol(object$model_matrix) > 1){
    X_cov <- object$model_matrix[,-1]
  } else{
    X_cov <- NULL
  }

  #build covariance matrix
  if (model == "alpha1") {
    Q <- Qalpha1(c(tau, kappa), graph)
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "WM alpha2") {

    graph$buildC(2,BC==0)
    n_const <- length(graph$CoB$S)
    if (n_const > 0) {
      Tc <- graph$CoB$T[-seq_len(n_const), , drop = FALSE]
    } else {
      Tc <- graph$CoB$T
    }
    Q <- Qalpha2(c(tau, kappa), graph, BC = BC)
    Qtilde <- Tc %*% Q %*% t(Tc)
    Sigma.overdetermined = t(Tc) %*% solve(Qtilde) %*% Tc
    PtE = graph$get_PtE()
    index.obs <- 4*(PtE[, 1] - 1) + (1 * (PtE[, 2] == 0)) +
      (3 * (PtE[, 2] != 0))
    Sigma <-  as.matrix(Sigma.overdetermined[index.obs, index.obs])
  } else if (model == "GL1"){
    Q <- (kappa^2 * Matrix::Diagonal(graph$nV, 1) + graph$Laplacian[[1]]) * tau^2 # DOES NOT WORK FOR REPLICATES
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "GL2"){
    K <- kappa^2*Matrix::Diagonal(graph$nV, 1) + graph$Laplacian[[1]] # DOES NOT WORK FOR REPLICATES
    Q <- K %*% K * tau^2
    Sigma <- as.matrix(solve(Q))[graph$PtV, graph$PtV]
  } else if (model == "isoExp"){
    graph$compute_resdist(full = TRUE)
    Sigma <- as.matrix(sigma^2 * exp(-kappa*graph$res_dist[[".complete"]]))
  } else {
    stop("wrong model choice")
  }
  Sigma.o <- Sigma

  diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2

  if(is.null(ind)){
    ind <- 1:length(y_graph)
  }
  mu.p <- var.p <- logscore <- crps <- scrps <- rep(0, length(y_graph))
  mae <- rmse <- rep(0, length(y_graph))
  for (j in 1:length(unique(ind))) {
    i <- which(ind == j)

    y_cv <- y_graph[-i]
    v_cv <- y_cv
    if(!is.null(X_cov)){
      v_cv <- v_cv - as.vector(X_cov[-i, , drop = FALSE] %*% beta_cov)
      mu_fe <- as.vector(X_cov[i, , drop = FALSE] %*% beta_cov)
    } else {
      mu_fe <- 0
    }

    mu.p[i] <-Sigma[i, -i] %*% solve(Sigma.o[-i, -i], v_cv) + mu_fe
    Sigma.p <- Sigma.o[i, i] - Sigma.o[i, -i] %*% solve(Sigma.o[-i, -i],
                                                        Sigma.o[-i, i])
    var.p[i] <- diag(Sigma.p)
    logscore[i] <- LS(y_graph[i], mu.p[i], sqrt(var.p[i]))
    crps[i] <- CRPS(y_graph[i], mu.p[i], sqrt(var.p[i]))
    scrps[i] <- SCRPS(y_graph[i], mu.p[i], sqrt(var.p[i]))
    mae[i] <- abs(y_graph[i] - mu.p[i])
    rmse[i] <- (y_graph[i] - mu.p[i])^2
  }
  return(list(mu = mu.p,
              var = var.p,
              logscore = -mean(logscore),
              crps = -mean(crps),
              scrps = -mean(scrps),
              mae = mean(mae),
              rmse = sqrt(mean(rmse))))
}


#' @noRd
CRPS <- function(y, mu, sigma)
{
  return(-Exy(mu, sigma, y) + 0.5 * Exx(mu, sigma))
}

#' @noRd
SCRPS <- function(y, mu, sigma)
{
  return(-Exy(mu, sigma, y) / Exx(mu, sigma) - 0.5 * log(Exx(mu, sigma)))
}


#' @noRd
LS <- function(y, mu, sigma)
{
  return(dnorm(y, mean = mu, sd = sigma, log = TRUE))
}


#' @noRd
Exx <- function(mu, sigma) {
  #X-X' = N(0,2*sigma^2)
  return(Efnorm(0, sqrt(2) * sigma))
}



#compute E[|X-y|] when X is N(mu,sigma^2)

#' @noRd
Exy <- function(mu, sigma, y) {
  #X-y = N(mu-y,sigma^2)
  return(Efnorm(mu - y, sigma))
}

#' @noRd
Efnorm <- function(mu, sigma) {
  return(sigma * sqrt(2 / pi) * exp(-(mu ^ 2) / (2 * sigma ^ 2)) + mu * (1 -
                                                                           2 * pnorm(-mu / sigma)))
}





#' Leave-one-out pseudo-crossvalidation for `graph_lme` models assuming observations at
#' the vertices of metric graphs
#'
#' This function performs pseudo-crossvalidation by computing leave-one-out predictions
#' using the posterior distribution from a fitted model. In pseudo-crossvalidation,
#' the model parameters are kept fixed at the values estimated from the full dataset
#' (those provided in the object), rather than re-estimating them for each fold.
#'
#' @param object A fitted model using the `graph_lme()` function or a named list of fitted objects using the `graph_lme()` function.
#' @param factor Which factor to multiply the scores. The default is 1.
#' @param tibble Return the scores as a `tidyr::tibble()`
#' @param which_repl Which replicates to consider?
#' @return Vector with the posterior expectations and variances as well as
#' mean absolute error (MAE), root mean squared errors (RMSE), and three
#' negatively oriented proper scoring rules: log-score, CRPS, and scaled
#' CRPS.
#' @export
posterior_crossvalidation_loo <- function(object, factor = 1, tibble = TRUE, which_repl = NULL)
{
  if(!inherits(object,"graph_lme") && !is.list(object)){
    stop("object should be of class graph_lme or a list of objects of class graph_lme.")
  }

  # rspde_lme fits don't expose the (tau, kappa) precision-based
  # parameterisation this routine assumes — coeff$random_effects[1:2]
  # mean something different (sigma, range or tau, kappa under spde
  # parameterisation but with a fractional alpha). The model-type
  # dispatch below would silently fall through to GL1/GL2 and return
  # finite but wrong scores. Refuse to do that.
  if(inherits(object, "rspde_lme")){
    stop("posterior_crossvalidation_loo does not support rspde_lme (FEM / ",
         "fractional) fits. Use posterior_crossvalidation(object, ",
         "mode = \"loo\", true_CV = FALSE) instead.")
  }

  if(!inherits(object,"graph_lme")){
    if(is.null(names(object))){
      warning("The list with fitted models does not contain names for the models, thus the results will not be properly named.")
    }
    results_list <- lapply(object, function(obj){posterior_crossvalidation_loo(obj, factor=factor, tibble=FALSE)})
    res <- list()
    res[["mu"]] <- lapply(results_list, function(dat){dat[["mu"]]})
    res[["var"]] <- lapply(results_list, function(dat){dat[["var"]]})
    res[["scores"]] <- lapply(results_list, function(dat){dat[["scores"]]})
    scores <- do.call(rbind, res[["scores"]])
    row.names(scores) <- names(res[["scores"]])
    if(tibble){
      scores[["Model"]] <- row.names(scores)
      scores <- tidyr::as_tibble(scores)
      scores <- scores[, c("Model", "logscore", "crps", "scrps", "mae", "rmse")]
    }
    res[["scores"]] <- scores
    return(res)
  }

  graph <- object$graph$clone()

  # Resolve the effective replicate set for this CV call:
  #   * default: the replicates the fit was actually built on
  #     (object$which_repl, populated by graph_lme).
  #   * user override (function arg which_repl): must be a SUBSET of
  #     object$which_repl — scoring with parameters that were never
  #     optimised against a replicate would otherwise silently return
  #     misleading (or empty) results.
  # Apply the resolved set as a single filter on the graph private data
  # AND on model_matrix below, so repl_vec / y_graph / X_cov stay
  # aligned with the fit's actual observations.
  if (is.null(which_repl)) {
    effective_repl <- object$which_repl
  } else {
    if (!is.null(object$which_repl)) {
      bad <- setdiff(as.character(which_repl),
                     as.character(object$which_repl))
      if (length(bad) > 0) {
        stop("posterior_crossvalidation_loo: which_repl values [",
             paste(bad, collapse = ", "),
             "] are not in the fit's replicate set [",
             paste(as.character(object$which_repl), collapse = ", "),
             "]. The fit's parameters were not optimised for those ",
             "replicates, so scoring them would be misleading.")
      }
    }
    effective_repl <- which_repl
  }
  d <- graph$.__enclos_env__$private$data
  n_data <- length(d[[".group"]])
  if (!is.null(effective_repl) && ".group" %in% names(d)) {
    .keep_repl <- as.character(d[[".group"]]) %in%
                    as.character(effective_repl)
  } else {
    .keep_repl <- rep(TRUE, n_data)
  }
  if (any(!.keep_repl)) {
    graph$.__enclos_env__$private$data <-
      lapply(d, function(col) col[.keep_repl])
  }

  graph$observation_to_vertex(mesh_warning = FALSE)

  beta_cov <- object$coeff$fixed_effects
  sigma_e <- object$coeff$measurement_error
  tau <- object$coeff$random_effects[1]
  kappa <- object$coeff$random_effects[2]

  # if(tolower(object$latent_model$type) == "whittlematern"){
  #   if(object$parameterization_latent == "matern"){
  #     kappa <- ifelse(object$latent_model$alpha == 1,
  #                 sqrt(8 * 0.5) / kappa, sqrt(8 * (1.5)) / kappa)
  #   }
  # }

  if(tolower(object$latent_model$type) == "isocov"){
    if(object$latent_model$cov_function_name == "other"){
      stop("Currently the cross-validation is only implemented for the exponential covariance function.")
    }
    model <- "isoExp"
    sigma <- object$coeff$random_effects[1]
  } else if(tolower(object$latent_model$type) == "whittlematern"){
    if(object$latent_model$alpha == 1){
      if(object$latent_model$directional==0){
        model <- "WM alpha1"
      }else{
        model <- "WMD alpha1"
      }
    } else {
      model <- "WM alpha2"
    }
  } else{
    graph$compute_laplacian(full=TRUE)
    if(object$latent_model$alpha == 1){
      model <- "GL1"
    } else {
      model <- "GL2"
    }
  }

  BC <- object$BC

  #setup matrices for prediction
  if(model == "isoExp"){
    graph$compute_resdist(full = TRUE)
    Sigma <- as.matrix(sigma^2 * exp(-kappa*graph$res_dist[[".complete"]]))

    # nV <- nrow(graph$res_dist[[".complete"]]) - length(graph$get_data()[[".group"]])

    # Sigma <- Sigma[-c(1:nV), -c(1:nV)]

    Sigma.o <- Sigma

    diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2
  } else if(model == "WM alpha2"){
    if(is.null(graph$C)){
      graph$buildC(2)
    } else if(graph$CoB$alpha == 1){
      graph$buildC(2)
    }

    n_const <- length(graph$CoB$S)
    if (n_const > 0) {
      Tc <- graph$CoB$T[-seq_len(n_const), , drop = FALSE]
    } else {
      Tc <- graph$CoB$T
    }
    Q <- Qalpha2(c(tau, kappa), graph, BC = BC)
    Qtilde <- Tc %*% Q %*% t(Tc)
    Sigma.overdetermined = t(Tc) %*% solve(Qtilde) %*% Tc
    PtE = graph$get_PtE()
    index.obs <- 4*(PtE[, 1] - 1) + (1 * (PtE[, 2] == 0)) +
      (3 * (PtE[, 2] != 0))
    Sigma <-  as.matrix(Sigma.overdetermined[index.obs, index.obs])
    Sigma.o <- Sigma
    diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2
  }else if(model == "WMD alpha1"){
    if(is.null(graph$C)){
      graph$buildDirectionalConstraints(1)
    } else if(graph$CoB$alpha == 2){
      graph$buildDirectionalConstraints(1)
    }

    Q_edges <- Qalpha1_edges(c(tau,kappa), graph, w = 0,BC=BC, build=TRUE)
    n_const <- length(graph$CoB$S)
    if (n_const > 0) {
      Tc <- graph$CoB$T[-seq_len(n_const), , drop = FALSE]
    } else {
      Tc <- graph$CoB$T
    }
    Q_T <- Matrix::forceSymmetric(Tc%*%Q_edges%*%t(Tc))
    Sigma.overdetermined <- as.matrix(t(Tc)%*%solve(Q_T)%*%Tc)
    PtE = graph$get_PtE()
    index.obs <- 2*(PtE[, 1] - 1) + (PtE[, 2]> 1-0.001) + 1
    Sigma <-  as.matrix(Sigma.overdetermined[index.obs, index.obs])
    Sigma.o <- Sigma
    diag(Sigma.o) <- diag(Sigma.o) + sigma_e^2
  }else if (model %in% c("WM alpha1", "GL1", "GL2")) {
    if(model == "WM alpha1"){
      Q <- Qalpha1(c(tau, kappa), graph, BC = BC)
    } else if(model == "GL1"){
      Q <- (kappa^2*Matrix::Diagonal(graph$nV,1) + graph$Laplacian[[1]]) * tau^2
    } else if (model == "GL2") {
      K <- (kappa^2*Matrix::Diagonal(graph$nV,1) + graph$Laplacian[[1]])
      Q <- K %*% K * tau^2
    }
  } else {
    stop("Wrong model choice.")
  }

  if(!is.matrix(object$model_matrix)){
    object$model_matrix <- matrix(object$model_matrix, ncol=1)
  }

  # Slice model_matrix to the same observation subset the graph clone
  # now contains. graph_lme keeps model_matrix at full data length even
  # when which_repl restricts the fit, so without this y_graph and
  # repl_vec would have different lengths and y_graph[idx_repl_j] would
  # silently recycle.
  if(nrow(object$model_matrix) == length(.keep_repl)){
    mm <- object$model_matrix[.keep_repl, , drop = FALSE]
  } else {
    mm <- object$model_matrix
  }

  y_graph <- mm[,1]

  ind <- 1:length(y_graph)

  if(ncol(mm) > 1){
    X_cov <- mm[,-1]
  } else{
    X_cov <- NULL
  }

  if(!is.null(X_cov)){
    if(all(dim(X_cov) == c(0,1))){
      names_temp <- colnames(X_cov)
      X_cov <- matrix(1, nrow = length(y_graph))
      colnames(X_cov) <- names_temp
    }
  }

  repl_vec <- graph$.__enclos_env__$private$data[[".group"]]

  if(is.null(which_repl)){
    repl <- unique(repl_vec)
  } else {
    repl <- which_repl
  }

  n_obs <- sum(repl_vec == repl[1])

  # Allocate per-observation result vectors over ALL replicates. Each
  # entry corresponds to a row of the underlying data (in y_graph
  # order); earlier code only allocated length n_obs and overwrote those
  # n_obs slots on every replicate iteration, so the returned $mu / $var
  # silently held just the LAST replicate's predictions.
  n_total <- n_obs * length(repl)
  mu.p <- var.p <- logscore <- crps <- scrps <- rep(NA_real_, n_total)
  mae  <- rmse  <- rep(NA_real_, n_total)

  for(j in seq_along(repl)){
    idx_repl_j <- repl_vec == repl[j]
    pos_r <- which(idx_repl_j)
    y_graph_repl <- y_graph[idx_repl_j]
    if(!is.null(X_cov)){
      X_cov_repl <- X_cov[idx_repl_j, , drop = FALSE]
    }

    for(i in seq_len(n_obs)){
      y_cv <- y_graph_repl[-i]
      v_cv <- y_cv
      if(!is.null(X_cov)){
        v_cv <- v_cv - as.vector(X_cov_repl[-i, , drop = FALSE] %*% beta_cov)
        mu_fe <- as.vector(X_cov_repl[i, , drop = FALSE] %*% beta_cov)
      } else {
        mu_fe <- 0
      }

      slot <- pos_r[i]

      if(model == "isoExp" || model == "WM alpha2" || model == "WMD alpha1"){
        mu.p[slot] <- Sigma[i, -i] %*% solve(Sigma.o[-i, -i], v_cv) + mu_fe
        Sigma.p <- Sigma.o[i, i] - Sigma.o[i, -i] %*% solve(Sigma.o[-i, -i],
                                                            Sigma.o[-i, i])
        var.p[slot] <- diag(Sigma.p)
      } else {
        .row_idx <- graph$PtV[-i]
        A <- Matrix::sparseMatrix(i = seq_along(.row_idx), j = .row_idx,
                                  x = 1, dims = c(length(.row_idx), graph$nV))
        Q.p <- Q + t(A) %*% A / sigma_e^2
        mu.p[slot] <- solve(Q.p,
                           as.vector(t(A) %*% v_cv / sigma_e^2))[graph$PtV[i]] + mu_fe
        v <- rep(0, dim(Q.p)[1])
        v[graph$PtV[i]] <- 1
        var.p[slot] <- solve(Q.p, v)[graph$PtV[i]] + sigma_e^2
      }

      sd_pred <- sqrt(var.p[slot])
      logscore[slot] <- LS(y_graph_repl[i], mu.p[slot], sd_pred)
      crps[slot]     <- CRPS(y_graph_repl[i], mu.p[slot], sd_pred)
      scrps[slot]    <- SCRPS(y_graph_repl[i], mu.p[slot], sd_pred)
      mae[slot]      <- abs(y_graph_repl[i] - mu.p[slot])
      rmse[slot]     <- (y_graph_repl[i] - mu.p[slot])^2
    }
  }

  res <- list(mu = mu.p,
              var = var.p)
  # Each per-(replicate, obs) entry contributes once to the mean — this is
  # equivalent to the old "accumulate then divide by length(repl)" idiom
  # but generalises cleanly to per-observation results.
  res[["scores"]] <- data.frame(
    logscore = -factor * mean(logscore, na.rm = TRUE),
    crps     = -factor * mean(crps,     na.rm = TRUE),
    scrps    = -factor * mean(scrps,    na.rm = TRUE),
    mae      =  factor * mean(mae,      na.rm = TRUE),
    rmse     =  factor * sqrt(mean(rmse, na.rm = TRUE)))
  attr(res[["scores"]], "factor") <- factor
  if(tibble){
    res[["scores"]] <- tidyr::as_tibble(res[["scores"]])
    return(res)
  } else{
    return(res)
  }
}

Try the MetricGraph package in your browser

Any scripts or data that you put into this service are public.

MetricGraph documentation built on Nov. 11, 2026, 1:09 a.m.