Jekyll2022-03-23T08:46:23-07:00https://www.hzaharchuk.com/feed.xmlHolly A. ZaharchukPhD CandidateHolly A. Zaharchukhzaharchuk@psu.eduReproducible scientific manuscripts with R Markdown2021-04-01T00:00:00-07:002021-04-01T00:00:00-07:00https://www.hzaharchuk.com/posts/2021/04/blog-post-2<p>An <a href="https://github.com/hollzzar/manuscript-template">R Markdown template</a> for creating reproducible scientific manuscripts in Microsoft Word</p>
<h1 id="reproducible-scientific-manuscripts">Reproducible scientific manuscripts</h1>
<h2 id="r-markdown-and-manuscripts">R Markdown and manuscripts</h2>
<p>Of the many great resources for creating reproducible documents in R Markdown, two packages stand out: <a href="https://github.com/rstudio/bookdown"><code class="language-plaintext highlighter-rouge">bookdown</code></a> and <a href="https://github.com/crsh/papaja"><code class="language-plaintext highlighter-rouge">papaja</code></a>.
<code class="language-plaintext highlighter-rouge">bookdown</code> is a powerful tool for creating not only books, but also other documents that cross-reference tables, figures, and sections/chapters.
<code class="language-plaintext highlighter-rouge">papaja</code> is specifically designed for APA-formatted documents and includes functions for creating tables and figures.</p>
<p>Although many researchers rely on Word-based work flows, I found the existing resources for combining R Markdown and Microsoft Word to be limited.
The documentation for <code class="language-plaintext highlighter-rouge">bookdown</code> is clear and thorough, but privileges the more flexible and easily customized HTML and PDF outputs over Word outputs.
<code class="language-plaintext highlighter-rouge">papaja</code> also privileges PDF over Word outputs in its documentation and functionality.
As a result, these packages fall short of providing the flexible and customizable Word outputs needed for writing scientific manuscripts.
The work-around approach—creating PDF outputs and converting the underlying .tex files to Word with Pandoc—also produces less than satisfactory results.</p>
<p>I wanted to create Microsoft Word documents using R Markdown that (a) already had the formatting I needed without a lot of post-output effort and (b) were also easily customized depending on the journal/audience.
Thus, I combined the strengths of <code class="language-plaintext highlighter-rouge">bookdown</code> and <code class="language-plaintext highlighter-rouge">papaja</code> into the flexible template described below.</p>
<h2 id="the-template">The template</h2>
<p>I developed <a href="https://github.com/hollzzar/manuscript-template">this template</a> for creating scientific manuscripts to take advantage of R Markdown’s reproducibility, Word’s ease of collaboration, and <code class="language-plaintext highlighter-rouge">bookdown</code>’s cross-referencing capabilities.</p>
<h3 id="organization">Organization</h3>
<p>The R Markdown files and R scripts for the template are organized in a hierarchical fashion.
This structure keeps the information tidy and concise at each stage, from data wrangling, analysis, and visualization to presentation/writing.</p>
<p>There are separate R Markdown files for each section:</p>
<ul>
<li>Title page</li>
<li>Abstract</li>
<li>Introduction</li>
<li>Methods</li>
<li>Results</li>
<li>Conclusion/discussion</li>
<li>Acknowledgments/funding</li>
</ul>
<p>There is more information about using <a href="https://www.hzaharchuk.com/rmarkdown-guide/organization.html#child-documents"><code class="language-plaintext highlighter-rouge">child</code> documents</a> and <a href="https://www.hzaharchuk.com/rmarkdown-guide/organization.html#source">source code</a> in my <a href="https://www.hzaharchuk.com/rmarkdown-guide/">R Markdown Guide</a>.</p>
<h3 id="formatting">Formatting</h3>
<p>The formatting can be easily customized by <a href="https://www.hzaharchuk.com/rmarkdown-guide/template.html#word-1">changing the Word styles</a> in the <em>template.docx</em> file or editing the <a href="https://pandoc.org/MANUAL.html#fenced-code-blocks">fenced code blocks</a> in the R Markdown files.</p>
<p>The template file tells Pandoc how to convert from markdown text to Word output.
Pandoc automatically assigns particular types of text to particular Word styles; for example, first level headers in R Markdown receive the <em>Header 1</em> format in Word, as shown in the image below.
These Word styles are defined in the template document.</p>
<p><img src="/images/template_word.png" alt="" /></p>
<p>In order to edit these styles:</p>
<ol>
<li>Open the template document</li>
<li>Open the <em>Styles Pane</em></li>
<li>Highlight the text whose format you want to change</li>
<li>Make the desired changes (e.g., italicizing <em>Header 1</em>)</li>
<li>Locate the format in the <em>Styles Pane</em></li>
<li>Select <em>Update to Match Selection</em> from the drop-down menu</li>
</ol>
<p>If you want to specify exactly which Word style a chunk of text should receive, you can use fenced code blocks.
Below, I specify that I want the <em>Abstract text</em> format, which I defined in my template file, to apply to the text between the three colons.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>---
output: bookdown::word_document2
---
# Abstract {-}
::: {custom-style="Abstract text"}
Your abstract goes here.
:::
</code></pre></div></div>
<h2 id="limitations">Limitations</h2>
<p>There are two main limitations to using Word outputs.
One is that, to my knowledge, there is no way to include line numbers or update headers automatically.
This will need to be done in the Word output.</p>
<p>More importantly, there is currently no way easy way to incorporate track-changes from collaborators.
I recommend using Git to compare your document to your collaborators’ or using the <em>Combine Documents</em> functionality in Word.
Unfortunately, neither of these solutions updates your actual R Markdown documents.
This will need to be done by hand if needed.</p>Holly A. Zaharchukhzaharchuk@psu.eduAn R Markdown template for creating reproducible scientific manuscripts in Microsoft WordFrom EEG to ERP: An automated processing pipeline2021-03-11T00:00:00-08:002021-03-11T00:00:00-08:00https://www.hzaharchuk.com/posts/2021/03/blog-post-1<p>Streamlining data processing, analysis, and visualization for EEG/ERPs with MATLAB and R</p>
<h1 id="scripts">Scripts</h1>
<p><a href="https://sites.psu.edu/bildlab/">My lab</a> uses a combined NeuroScan and Brain Vision EEG (electroencephalography) system with 31 active Ag/AgCl electrodes and two bipolar channels.</p>
<p>I developed the processing pipeline presented here for my first experiment on dialectal variation in syntax (attested double modal vs. single modal constructions) in two groups of participants (Unmarked and Southern).</p>
<p>On my Github, you can find the full repositories for the <a href="https://github.com/hollzzar/eeg-data-scripts">MATLAB scripts</a> and <a href="https://github.com/hollzzar/erp-data-scripts">R scripts</a> that are described below.</p>
<h2 id="matlab-data-processing">MATLAB: Data processing</h2>
<h3 id="pre-processing">Pre-processing</h3>
<p>I adapted a MATLAB script from my former labmate <a href="https://www.linkedin.com/in/carla-fernandez-5ab404bb/">Carla Fernandez</a> to <a href="https://github.com/hollzzar/eeg-data-scripts/blob/master/DM_process.m">pre-process</a> the EEG data, which includes filtering, re-referencing, epoching, baseline correction, and artefact rejection.</p>
<h3 id="erps-event-related-potentials">ERPs (event-related potentials)</h3>
<p>I wrote <a href="https://github.com/hollzzar/eeg-data-scripts/blob/master/DM_analysis.m">this MATLAB script</a> to calculate by-subject (individual) and across-subject (grand) average ERPs for statistical analysis and plotting, respectively.</p>
<p>The script exports the processed data as TXT files for use in R.</p>
<h2 id="r-statistical-analyses-and-data-visualization">R: Statistical analyses and data visualization</h2>
<h3 id="traditional-anovas">(Traditional) ANOVAs</h3>
<p><a href="https://github.com/hollzzar/erp-data-scripts/blob/main/1a_group_analysis.R">This R script</a> uses the processed data from MATLAB to run a series of within- and between-group ANOVAs, format the results according to APA guidelines, and create data tables that are passed onto the plotting scripts, output to CSV files, and joined with the cleaned behavioral data for correlation analyses.</p>
<p>The ANOVAs were run in two time windows, corresponding to the early anterior negativity (EAN) and P600 components, and in two sets of electrodes: midline (Fz, Cz, Pz) and lateral (LF, RF, LP, RP).</p>
<h3 id="visualization">Visualization</h3>
<p>Two additional scripts work together to <a href="https://github.com/hollzzar/erp-data-scripts/blob/main/2_plot_format.R">set up plotting parameters</a> and <a href="https://github.com/hollzzar/erp-data-scripts/blob/main/3_erp_plots.R">generate butterfly plots</a>.</p>
<p>These scripts pull the significance levels from the analyses in each time window and group and adds them to the ERP plots automatically.</p>
<p>Here is an example of the final plot from my Southern participant group: <img src="/images/SUSE_erp.png" alt="Southern group butterfly plot" /></p>
<h3 id="bayes-factors">Bayes factors</h3>
<p>Contrary to my primary hypothesis, group was not a significant predictor of ERP responses.</p>
<p>In order to provide statistical support for the invariance of group, I learned how to calculate Bayes factors, and thought I would share what I learned.</p>
<p>You can find an example with descriptions <a href="https://htmlpreview.github.io/?https://github.com/hollzzar/erp-data-scripts/blob/main/4_bayes_example.html">here</a>.</p>Holly A. Zaharchukhzaharchuk@psu.eduStreamlining data processing, analysis, and visualization for EEG/ERPs with MATLAB and R