Learn R Programming

ridgetorus (version 1.0.3)

ridge_scores: Scores and scales for Fourier-fitted ridge curves

Description

Computation of PCA scores for Fourier-fitted ridge curves. The scores are defined as follows:

  • First scores: signed distances along the ridge curve of the data projections to \(\mu\).

  • Second scores: signed toroidal distances from the data points to their ridge projections.

The scores can be scaled to \((-\pi, \pi)\) or remain as \((l / 2, m_2)\), where \(l\) is the length of the curve and \(m_2\) is the maximal absolute second score.

Usage

ridge_scores(
  x,
  mu = c(0, 0),
  coefs = list(cos_a = c(0, 0), sin_b = 0),
  ind_var = 1,
  N = 500,
  scale = TRUE,
  at2 = TRUE
)

max_score_2( mu = c(0, 0), coefs = list(cos_a = c(0, 0), sin_b = 0), ind_var = 1, L = 25, f = 2, at2 = TRUE )

Value

ridge_scores returns a list with:

scores

a matrix of size c(nx, 2) with the ridge scores.

scales

a vector of length 2 with the scale limits for the axes.

max_score_2 computes the maximum allowed second score to rescale if scale = TRUE.

Arguments

x

a matrix of size c(nx, 2) with angular coordinates.

mu

a vector of size 2 giving \((\mu_1, \mu_2)\). Defaults to c(0, 0).

coefs

list of coefficients cos_a (\(a_k\)) and sin_b (\(b_k\) giving the Fourier fit of the ridge curve. Defaults to list(cos_a = c(0, 0), sin_b = 0). See examples.

ind_var

index \(j\) of the variable that parametrizes the ridge. Defaults to 1.

N

number of discretization points for approximating curve lengths. Defaults to 5e2.

scale

scale the resulting scores to \([-\pi, \pi)^2\)? Defaults to TRUE.

at2

do the atan2 fit instead of the sine fit (only using \(S_m\))? Defaults to TRUE. at2 = FALSE is not recommended to use.

L

grid along he variable ind_var used for searching the maximum allowed second score. Defaults to 25.

f

factor for shrinking the grid on the variable that is different to ind_var. Defaults to 2.

Details

The mean \(\mu\) corresponds to the first score being null.

Examples

Run this code
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