tensor_predictors/sim/plots.R

101 lines
3.9 KiB
R

if (!endsWith(getwd(), "/sim")) {
setwd("sim")
}
sim.plot <- function(file.prefix, date, to.file = FALSE) {
# file.prefix <- "sim-ising-small"
# # file.prefix <- "sim-normal"
# date <- "20221012" # yyyymmdd, to match all "[0-9]{6}"
time <- "[0-9]{4}" # HHMM, to match all "[0-9]{4}"
colors <- c(
PCA = "#a11414",
HOPCA = "#2a62b6",
TSIR = "#9313b9",
GMLM = "#247407"
)
line.width <- 1.75
margins <- c(5.1, 4.1, 4.1, 0.1)
sim <- Reduce(rbind, Map(function(path) {
df <- read.csv(path)
df$n <- as.integer(tail(head(strsplit(path, "[-.]")[[1]], -1), 1))
df
}, list.files(".", pattern = paste0(
"^", file.prefix, "-", date, "T", time, "-[0-9]+[.]csv$", collapse = ""
))))
stats <- aggregate(. ~ n, sim, mean)
q75 <- aggregate(. ~ n, sim, function(x) quantile(x, 0.75))
q25 <- aggregate(. ~ n, sim, function(x) quantile(x, 0.25))
if (to.file) {
width <- 720
png(paste(file.prefix, "-", date, ".png", sep = ""),
width = width, height = round((6 / 11) * width, -1),
pointsize = 12)
}
layout(mat = matrix(c(
1, 2,
3, 3
), 2, 2, byrow = TRUE),
widths = c(1, 1),
heights = c(12, 1), respect = FALSE)
# layout.show(3)
with(stats, {
par(mar = margins)
plot(range(n), 0:1,
type = "n", bty = "n", main = "Subspace Distance",
xlab = "Sample Size", ylab = "Error")
lines(n, dist.subspace.gmlm, col = colors["GMLM"], lwd = line.width)
lines(n, dist.subspace.hopca, col = colors["HOPCA"], lwd = line.width)
lines(n, dist.subspace.pca, col = colors["PCA"], lwd = line.width)
lines(n, dist.subspace.tsir, col = colors["TSIR"], lwd = line.width)
xn <- c(q75$n, rev(q25$n))
polygon(x = xn, y = c(q75$dist.subspace.gmlm, rev(q25$dist.subspace.gmlm)),
col = adjustcolor(colors["GMLM"], alpha.f = 0.3), border = NA)
polygon(x = xn, y = c(q75$dist.subspace.hopca, rev(q25$dist.subspace.hopca)),
col = adjustcolor(colors["HOPCA"], alpha.f = 0.3), border = NA)
polygon(x = xn, y = c(q75$dist.subspace.pca, rev(q25$dist.subspace.pca)),
col = adjustcolor(colors["PCA"], alpha.f = 0.3), border = NA)
polygon(x = xn, y = c(q75$dist.subspace.tsir, rev(q25$dist.subspace.tsir)),
col = adjustcolor(colors["TSIR"], alpha.f = 0.3), border = NA)
})
with(stats, {
par(mar = margins)
plot(range(n), 0:1,
type = "n", bty = "n", main = "RMSE (Prediction Error)",
xlab = "Sample Size", ylab = "Error")
xn <- c(q75$n, rev(q25$n))
polygon(x = xn, y = c(q75$error.pred.gmlm, rev(q25$error.pred.gmlm)),
col = adjustcolor(colors["GMLM"], alpha.f = 0.3), border = NA)
polygon(x = xn, y = c(q75$error.pred.hopca, rev(q25$error.pred.hopca)),
col = adjustcolor(colors["HOPCA"], alpha.f = 0.3), border = NA)
polygon(x = xn, y = c(q75$error.pred.pca, rev(q25$error.pred.pca)),
col = adjustcolor(colors["PCA"], alpha.f = 0.3), border = NA)
polygon(x = xn, y = c(q75$error.pred.tsir, rev(q25$error.pred.tsir)),
col = adjustcolor(colors["TSIR"], alpha.f = 0.3), border = NA)
lines(n, error.pred.gmlm, col = colors["GMLM"], lwd = line.width)
lines(n, error.pred.hopca, col = colors["HOPCA"], lwd = line.width)
lines(n, error.pred.pca, col = colors["PCA"], lwd = line.width)
lines(n, error.pred.tsir, col = colors["TSIR"], lwd = line.width)
})
par(mar = rep(0, 4))
plot(1:2, 1:2, type = "n", bty = "n", axes = FALSE, xlab = "", ylab = "")
legend("center", legend = names(colors), col = colors, lwd = line.width,
lty = 1, bty = "n", horiz = TRUE)
if (to.file) {
dev.off()
}
}
sim.plot("sim-ising-small", "20221012", TRUE)
sim.plot("sim-normal", "20221012", TRUE)