R Markdown

This file contains an R code for creating a plot of your raw data + estimate of the effect as predicted by the model.

library(ggplot2)
library(effects)
library (lme4)
## Loading required package: Matrix
library (plyr)

load(file="d_test.RData")

model<- lmer(variable ~                        #run your mixed effects regression model
            target_effect*condition+ 
            (1+target_effect|random_effect),
             data=d_test, REML=F)
summary(model)
## Linear mixed model fit by maximum likelihood  ['lmerMod']
## Formula: 
## variable ~ target_effect * condition + (1 + target_effect | random_effect)
##    Data: d_test
## 
##      AIC      BIC   logLik deviance df.resid 
##   -280.7   -252.6    148.3   -296.7      240 
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.41259 -0.63572  0.01945  0.66080  2.06649 
## 
## Random effects:
##  Groups        Name          Variance  Std.Dev. Corr 
##  random_effect (Intercept)   2.678e-03 0.051749      
##                target_effect 3.051e-05 0.005523 -0.48
##  Residual                    1.626e-02 0.127501      
## Number of obs: 248, groups:  random_effect, 23
## 
## Fixed effects:
##                           Estimate Std. Error t value
## (Intercept)               0.756361   0.034709  21.791
## target_effect            -0.058751   0.005723 -10.266
## condition2               -0.195569   0.043770  -4.468
## target_effect:condition2  0.042088   0.007605   5.534
## 
## Correlation of Fixed Effects:
##             (Intr) trgt_f cndtn2
## target_ffct -0.899              
## condition2  -0.631  0.617       
## trgt_ffct:2  0.586 -0.664 -0.929
d1 <- effect("target_effect:condition", model)   # save the estimates for your target effect
d1 <-as.data.frame(d1)

d2 <- ddply(d_test, .(target_effect,condition), summarise, mean_variable = mean(variable,na.rm=TRUE))
  
  
 ggplot()+
  geom_line(data=d2,aes(x = target_effect, y = mean_variable, color=condition),lwd=1.4)+
  geom_point(data=d2,aes(x = target_effect, y = mean_variable, color=condition),size=4)+
  geom_line(data=d1,aes(target_effect, fit, color=condition),lwd=1, linetype="dotted")+
  geom_point(data=d1,aes(target_effect, fit, color=condition),size=2)+
  geom_errorbar(data=d1,aes(x=target_effect, ymin=lower, ymax=upper, color=condition))+
  scale_x_continuous("Target Effect", breaks = c(1:8))+
  scale_y_continuous("Mean Variable")+
  ggtitle("Variable by Target Effect and Condition")+
  theme_bw()+
  theme(text = element_text(size = 14), axis.text.x = element_text(size=14), legend.text = element_text(size = 14))