<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models/"></div></div><div><a data-url="http://www.r-bloggers.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models/" data-counturl="http://www.r-bloggers.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models/" data-count="horizontal" data-text="R for Publication by Page Piccinini: Lesson 6, Part 2 – Linear Mixed Effects Models" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models-lmem/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In today’s lesson we’ll continue to learn about linear mixed effects models (LMEM), which give us the power to account for multiple types of effects in a single model. This is Part 2 of a two part lesson. I’ll be taking for granted that you’ve completed <a target="_blank" rel="nofollow" href="https://pagepiccinini.com/r-course/lesson-6-part-1-linear-mixed-effects-models/">Lesson 6, Part 1</a>, so if you haven’t done that yet be sure to go back and do it.</p>
<p>By the end of this lesson you will:</p>
<blockquote><ul>
<li>Learn about other methods for LMEMs.</li>
<li>Update our LMEMs in R.</li>
<li>Summarise the results in an R Markdown document.</li>
</ul>
</blockquote>
<p>We’ll be working off of the same directory as in Part 1, just adding new scripts. There is a video in end of this post which provides the background on the additional math of LMEM and reintroduces the data set we’ll be using today. There is also some extra explanation of some of the new code we’ll be writing. For all of the coding please see the text below. A PDF of the slides can be downloaded <a target="_blank" href="http://datascienceplus.com/wp-content/uploads/2016/05/rcourse_lesson6_pt2.pdf">here</a>. All of the data and completed code for the lesson can be found <a target="_blank" rel="nofollow" href="https://github.com/pagepiccinini/rcourse_lesson6/tree/master">here</a>.</p>
<h2>Lab Problem</h2>
<p>Unlike past lessons where our data came from some outside source, today we’ll be looking at data collected from people who actually took this course. Participants did an online version of the Stroop task. In the experiment, participants are presented color words (e.g. “red”, “yellow”) where the color of the ink either matches the word (e.g. “red” written in red ink) or doesn’t match the word (e.g. “red” written in yellow ink). Participants have to press a key to say what the color of the ink is, NOT what the text of the word is. Participants are generally slower and less accurate when the there is a mismatch (incongruent trial) than when there is a match (congruent trial). Furthermore, we’re going to see how this effect may change throughout the course of the experiment. Our research questions are below.</p>
<blockquote><ul>
<li>Congruency: Are responses to incongruent trials less accurate and slower than to congruent trials?</li>
<li>Experiment half: Are responses more accurate and faster in the second half of the experiment than the first half of the experiment?</li>
<li>Congruency x Experiment half: Is there an interaction between these variables?</li>
</ul>
</blockquote>
<h2>Setting up Your Work Space</h2>
<p>You should already have your workspace setup from <a target="_blank" href="http://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-6-part-1-linear-mixed-effects-models-lmem/">Lesson 6, Part 1</a>. Go back into the project from the first part of the lesson. Before beginning the new script go to the “Git” icon and click the “Pull Branches” button. An example screen shot pointing to the button is provided below.</p>
<p><a title="" data-rel="lightbox-0" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1-490x313.png?resize=456%2C291 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1-768x491.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1-1024x654.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1-600x383.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1-300x192.png 300w" alt="lesson6-part2-image1" class="wp-image-4252" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image1-490x313.png?resize=456%2C291" data-recalc-dims="1"></a></p>
<p>You should get a message saying “Already up-to-date.” (see below for an example). This button pulls down any updates to the repository that are not currently on your computer. Now, since you’re the only one pushing to this repository there shouldn’t have been any updates, thus “Already up-to-date”. However, if you’re working on a project with other people and they all have access to the Bitbucket repository, they may have committed changes in between the times you worked on it, and you want to be sure to pull down those changes before continuing any work. Even though that’s not the case with a repository like this where you’re the only person with access, it’s good to get into the habit of pulling every time you come back to a project for any future projects that you collaborate on with other people.</p>
<p><a title="" data-rel="lightbox-1" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2-490x313.png?resize=456%2C291 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2-768x491.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2-1024x654.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2-600x383.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2-300x192.png 300w" alt="lesson6-part2-image2" class="wp-image-4253" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image2-490x313.png?resize=456%2C291" data-recalc-dims="1"></a></p>
<p>Now that you’re all up to date we can start scripting again.</p>
<h2>Statistics Script (Part 2)</h2>
<p>Open a new script and on the first few lines write the following, same as for first statistics script. Note, the results of the models reported in this lesson are based on version 1.1.12 of lme4.</p>
<pre>## READ IN DATA ####
source(&quot;scripts/rcourse_lesson6_cleaning.R&quot;)

## LOAD PACKAGES ####
library(lme4)</pre>
<p>We’ll also make our usual header for organizing our data. As for the first statistics script, let’s start with the accuracy data. Unlike in our first script though, now we’re going to add a couple new columns to make our contrast coded variables. Basically for each variable I’m setting one level to “-0.5” and one to “0.5”, so now “0” is directly in between the two levels, but the difference is still “1”. For more details on the code watch the video. In other tutorials you may see that people use the <code>contrasts()</code> call to change data to contrast coding. This also works fine and will produce the same results, I just prefer this method, but do whatever you feel most comfortable with. When you’re ready copy and run the code below.</p>
<pre>## ORGANIZE DATA ####
# Accuracy data
data_accuracy_stats = data_accuracy_clean %>%
                      mutate(congruency_contrast = ifelse(congruency == &quot;con&quot;, -0.5, 0.5)) %>%
                      mutate(half_contrast = ifelse(half == &quot;first&quot;, -0.5, 0.5))</pre>
<p>We can also double check that our contrast coding worked appropriately with an <code>xtabs()</code> call. Below are two <code>xtabs()</code> calls with our original columns and our new contrast coded columns.</p>
<pre>xtabs(~congruency+congruency_contrast, data_accuracy_stats)
xtabs(~half+half_contrast, data_accuracy_stats)</pre>
<p>Below are the outputs of the two calls. As expected for the variable “congruency”, “con” is to “-0.5” and “incon” to “0.5”, and for the variable “half”, “first” is set to “-0.5” and “second” to “0.5”. In the script on GitHub I’ve also included some more calls to double check the random effects structure, but we won’t go over them here since it is the same as in the first statistics script, just changing to our new contrast coded variables.</p>
<p><a title="" data-rel="lightbox-2" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image3.png"><img sizes="(max-width: 434px) 100vw, 434px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image3.png?resize=434%2C132 434w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image3-300x91.png 300w" alt="lesson6-part2-image3" class="wp-image-4254" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image3.png?resize=434%2C132" data-recalc-dims="1"></a></p>
<p><a title="" data-rel="lightbox-3" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image4.png"><img sizes="(max-width: 316px) 100vw, 316px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image4.png?resize=316%2C136 316w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image4-300x129.png 300w" alt="lesson6-part2-image4" class="wp-image-4255" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image4.png?resize=316%2C136" data-recalc-dims="1"></a></p>
<p>Now that our variables are contrast coded we can build the new model. Again we’ll start with a model with the maximal random effects structure. The model is the same as our model from the previous lesson except the names of the variables are updated to our contrast coded ones for both the fixed effects and as slopes in the random effects. Copy and run the code below.</p>
<pre>## BUILD MODELS FOR ACCURACY ANALYSIS ####
# Full model
accuracy.glmer = glmer(accuracy ~ congruency_contrast * half_contrast +
                                (1+congruency_contrast*half_contrast|subject_id) +
                                (1+half_contrast|item), family = &quot;binomial&quot;,
                                data = data_accuracy_stats)</pre>
<p>Once again the model did not converge with the maximal random effects structure, matching our model from before. Copy and run the code below.</p>
<pre>accuracy.glmer = glmer(accuracy ~ congruency_contrast * half_contrast +
                                  (1|subject_id) +
                                  (0+half_contrast|subject_id) +
                                  (1|item), family = &quot;binomial&quot;,
                                  data = data_accuracy_stats)</pre>
<p>Again we’ll use the <code>summary()</code> call to get more information about the model. Copy and run the code below.</p>
<pre># Summarise model and save
accuracy.glmer_sum = summary(accuracy.glmer)
accuracy.glmer_sum</pre>
<p>Part of the output of the summary call is below. Now that we’ve contrast coded our variables we can interpret the results as we would an ANOVA. The intercept is now the grand mean of the data (in logit space) given our random effects. The effect of congruency tells us that subjects are less accurate on incongruent trials (since incongruent is the positive value and the estimate is negative), but now we can say this across the whole experiment, not just within the first half of the experiment. There continues to be no effect of experiment half and no interaction.</p>
<p><a title="" data-rel="lightbox-4" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5-490x129.png?resize=456%2C120 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5-768x202.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5-1024x269.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5-600x158.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5-300x79.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5.png 1088w" alt="lesson6-part2-image5" class="wp-image-4256" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image5-490x129.png?resize=456%2C120" data-recalc-dims="1"></a></p>
<p>I won’t go over the code for looking at the random effects coefficients, but the code is in the script on GitHub. Instead of trusting the p-values from the z-scores though, we’re going to use model comparison. Next we can test for our first variable, congruency. To do this we’ll build the same model but subtracting “congruency_half”. Copy and run the code below.</p>
<pre># Test for effect of congruency
accuracy_congruency.glmer = glmer(accuracy ~ congruency_contrast * half_contrast -
                                                    congruency_contrast +
                                             (1|subject_id) +
                                             (0+half_contrast|subject_id) +
                                             (1|item), family = &quot;binomial&quot;,
                                             data = data_accuracy_stats)</pre>
<p>With our model built we can make and view the comparison between the original full model and the model without congruency. Copy and run the code below.</p>
<pre>accuracy_congruency.anova = anova(accuracy.glmer, accuracy_congruency.glmer)
accuracy_congruency.anova</pre>
<p>Below is the output of the <code>anova()</code> call (which is really a likelihood ratio test). As you can see the AIC for the reduced model (“accuracy_congruency.glmer”) is larger than for the full model (“accuracy.glmer”) suggesting that including congruency does significantly improve the fit of the model (remember, lower AIC is better). Indeed this is confirmed by the likelihood ratio test that finds a significant difference between the two models (with significance set at p &lt; 0.05). So, as expected there is a significant effect of congruency.</p>
<p><a title="" data-rel="lightbox-5" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/lesson6-part2-image6_UPDATED.png"><img alt="lesson6-part2-image6" class="wp-image-4257" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/lesson6-part2-image6_UPDATED.png?resize=456%2C40" data-recalc-dims="1"></a></p>
<p>Let’s do the same thing now for the variable experiment half. Below is the full code block making the new model and the <code>anova()</code> call to compare to the original full model.</p>
<pre># Test for effect of experiment half
accuracy_half.glmer = glmer(accuracy ~ congruency_contrast * half_contrast -
                                              half_contrast +
                                       (1|subject_id) +
                                       (0+half_contrast|subject_id) +
                                       (1|item), family = &quot;binomial&quot;,
                                       data = data_accuracy_stats)

accuracy_half.anova = anova(accuracy.glmer, accuracy_half.glmer)
accuracy_half.anova</pre>
<p>Below is the output of the model comparison. This time the reduced model (“accuacy_half.glmer”) has a lower AIC than the full model, but this difference is not significant, so including (or excluding) experiment half does not significantly improve the model.</p>
<p><a title="" data-rel="lightbox-6" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/lesson6-part2-image7_UPDATED.png"><img alt="lesson6-part2-image7" class="wp-image-4258" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/lesson6-part2-image7_UPDATED.png?resize=456%2C42" data-recalc-dims="1"></a></p>
<p>Our final model subtracts the interaction of congruency and experiment half. Remember that the <code>*</code> symbol includes both the main effects and the interaction, to only subtract the interaction we use <code>:</code>. Copy and run the code below.</p>
<pre># Test for interaction of congruency x experiment half
accuracy_congruencyxhalf.glmer = glmer(accuracy ~ congruency_contrast * half_contrast -
                                                         congruency_contrast:half_contrast +
                                                  (1|subject_id) +
                                                  (0+half_contrast|subject_id) +
                                                  (1|item), family = &quot;binomial&quot;,
                                                  data = data_accuracy_stats)

accuracy_congruencyxhalf.anova = anova(accuracy.glmer, accuracy_congruencyxhalf.glmer)
accuracy_congruencyxhalf.anova</pre>
<p>Below is the output of the model comparison. Again the reduced model has a slightly lower AIC, but not significantly so. So, the interaction does not significantly affect the model.</p>
<p><a title="" data-rel="lightbox-7" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/lesson6-part2-image8_UPDATED.png"><img alt="lesson6-part2-image8" class="wp-image-4259" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/lesson6-part2-image8_UPDATED.png?resize=456%2C36" data-recalc-dims="1"></a></p>
<p>Overall our results from both the figure and our first LMEM are confirmed. There is an effect of congruency on accuracy, but not experiment half and there is no interaction. Now let’s move on to the reaction time analysis.</p>
<p>Start by going back to the top of the script to the “ORGANIZE DATA” section. Below the <code>xtabs()</code> calls for the accuracy analysis we’re going to make our data frame for the reaction time analysis. We’ll do the same thing we did for the accuracy data by adding two columns contrast coding our independent variables. Copy and run the code below.</p>
<pre># RT data
data_rt_stats = data_rt_clean %>%
                mutate(congruency_contrast = ifelse(congruency == &quot;con&quot;, -0.5, 0.5)) %>%
                mutate(half_contrast = ifelse(half == &quot;first&quot;, -0.5, 0.5))</pre>
<p>As for the accuracy data we can also run a couple <code>xtabs()</code> calls to double check our work. The code is below, but I won’t put the output here. Copy and run the code to double check for yourself though.</p>
<pre>xtabs(~congruency+congruency_contrast, data_rt_stats)
xtabs(~half+half_contrast, data_rt_stats)</pre>
<p>Now we can start building our model. Below is the code for the model with maximal random effects structure. Like our accuracy model all of the references to our fixed effects are changed to our new contrast coded variables. There is also the addition of the call <code>REML = F</code>. This has to do with how the variance is computed and we need to set it to FALSE for when we later do our model comparison to get our p-values. Copy and run the code below.</p>
<pre>## BUILD MODELS FOR REACTION TIME ANALYSIS ####
# Full model
rt_log10.lmer = lmer(rt_log10 ~ congruency_contrast * half_contrast +
                                (1+congruency_contrast*half_contrast|subject_id) +
                                (1+half_contrast|item), REML = F,
                                data = data_rt_stats)</pre>
<p>Unlike in the dummy coded model, the model does not converge. However, all we have to do is uncorrelate the random intercept and random slope for “item”. Copy and run the updated code below.</p>
<pre>rt_log10.lmer = lmer(rt_log10 ~ congruency_contrast * half_contrast +
                                (1+congruency_contrast*half_contrast|subject_id) +
                                (1|item) +
                                (0+experiment_half|item), REML = F,
                                data = data_rt_stats)</pre>
<p>Let’s get the summary of the model and look at it. Copy and run the code below.</p>
<pre>rt_log10.lmer_sum = summary(rt_log10.lmer)
rt_log10.lmer_sum</pre>
<p>Below is part of the summary output. Remember, we don’t get p-values with <code>lmer()</code> but we can get initial impressions based on the t-values (absolute values greater than 2 likely significant at p &lt; 0.05). Based on these t-values we appear to have an effect of congruency and experiment half, but now we can confidently say this applies to the data as a whole, not just given specific baselines.</p>
<p><a title="" data-rel="lightbox-8" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9-490x112.png?resize=456%2C104 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9-768x175.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9-600x137.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9-300x68.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9.png 904w" alt="lesson6-part2-image9" class="wp-image-4260" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image9-490x112.png?resize=456%2C104" data-recalc-dims="1"></a></p>
<p>To test for the effect of congruency, copy and run the code below.</p>
<pre># Test for effect of congruency
rt_log10_congruency.lmer = lmer(rt_log10 ~ congruency_contrast * half_contrast -
                                       congruency_contrast +
                                (1+congruency_contrast*half_contrast|subject_id) +
                                (1|item) +
                                (0+experiment_half|item), REML = F,
                                data = data_rt_stats)

rt_log10_congruency.anova = anova(rt_log10.lmer, rt_log10_congruency.lmer) rt_log10_congruency.anova</pre>
<p>The reduced model has a larger AIC then the full model (yes, 1143.3 is smaller than 1164.0 but they are negative, and -1149.2 is larger than -1169.8). We have this difference statistically confirmed by the likelihood ratio test, so indeed having congruency significantly improves the model.</p>
<p><a title="" data-rel="lightbox-9" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10-490x40.png?resize=456%2C37 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10-768x62.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10-1024x83.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10-600x48.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10-300x24.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10.png 1262w" alt="lesson6-part2-image10" class="wp-image-4262" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image10-490x40.png?resize=456%2C37" data-recalc-dims="1"></a></p>
<p>Next we can test for the effect of experiment half. Copy and run the code below.</p>
<pre># Test for effect of experiment half
rt_log10_half.lmer = lmer(rt_log10 ~ congruency_contrast * half_contrast -
                                       half_contrast +
                                (1+congruency_contrast*half_contrast|subject_id) +
                                (1|item) +
                                (0+experiment_half|item), REML = F,
                                data = data_rt_stats)

rt_log10_half.anova = anova(rt_log10.lmer, rt_log10_half.lmer)
rt_log10_half.anova</pre>
<p>Below is the output of the model comparison. We again get a larger (less negative) AIC for the reduced model. This difference is confirmed to be significant. So, as expected adding experiment half improves the model.</p>
<p><a title="" data-rel="lightbox-10" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11-490x46.png?resize=456%2C43 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11-768x71.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11-1024x95.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11-600x56.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11-300x28.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11.png 1184w" alt="lesson6-part2-image11" class="wp-image-4263" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image11-490x46.png?resize=456%2C43" data-recalc-dims="1"></a></p>
<p>Finally, below is the code to test the interaction. Copy and run the code below.</p>
<pre># Test for interaction of congruency and experiment half
rt_log10_congruencyxhalf.lmer = lmer(rt_log10 ~ congruency_contrast * half_contrast -
                                       congruency_contrast:half_contrast +
                                (1+congruency_contrast*half_contrast|subject_id) +
                                (1|item) +
                                (0+experiment_half|item), REML = F,
                                data = data_rt_stats)

rt_log10_congruencyxhalf.anova = anova(rt_log10.lmer, rt_log10_congruencyxhalf.lmer)
rt_log10_congruencyxhalf.anova</pre>
<p>Below is the model comparison output. As expected, the interaction does not appear to have any effect on the model.</p>
<p><a title="" data-rel="lightbox-11" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12-490x39.png?resize=456%2C36 490w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12-768x62.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12-1024x82.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12-600x48.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12-300x24.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12.png 1296w" alt="lesson6-part2-image12" class="wp-image-4264" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/lesson6-part2-image12-490x39.png?resize=456%2C36" data-recalc-dims="1"></a></p>
<p>Overall our initial expectations are confirmed. Subjects are slower on incongruent trials and faster in the second half of the experiment, however there is no interaction of these variables. Across all of the data we get congruency effects for both accuracy and reaction times but experiment half only matters for reaction times, not accuracy.</p>
<p>One quick word to the wise for using this method. I have found random effects structures that converged for the full model but then wouldn’t converge for some of the reduced models. Following the edict that we can’t trust models that don’t converge I then further reduced my original model until all models (the full one and all of the reduced models) converged. This can take a while and be frustrating but it’s better to be sure that all of your models are converging.</p>
<p>You’ve now successfully gotten rid of the baseline issue and used a more robust method for significance testing! Save your script in the “scripts” folder and use a name ending in “_pt2_statistics”, for example mine is called “rcourse_lesson6_pt2_statistics”. Once the file is saved commit the change to Git. My commit message will be “Made part 2 statistics script.”. Finally, push the commit up to Bitbucket.</p>
<h2>Write-up</h2>
<p>Let’s make our write-up to summarise what we did today. First save your current working environment to a file such as “rcourse_lesson6_environment” to your “write_up” folder. If you forgot how to do this go back to <a target="_blank" href="http://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-1-r-basics/">Lesson 1</a>. Open a new R Markdown document and follow the steps to get a new script. As before delete everything below the chuck of script enclosed in the two sets of <code>---</code>. Then on the first line use the following code to load our environment.</p>
<pre>```{r, echo=FALSE}
load(&quot;rcourse_lesson6_environment.RData&quot;)
```</pre>
<p>Let’s make our sections for the write-up. I’m going to have four: 1) Introduction, 2) Methods, 3) Results, and 4) Conclusion. I’ll also have a few subsections. See below for structure.</p>
<pre># Introduction


# Methods

## Participants

## Materials

## Procedure


# Results

## Accuracy

## Reaction Times


# Conclusion</pre>
<p>In each of my sections I can write a little bit for any future readers. For example below is my Introduction. You’ll notice that I include the link to Wikipedia article on the Stroop effect.</p>
<pre># Introduction

The [Stroop effect](https://en.wikipedia.org/wiki/Stroop_effect) is a well known example in psychology of when it is difficult to ignore conflicting information. My data comes from participants in an R course who conducted the experiment online.</pre>
<p>In my Methods section you’ll see I use a lot of inline R code. This is useful if you add more data later and want to update your report without having to manually recompute information. For example, in the Participants section I specify the number of native and non-native French speakers simply using R code. See below for the example.</p>
<pre>## Participants

Participants were `r length(unique(data_subjects$subject_id))` French speakers, some native (`r length(unique(subset(data_subjects, native_french == &quot;yes&quot;)$subject_id))`) and some non-native (`r length(unique(subset(data_subjects, native_french == &quot;no&quot;)$subject_id))`) French speakers. There were `r length(unique(subset(data_subjects, sex == &quot;female&quot;)$subject_id))` females and `r length(unique(subset(data_subjects, sex == &quot;male&quot;)$subject_id))` males. The average age of all participants was `r round(mean(data_subjects$age), 2)` years old.</pre>
<p>Moving to the Results section, I again use a lot of inline R code to present the results of the analysis. Below is an example from the accuracy analysis.</p>
<pre>The model found a significant effect of congruency, such that there was lower accuracy on incongruent trials compared to congruent trial [$\beta$ = `r round(coef(accuracy.glmer_sum)[2,1], 2)`, *SE* = `r round(coef(accuracy.glmer_sum)[2,2], 2)`, $\chi^2$(1) = `r round(accuracy_congruency.anova$Chisq[2], 2)`, *p*  $&lt;$ 0.05]. There was no effect of experiment half and no significant interaction of congruency and experiment half.</pre>
<p>Go ahead and fill out the rest of the document to include the full results and a conclusion, you can also look at the full version of my write-up with the link provided at the top of the lesson. When you are ready, save the script to your “write_up” folder (for example, my file is called “rcourse_lesson6_writeup”) and compile the HTML or PDF file. Once your write-up is made, commit the changes to Git. My commit message will be “Made write-up.”. Finally, push the commit up to Bitbucket. If you are done with the lesson you can go to your Projects menu and click “Close Project”.</p>
<p>Congrats! You can now do an LMEM with contrast coding and get p-values with model comparison in R!</p>
<h2>Conclusion and Next Steps (Beyond this Course)</h2>
<p>Today you continued learning about LMEMs by using contrast coding and model comparison, thus taking care of our baseline issues in a model with an interaction without having to run an ANOVA. You also extended your use of inline R code in an R Markdown document. This lesson marks the end of the course R for Publication. I hope you found the course useful and will be able to use what you learned here in future R programming!</p>
<ul>
<li><a target="_blank" href="https://www.youtube.com/watch?v=YNSV5RQiqfQ">Lesson 6, Part 2: Linear Mixed Effects Models</a></li>
</ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toR for Publication by Page Piccinini: Lesson 6, Part 1 – Linear Mixed Effects Models" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-6-part-1-linear-mixed-effects-models-lmem/">R for Publication by Page Piccinini: Lesson 6, Part 1 – Linear Mixed Effects Models</a></li>
<li><a title="Permanent Link toCross-Validation: Estimating Prediction Error" target="_blank" rel="bookmark" href="http://datascienceplus.com/cross-validation-estimating-prediction-error/">Cross-Validation: Estimating Prediction Error</a></li>
<li><a title="Permanent Link toInteractive Performance Evaluation of Binary Classifiers" target="_blank" rel="bookmark" href="http://datascienceplus.com/interactive-performance-evaluation-of-binary-classifiers/">Interactive Performance Evaluation of Binary Classifiers</a></li>
<li><a title="Permanent Link toPredicting wine quality using Random Forests" target="_blank" rel="bookmark" href="http://datascienceplus.com/predicting-wine-quality-using-random-forests/">Predicting wine quality using Random Forests</a></li>
<li><a title="Permanent Link toBayesian regression with STAN Part 2: Beyond normality" target="_blank" rel="bookmark" href="http://datascienceplus.com/bayesian-regression-with-stan-beyond-normality/">Bayesian regression with STAN Part 2: Beyond normality</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=qOQ85ysUueQ:52jr7aWHapY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=qOQ85ysUueQ:52jr7aWHapY:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=qOQ85ysUueQ:52jr7aWHapY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=qOQ85ysUueQ:52jr7aWHapY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models/"></div></div><div><a data-url="http://www.r-bloggers.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models/" data-counturl="http://www.r-bloggers.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models/" data-count="vertical" data-text="R for Publication by Page Piccinini: Lesson 6, Part 2 – Linear Mixed Effects Models" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/r-for-publication-by-page-piccinini-lesson-6-part-2-linear-mixed-effects-models-lmem/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/TPUgva1tkyY"></a></div>