EdoardoCostantini / gspcr

Generalized Supervised Principal Component Regression
Other
1 stars 0 forks source link

Negative value in `cp_thrs_PR2` computation #26

Open EdoardoCostantini opened 1 year ago

EdoardoCostantini commented 1 year ago

In cp_thrs_PR2(), it can happen that:

CNR2 <- 1 - exp(-2 / nrow(ivs) * (univ_mods$lls - univ_mods$ll0))

returns a negative value. This could happen if the likelihood of the more complex model is almost identical to the likelihood of the null model. In using this package, it could be that one of the possible predictors is so bad that this happens.

This was fixed in #ce6cc01 by taking the absolute value of the resulting CNR2 before taking the square root. This is not the most elegant solution. You could instead use some numerical trick. For example:

  1. Check if the difference univ_mods$lls - univ_mods$ll0 is smaller than floating point precision. If so, just set CNR2 to zero.
  2. After computing CNR2, check if it is less than 0. If so, set it to 0.
  3. Can also think of other transformations of the value to make things more stable (add values, multiple, take logs) and then reverse later.
EdoardoCostantini commented 1 year ago

Added data for replicating the issue in #1b55040

EdoardoCostantini commented 1 year ago

Implemented solution 2:

  1. After computing CNR2, check if it is less than 0. If so, set it to 0.

in #289e3b3

Still needs testing.

EdoardoCostantini commented 1 year ago

devtools::check() and testthat do not see the data for replication of error. Need to fix that before you can test the change.