RSAVS_Determine_Mu: Determine the subgroup effects

View source: R/pre_define_functions.r

RSAVS_Determine_MuR Documentation

Determine the subgroup effects

Description

This function determines the final mu vector given the grouping results.

Usage

RSAVS_Determine_Mu(
  mu_vec,
  group_res,
  klim = c(2, 7, 4),
  usepam = (length(mu_vec) < 2000),
  round_digits = NULL
)

Arguments

mu_vec

numerical vector. n = length(mu_vec) is the number of observations. Presumably it comes from the ADMM algorithm and is not a very good grouping result.

group_res

a list, containing the grouping results. Each element of group_res is a list containing the indecs from the same subgroup. See RSAVS_S_to_Groups for the output structure.

klim

a length-3 integer vector, storing 'kmin', 'kmax', 'dudahart_kmax' for specifying settings for clustering algorithm.

usepam

boolen, whether to use 'cluster::pam()' or instead 'cluster::clara()' to estimate center of clusters.

round_digits

a positive integer digit. If group_res is missing and round_digits is provided, then the function will directly round the mu_vec to round_digits to provide the final grouping results.

Details

Currently, the resulting subgroup effects are the average value of mu_vec for those belonging to the same subgroup according to group_res.

If group_res is missing and round_digits is NULL, then a simple K-means is performed on mu_vec to get the estimated grouping result.

Value

a length-n vector of subgroup effects. Observations belonging to the same subgroup should have the same effects.

Examples

# basic settings
set.seed(1024)
n <- 100    # number of observations
group_center <- c(-1, 0, 1)    # three group centers
# subgroup effect vector    
alpha_true <- sample(group_center, size = n, replace = TRUE)
alpha_est <- alpha_true + rnorm(n, sd = 0.25)    # add noise to group effect
table(alpha_true)

# Use `group_res` to determine estimated group effect from `alpha_est`
d_mat <- RSAVS:::RSAVS_Generate_D_Matrix(n)    # pair-wise difference matrix
# Normally, s_vec should come from the algorithm
#   Here we just use a simple estimate, which might lead to not so satisfing results
s_vec <- round(d_mat %*% alpha_est, 0)    
group_res <- RSAVS:::RSAVS_S_to_Groups(s_vec, n)
table(RSAVS:::RSAVS_Determine_Mu(alpha_est, group_res = group_res))

# Use `pamk` to determine estiamted group effect
table(RSAVS:::RSAVS_Determine_Mu(alpha_est))

# Use directly rounding to determine estimated group effect
table(RSAVS:::RSAVS_Determine_Mu(alpha_est, round_digits = 1))

fenguoerbian/RSAVS documentation built on Oct. 25, 2024, 3:16 p.m.