Re: Anova() in car not works after loading piecewiseSEM package

varin sacha via R-help <[email protected]> Sun, 19 Jul 2026 12:24:14 +0200
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
Thanks Duncan. This seems like a promising workaround. You should try re-re=
gistering 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 probl=
em is caused by the incompatible MuMIn::model.frame.lme() S3 registration r=
ather than by car::Anova() itself.




> Le 19 juil. 2026 =C3=A0 12:14, Duncan Murdoch <[email protected]> =
a =C3=A9crit :
> =

> =EF=BB=BFOn 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=E2=80=99ve provided should make it straightforward for them to in=
vestigate.
>>>> =

>>>> 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=E2=80=94specifically, 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=3D
>>> 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 =3D FALSE, ...)
>> But Anova() calls it (per traceback()) with:
>> model.frame.lme(object, data, xlev =3D 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 fr=
om 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 cou=
ld be for the MuMIn package to change its definition to something compatibl=
e with the multcomp definition, but the two functions appear to do differen=
t things.  I don't know if they can be made compatible.
> =

> Another problem is in the stats package.  The stats:::model.matrix.defaul=
t method makes a call to
> =

>  data <- model.frame(object, data, xlev =3D 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 =3D data, xlev =3D xlev)
> =

> would fix the issue of binding data to the "random" argument, but you'd s=
till 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 =3D Orthodont, random =3D ~ 1)
>>>> Anova(fm2)
>>> Error in if (random) { : the condition has length > 1
>>> =

>>> =

>>>>> =

>>>>>> Le 19 juil. 2026 =C3=A0 08:22, Jinsong Zhao <[email protected]> a =C3=
=A9crit :
>>>>>> =

>>>>>> =EF=BB=BFThank 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 =3D Orthodont, random =3D ~ 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 =3D xlev)
>>>>>> 7: model.frame(object, data, xlev =3D xlev)
>>>>>> 6: model.matrix.default(mod, data =3D structure(list(distance =3D 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 =3D 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 =3D
>>>>>> 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 =3D 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
>>>>>> =3D c("ordered",
>>>>>>     "factor")), Sex =3D 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 =3D c("Male", "Female"), class =3D
>>>>>> "factor")), row.names =3D 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
>>>>>> =3D ~Sex, class =3D c("nfnGroupedData",
>>>>>>     "nfGroupedData", "groupedData", "data.frame"), formula =3D
>>>>>> distance ~
>>>>>>         age | Subject, labels =3D list(x =3D "Age", y =3D "Distance =
from
>>>>>> pituitary to pterygomaxillary fissure"), units =3D list(
>>>>>>         x =3D "(yr)", y =3D "(mm)"), FUN =3D structure(function (x)
>>>>>>     max(x, na.rm =3D TRUE), source =3D "function (x) max(x, na.rm =
=3D
>>>>>> TRUE)"), order.groups =3D TRUE),
>>>>>>         contrasts.arg =3D list(Sex =3D structure(c(0, 1), dim =3D 2:=
1,
>>>>>> dimnames =3D list(
>>>>>>             c("Male", "Female"), "Female"))))
>>>>>> 5: NextMethod(formula(object), data =3D data, contrasts.arg =3D
>>>>>> object$contrasts)
>>>>>> 4: model.matrix.lme(mod)
>>>>>> 3: model.matrix(mod)
>>>>>> 2: Anova_II_lme(mod, vcov., singular.ok =3D 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=3DChinese (Simplified)_China.utf8
>>>>>> [2] LC_CTYPE=3DChinese (Simplified)_China.utf8
>>>>>> [3] LC_MONETARY=3DChinese (Simplified)_China.utf8
>>>>>> [4] LC_NUMERIC=3DC
>>>>>> [5] LC_TIME=3DChinese (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] =E2=80=983.1.5=E2=80=99
>>>>>>> packageVersion("piecewiseSEM")
>>>>>> [1] =E2=80=982.3.1=E2=80=99
>>>>>>> packageVersion("nlme")
>>>>>> [1] =E2=80=983.1.170=E2=80=99
>>>>>> =

>>>>>> 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=E2=80=99t 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=E2=80=99s failing and
>>>>>>> whether the problem lies in car, piecewiseSEM, or an
>>>>>>> incompatibility between the two packages.
>>>>>>> =

>>>>>>> =

>>>>>>> =

>>>>>>>>> Le 19 juil. 2026 =C3=A0 04:58, Jinsong Zhao <[email protected]> a =
=C3=A9crit :
>>>>>>>> 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 =3D Orthodont, random =3D ~=
 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.
> =