mu <- c(-0.5, 1.65)
th <- seq(-pi, pi, l = 200)
K <- 5
coefs <- list(cos_a = 1 / (1:(K + 1))^3, sin_b = 1 / (1:K)^3)
n <- 10
col <- rainbow(n)
set.seed(13213)
old_par <- par(no.readonly = TRUE)
par(mfrow = c(2, 2))
for (j in 1:2) {
# Simulate synthetic data close to the ridge curve
rid <- ridge_curve(theta = th, mu = mu, coefs = coefs, ind_var = j)
ind <- sort(sample(length(th), size = n))
eps <- 0.25 * matrix(runif(2 * n, -1, 1), nrow = n, ncol = 2)
x <- sdetorus::toPiInt(rid[ind, ] + eps)
# Plot ridge and synthetic data, with signs from the second scores
s <- ridge_scores(x, mu = mu, coefs = coefs, ind_var = j)$scores
plot(x, xlim = c(-pi, pi), ylim = c(-pi, pi), axes = FALSE,
xlab = expression(theta[1]), ylab = expression(theta[2]), col = col,
pch = ifelse(sign(s[, 2]) == 1, "+", "-"), cex = 1.25)
sdetorus::linesTorus(rid[, 1], rid[, 2], lwd = 2)
abline(v = mu[1], lty = 3)
abline(h = mu[2], lty = 3)
points(mu[1], mu[2], pch = "*", cex = 3)
sdetorus::torusAxis()
# Projections
theta_projs <- proj_ridge_curve(x = x, mu = mu, coefs = coefs,
ind_var = j, ridge_curve_grid = rid,
)$theta_proj
projs <- ridge_curve(theta = theta_projs, mu = mu, coefs = coefs,
ind_var = j)
for (i in 1:n) {
sdetorus::linesTorus(x = c(x[i, 1], projs[i, 1]),
y = c(x[i, 2], projs[i, 2]),
col = col[i], lty = 3)
}
# Scores plot
plot(s, xlim = c(-pi, pi), ylim = c(-pi, pi), axes = FALSE,
xlab = "Score 1", ylab = "Score 2", col = col,
pch = ifelse(sign(s[, 2]) == 1, "+", "-"))
sdetorus::torusAxis()
abline(v = 0, lty = 3)
abline(h = 0, lty = 3)
points(0, 0, pch = "*", cex = 3)
}
par(old_par)
Run the code above in your browser using DataLab