[R] Regression on Circle/Sphere: Correlation Coefficient?
Gabor Grothendieck
ggrothend|eck @end|ng |rom gm@||@com
Wed Nov 25 22:38:59 CET 2026
If I understand correctly you are not really looking for a correlation
coefficient but rather a measure of the dependence
of (xPred, yPred) on (x, y). In that case you want a number between
0 and 1 and the KPC package can provide such a measure
between points on a manifold S in R^n. The interesting thing about it
is that you don't have to specify S.
Using your data:
library(KPC)
library(kernlab)
X <- cbind(x, y)
Y <- cbind(xPred, yPred)
ky <- rbfdot(1 / (2 * median(dist(Y))^2))
kz <- rbfdot(1 / (2 * median(dist(X))^2))
KPCRKHS(
Y = Y,
X = NULL,
Z = X,
ky = ky,
kxz = kz
)
## [1] 0.9758999
The package also has another estimator:
KMAc(
Y = Y,
X = Z,
k = ky,
Knn = 1
)
## [1] 0.9900212
On Tue, Nov 24, 2026 at 6:00 PM Leonard Mada <lmada using umft.ro> wrote:
>
> Dear R Users,
>
> I stumbled upon an interesting question. I fitted a set of points to a
> circle or to a circle segment (or sphere cap) and I wondered if there is
> something equivalent to a correlation coefficient?
>
> I have a dilemma in constructing such a coefficient. A value of 0 means
> no correlation and a value of 1 means perfect correlation. But I do not
> know if a value of -1 is meaningful or how to interpret it.
>
> The example below illustrates the method:
>
> # Regression on Circle (2D)
> lm.circle = function(x, y) {
> xm = mean(x); ym = mean(y);
> dx = x - xm; dy = y - ym;
> n = length(x);
> S2 = x^2 + y^2;
> Sdx = sum(S2*dx);
> Sdy = sum(S2*dy);
> #
> divx = 2*(sum(x*dx) * sum(dy*y) - sum(x*dy) * sum(dx*y));
> x0 = (Sdx * sum(dy*y) - Sdy * sum(dx*y)) / divx;
> #
> y0 = - (Sdx * sum(dy*x) - Sdy * sum(dx*x)) / divx;
> # Radius:
> r = sum((x-x0)^2, (y-y0)^2);
> r = sqrt(r/n);
> #
> fit = list(C = c(x0, y0), R = r);
> return(fit);
> }
>
> ### Plot Circle:
> lines.circle = function(r, centre = c(0,0), ..., n = 33) {
> phi = seq(0, 2*pi, length.out = n);
> phi[n] = phi[1];
> x = r*sin(phi) + centre[1];
> y = r*cos(phi) + centre[2];
> lines(x, y, ...);
> invisible(cbind(x,y));
> }
> ### Example:
> n = 30;
> r = 10;
> x0 = 5; y0 = 2; # Center
> # Random data:
> phi = (2*pi)*runif(n, 0, 3/5);
> dri = runif(n, -1,1) / 10 * r;
> x = x0 + (r+dri) * sin(phi);
> y = y0 + (r+dri) * cos(phi);
>
> # Fit
> fit = lm.circle(x, y);
> Cxy = fit$C;
> phi.fit = atan2(x - Cxy[1], y - Cxy[2]);
> # Predicted:
> xPred = fit$R * sin(phi.fit) + Cxy[1];
> yPred = fit$R * cos(phi.fit) + Cxy[2];
>
> # Plot:
> plot(x, y, xlim = range(x) + c(-1,1), ylim=range(y) + c(-1,1), asp=1)
> lines.circle(fit$R, fit$C, col = "#C28092D2")
> points(xPred, yPred, col = "red")
>
>
> The code to fit a 3D sphere (which is generalizable) is on GitHub:
> https://github.com/discoleo/R/blob/master/Stat/Regression.Circle.R
>
> Various packages deal with cyclic data, but focus on testing uniformity
> or performing regression once the angles are known (e.g. circular,
> circularKDE, circda). I am unaware of any packages fitting spatial data
> to a circle/sphere.
> My primary interest was in a slightly different direction (fitting some
> curved axis or cutting planes to 3D protein structures); but the
> question above was interesting in itself.
>
> Many thanks in advance for any hints.
>
> Sincerely,
>
> Leonard
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list