1

I am doing a stratified analysis of a mixed effect model using nlme's lme function and thus employing purrr's map function. At times, the number of observations is too small and I get errors from nlme that the matrix is non-positive definite. One of these two errors appears:

  • Error in MEEM(object, conLin, control$niterEM) : Singularity in backsolve at level 0, block 1
  • Error in chol.default((value + t(value))/2) : the leading minor of order 1 is not positive definite

Rather than stopping everything, I rather substitute "NA" using purrr::possibly() when an error occurs.

Here is a trival example to demonstrate this issue.

library(nlme)
library(purrr)
set.seed(423)
 fm2 <- nlme::lme(distance ~ age + Sex, data = Orthodont[sample(108, 3), ], random = ~ 1)
# Error in chol.default((value + t(value))/2) : 
#  the leading minor of order 1 is not positive definite


## Using purrr::possibly()
set.seed(423)
fm2 <- purrr::possibly(
lme(distance ~ age + Sex, data = Orthodont[sample(108, 3), ], random = ~ 1), 
NA
)
# Error in chol.default((value + t(value))/2) : 
#   the leading minor of order 1 is not positive definite

Because I am using possibly() on the nlme function and not the function within lme(), would this explain why possibly() is not working?

Thanks

Session Info

R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042), RStudio 2022.2.2.485

Locale:
  LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
  LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

Package version:
  lattice_0.20.45 magrittr_2.0.3  nlme_3.1-157    purrr_0.3.4      rlang_1.0.2       
phargart
  • 655
  • 3
  • 14

1 Answers1

0

possibly takes a function as input. We can do

lme2 <- purrr::possibly(lme, otherwise = NA)
set.seed(423)
 fm2 <- 
 lme2(distance ~ age + Sex, data = Orthodont[sample(108, 3), ], random = ~ 1)
fm2
[1] NA
akrun
  • 874,273
  • 37
  • 540
  • 662
  • The `lme2()` above has an error, the `random` argument should be "~1". – phargart May 25 '22 at 16:53
  • @phargart thanks for correcting. It was a typo – akrun May 25 '22 at 16:54
  • Thank you @akrun. So, you have to store the `possibly()` function as an object instead of applying the function directly. I figured out that whatever arguments are passed to the `possibly.f()` (eg.`lme2`) function is passed directly to the function `f()` or in this case `lme()`. – phargart May 25 '22 at 16:56
  • 1
    @phargart you don't need to create an object. It can be on the fly i.e. `fm2 <- purrr::possibly(lme, otherwise = NA)(distance ~ age + Sex, data = Orthodont[sample(108, 3), ], random = ~ 1)` – akrun May 25 '22 at 16:57
  • 1
    `possibly()` takes a function as input and exports it as a function...so the code above would be similar if it is a function. Using an object makes the code cleaner for sure. – phargart May 25 '22 at 17:01
  • 1
    @phargart also you can reuse the derived function at multiple places – akrun May 25 '22 at 17:02
  • This post: https://stackoverflow.com/questions/50486527/how-to-use-map-with-possibly is similar to this question but slightly different. – phargart May 26 '22 at 22:22