Preface
R for Psychological Science
A series of workshops for learning R quickly and practically to do psychological (or any) research better.
This guide is a series of workshops we created to help students and researchers learn how to quickly and pragmatically learn R for psychological science. While there are many (many) resources for learning R out in the world, we were surprised to find no comprehensive basic tutorial that equips learners in psychology with the specific tools they need to go through each step of the data analysis process. That’s what we tried to do here. And, hopefully, it can help new R users learn what they need and apply it to their data quicker and with fewer headaches. Give it a try and let us know how it goes!
0.1 What’s in this guide
Currently, this guide consists of 9 workshops. These teach you how to
- Understand and use R
- Get your data into R
- Manipulate and clean your data
- Visualize your data, and build the skills to build any plot you want
- Handle outliers, and assess the reliability and dimensionality of your measures
- Test differences between groups (t-tests, ANOVA)
- Measure relationships with correlation and regression, and check their assumptions
- Ask when and why questions with moderation and mediation
- Model binary and count outcomes, where ordinary regression breaks down
Each workshop ends with practice problems and worked answers, plus a quick reference for everything it covered. You can download any workshop as an R Markdown file and follow along in R.
Two more workshops are still in progress:
- Basic programming and iteration
- A workshop to bring these skills together into a reproducible workflow
0.3 How to use these workshops
You may notice that I have called the sections on this website workshops rather than chapters. Why? Well, first of all, they were originally taught as workshops: I led groups of students and researchers through each part in about an hour workshop. Second of all, I think they still hold the utility of something to be done rather than just read. Since we can’t go through the workshops together in person, I have put my voice and everything I would say in the workshop into the text between code chunks–so, now you can do each by yourself as a self-guided workshop.
What do these workshops help with beyond AI or other online R books? In making these workshops, we stressed comprehensive pragmatism (yes I did just come up with that): walking you through each step, from scratch, starting from no knowledge and getting to the point of being able to go through each step of the analysis process by yourself for your own project. It is pragmatic in the sense that we tried to give you practical code (and only this code) to do what you need in the analysis process. It is comprehensive in the sense that we tried to give you everything you need to go from 0 to hero for most basic analyses and data types we run into in human subjects research.
Here is how to use these workshops:
0.3.1 Before you start
Download the workshop. At the top of every workshop there is a link that says “Download this workshop as an R Markdown file”. Click it and save the file–maybe to a folder on your computer to keep these workshops.
Open it in RStudio and work in there, following along in the browser. Doing it yourself gets you working in R yourself; the browser provides a nice clean interface for reading or following along more interactively.
Get the data. Each workshop reads in one or two data files, and links to them are included where they are first used. Save them in the same folder as the R Markdown file, so R can find them.
0.3.2 While you work through it
Run every chunk yourself. Run through each part by yourself, and follow along in a browser, if you want. If you’re feeling brave, try tweaking parts of the code I have provided to see how it changes what it gives you. If you get different output than what shows online, try to work out why. If you are ever curious about anything, and think “What if I did…”, try it!! Please!! This curiosity and a little meandering is what leads to the best insights.
Trust the order. Each workshop is built so that everything uses only what has already been explained. If you hit something that looks unfamiliar, it has almost certainly appeared earlier, so it is worth going back rather than pressing on.
Do the practice problems. They appear in shaded boxes throughout, each with an empty chunk to work in and a collapsible Answer to check your work.
It goes without saying, but try the problem before opening the answer. Write something, run it, see what happens. Getting an error and working out why is the single most useful thing you can do while learning R–far more useful than reading a correct answer and thinking “yes, that makes sense”. When you do open the answer, compare it to what you wrote. If yours differs but works, that is fine; there is usually more than one way. If yours doesn’t work, the answer usually has a short note explaining the part that people often trip up on.
Get unstuck. Being stuck is normal and not a sign that you are bad at this. A few good moves, roughly in order:
- Read the error message. It is trying to tell you something specific.
- Use R’s own help:
?function_namefor any function you don’t recognize. - Ask AI for more details or for examples of any of the topics we go through. For any errors or other issues you run into, you can ask for help resolving it, but also ask it to to explain so you understand what is going on.
- Search online. Adding
[R]to a Stack Overflow search filters to R questions.
There is more on getting help in Notes on learning R.
0.3.3 After you finish
Give two minutes of feedback. There is a QR code at the end of every workshop. They say feedback is the spice of life (or I do, anyway). We use it to work out which parts land and which parts need reworking, and we can only improve this as a resource that is actually useful with your help!
Keep the quick reference. Every workshop ends with a table of everything it covered, grouped by what you are trying to do. Come back to these as a quick reference or when you are analyzing your data in three months.
Use the function sheet. Across the workshops we build several functions of our own that make common tasks less tedious–correlation tables, alpha tables, outlier detection. They are collected in The function sheet, with a line-by-line explanation of each one and a link to download them all as a single R script. Download these and use them in your own code as they are useful.