plot fitted line in linear mixed model with R

Viewed 2426

I have been trying to solve this for 1 day. I'm new to linear mixed models, so I guess this explains my failure. I have quickly created some data, for the only purpose of illustration:

    #Data
df <- data.frame(
  subject=rep(c("1","2","3","4","5","6"),each=100),
  order=rep(1:20),
  similarity = rep(c("Similar", "Dissimilar"), each=150,times=2),
  relate = rep(c("related", "unrelated"), each=75,times=4),
  stack = as.numeric(rep(c("112","155","76","88","90","122","145","102","159","233")), each=60), 
  target= rep(c("banana","apple","peach","pineapple","coconut","cherry"),times=10)
)

# add RT data
df$RT <- 0.02*df$order +                   
  -6*as.numeric(df$similarity=="Similar")* as.numeric(df$stack) +
  6*as.numeric(df$similarity=="Dissimilar")* as.numeric(df$stack) +
  4*as.numeric(df$stack)*as.numeric(df$relate=="unrelated") +
  -11*as.numeric(df$target=="banana")*as.numeric(df$order>1 & df$order<6)+
  df$stack/10*rnorm(600, mean=0, sd=2)  

df$RT<--1*df$RT

Here is my model:

 ##model
model=lmer(RT~similarity*relate*stack  
                  +order + (1|subject) 
                  + (1|target),data=df,REML=F,control=lmerControl(optimizer = c("bobyqa")))

df$fit<-predict(model) ##add fitted values

Results:

Linear mixed model fit by maximum likelihood t-tests use Satterthwaite approximations to degrees of freedom [
lmerMod]
Formula: RT ~ similarity * relate * stack + order + (1 | subject) + (1 |      target)
   Data: df
Control: lmerControl(optimizer = c("bobyqa"))

     AIC      BIC   logLik deviance df.resid 
  5668.6   5721.3  -2822.3   5644.6      588 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.5247 -0.6163  0.0226  0.5944  4.0280 

Random effects:
 Groups   Name        Variance Std.Dev.
 subject  (Intercept)   0.0     0.00   
 target   (Intercept)   0.0     0.00   
 Residual             713.2    26.71   
Number of obs: 600, groups:  subject, 6; target, 6

Fixed effects:
                                         Estimate Std. Error        df  t value Pr(>|t|)    
(Intercept)                              -7.46457    6.74238 600.00000   -1.107    0.269    
similaritySimilar                        -0.86579    9.41010 600.00000   -0.092    0.927    
relateunrelated                          13.96619    9.43009 600.00000    1.481    0.139    
stack                                    -5.92555    0.05030 600.00000 -117.802   <2e-16 ***
order                                    -0.06343    0.19765 600.00000   -0.321    0.748    
similaritySimilar:relateunrelated        -8.96977   13.33903 600.00000   -0.672    0.502    
similaritySimilar:stack                  12.00979    0.07024 600.00000  170.974   <2e-16 ***
relateunrelated:stack                    -4.12125    0.06952 600.00000  -59.283   <2e-16 ***
similaritySimilar:relateunrelated:stack   0.08997    0.09835 600.00000    0.915    0.361    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
             (Intr) smlrtS rltnrl stack  order  smlrtySmlr:r smlrtySmlr:s rltnr:
simlrtySmlr  -0.696                                                             
relatenrltd  -0.692  0.499                                                      
stack        -0.895  0.661  0.662                                               
order        -0.162 -0.010 -0.026 -0.160                                        
smlrtySmlr:r  0.487 -0.706 -0.707 -0.470  0.033                                 
smlrtySmlr:s  0.655 -0.945 -0.472 -0.702  0.025  0.667                          
rltnrltd:st   0.662 -0.477 -0.945 -0.709  0.022  0.668        0.505             
smlrtySml::  -0.465  0.675  0.668  0.504 -0.035 -0.945       -0.715       -0.707

Obviously the model might look weird, as I didn't spend too much time trying to reproduce the original dataset, which I cannot share here. What I wanted to do is simply showing the model fitted line for RTs as a function of stack, in the two different conditions similarity=="Dissimilar" and similarity =="Similar". This is probably hindered by my lack of understanding of the theory of the model, but should be pretty straightforward to do so, or I'm missing something? Any advice on how to do that in ggplot? Thanks in advance.

1 Answers
Related