3

We have the following model:

Y ~ X*Condition + (X*Condition|subject)
  • Y = dichotomous variable; values 0,1
  • X = continous variable; values ca. 0-3000
  • Condition = dichotomous variable; values A,B
  • subject = grouping variable, 219 subjects

The experiment we conducted is a two-choice task where people have to choose the correct answer. There are two conditions in the task (condition A and B), one of which makes the task harder. The design of experiment is repeated - all participants have values for x and y in both Condition A and Condition B.

Now, I want to get the correlation of random slopes (X) between Condition A and Condition B. In a attr(VarCorr(model),"correlation") of the above postulated model, I get this:

                 (Intercept)       X      ConditionB    X:ConditionB
(Intercept)        1.0000000 -0.6398171 -0.56397675       0.57414877
X                 -0.6398171  1.0000000 -0.11525897      -0.98347634
ConditionB        -0.5639767 -0.1152590  1.00000000       0.08207216
X:ConditionB       0.5741488 -0.9834763  0.08207216       1.00000000

It would seem that the correlation I look for is -0.98347634. However, that is not the case. I am looking for correlation between the random slope for reference Condition A (which equals coef(model)$subject$X), and random slope for Condition B (which equals coef(model)$subject$X + coef(model)$subject$X:ConditionB).

Instead, I get the correlation (-.983476) of coef(model)$subject$X and coef(model)$subject$X:ConditionB.

Can someone point me in the right direction?

User33268
  • 1,408
  • 2
  • 10
  • 21
  • 2
    Let's say you have random variables $A$ and $B$ and you know variance of $A$, variance of $B$, and correlation between $A$ and $B$. You want to compute correlation between $A$ and $A+B$. Is that the correct description of your problem? – amoeba Jan 10 '18 at 09:27
  • Yes, that's true. I know that I could always extract random values by `coef(model)$subject`, do the manual addition and then calculate the correlation with `cor()` but I also know it is not advisable to work with concrete values of latent variables for a reason that is, to be hones, not entirely clear to me (in factor analysis it is about factor indeterminacy, but here, I am not sure). P.S. If I don't "need" the correlation between X and X:ConditionB, is it at all justified to estimate it, or is it better to set it to 0? – User33268 Jan 10 '18 at 09:56
  • 2
    `it is not advisable to work with concrete values of latent variables` - but this is not what I meant. My (implicit) suggestion was to work out corr(A+B, A) directly from var(A), var(B), and cov(A, B). Hint: cov(A+B,A) = cov(A,A)+cov(A,B) = var(A) + cov(A,B). – amoeba Jan 10 '18 at 10:06
  • Indeed, you are right! We get into so complex things that sometimes we forget the basics. P.S. Warn me if this is OT, but I have to ask this as it is a relevant question: Is there any case where extracting values and working with them would be justified? – User33268 Jan 10 '18 at 10:11
  • I am not an expert. I would say it can be fine, but if there is a way to avoid it (as in this case) then I'd prefer the "direct" method. – amoeba Jan 10 '18 at 10:21
  • Thank you! Do you want to post an answer with covariance calculation? Or should I do it? – User33268 Jan 10 '18 at 12:12
  • If you have it worked out, go ahead. Note that you will need the covariance matrix of random effects, not only the correlation matrix (in the Q). – amoeba Jan 10 '18 at 12:13

1 Answers1

3

Thanks to @amoeba the answer is fairly basic. Although lme4 doesn't compute this correlation directly, we can calculate it easily. The problem can be broken down to calculation of the correlation between $x$ (X) and $x+y$ (X+X:ConditionB).

$$cor(x,x+y) = \frac{Cov(x,x+y)}{SD(x)SD(x+y)} = \frac{Cov(x,x)+Cov(x,y)}{SD(x)\sqrt{Var(x+y)}}=\frac{Var(x)+Cov(x,y)}{SD(x)\sqrt{Var(x)+Var(y)+2Cov(x,y)}}$$

All of these elements are known to us from VarCorr(model)$subject.

                 (Intercept)       X     ConditionB       X:ConditionB
(Intercept)       0.34132791 -0.10782538 -0.12072986       0.06516589
X                -0.10782538  0.08320674 -0.01218208      -0.05511289
ConditionB       -0.12072986 -0.01218208  0.13425646       0.00584216
X:ConditionB      0.06516589 -0.05511289  0.00584216       0.03774157
attr(,"stddev")
       (Intercept)       X               ConditionB       X:ConditionB
       0.5842328        0.2884558        0.3664102        0.1942719 
attr(,"correlation")
                 (Intercept)       X     ConditionB       X:ConditionB
(Intercept)        1.0000000 -0.6398171 -0.56397675       0.57414877
X                 -0.6398171  1.0000000 -0.11525897      -0.98347634
ConditionB        -0.5639767 -0.1152590  1.00000000       0.08207216
X:ConditionB       0.5741488 -0.9834763  0.08207216       1.00000000

Concretely, in the example above the solution would be (rounded):

$$\frac{0.0832-0.0551}{0.2884*\sqrt{0.0832+0.0377+2*(-0.0551)}} = 0.9419$$

Edit 1 - comparison with extracted random effects If we try to manually calculate the correlation by extracting random effects we get the following: coef(model)$subject %>% mutate(XB = X + X:ConditionB) %>% cor()

                     (Intercept)       X    ConditionB        X:ConditionB     XB
    (Intercept)        1.0000000 -0.8592069 -0.58460738       0.84387641 -0.8749694
   X                  -0.8592069  1.0000000  0.10740837      -0.99765252  0.9943712
    ConditionB        -0.5846074  0.1074084  1.00000000      -0.09795365  0.1210422
    X:ConditionB       0.8438764 -0.9976525 -0.09795365       1.00000000 -0.9847813
    XB                -0.8749694  0.9943712  0.12104216      -0.98478132  1.0000000

The results are actually not that different - cor(X,XB) is 0.99.

User33268
  • 1,408
  • 2
  • 10
  • 21
  • +1. Out of curiosity, what do you get if you use the values of the subject-level slopes from `coef(model)$subject`? – amoeba Jan 10 '18 at 13:58
  • Nice. Actually 0.99 looks to me like a pretty huge overestimation of 0.94. I mean, the absolute difference is small, but it's 6 times closer to 1. – amoeba Jan 10 '18 at 14:21