[R] Regression on Circle/Sphere: Correlation Coefficient?
Leonard Mada
|m@d@ @end|ng |rom um|t@ro
Wed Nov 25 21:11:09 CET 2026
Dear R Users,
This message completes some information about the circular package.
After a more through inspection of that package, I detected the function
lsfit.circle - but did not identify a proper goodness of fit estimator:
lsfit.circle(x, y, init = NULL, units = c("radians", "degrees"), ...)
To quote: "lsfit.circle uses the optim function to minimize the sum of
the squared residuals between the observations and the optimally fitting
circle." Note that it is actually possible to fit using a linear LS formula.
It also makes reference to the following article:
Coope, I. (1993). Circle fitting by linear and non-linear least squares.
Journal of Optimization Theory and Applications, 76, 381-388.
The article provides the matrix formula for a linear LS optimization. My
own 2D formula is a linear LS formula (and solving the system by hand) -
while the formula for the 3D fit on GitHub is in matrix-form and is
easily extendible to higher dimensions:
https://github.com/discoleo/R/blob/master/Stat/Regression.Circle.R
Note that the article does NOT include a goodness-of-fit estimator
beyond the sum of the residuals. The comparison between 2 methods is:
"The circle in figure 3 differs only slightly from that of figure 1 but
that in figure 4 gives a much more visually pleasing fit than that of
the "total least squares" fit of figure 2;"
It does NOT analyse if the points fit well the circle, either.
2. Additional Package Suggestions (by Jeff)
I will take a closer look to package conicfit (2015), as well as
fitConic (2023) and RConics (2025) - probably over the next few days.
Many thanks for the hint. I hope that this information is useful to
others as well.
Sincerely,
Leonard
On 11/25/2026 1:03 PM, Leonard Mada wrote:
> Dear Jeff,
>
> Thank you for the reference.
>
> I did mention in the initial post the packages circular, circularKDE,
> circda - but they focus on circular data and statistics on circular
> data (i.e. the angles) - not on geometric coordinates that fit on a
> circle.
>
> I have looked now more carefully inside package "circular" - though it
> does not provide a vignette, nor references to some articles (only to
> a book).
>
> The function cor.circular in package *circular* assumes x and y to be
> angles:
>> The correlation coefficient is computed like Pearson's product moment
>> correlation for two linear variables X and Y. In the computational
>> formula, however, (xi - xbar) and (yi - ybar) are replaced by sin(xi
>> - xbar) and sin(yi - ybar), where xbar and ybar in the second two
>> expressions are the mean directions of the samples.
>
> In my example, I fit (x, y)-coordinates to a circle and the result is
> the circle that fits best the points: the actual points (x, y) may
> deviate from the estimated circle and I was pondering how to measure
> the goodness of fit or the "circle"-correlation for the set of points.
>
> In the meantime, I feel that the value of 0 correlation (or goodness
> of fit) is not properly defined either (as any point will be somewhat
> closer to some segment of the circle).
>
> I hope this clarifies my initial post.
>
> Sincerely,
>
> Leonard
>
>
>
>
> On 11/25/2026 2:22 AM, Jeff Newmiller wrote:
>> `circular` package?
>>
>>
>> On November 24, 2026 2:59:58 PM PST, 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
>> <https://www.R-project.org/posting-guide.html> and provide
>> commented, minimal, self-contained, reproducible code.
>>
>> --
>> Sent from my phone. Please excuse my brevity.
[[alternative HTML version deleted]]
More information about the R-help
mailing list