How to find a best fit circle/ellipse using R? -
i've been reading few methods fit circle data (like this). see how methods work on real data , thought of using r this. tried searching rseek packages can came nothing useful.
so, there packages compute best fit circle given data set (similar how lm()
fit linear model data set)? otherwise, how might 1 perform such task in r?
here's naive implementation of function minimises ss(a,b,r) paper:
fitss <- function(xy, a0=mean(xy[,1]), b0=mean(xy[,2]), r0 = mean(sqrt((xy[,1]-a0)^2 + (xy[,2]-b0)^2)), ...){ ss <- function(abr){ sum((abr[3] - sqrt((xy[,1]-abr[1])^2 + (xy[,2]-abr[2])^2))^2) } optim(c(a0,b0,r0), ss, ...) }
i've written couple of supporting functions generate random data on circles , plot circles. hence:
> xy = sim_circles(10) > f = fitss(xy) > plot(xy,asp=1,xlim=c(-2,2),ylim=c(-2,2)) > lines(circlexy(f$par))
note doesn't use gradients nor check error code convergence. can supply initial values or can have guess.
Comments
Post a Comment