[R-sig-ME] [R] understanding I() in lmer formula

Ben Bolker bbolker at gmail.com
Wed Jun 14 03:24:52 CEST 2017


FWIW my trivial efforts to replicate this/verify my guess that the
problem is related to I() in a random-effects term have failed.

This doesn't fail in an obvious way:

  set.seed(101)
  dd <- data.frame(x=rnorm(1000),y=rnorm(1000),z=rnorm(1000),
    f=factor(rep(1:20,50)))
  library(lme4)
  lmer(z~(1+x+I(x*y)||f),data=dd)
  lmer(z~(1+x+I(y*x)||f),data=dd)

Perhaps we're running afoul of the 500-character line-break in
deparse() somewhere ... ?


On Tue, Jun 13, 2017 at 8:54 PM, Ben Bolker <bbolker at gmail.com> wrote:
> It would definitely be good to get a reproducible example.  Looking at
> what's there, I'm *guessing* that the problem is with the I(x*y) in
> the *random effects* terms - there is quite likely some
> not-entirely-standard formula parsing/deconstruction going on there.
>
>    A workaround is likely to be defining the equivalent of x*y as an
> auxiliary variable in your data frame, rather than trying to include
> it on the fly.
>
>   16 terms in a random effect specification, even when they're
> diagonal, seems pretty large.  I hope you have a big data set ...
>
>
> On Tue, Jun 13, 2017 at 8:43 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
>>
>>> On Jun 13, 2017, at 7:17 PM, Don Cohen <don-r-help at isis.cs3-inc.com> wrote:
>>>
>>> Andrew Robinson writes:
>>>
>>>> can you provide a minimal executable example?
>>> I was hoping it was going to be something simple.
>>> It looks to me like this is related to parsing and has nothing
>>> to do with the data.
>>> I start by trying to simplify my formula and find that even deleting
>>> seemingly irrelevant parts leads to the same kinds of errors.
>>>
>>>>> but what is that "~" doing in the error message??
>>> I wish I knew!
>>>
>>> Here's an example that's not exactly small, but at least smaller
>>> than I started with.  The first one seems to work and then I move
>>> the # to the other line that I'd expect to have the same meaning.
>>>
>>>> fullx=lmer(log.corti~
>>>    z.n.fert.females*z.n.males+
>>>    is.alpha2*(z.infanticide.susceptibility_new+z.min.co.res+z.co.res+z.log.tenure)+
>>>    z.age.at.sample+sin.season+cos.season+
>>>    (1+z.n.fert.females+z.n.males+is.alpha2.subordinate+z.infanticide.susceptibility_new+
>>>     z.min.co.res+z.log.tenure+z.co.res+
>>>     z.age.at.sample+sin.season+cos.season+
>>>     I(z.n.fert.females*z.n.males)+
>>>       I(is.alpha2.subordinate*z.co.res)+
>>>      I(z.min.co.res*is.alpha2.subordinate)+
>>>      #  I(is.alpha2.subordinate*z.min.co.res)+
>>>     int.is.a.log.ten||monkeyid),
>>>        data=fe.re.xx$data, REML=F, control=contr)
>>> + + + + + + + + + + + + > fullx=lmer(log.corti~
>>>    z.n.fert.females*z.n.males+
>>>    is.alpha2*(z.infanticide.susceptibility_new+z.min.co.res+z.co.res+z.log.tenure)+
>>>    z.age.at.sample+sin.season+cos.season+
>>>    (1+z.n.fert.females+z.n.males+is.alpha2.subordinate+z.infanticide.susceptibility_new+
>>>     z.min.co.res+z.log.tenure+z.co.res+
>>>     z.age.at.sample+sin.season+cos.season+
>>>     I(z.n.fert.females*z.n.males)+
>>>       I(is.alpha2.subordinate*z.co.res)+
>>>     # I(z.min.co.res*is.alpha2.subordinate)+
>>>       I(is.alpha2.subordinate*z.min.co.res)+
>>>     int.is.a.log.ten||monkeyid),
>>>        data=fe.re.xx$data, REML=F, control=contr)
>>> + + + + + + + + + + + + Error in is.alpha2.subordinate * ~z.min.co.res :
>>>  non-numeric argument to binary operator
>>>>
>>>
>>> And if I start with the one that works and delete something
>>> seemingly irrelevant, like z.infanticide.susceptibility_new+
>>>
>>>> fullx=lmer(log.corti~
>>>    z.n.fert.females*z.n.males+
>>>    is.alpha2*(z.infanticide.susceptibility_new+z.min.co.res+z.co.res+z.log.tenure)+
>>>    z.age.at.sample+sin.season+cos.season+
>>>    (1+z.n.fert.females+z.n.males+is.alpha2.subordinate+ # z.infanticide.susceptibility_new+
>>>     z.min.co.res+z.log.tenure+z.co.res+
>>>     z.age.at.sample+sin.season+cos.season+
>>>     I(z.n.fert.females*z.n.males)+
>>>       I(is.alpha2.subordinate*z.co.res)+
>>>      I(z.min.co.res*is.alpha2.subordinate)+
>>>      #  I(is.alpha2.subordinate*z.min.co.res)+
>>>     int.is.a.log.ten||monkeyid),
>>>        data=fe.re.xx$data, REML=F, control=contr)
>>> + + + + + + + + + + + + Error in is.alpha2.subordinate * ~z.co.res :
>>>  non-numeric argument to binary operator
>>>
>>> I know this is not the minimal executable example requested, but I'm
>>> hoping someone will have an idea before I try to simplify it further
>>> and provide all you need to run it.
>>
>>
>> A few thoughts:
>>
>> 1. Strip down the formula so that only the term that causes the error is present.
>>
>> If the error happens with only that term, let us know.
>>
>> If not, add each other term back in AFTER the one problematic term, one by one, until you get the error, if it happens. When that happens, keep the original term and that additional term only, and see if the error still happens with just those two terms. Let us know.
>>
>> If the error does not recur, then there may be something tied to term order, which would be curious.
>>
>>
>> 2. Can you provide the output of:
>>
>>   str(fe.re.xx$data)
>>
>> which is presumably a data frame called 'data' within a larger object called 'fe.re.xx'. Hopefully, the number of columns in the data frame is "reasonable".
>>
>> Perhaps there is something about the structure of your data frame that is causing some issue.
>>
>>
>> 3. Also, make sure that the components of each term are only in the data frame object and not otherwise in your working environment, so that there is no risk of confusion as to what objects are being passed to the formula. Essentially, use ls() to scan the content of your working environment and see if any show there.
>>
>>
>> 4. Start a new, **clean** R session and see if the error still occurs. Be sure that there are no other objects in the environment or a corrupted environment causing issues. If you have a .RData file that is loaded with a new session, delete it or move it, if you need it. Also check for a .Rprofile file that might affect your working environment.
>>
>> If you can run 'R --vanilla' from the CLI, to start a new R session, that would help with getting a clean session.
>>
>>
>> Regards,
>>
>> Marc Schwartz
>>
>> _______________________________________________
>> R-sig-mixed-models at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models



More information about the R-sig-mixed-models mailing list