Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when passing formula to mixed() with more than 60 characters #5

Closed
smithdanielle opened this issue Oct 7, 2015 · 3 comments
Closed

Comments

@smithdanielle
Copy link

I first noticed this problem within my own data, but have since been able to replicate it with a built-in dataset.

I can't quite figure out the pattern, but here's what I've found so far; when specifying a formula with up to four fixed terms, afex's mixed function works as expected:

library(afex)
library(languageR)

data(lexdec, package = "languageR")

mixed(RT ~ Correct*Trial + PrevType * meanWeight + (1|Subject) + (1|Word), data = lexdec)
mixed(RT ~ Correct*Trial + PrevType * meanWeight + NativeLanguage + Length + (1|Subject) + (1|Word), data = lexdec)
mixed(RT ~ Correct*Trial + PrevType * meanWeight + NativeLanguage*Length + (1|Subject) + (1|Word), data = lexdec)

However, when specifying a formula with five or more fixed terms, it seems that mixed is unable to parse any term from the fifth onward, and an error is thrown up as a result:

 mixed(RT ~ Correct + Correct:Trial + PrevType * meanWeight + NativeLanguage + Length + (1|Subject) + (1|Word), data = lexdec)
Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:7: unexpected symbol
1: RT~Correct + Correct:Trial + PrevType * meanWeight + NativeLanguage + +(1 | Subject) + (1 | Word)
2: RT    Length
         ^

mixed(RT ~ Correct + Trial + PrevType * meanWeight  + NativeLanguage * Length + Frequency + (1|Subject) + (1|Word), data = lexdec)
Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:7: unexpected symbol
1: RT~Correct + Trial + PrevType * meanWeight + NativeLanguage * Length + +(1 | Subject) + (1 | Word)
2: RT    Frequency
         ^

mixed(RT ~ Correct + Trial + PrevType + meanWeight  + NativeLanguage * Length + Frequency + PrevType:meanWeight + (1|Subject) + (1|Word), data = lexdec)
Error in parse(text = x, keep.source = FALSE) : 
  <text>:2:7: unexpected symbol
1: RT~Correct + Trial + PrevType + meanWeight + NativeLanguage * Length + +(1 | Subject) + (1 | Word)
2: RT    Frequency
         ^

In cases where a working formula is specified, this warning message always appears:

In addition: Warning messages:
1: In stri_c(..., sep = sep, collapse = collapse, ignore_null = TRUE) :
  longer object length is not a multiple of shorter object length

lmer is still working as expected:

lmer(RT ~ Correct + Trial + PrevType * meanWeight + 
+           Frequency + NativeLanguage * Length + (1|Subject) + (1|Word), data = lexdec)
Linear mixed model fit by REML ['lmerMod']
Formula: RT ~ Correct + Trial + PrevType * meanWeight + Frequency + NativeLanguage *      Length + (1 | Subject) + (1 | Word)
   Data: lexdec
REML criterion at convergence: -972.8595
Random effects:
 Groups   Name        Std.Dev.
 Word     (Intercept) 0.04748 
 Subject  (Intercept) 0.13547 
 Residual             0.16831 
Number of obs: 1659, groups:  Word, 79; Subject, 21
Fixed Effects:
               (Intercept)            Correctincorrect                       Trial                PrevTypeword                  meanWeight  
                  6.486675                   -0.064751                   -0.000245                   -0.009240                    0.038132  
                 Frequency         NativeLanguageOther                      Length     PrevTypeword:meanWeight  NativeLanguageOther:Length  
                 -0.048023                    0.054975                    0.003433                   -0.020189                    0.017017

All these formulas used to work in a previous version of afex (afraid I don't know exactly what I was running... whatever the most up-to-date version was in March 2015).

SessionInfo() can be seen here.

EDIT: I can confirm that mixed gives the expected output for the following formula when using afex 0.13-145 from CRAN, though I do get an error regarding object length, as above:

mixed(RT ~ Correct + Trial + PrevType * meanWeight + 
                    Frequency + NativeLanguage * Length + (1|Subject) + (1|Word), data = lexdec)
Contrasts set to contr.sum for the following variables: Correct, PrevType, NativeLanguage, Subject, Word
Numerical variables NOT centered on 0 (i.e., interpretation of all main effects might be difficult if in interactions): Trial, meanWeight, Frequency, Length
Fitting 10 (g)lmer() models:
[..........]
Obtaining 9 p-values:
[.........]
                 Effect     F ndf     ddf F.scaling p.value
1               Correct  8.15   1 1627.73      1.00    .004
2                 Trial  7.57   1 1592.43      1.00    .006
3              PrevType  0.17   1 1605.39      1.00     .68
4            meanWeight 14.85   1   75.39      1.00   .0002
5             Frequency 56.53   1   76.08      1.00  <.0001
6        NativeLanguage  0.70   1   27.11      1.00     .41
7                Length  8.70   1   75.83      1.00    .004
8   PrevType:meanWeight  6.18   1 1601.18      1.00     .01
9 NativeLanguage:Length 14.24   1 1555.49      1.00   .0002
Warning message:
In stri_c(..., sep = sep, collapse = collapse, ignore_null = TRUE) :
  longer object length is not a multiple of shorter object length
@smithdanielle smithdanielle changed the title Error when specifying a formula within mixed() when the formula is long or there are multiple interaction terms Error when specifying a formula within mixed() when the formula contains more than four fixed terms Oct 7, 2015
@singmann
Copy link
Owner

singmann commented Oct 8, 2015

The reason for this bug is the (by default) 60 characters limit of deparse and not the number of factors. The following reproduces the bug:

data(obk.long)
obk2 <- obk.long
colnames(obk2) <- sapply(colnames(obk2), function(x) paste0(x, x, x, x, x, x))
mixed(valuevaluevaluevaluevaluevalue ~ treatmenttreatmenttreatmenttreatmenttreatmenttreatment * phasephasephasephasephasephase * hourhourhourhourhourhour + (1|idididididid), obk2, method = "LRT")

The problem can be pushed to only appear with a formula of length 500 characters by simply changing the width.cutoff argument to 500L. Done in 0f652d7

@singmann singmann changed the title Error when specifying a formula within mixed() when the formula contains more than four fixed terms Error when passing formula to mixed() with more than 60 characters Oct 8, 2015
singmann pushed a commit that referenced this issue Oct 8, 2015
added corresponding tests, see #5
@singmann singmann closed this as completed Oct 8, 2015
@singmann
Copy link
Owner

singmann commented Oct 8, 2015

@smithdanielle Thanks a lot for reporting that. Very helpful. Please let me know if you find anything else.

@RNMahon
Copy link

RNMahon commented Oct 21, 2019

Is there an easy way on the user side to adjust the width.cutoff? I have a formula with 597 characters (long variable names from another program) and was not able to open the source files to edit this line. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants