R/NormalBlockMeanKnownClusters.R

## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
##  CLASS NormalBlockMeanKnownClusters #################
## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#' Normal-Block-Mean Model with Known Clustering
#'
#' R6 class for a Normal-Block-Mean model with a known clustering.
#' @examples
#' ex <- generate_normal_block_mean_data(n = 50, p = 20, d = 1, q = 3)
#' data <- NormalBlockData$new(ex$Y, ex$X)
#' model <- normal_block(data, blocks = ex$parameters$C, model = "mean")
#' plot(model)
#' @export
NormalBlockMeanKnownClusters <- R6::R6Class(
  classname = "NormalBlockMeanKnownClusters",
  inherit   = NormalBlockMeanBase,
  ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  ## PUBLIC MEMBERS ----
  ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  public = list(
    #' @description Create a new [`NormalBlockMeanKnownClusters`] object.
    #' @param data object of NormalBlockData class, with responses and design matrix
    #' @param C p x q clustering matrix, C_jk = 1 if variable j belongs to cluster k
    #' @param sparsity to apply on variance matrix when calling GLASSO
    #' @param control structured list of more specific parameters, to generate with NB_control
    #' @return A new [`NormalBlockMeanKnownClusters`] object
    initialize = function(data, C, sparsity = 0, control = NB_control()) {
      check_known_clustering(C, data$p)
      super$initialize(data, ncol(C), sparsity, control)
      private$C <- C
    }
  ),

  ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  ## PRIVATE MEMBERS ----
  ## %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  private = list(
    known_clusters = TRUE,


    B_estimator = function(Omega = private$Omega){
      OC <- Omega %*% private$C
      return(t(solve(crossprod(private$C, OC), t(self$data$XtXm1 %*% self$data$XtY %*% OC))))
    },

    Sigma_estimator = function(B = private$B){
      R     <- self$data$Y - tcrossprod(self$data$X %*% B, private$C)
      Sigma <- crossprod(R) / self$n
      return(Sigma)
    },

    compute_loglik = function(B = private$B, Omega = private$Omega){
      log_det_Omega <- as.numeric(determinant(Omega, logarithm = TRUE)$modulus)
      R <- self$data$Y - tcrossprod(self$data$X %*% B, private$C)
      l <- - self$n * self$p * log(2 * pi) / 2 + self$n * log_det_Omega / 2 - sum((R %*% Omega) * R) / 2
      ## penalized objective (see the C++ core): $loglik adds the penalty back
      if (self$sparsity > 0) l <- l - self$sparsity * sum(abs(self$sparsity_weights * Omega))
      return(as.numeric(l))
    },

    ## Runs the EM recursion via the Rcpp/Armadillo core (src/exports.cpp,
    ## NormalBlockMeanKnownClusters_fit).
    EM_optimize = function(control) {
      init <- private$optim_initialize()
      res  <- NormalBlockMeanKnownClusters_fit(
        Y = self$data$Y, X = self$data$X, C = private$C,
        B0 = init$B, Omega0 = init$Omega,
        sparsity = self$sparsity, sparsity_weights = self$sparsity_weights,
        noise_covariance = private$res_covariance,
        accelerate = private$use_acceleration(control),
        niter = control$niter, threshold = control$threshold
      )
      private$niter <- res$niter
      list(B = res$B, Omega = res$Omega, ll_list = res$objective)
    },

    ## Reference R implementation of the same recursion, kept since the C++
    ## port is being validated against it (test-cpp-normal-block-mean.R).
    EM_optimize_R = function(control){
      init_params <- private$optim_initialize()
      B           <- init_params$B
      Omega       <- init_params$Omega
      ll_prev     <- private$compute_loglik(B, Omega)
      ll_list     <- c(ll_prev)
      warm        <- NULL
      for(i in 1:control$niter){
        B     <- private$B_estimator(Omega)
        Sigma <- private$Sigma_estimator(B)
        og    <- private$omega_from_sigma_warm(Sigma, warm)
        Omega <- og$Omega; warm <- og$warm

        ll_current <- private$compute_loglik(B, Omega)
        ll_list     <- c(ll_list, ll_current)
        if(abs(ll_current - ll_prev) < control$threshold){
          break
        }else{ll_prev <- ll_current}
      }
      private$niter <- i
      list(B = B, Omega = Omega, ll_list = ll_list)
    }

  )
)

Try the normalblockr package in your browser

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

normalblockr documentation built on Nov. 24, 2026, 1:06 a.m.