- Sorting out your RStudio working environment.
- Getting you ready to produce reproducible reports in RMarkdown.
.pdf, .html, .docx, .docrmarkdowninstall.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 Markdownoutput: 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, summariseCTRL+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
myscatterplotecho = 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 BibTeXreferences.bibblomkvist2017reference@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 slidespapajaNext week we will start with data visualisation, so make sure you understand RMarkdown and R code.
rmarkdown website.Andrews, M. (2021). Doing data science in R: An introduction for Social Scientists. SAGE Publications Ltd.
Blomkvist, A. W., Eika, F., Rahbek, M. T., Eikhof, K. D., Hansen, M. D., Søndergaard, M., Ryg, J., Andersen, S., & Jørgensen, M. G. (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, D. E. (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, J., De Maeyer, S., Leijten, M., & VaWaes, L. (2024). Modelling typing disfluencies as finite mixture process. Reading and Writing, 37(2), 359–384. https://doi.org/10.1007/s11145-023-10489-4
Wickham, H., & Grolemund, G. (2016). R for data science: Import, tidy, transform, visualize, and model data. O’Reilly Media, Inc.
Xie, Y. (2017). Dynamic documents with R and knitr. Chapman; Hall/CRC.