# \donttest{
## Example 1: AP/TM reconstruction on a symmetric 20x20 matrix
## (10 percent known entries)
RNGkind("L'Ecuyer-CMRG")
set.seed(123456789)
m <- 20L
p <- 20L
# sample (dataset)
X_true <- abs(matrix(rnorm(m * p), nrow = m, ncol = p))
X_true <- 0.5 * (X_true + t(X_true)) # symmetric
idx <- sample.int(
m * p,
size = max(1L, floor(0.1 * (m * p))), # 10 percent known
replace = FALSE
)
M <- diag(m * p)[idx, , drop = FALSE]
b_row <- rowSums(X_true)
b_col <- colSums(X_true)
b_val <- matrix(as.numeric(X_true)[idx], ncol = 1L)
# model (unique MNBLUE estimator)
result <- tmpinv(
M = M,
b_row = b_row,
b_col = b_col,
b_val = b_val,
bounds = c(0, NA), # non-negativity
symmetric = TRUE,
r = 1L,
alpha = 1.0
)
# coefficients
print("true X:")
print(round(X_true, 4))
print("X_hat:")
print(round(result$x, 4))
# numerical stability
print("\nNumerical stability:")
print(paste(" kappaC :", result$model$kappaC))
print(paste(" kappaB :", result$model$kappaB))
print(paste(" kappaA :", result$model$kappaA))
# diagnostics
print("\nGoodness-of-fit:")
print(paste(" NRMSE :", result$model$nrmse))
print(paste(" Diagnostic band (min):", min(result$model$x_lower)))
print(paste(" Diagnostic band (max):", max(result$model$x_upper)))
# bootstrap NRMSE t-test
tt <- rclsp::ttest(
result$model,
sample_size = 30L,
seed = 123456789L,
distribution = rnorm,
partial = TRUE
)
print("\nBootstrap t-test:")
print(tt)
## Example 2: AP/TM reconstruction on a 40x40 matrix
## with zero diagonal and reduced (20,20) submodels
## (20 percent known entries)
RNGkind("L'Ecuyer-CMRG")
set.seed(123456789)
m <- 40L
p <- 40L
# sample (dataset)
X_true <- abs(matrix(rnorm(m * p), nrow = m, ncol = p))
diag(X_true) <- 0 # zero diagonal
idx <- sample.int(
m * p,
size = max(1L, floor(0.2 * (m * p))), # 20 percent known
replace = FALSE
)
M <- diag(m * p)[idx, , drop = FALSE]
b_row <- rowSums(X_true)
b_col <- colSums(X_true)
b_val <- matrix(as.numeric(X_true)[idx], ncol = 1L)
# model (reduced models of size 20x20)
result <- tmpinv(
M = M,
b_row = b_row,
b_col = b_col,
b_val = b_val,
zero_diagonal = TRUE,
reduced = c(20L, 20L),
bounds = c(0, NA),
r = 1L,
alpha = 1.0
)
print("true X:")
print(round(X_true, 4))
print("X_hat:")
print(round(result$x, 4))
# numerical stability across submodels
kC <- sapply(result$model, function(CLSP) CLSP$kappaC)
kB <- sapply(result$model, function(CLSP) CLSP$kappaB)
kA <- sapply(result$model, function(CLSP) CLSP$kappaA)
print("\nNumerical stability (min-max across models):")
print(paste(" kappaC :", range(kC)))
print(paste(" kappaB :", range(kB)))
print(paste(" kappaA :", range(kA)))
# diagnostics (min-max)
nrmse <- sapply(result$model, function(CLSP) CLSP$nrmse)
x_low <- unlist(lapply(result$model, function(CLSP) CLSP$x_lower))
x_up <- unlist(lapply(result$model, function(CLSP) CLSP$x_upper))
print("\nGoodness-of-fit (min-max across models):")
print(paste(" NRMSE :", range(nrmse)))
print(paste(" Diagnostic band (min):", range(x_low)))
print(paste(" Diagnostic band (max):", range(x_up)))
# bootstrap t-tests across all block models
print("\nBootstrap t-tests:")
tests <- lapply(
result$model,
function(CLSP) rclsp::ttest(
CLSP,
sample_size = 30L,
seed = 123456789L,
distribution = rnorm,
partial = TRUE
)
)
print(tests)
# }
Run the code above in your browser using DataLab