2
$\begingroup$

I'd like your opinion on a very strange behavior that I recently encountered running glmer(). The problem is that when I make the dependent variable into a logical vector, glmer behaves weirdly. My dependent variable is Accuracy, and it is coded in terms of 1 (accurate response) and 0 (wrong response). What puzzles me is that transforming accuracy to a logical vector should work the same way for glmer, as a logical vector is coded in terms of TRUE or FALSE, having also 2 levels. However, glmer gives me different results depending on the transformation of the dependent variable I use. Have you guys encountered this before? Do you know why it happens? Below is sample code so you can replicate the problem yourselves.

#Create fake data
Subject   <- c(rep("S1",4), rep("S2",4), rep("S3",4), rep("S4",4))
Item      <- rep(c("I1","I2","I3","I4"),4)
Factor1   <- c(c(rep("e1",2),rep("e2",2)), c("e1","e2","e2","e1"), 
           c(rep("e2",2),rep("e1",2)), c("e2","e1","e1","e2"))                  
Accuracy  <- c(1,1,0,0,1,0,1,0,1,0,1,1,1,1,1,1)

#Create data frame and make "Accuracy" into a factor with 2 levels
data          <- data.frame(Subject,Item,Factor1, Accuracy)
data$Accuracy <- factor(data$Accuracy)  #Accuracy is a factor w/ 2 levels
#Run glmer
m1 <- glmer(Accuracy ~ Factor1 + (1+Factor1|Subject) + (1+Factor1|Item), family = "binomial", data= data)  
summary(m1)
Fixed effects:
            Estimate Std. Error z value Pr(>|z|)  
(Intercept)    1.946      1.069   1.820   0.0687 .
Factor1e2     -1.946      1.282  -1.518   0.1290  
---
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

That is the output of the first model. Now, look at what happens if I transform data$Accuracy into a logical vector when I run the model:

m2 <- glmer(as.logical(as.numeric(Accuracy)) ~ Factor1 + (1+Factor1|Subject) + (1+Factor1|Item), family = "binomial", data= data)  
summary(m2)

Fixed effects:
             Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.557e+01  1.259e+05       0        1
Factor1e2   2.223e-06  1.781e+05       0        1

As you can see, now the coefficient estimates are very different. As I said, this seems very puzzling to me and I'd like yo know if you have some thoughts on why this should be.

Thanks a lot!

--Sol

$\endgroup$
2
  • 1
    $\begingroup$ I'm not sure why, but breaking it into two steps works for me: AccLog <- as.logical(as.numeric(Accuracy)) then run m2 with AccLog. $\endgroup$ Commented Oct 15, 2013 at 18:57
  • $\begingroup$ You get the same result with glm, not just glmer. $\endgroup$ Commented Oct 15, 2013 at 19:00

1 Answer 1

7
$\begingroup$

More of a programming question. Compare:

> as.logical(as.numeric(data$Accuracy)) 
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE 
[16] TRUE 
> as.logical(as.numeric(Accuracy)) 
 [1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE 
[13]  TRUE  TRUE  TRUE  TRUE 

You're performing the former with your call to glmer since you are using the data = ... argument

m2 <- glmer(as.logical(as.numeric(Accuracy)) ~ Factor1 + (1+Factor1|Subject) + (1+Factor1|Item), family = "binomial", data= data)

As to why this is happening:

> as.numeric(data$Accuracy) 
 [1] 2 2 1 1 2 1 2 1 2 1 2 2 2 2 2 2 
> as.numeric(Accuracy) 
 [1] 1 1 0 0 1 0 1 0 1 0 1 1 1 1 1 1

Basically as.numeric returns the numeric representation of the levels of a factor variable, and then as.logical treats all non-zero values as TRUE (not entirely sure about negative values, actually). To get the original values back, you need to use

> as.numeric(levels(data$Accuracy)[data$Accuracy]) 
 [1] 1 1 0 0 1 0 1 0 1 0 1 1 1 1 1 1 

Thus...

> m2 <- glmer(as.logical(as.numeric(levels(Accuracy)[Accuracy])) ~ Factor1 + (1+Factor1|Subject) + (1+Factor1|Item), family = "binomial", data= data)   
> summary(m2) 
... 
Fixed effects: 
            Estimate Std. Error z value Pr(>|z|)   
(Intercept)    1.946      1.069   1.820   0.0687 . 
Factor1e2     -1.946      1.282  -1.518   0.1290  
$\endgroup$
3
  • 1
    $\begingroup$ it might be worth submitting this as an issue at github.com/lme4/lme4/issues -- I think it should work correctly with a factor, as glm() does ... $\endgroup$
    – Ben Bolker
    Commented Oct 16, 2013 at 0:54
  • 2
    $\begingroup$ oops. Reading this more carefully I see that it isn't a glmer issue at all. I will say that at least the development version of lme4 gives an error Response is constant - cannot fit the model , which at least gives a clue ... $\endgroup$
    – Ben Bolker
    Commented Oct 16, 2013 at 13:43
  • $\begingroup$ @BenBolker: ha, yes, an error message like that would have been helpful! $\endgroup$
    – Sol
    Commented Oct 19, 2013 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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