- Sorting out your RStudio working environment.
- Getting you ready to produce reproducible reports in RMarkdown.
.pdf
, .html
, .docx
, .doc
rmarkdown
install.packages('tidyverse') install.packages('psyntur')
setwd()
headaches.blomkvist.csv
from NOW and move it into the “data” folder.From the RStudio menu …
File
> New File
> R Markdown
output: html_document
: we will use html for simplicitybasics.Rmd
.Rproj
file.Knit
) documenttidyverse
basicsc()
)mean
, sum
, min
, max
, sd
<-
)tidyverse
, psyntur
)read_csv
(next slide)tibble
?psyntur::faithfulfaces
(many more in datasets
)tidyverse
functions: select
, filter
, slice
(indexing, e.g. [1]
), pull
, summarise
CTRL+ALT+I
or OPTION+ALT+I
(i.e. the letter “i”) called packages
and load libraries needed:library(tidyverse) library(psyntur)
loaddata
and load the blomkvist.csv
data published in Blomkvist et al. (2017)# Load data bk_alldata <- read_csv("data/blomkvist.csv") # Select a few variables bk_selected <- select(blomkvist_alldata, id, smoker, age, rt = rt_hand_d) # Remove missing data blomkvist <- drop_na(bk_selected)
{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
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)
out.width = 75%
.\@ref(fig:myscatterplot)
in the text."A scatterplot of age and reaction time can be found in Figure \@ref(fig:myscatterplot)."
In the YAML preamble change
output: html_document
to
output: bookdown::html_document2
(smoker_rt <- summarise(blomkvist, mean = mean(rt), sd = sd(rt), .by = smoker))
# A tibble: 3 × 3 smoker mean sd <chr> <dbl> <dbl> 1 former 653. 152. 2 no 635. 203. 3 yes 633. 217.
library(knitr) kable(smoker_age, booktabs = TRUE, digits = 2, align = 'c', # centre value in each column caption = 'Descriptives of age by smoker.')
smoker
” and cross-reference the table in the text using Table \@ref(tab:smoker)
.references.bib
(save in same working directory as your .Rmd
file).bib
entry for Blomkvist et al. (2017) from Google Scholar and paste it into references.bib
:
cite
and BibTeX
references.bib
blomkvist2017reference
@blomkvist2017reference
or [@blomkvist2017reference]
.# References
”bibliography: references.bib biblio-style: apalike
# 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")
File
> New File
> R Markdown
(e.g. readthedown
or robobook
for documents)rmdshower::shower_presentations
and ioslides_presentation
for slidespapaja
Next week we will start with data visualisation, so make sure you understand RMarkdown and R code.
rmarkdown
website.Andrews, Mark. 2021. Doing Data Science in R: An Introduction for Social Scientists. SAGE Publications Ltd.
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.