2
0
Fork 0
CVE/simulations/method_compair_M2.R

74 lines
2.4 KiB
R
Raw Normal View History

2019-12-17 11:07:33 +00:00
# Specialized comparison for dataset M2 with others than the default parameters.
#
# Results are written to a CSV file with a dataset column and its simulation nr.
#
# Note:
# All methods are called via interface functions defined in
# 'simulation_interfaces.R'. In addition this file provides a few convenience
# functions, namely: 'progress_logger' and 'subspace_dist'.
#
# Dependencies (list of packages loaded):
# - MASS
# - dr
# - MAVE
# - CVE
source('simulation_interfaces.R') # depends on MASS, dr, MAVE and CVE
# Number of simulations
NR.SIM <- 100L
# Dataset param grid
grid <- expand.grid(
pmix = c(0.3, 0.4, 0.5),
lambda = c(0, 0.5, 1, 1.5)
)
# Methods for comparison, see 'simulation_interfaces.R' for their definitions.
methods <- c('CVE', 'wCVE', 'rCVE', 'meanOPG', 'meanMAVE')
# Build result data matrix (repeat each dataset simulation times).
result <- matrix(NA, nrow = nrow(grid) * NR.SIM,
ncol = ncol(grid) + length(methods))
colnames(result) <- c(colnames(grid), methods)
# Create a progress logger for length many reports.
logger <- progress_logger(nrow(grid) * NR.SIM * length(methods))
count <- 0
for (sim in seq_len(NR.SIM)) {
for (i in seq_len(nrow(grid))) {
# Create new dataset.
pmix <- grid[i, "pmix"]
lambda <- grid[i, "lambda"]
with(dataset("M2", pmix = pmix, lambda = lambda), {
X <<- X; Y <<- Y; B <<- B
})
count <- count + 1
result[count, "pmix"] <- pmix
result[count, "lambda"] <- lambda
# And each reference method.
for (method in methods) {
# Report simulation progress to user.
logger(method)
# Try/Catch to avoid simulation stop on error, this should not
# accure for CVE but may be the case for others like OPG.
tryCatch({
# Call dimension reduction method.
dr <- eval(call(method, X = X, Y = Y, k = ncol(B)))
# Compute distance of estimated `B` to true `B`.
result[count, method] <- subspace_dist(B, coef(dr, ncol(B)))
}, error = function(e) {}) # No-Operation error handler!
}
}
# explicit call to the garbage collector.
gc()
}
path <- paste0(getwd(),
format(Sys.time(), '/results/method_compair_M2_%Y-%m-%dT%H%M%S'))
# Write entire simulation results into a single file.
write.csv(result, file = paste0(path, ".csv"), row.names = FALSE)