0
$\begingroup$

I have a simple data set and would like to get case estimates of slopes for particular conditions. Here is an example data set:

    scores <- c(12, 15, 64, 23, 54, 88, 12, 13, 12, 13, 14, 15, 
                12, 15, 16, 14, 16, 17, 11, 11, 22, 11, 22, 22,
                15, 18, 33, 25, 45, 54, 33, 31, 21, 23, 24, 26, 
                12, 16, 55, 66, 67, 88, 21, 20, 21, 15, 21, 17, 
                23, 27, 47, 57, 64, 59, 9, 23, 21, 12, 13, 14, 
                18, 17, 13, 18, 19, 21, 14, 14, 17, 21, 12, 13)
    dat <- data.frame(Subject=rep(c(1,2,3,4,5,6), each=6), 
           Condition=rep(c("A","B"),each=6*6), Score=scores, 
           Day=rep(c(1,2,3,4,5,6),times=6))

Let's say I'm interested in extracting the slopes for Day separately for condition A and condition B. I expect learning to vary across subjects so I would like to get subject-specific estimates so that I can explore differences between subjects later on.

I tried this:

    fm <- lmer(Score ~ Day*Condition + (Day:Condition|Subject), 
                    dat)
    coef(fm)$Subject

      Day:ConditionA (Intercept)      Day ConditionB Day:ConditionB
    1       9.172407   -3.349619 3.771429   2.555556      10.997753
    2      -4.424986   17.455423 3.771429   2.555556      -4.025960
    3      -2.507177   11.121670 3.771429   2.555556       5.111748
    4      -3.081593   16.352104 3.771429   2.555556      -4.507647
    5       2.609281    9.564107 3.771429   2.555556      -2.182904
    6      -1.767931   14.989648 3.771429   2.555556      -4.392

I can see I get different coefficients for each Subject. But would this be the correct way to extract subject-specific estimates of learning? For example, is the value 9.171407 just under Day:ConditionA the slope value for ConditionA across days for Subject 1?

$\endgroup$
1
$\begingroup$

The broom (broom.mixed) package allows you to automatically compute the subject-specific predictions with the augment function (fm_augmented <- broom.mixed::augment(fm).)

Edit in function of your comment:

For me, it is helpful to manually calculate some fitted values based on the coefficients to get proper understanding. It is important to bear in mind that all these coefficients are relative to the reference category (in this case, Day 1, Condition A).

If you call fixef(fm), you get:

(Intercept) Day ConditionB Day:ConditionB

11.0222222 3.7714286 2.5555556 0.1666667

ranef(fm) gets you:

(Intercept) Day:ConditionA Day:ConditionB

1 -14.37386101 9.172822 10.831499

2 6.43403597 -4.425144 -4.192791

...

From an augmented(fm) (augmented_fm <- broom.mixed::augment(fm)) you can get the predicted values for each cell in your data. Some worked examples:

  1. S1, day1, A: 9,59

= 11.02 (global intercept) + 0 (condition A = reference cat) + 3,77x1 (Day coef + 0) -14,37 (Subject intercept) + 9.1728 x1 (additional subject slope for condition A)

  1. S1, day1, B: 13,87

= 11.02 (global intercept) + 2.56 (condition B) + 3.93 x 1 (Day coef + additional coef for condition B) -14,37 (Subject intercept) + 10.83 x1 (additional subject slope for condition B)

  1. S2, day2, A: 16,14 = 11.02 + 0 + 3.77x2 + 6.43 -4.43x2

  2. S2, day2, B: 19,69 = 11.02 + 2.55 + 3.93*2 + 6.43 -4.19x2

Conclusion: None of these coefficients by itself corresponds to your specific slopes. Getting the overall slope for a particular subject in condition A involves taking the overall slope associated with Day + the Subject-specific slope for condition A (for subject 1: 3,77 + 9.1728).

For your non-reference category, things are slightly more complicated, as you additionally need to add the general condition B slope increment (e.g., for subject 1: 3.77 + 0.1667 + 10.83).

Hope this helps!

$\endgroup$
3
  • $\begingroup$ Thanks @KrisBae, but I don't think augment(fm) is giving me what I asked in my question. I'd like to extract subject-specific slopes that correspond to Day:ConditionA and Day:Condition B, not having a prediction for each day and condition. I have updated my question. $\endgroup$
    – locus
    Jul 21 '21 at 14:24
  • $\begingroup$ Thank you @KrisBae, yes that is very helpful and useful to know! So you would go through every Subject and compute the Subject-specific slopes manually like you did in the Conclusion. But I wonder if there isn't a package that can extract those values automatically, maybe emmeans? $\endgroup$
    – locus
    Jul 22 '21 at 10:11
  • $\begingroup$ You're welcome! No package needed; in the end, its the simple summation of the right columns. i.e., let A <- fixef(fm) and B <- ranef(fm), then you can get them like this: Slope_A <- A[2] + B$Subject[2] - Slope_B <- A[2] + A[4] + B$Subject[3]. $\endgroup$
    – KrisBae
    Jul 22 '21 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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