On 2026-07-19 8:07 a.m., Kamil Bartoń wrote:

On 2026-07-19 13:56, Duncan Murdoch wrote:
On 2026-07-19 7:39 a.m., Kamil Bartoń wrote:
The problem may be that `car:::model.matrix.lme` uses:

NextMethod(formula(object), [...]

while the first argument for `NextMethod` should be a name of a generic 
function. This is why
it is the lme object that is passed to `model.matrix.default` (which in turn 
looks for
`model.frame.lme`, which is not implemented in "nlme") instead of a formula.

I suppose the line in  `car:::model.matrix.lme` should be changed to:

model.frame(formula(object), [...])

to work as intended.

You're right that NextMethod is not being called the way the docs say it should 
be, but it does
appear to dispatch correctly.  When given an "lme" object, NextMethod calls the 
"default" method.


But the code suggests the authors' intention was to call the "default" method 
but with model formula
as the first argument, and not the "lme" object.


I think you mean it should be changed to model.matrix( .... ), since calling model.frame( .... ) there gives an error.

But at that point in the code the two lines return identical results. That is,

NextMethod(formula(object), data = data, contrasts.arg = object$contrasts)

(which is what is used in the function), and

model.matrix(formula(object), data = data, contrasts.arg = object$contrasts)

(my correction to your suggestion), give the same answer.

Duncan Murdoch




Duncan Murdoch


~kB




On 2026-07-19 13:21, varin sacha wrote:
Sorry,

There is no nlme:::model.frame.lme. That makes an S3 method signature mismatch 
the most likely
explanation. The question of how best to resolve it is probably one for the 
MuMIn package
maintainer.

Best


Le 19 juil. 2026 à 12:30, varin sacha via R-help <[email protected]> a écrit 
:

Thanks Duncan. This seems like a promising workaround. You should try 
re-registering the
multcomp method:

registerS3method("model.frame", "lme", multcomp:::model.frame.lme)

And then test:

car::Anova(fm2)

If this resolves the issue, it would provide strong evidence that the problem 
is caused by the
incompatible MuMIn::model.frame.lme() S3 registration rather than by 
car::Anova() itself.




Le 19 juil. 2026 à 12:14, Duncan Murdoch <[email protected]> a écrit :

On 2026-07-19 4:55 a.m., Jinsong Zhao wrote:
On 7/19/2026 3:52 PM, Jinsong Zhao wrote:
On 7/19/2026 2:59 PM, Jinsong Zhao wrote:

On 7/19/2026 2:38 PM, varin sacha wrote:
Ok !
According to what I see, the error is occurring inside
car:::Anova.lme(), specifically during the construction of the model
matrix.

It therefore appears to be a genuine compatibility problem between
the current CRAN versions of car (3.1-5), piecewiseSEM (2.3.1),
and/or nlme (3.1-170).

The fact that you reproduced the same behavior on both Windows and
FreeBSD also suggests that the issue is not platform-specific.

I think this would be worth reporting to the package maintainers
(perhaps starting with car, since the traceback shows that the
failure occurs inside Anova.lme(), while mentioning that the problem
only arises after loading piecewiseSEM). The reproducible example
you’ve provided should make it straightforward for them to investigate.

The current maintainers of car and piecewiseSEM packages are also
copied on this thread. As a regular user, I am just wondering what's
behind the change in Anova()'s behavior—specifically, what gets
modified after attaching piecewiseSEM?

Best,

Jinsong

A small step forward toward the root of the issue: I've just
discovered that the model.frame.lme() function defined in the MuMIn
package is what caused this problem (so I've copied this email to the
maintainer of MuMIn).

However, I'm still unclear as to why Anova() calls model.frame.lme()
in the first place, given that car does not depend on the MuMIn
package. And directly invoking MuMIn:::model.frame.lme(fm2, random=
TRUE) did not cause error.

I think I've found the root cause.
piecewiseSEM imports MuMIn, where model.frame.lme is registered as an S3
method:
S3method(model.frame, lme)
Its definition is:
model.frame.lme <- function(formula, random = FALSE, ...)
But Anova() calls it (per traceback()) with:
model.frame.lme(object, data, xlev = xlev)
This passes data to the random argument, causing a type mismatch and the
error:
Error in if (random) { : the condition has length > 1
I've diagnosed the issue, but I don't yet know how to fix it.

This looks hard to fix.

One problem is that there are two definitions for model.frame.lme, one from 
MuMIn and the other
from multcomp.  The one from MuMIn is being called.  If the one in multcomp was 
called, things
would be fine. Perhaps a fix could be for the MuMIn package to change its 
definition to
something compatible with the multcomp definition, but the two functions appear 
to do different
things.  I don't know if they can be made compatible.

Another problem is in the stats package.  The stats:::model.matrix.default 
method makes a call to

data <- model.frame(object, data, xlev = xlev)

The definition of the generic model.frame() looks like

function (formula, ...)
UseMethod("model.frame")

so stats:::model.matrix.default has no basis for assuming that the second 
argument is the
data.  Changing that call to

data <- model.frame(object, data = data, xlev = xlev)

would fix the issue of binding data to the "random" argument, but you'd still 
end up calling
the "wrong" method.

Maybe someone else has an elegant idea to fix this?

Duncan Murdoch

Best,
Jinsong
Best,

Jinsong

library(MuMIn)
library(nlme)
library(car)
Loading required package: carData
fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
Anova(fm2)
Error in if (random) { : the condition has length > 1



Le 19 juil. 2026 à 08:22, Jinsong Zhao <[email protected]> a écrit :

Thank for the instruction. Here is the whole outputs:

library(piecewiseSEM)
Registered S3 method overwritten by 'lme4':
     method           from
     na.action.merMod car

     This is piecewiseSEM version 2.3.0.2.


     Questions or bugs can be addressed to <[email protected]>.
library(nlme)
library(car)
Loading required package: carData
fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
Anova(fm2)
Error in if (random) { : the condition has length > 1
car::Anova(fm2)
Error in if (random) { : the condition has length > 1
car:::Anova.lme(fm2)
Error in if (random) { : the condition has length > 1
traceback()
8: model.frame.lme(object, data, xlev = xlev)
7: model.frame(object, data, xlev = xlev)
6: model.matrix.default(mod, data = structure(list(distance = c(26,
      25, 29, 31, 21.5, 22.5, 23, 26.5, 23, 22.5, 24, 27.5, 25.5, 27.5,
      26.5, 27, 20, 23.5, 22.5, 26, 24.5, 25.5, 27, 28.5, 22, 22, 24.5,
      26.5, 24, 21.5, 24.5, 25.5, 23, 20.5, 31, 26, 27.5, 28, 31, 31.5,
      23, 23, 23.5, 25, 21.5, 23.5, 24, 28, 17, 24.5, 26, 29.5, 22.5,
      25.5, 25.5, 26, 23, 24.5, 26, 30, 22, 21.5, 23.5, 25, 21, 20,
      21.5, 23, 21, 21.5, 24, 25.5, 20.5, 24, 24.5, 26, 23.5, 24.5,
      25, 26.5, 21.5, 23, 22.5, 23.5, 20, 21, 21, 22.5, 21.5, 22.5,
      23, 25, 23, 23, 23.5, 24, 20, 21, 22, 21.5, 16.5, 19, 19, 19.5,
      24.5, 25, 28, 28), age = c(8, 10, 12, 14, 8, 10, 12, 14, 8, 10,
      12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12,
      14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14,
      8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8,
      10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10,
      12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12,
      14, 8, 10, 12, 14, 8, 10, 12, 14, 8, 10, 12, 14), Subject =
structure(c(15L,
      15L, 15L, 15L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 14L, 14L, 14L,
      14L, 2L, 2L, 2L, 2L, 13L, 13L, 13L, 13L, 5L, 5L, 5L, 5L, 6L,
      6L, 6L, 6L, 11L, 11L, 11L, 11L, 16L, 16L, 16L, 16L, 4L, 4L, 4L,
      4L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 12L,
      12L, 12L, 12L, 1L, 1L, 1L, 1L, 20L, 20L, 20L, 20L, 23L, 23L,
      23L, 23L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 21L, 21L, 21L,
      21L, 19L, 19L, 19L, 19L, 22L, 22L, 22L, 22L, 24L, 24L, 24L, 24L,
      18L, 18L, 18L, 18L, 17L, 17L, 17L, 17L, 27L, 27L, 27L, 27L),
levels = c("M16",
      "M05", "M02", "M11", "M07", "M08", "M03", "M12", "M13", "M14",
      "M09", "M15", "M06", "M04", "M01", "M10", "F10", "F09", "F06",
      "F01", "F05", "F07", "F02", "F08", "F03", "F04", "F11"), class
= c("ordered",
      "factor")), Sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
      2L, 2L, 2L, 2L), levels = c("Male", "Female"), class =
"factor")), row.names = c("1",
      "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
      "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
      "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
      "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
      "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
      "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
      "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
      "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
      "91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
      "101", "102", "103", "104", "105", "106", "107", "108"), outer
= ~Sex, class = c("nfnGroupedData",
      "nfGroupedData", "groupedData", "data.frame"), formula =
distance ~
          age | Subject, labels = list(x = "Age", y = "Distance from
pituitary to pterygomaxillary fissure"), units = list(
          x = "(yr)", y = "(mm)"), FUN = structure(function (x)
      max(x, na.rm = TRUE), source = "function (x) max(x, na.rm =
TRUE)"), order.groups = TRUE),
          contrasts.arg = list(Sex = structure(c(0, 1), dim = 2:1,
dimnames = list(
              c("Male", "Female"), "Female"))))
5: NextMethod(formula(object), data = data, contrasts.arg =
object$contrasts)
4: model.matrix.lme(mod)
3: model.matrix(mod)
2: Anova_II_lme(mod, vcov., singular.ok = singular.ok)
1: car:::Anova.lme(fm2)
sessionInfo()
R version 4.6.1 (2026-06-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default
     LAPACK version 3.12.1

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8
[2] LC_CTYPE=Chinese (Simplified)_China.utf8
[3] LC_MONETARY=Chinese (Simplified)_China.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.utf8

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets methods base

other attached packages:
[1] car_3.1-5          carData_3.0-6      nlme_3.1-170
piecewiseSEM_2.3.1

loaded via a namespace (and not attached):
    [1] Matrix_1.7-5       jsonlite_2.0.0     compiler_4.6.1 Rcpp_1.1.2
    [5] DiagrammeR_1.0.12  splines_4.6.1      boot_1.3-32 fastmap_1.2.0
    [9] lattice_0.22-9     TH.data_1.1-5      Formula_1.2-5
MuMIn_1.48.19
[13] rbibutils_2.4.1    htmlwidgets_1.6.4  MASS_7.3-66
visNetwork_2.1.4
[17] nloptr_2.2.1       insight_1.5.2      minqa_1.2.8
RColorBrewer_1.1-3
[21] rlang_1.3.0        multcomp_1.4-31 performance_0.17.1
estimability_2.0.0
[25] cli_3.6.6          magrittr_2.0.5     Rdpack_2.6.6 emmeans_2.0.4
[29] digest_0.6.39      grid_4.6.1         mvtnorm_1.4-2
sandwich_3.1-2
[33] lme4_2.0-6         reformulas_0.4.4   glue_1.8.1 codetools_0.2-20
[37] zoo_1.8-15         survival_3.8-9     abind_1.4-8 stats4_4.6.1
[41] tools_4.6.1        htmltools_0.5.9
packageVersion("car")
[1] ‘3.1.5’
packageVersion("piecewiseSEM")
[1] ‘2.3.1’
packageVersion("nlme")
[1] ‘3.1.170’

All the packages are installed from CRAN, and updated to the latest
version. I also run the codes on FreeBSD 15.1, the same output.

Best,

Jinsong

On 7/19/2026 2:02 PM, varin sacha wrote:
Hi,

Could this be a package compatibility bug?

Since Anova(fm2), car::Anova(fm2), and even car:::Anova.lme(fm2)
all produce the same error, it doesn’t appear to be a simple
namespace masking issue.

Could you post the output of:

traceback()
sessionInfo()
packageVersion("car")
packageVersion("piecewiseSEM")
packageVersion("nlme")

That should help identify the exact call that’s failing and
whether the problem lies in car, piecewiseSEM, or an
incompatibility between the two packages.



Le 19 juil. 2026 à 04:58, Jinsong Zhao <[email protected]> a écrit :
I have tried each solution in a new R session, and neither works.

library(piecewiseSEM)
Registered S3 method overwritten by 'lme4':
     method           from
     na.action.merMod car

     This is piecewiseSEM version 2.3.0.2.


     Questions or bugs can be addressed to <[email protected]>.
library(nlme)
library(car)
Loading required package: carData
fm2 <- lme(distance ~ age + Sex, data = Orthodont, random = ~ 1)
Anova(fm2)
Error in if (random) { : the condition has length > 1
car::Anova(fm2)
Error in if (random) { : the condition has length > 1
car:::Anova.lme(fm2)
Error in if (random) { : the condition has length > 1

______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





______________________________________________
[email protected] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to