Skip to content

Use of which() may create erroneous indices #596

@emstruong

Description

@emstruong

x <- which(Lambda0[vars_cf, y] != 0)

Through both the GSCA/GSCA_M and IGSCA code there are situations where which is used to index which elements are to be updated.

As far as I can tell, when the specified model is single-indicator or single-construct there are hard-to-detect situations where the indexing can be broken.

For example, take the following two data

Lambda0_A <- matrix(
  c(0.4, 0.0, 
     0.0, 0.5),
  nrow = 2,
  byrow = TRUE,
  dimnames = list(c("xi1", "xi2"), c("x11", "x21"))
)

vars_cf <-  'xi2'

This creates the following buggy behavior

which(Lambda0_A[vars_cf, 2] != 0)

which returns

[1] 1

This isn't correct because xi2 is in the second row.

Improved behavior may be found by running the following

(rowSums(Lambda0_A[vars_cf, 2, drop = FALSE]) != 0)  |> which()  |> names()

which returns

"xi2"

Which is semantically clearer.

Some other advantages is that as long as the arrays are named correctly, then this indexing should fail noisily, instead of silently, which is important for correctness.

I am applying this fix throughout in my #594

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions