用R表示线性混合模型中的拟合线

时间:2017-05-23 11:39:10

标签: r mixed-models

我一直在努力解决这个问题。我是线性混合模型的新手,所以我想这解释了我的失败。 我已经快速创建了一些数据,仅用于说明目的:

    #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

这是我的模特:

 ##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

结果:

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

显然模型可能看起来很奇怪,因为我没有花太多时间来重现原始数据集,我在这里无法分享。 我想要做的只是简单地显示RT的模型拟合线作为堆栈的函数,在两个不同的条件下相似性==&#34;不相似&#34;和相似度==&#34;相似&#34;。这可能是因为我对模型理论缺乏了解而受到阻碍,但这样做应该非常简单,或者我错过了什么? 关于如何在ggplot中做到这一点的任何建议? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

一些想法。首先,尝试sjPlot包。它包含一个函数sjp.lmer,它可以为线性混合模型生成许多不同的摘要。例如,要按相似性绘制RT与堆栈的关系,您可以使用:

library(sjPlot)
sjp.lmer(model, type = "pred", vars = c("stack", "similarity"))

enter image description here

我还会安装broom包。它提供augment函数,它将从您的模型中生成整洁的数据框:

model %>% augment()

然后您可以将数据框传输到ggplot以实现您想要的效果;例如,拟合值与堆栈的简单散点图,按相似性:

model %>% augment() %>% 
  ggplot(aes(stack, RT)) + geom_point() + facet_grid(similarity ~ .)

enter image description here