gdlc / BGLR-R

GNU General Public License v3.0
100 stars 69 forks source link

Error in factanal(covmat = Cov$Omega, factors = Cov$nF) : unable to optimize from this starting value #73

Open igorkf opened 1 year ago

igorkf commented 1 year ago

Hi, thanks for the BGLR package!

I'm trying to fit an FA1 model, but sometimes I get this error:

Checking variance co-variance matrix K  for linear term 1
Ok
Setting linear term 1
MSx=0.782358413511471
FA covariance matrix
df0 set to 23 for all the traits
S0 was set to: 
       DEH1        GAH1        GAH2        GEH1        IAH1        INH1        MIH1        MNH1        NCH1        NEH1        NEH2        NEH3        NYH2        NYH3        NYS1        SCH1        TXH1        TXH2        TXH3 
  7.9556863  20.2783135 173.7748939  43.4470011  56.2167504   3.7032637   6.7851079  15.6662375   5.6775808 101.9856404   8.3571794  15.3495887  47.5418310   1.9690249  51.6612751  58.9084196   0.6233221  17.9751277  19.7048923 
       WIH1        WIH2        WIH3 
 98.1848348  20.2203369   0.2596944 
var was set to 100
Error in factanal(covmat = Cov$Omega, factors = Cov$nF) : 
  unable to optimize from this starting value

Curiously, if I try to run a couple of times, the model works, so I suppose it could be to a random initialization.

After looking for this error, I saw this post in StackOverflow: https://stackoverflow.com/questions/41146141/error-in-factor-analysis-starting-values

Does it make sense to increase the number of starting values or give us the option to change the lower bound?
The documentation of factanal says nstart = 1 and lower = 0.005.

Thanks!

igorkf commented 1 year ago

To overcome this problem I did a try-catch approach to fit the model until it works:

fit_model <- function() {
  mod <- Multitrait(...)  # your arguments here
  return(mod)
}

run <- function() {
  mod <- T
  while (is.list(mod) == F) {
    mod <- tryCatch({fit_model()}, error = function(e) { return(TRUE) })
  }
  return(mod)
}

set.seed(2023)
mod <- run()

Seems overkill but it works in the first 2 or 3 tries.

perpdgo commented 1 year ago

Hello Igor,

Thanks for sharing the details of the problem and the hints for the solutions,

Best regards.

On Thu, Jul 20, 2023 at 2:52 PM Igor Kuivjogi Fernandes < @.***> wrote:

To overcome this problem I did a try-catch approach to fit the model until it works:

fit_model <- function() { mod <- Multitrait(...) # your arguments here return(mod) }

run <- function(x) { success <- FALSE while (!success) { success <- tryCatch({fit_model()}, error = function(e) { return(FALSE) }) } return(success) }

set.seed(2023) mod <- run()

Seems overkill but it works in the first few tries.

— Reply to this email directly, view it on GitHub https://github.com/gdlc/BGLR-R/issues/73#issuecomment-1644589932, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC3ARLSDL5XHRTJCLN5BZQTXRGLAZANCNFSM6AAAAAA2R5A3YI . You are receiving this because you are subscribed to this thread.Message ID: @.***>