Skip to content

Commit 932da87

Browse files
committed
R2 error recovery
1 parent b48b014 commit 932da87

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: MyEllipsefit
22
Title: Uses the conicfit package to fit an ellipse, convenience function with an opional ggplot2 layer
3-
Version: 0.0.4.0
3+
Version: 0.0.4.2
44
Authors@R: person("Markus", "Loew", email = "[email protected]", role = c("aut", "cre"))
55
Description: Uses \code{conicfit} to calculate an ellipse to fit data. Convenience package to provide ellipsis parameters (axes, angle, area, and more). Provides a layer function for ggplot2 to visualise ellipses. See helpfile and example for differences between \code{car::ellipse} and this approach.
66
Depends:

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
MyEllipsefit 0.0.4.2
2+
----------------------------------------------------------------
3+
4+
* Conditional calculation of R2, gives NA for missing data
5+
instead of stopping
6+
17
MyEllipsefit 0.0.4.0
28
----------------------------------------------------------------
39

R/Ellipsefit.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,17 @@ Ellipsefit <- function(data, x, y, coords = FALSE, bbox = FALSE) {
7070

7171

7272
# calculate R2
73-
my.fit <- conicfit::fit.ellipseLMG(xy,
73+
my.fit <- try(conicfit::fit.ellipseLMG(xy,
7474
ParGini = geopara,
75-
LambdaIni = 1)
76-
SSres <- my.fit$RSS
77-
SStot <- stats::var(y, na.rm = TRUE)
78-
R2 <- 1 - (SSres / SStot)
79-
75+
LambdaIni = 1))
76+
if (inherits(my.fit, "try-error")) {
77+
R2 <- NA
78+
} else {
79+
SSres <- my.fit$RSS
80+
SStot <- stats::var(y, na.rm = TRUE)
81+
R2 <- 1 - (SSres / SStot)
82+
}
83+
8084
geopara.out <- as.data.frame(t(geopara))
8185
geopara.out$R2 <- R2
8286
names(geopara.out) <- c("Centerpoint_X", "Centerpoint_Y",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Fit parameters are:
2424
* The distance from the center to the perimenter along the minor axis.
2525
* The tilt angle of the ellipse.
2626
* The area of the ellipse.
27+
* The goodnes-of-fit R2 of the ellipse fit.
2728

2829
If *coords = TRUE*, returns a data frame with the coordinates of the ellipse in addition.
2930

0 commit comments

Comments
 (0)