mixOmicsTeam / mixOmics

Development repository for the Bioconductor package 'mixOmics '
http://mixomics.org/
150 stars 51 forks source link

Error of multiple of replacement in `perf` for plsda and possible solution #303

Open esheeep opened 5 months ago

esheeep commented 5 months ago

Hi mixOmics team and users,


🐞 Describe the bug: When run function perf on a plsda/ splsda object, this error might happen:

Error in ncomp_opt[measure, ijk] <- which(t(rowMeans(mat.error.rate[[measure_i]][[ijk]])) == : number of items to replace is not a multiple of replacement length

This is from the step to return the number of optimal component according to the error rates and happens when there are more than one component has the same minimum error rates. This error happens when using LOOV (validation = "loo, with always nrepeat = 1) or Mfold with nrepeat < 3 and more often when the sample size is very small.


πŸ’‘ Possible solution: Before another updated mixOmics comes out, here is my solution in case you also encounter it:

  1. download the source code of perf.
  2. find the function perf.mixo_plsda (line 740- end), copy.
  3. paste to a new R source document, modify and define a new function (e.g. named 'mod_perf.mixo_plsda'). Modify line 297 in the new doc (or line 1036 in the perf source code) to: ncomp_opt[measure, ijk] = which(t(rowMeans(mat.error.rate[[measure_i]][[ijk]])) == min(t(rowMeans(mat.error.rate[[measure_i]][[ijk]]))))[1] (just add a [1] at the end of this line in the orginal code to avoid multiple replacement). Then run the new function.
  4. call to environment() assures that the function will be able to call other hidden functions in mixOmics by using the code: environment(mod_perf.mixo_plsda) <- asNamespace("mixOmics").
  5. run the function for your splsda or plsda object, e.g. splsda.perf <- mod_perf.mixo_plsda(splsda, validation = "loo"). And it should work now!

Hope it helps!

Guan

iandanilevicz commented 2 months ago

Hi @esheeep, I tried validation = 'Mfold', nrepeat = 50 and it displayed the same error, so it is not restricted to small nrepeat, I tried the proposed change but without success. The bug persists, it doens't matter if I put loo or Mfold.

iandanilevicz commented 2 months ago

Hi all, Some additional issues in my case, the bug persists for plsda validation="loo" when there are 3 or more principal components and dist="max.dist", because when I select dist="centroids.dist" or dist = "mahalanobis.dist" is everything ok, of course, dist = "all"also doesn't run properly.