0

I'm trying to apply the lme function to my data, but the model gives follow message:

 mod.1 = lme(lon ~ sex + month2 + bat + sex*month2, random=~1|id, method="ML", data = AA_patch_GLM, na.action=na.exclude)

Error in MEEM(object, conLin, control$niterEM) : 
  Singularity in backsolve at level 0, block 1

dput for data, copy from https://pastebin.com/tv3NvChR (too large to include here)

 str(AA_patch_GLM)
'data.frame':   2005 obs. of  12 variables:
 $ lon     : num  -25.3 -25.4 -25.4 -25.4 -25.4 ...
 $ lat     : num  -51.9 -51.9 -52 -52 -52 ...
 $ id      : Factor w/ 12 levels "24641.05","24642.03",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ sex     : Factor w/ 2 levels "F","M": 1 1 1 1 1 1 1 1 1 1 ...
 $ bat     : int  -3442 -3364 -3462 -3216 -3216 -2643 -2812 -2307 -2131 -2131 ...
 $ year    : chr  "2005" "2005" "2005" "2005" ...
 $ month   : chr  "12" "12" "12" "12" ...
 $ patch_id: Factor w/ 45 levels "111870.17_1",..: 34 34 34 34 34 34 34 34 34 34 ...
 $ YMD     : Date, format: "2005-12-30" "2005-12-31" "2005-12-31" ...
 $ month2  : Ord.factor w/ 7 levels "January"<"February"<..: 7 7 7 7 7 1 1 1 1 1 ...
 $ lonsc   : num [1:2005, 1] -0.209 -0.213 -0.215 -0.219 -0.222 ...
 $ batsc   : num [1:2005, 1] 0.131 0.179 0.118 0.271 0.271 ...

What's the problem?

I saw a solution applying the lme4::lmer function, but there is another option to continue to use lme function?

1
  • Curious what your reasons are for continuing with lme (there are some reasonable ones ...)
    – Ben Bolker
    Commented Dec 17, 2022 at 23:00

1 Answer 1

0

The problem is that you have collinear combinations of predictors. In particular, here are some diagnostics:

## construct the fixed-effect model matrix for your problem
X <- model.matrix(~ sex + month2 + bat + sex*month2, data = AA_patch_GLM)
lc <- caret::findLinearCombos(X)
colnames(X)[lc$linearCombos[[1]]]
##  [1] "sexM:month2^6" "(Intercept)"   "sexM"          "month2.L"     
##  [5] "month2.C"      "month2^4"      "month2^5"      "month2^6"     
##  [9] "sexM:month2.L" "sexM:month2.C" "sexM:month2^4" "sexM:month2^5"

This is in a weird order, but it suggests that the sex × month interaction is causing problems. Indeed:

with(AA_patch_GLM, table(sex, month2))
## sex January February March April May June December
##   F     367      276   317   204  43    0        6
##   M     131       93    90   120 124   75      159

shows that you're missing data for one sex/month combination (i.e., no females were sampled in June).

You can:

  • construct the sex/month interaction yourself (data$SM <- with(data, interaction(sex, month2, drop = TRUE))) and use ~ SM + bat — but then you'll have to sort out main effects and interactions yourself (ugh)
  • construct the model matrix by hand (as above), drop the redundant column(s), then include all the resulting columns in the model:
d2 <- with(AA_patch_GLM,
           data.frame(lon,
                      as.data.frame(X),
                      id))
## drop linearly dependent column
## note data.frame() has "sanitized" variable names (:, ^ both converted to .)
d2 <- d2[names(d2) != "sexM.month2.6"]
lme(reformulate(colnames(d2)[2:15], response = "lon"),
    random=~1|id, method="ML", data = d2)

Again, the results will be uglier than the simpler version of the model.

  • use a patched version of nlme (I submitted a patch here but it hasn't been considered)
remotes::install_github("bbolker/nlme")

Not the answer you're looking for? Browse other questions tagged or ask your own question.