Skip to content

Commit 96a900a

Browse files
committed
fix: error if ca data only has two rows or columns (#45)
1 parent 29a7b08 commit 96a900a

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# explor (development version)
2+
3+
- Add more explicit error messages when data from a correspondance analysis had only two rows or two columns (#45)
4+
15
# explor 0.3.10
26

37
- Fix varsup and indsup functions renamed to supvar and supind in GDAtools

R/prepare_results_CA.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
prepare_results.CA <- function(obj) {
1111

1212
if (!inherits(obj, "CA")) stop("obj must be of class CA")
13+
if (!is.array(obj$row$coord) || !is.array(obj$col$coord)) stop("obj must have at least two dimensions on rows or columns")
1314

1415
## Axes names and inertia
1516
axes <- seq_len(ncol(obj$col$coord))

R/prepare_results_dudi_coa.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
prepare_results.coa <- function(obj) {
1111

1212
if (!inherits(obj, "coa")) stop("obj must be of class coa")
13+
if ((ncol(obj$li) < 2) || (ncol(obj$co) < 2)) stop("obj must have at least two dimensions on rows or columns")
1314

1415
if (!requireNamespace("ade4", quietly = TRUE)) {
1516
stop("the ade4 package is needed for this function to work.")

tests/testthat/test_prepare_results_CA.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ ca <- FactoMineR::CA(children[, 1:5],
88
)
99
res <- prepare_results(ca)
1010

11+
test_that("error if not at least three rows or cols", {
12+
tmp <- FactoMineR::CA(children[, 1:2])
13+
expect_error(prepare_results(tmp))
14+
tmp <- FactoMineR::CA(children[1:2, ])
15+
expect_error(prepare_results(tmp))
16+
})
17+
1118
test_that("Eigenvalues are equals", {
1219
expect_equal(unname(ca$eig[, "percentage of variance"]), res$eig$percent)
1320
})

tests/testthat/test_prepare_results_dudi.coa.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ coa$supc <- ade4::supcol(coa, col_sup)
1111
iner <- ade4::inertia.dudi(coa, row.inertia = TRUE, col.inertia = TRUE)
1212
res <- prepare_results(coa)
1313

14+
test_that("error if not at least three rows or cols", {
15+
tmp <- dudi.coa(tab[, 1:2], nf = 5, scannf = FALSE)
16+
expect_error(prepare_results(tmp))
17+
tmp <- dudi.coa(tab[1:2, ], nf = 5, scannf = FALSE)
18+
expect_error(prepare_results(tmp))
19+
})
20+
1421
test_that("Eigenvalues are equals", {
1522
expect_equal(coa$eig / sum(coa$eig) * 100, res$eig$percent)
1623
})

0 commit comments

Comments
 (0)