- Sorting out your RStudio working environment.
- Getting you ready to write reproducible reports in RMarkdown.
What is RMarkdown?
.pdf
, .html
, .docx
, .doc
setwd()
headaches.psyc30815-stats-3
blomkvist.csv
data set form the NOW Learning Roomrmarkdown
tinytex
# Is tidyverse installed? If not run install.packages('tidyverse')
install.packages('tinytex') tinytex::install_tinytex() # then restart RStudio
# Then, try tinytex:::is_tinytex() # should return TRUE
Click on File
> New File
> R Markdown
Document (PDF, WORD, HTML)
output: pdf_document
: use pdfs for writing manuscripts
Name your RMarkdown: fa-linear-regression.Rmd
Save in same directory as .Rproj
file.
You’ll see chunks of R code and inline R code.
Remainder is plain Markdown.
Knit > Knit to PDF
to compile .Rmd
to .pdf
. Wow!!!
Notice how the code was translated to in pdf.
Important: R code is executed from top down.
Other demos and examples are provided.
CTRL+ALT+I
or CMD+ALT+I
(i.e. the letter “i”)packages
and load libraries needed:library(tidyverse)
loaddata
and load Blomkvist et al. (2017) data:blomkvist <- read_csv("data/blomkvist.csv") %>% select(id, age, smoker, sex, rt = rt_hand_d) %>% drop_na()
{r setup, echo=FALSE}
setup
is a label of this chunk (optional; useful for cross-referencing of figures and tables).echo = FALSE
: don’t display chunk in output; echo = TRUE
: display chunk.knitr::opts_chunk$set(message = FALSE, # don't return messages warning = FALSE, # don't return warnings comment = NA, # don't comment output echo = TRUE, # display chunk (is default) eval = TRUE, # evaluate chunk (is default) out.width = '45%', # figure width fig.align='center') # figure alignment
# This is a section header ## This is a subsection header # This is another section header ## This is another subsection header ### This is a subsubsection header
myscatterplot
echo = F
cause we only need the figure.fig.cap = "A scatterplot."
in the chunk configurations.library(psyntur) scatterplot(x = age, y = rt, data = blomkvist)
ggplot2
ggplot(blomkvist, aes(x = age, y = rt)) + geom_point() + theme_classic()
out.width = 50%
.\ref{fig:myscatterplot}
in the text."A scatterplot of age and reaction time can be found in Figure \ref{fig:myscatterplot}."
Cross-referencing figures requires the use of pdf_document2
as output
format. In preamble replace output
with:
output: bookdown::pdf_document2: toc: false # removes the table of contents number_sections: false # removes section numbering
You may need to install bookdown
. We also turning off the table of content (toc
) and the section numbers.
header-includes: - \usepackage{booktabs}
booktabs
to improve type setting.library(psyntur) (smoker_age <- describe(data = blomkvist, by = smoker, mean = mean(age), sd = sd(age)))
# A tibble: 3 × 3 smoker mean sd <chr> <dbl> <dbl> 1 former 65.2 16.9 2 no 53.0 21.3 3 yes 50.6 17.5
library(kableExtra) smoker_age %>% kable(format = 'latex', booktabs = TRUE, digits = 2, align = 'c', # centre value in each column caption = 'Descriptives of age by smoker.') %>% kable_styling(position = 'center') # centre position of table
smoker
” and cross-reference the table in the text using Table \ref{tab:smoker}
.bibliography: refs.bib biblio-style: apalike
refs.bib
(save in same working directory as your .Rmd
file).bib
entry for Blomkvist et al. (2017) from Google Scholar and paste it into refs.bib
:
cite
and BibTeX
.bib
entry into refs.bib
blomkvist2017reference
@blomkvist2017reference
or [@blomkvist2017reference]
.# References
”# Fit the model and get the summary model <- lm(rt ~ sex, data = blomkvist) model_summary <- summary(model)
# Extract R^2 r2 <- model_summary$r.sq
The $R^2$ for this model is `r round(r2, 2)`.
Renders “The \(R^2\) for this model is 0.03.”
# Extract F statistic f_stat <- model_summary$fstatistic p_value <- pf(f_stat[1], f_stat[2], f_stat[3], lower.tail = FALSE)
The model summary can be summarised like so: $F(`r round(f_stat[2])`, `r round(f_stat[3])`) = `r round(f_stat[1],2)`$, $p `r format.pval(p_value, eps = 0.01)`$.
Renders “The model summary can be summarised like so: \(F(1, 263) = 7.48\), \(p <0.01\).”
p <- c(0.05, 0.02, 0.011, 0.005, 0.001) format.pval(p, eps = 0.01)
[1] "0.05" "0.02" "0.01" "<0.01" "<0.01"
'$'
symbols for inline mode.$\beta$
renders \(\beta\).$\beta_0$
is \(\beta_0\) and using '{}'
for more than one symbol as in $\beta_{01}$
which is \(\beta_{01}\)'^'
as in $\sigma^2$
which is \(\sigma^2\).$x + y$
, $x - y$
$\cdot$
or $\times$
to get \(\cdot\) or \(\times\), respectively, as in \(3 \cdot 2\)$/$
or $\div$
to get \(/\) or \(\div\), respectively, or $\frac{1}{2}$
for \(\frac{1}{2}\)$\pm$
renders to \(\pm\)install.packages("rmdformats")
or
install.packages("remotes") remotes::install_github("juba/rmdformats")
File
> New File
> R Markdown
(e.g. readthedown
or robobook
for documents)rmdshower::shower_presentations
and ioslides_presentation
for slidespapaja
kableExtra
check herermarkdown
website.Blomkvist, Andreas W., Fredrik Eika, Martin T. Rahbek, Karin D. Eikhof, Mette D. Hansen, Malene Søndergaard, Jesper Ryg, Stig Andersen, and Martin G. Jørgensen. 2017. “Reference Data on Reaction Time and Aging Using the Nintendo Wii Balance Board: A Cross-Sectional Study of 354 Subjects from 20 to 99 Years of Age.” PLoS One 12 (12): e0189598.
Knuth, Donald Ervin. 1984. “Literate Programming.” The Computer Journal 27 (2): 97–111.
Open Science Collaboration. 2015. “Estimating the Reproducibility of Psychological Science.” Science 349 (6251): aac4716.
Roeser, Jens, Sven De Maeyer, Mariëlle Leijten, and Luuk VaWaes. 2024. “Modelling Typing Disfluencies as Finite Mixture Process.” Reading and Writing 37 (2): 359–84. https://doi.org/10.1007/s11145-023-10489-4.
Wickham, Hadley, and Garrett Grolemund. 2016. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data. O’Reilly Media, Inc.
Xie, Yihui. 2017. Dynamic Documents with R and Knitr. Chapman; Hall/CRC.