compute_psm: Compute and Plot the Posterior Similarity Matrix

View source: R/04_MCMC_compute_psm.R

compute_psmR Documentation

Compute and Plot the Posterior Similarity Matrix

Description

The function computes and plots the posterior similarity matrix (PSM), either for the whole dataset, or separately for each group. The function takes as input an object from fit_CAM, fit_fiSAN, or fit_fSAN, used with the est_method = "MCMC" argument.

Usage

compute_psm(
  object,
  distributional = FALSE,
  group_specific = FALSE,
  plot = TRUE,
  ncores = 0
)

Arguments

object

An object of class SANmcmc.

distributional

Logical (default FALSE). If FALSE, the function computes the posterior similarity matrix (PSM) for the observational partition (i.e., between individual observations). If TRUE, it computes the PSM at the distributional level, that is, between groups.

group_specific

Logical (default FALSE). If FALSE, the function considers the overall PSM. If TRUE, the function considers the group-specific PSMs. This argument only affects the observational partition, i.e., when distributional is FALSE.

plot

Logical (default TRUE). Whether to plot the PSM.

ncores

A parameter to pass to the salso::salso() function. The number of CPU cores to use for parallel computing; a value of zero indicates the use of all cores of the system.

Value

The function compute_psm returns and plots the posterior similarity matrix. When distributional = FALSE, if group_specific = FALSE, the output is a matrix of dimension N x N; if group_specific = TRUE, the output is a list on length J (the number of groups), where each entry contains a matrix of dimension Nj x Nj. If distributional = TRUE, the output is a matrix of dimension J x J.

Examples

# Generate example data
set.seed(123)
y <- c(rnorm(100),rnorm(100,5))
g <- rep(1:2,rep(100,2))
plot(y,col=g)
# Fitting fiSAN via MCMC
est <- fit_fiSAN(y, g, est_method = "MCMC")
est
# Estimate PSM
psm_overall <- compute_psm(est)
# Estimate distributional PSM
psm_distrib <- compute_psm(est, distributional = TRUE)


sanba documentation built on Aug. 8, 2025, 6:15 p.m.