1.1 Installing software
Before we start the course, we have to install three things:
- R, a free program for statistical programming
- RStudio, a user interface which makes it easier to work with R; overlook our data, packages and output.
- Several packages, which are ‘add-ons’ for R with functions to do specific analyses. They also include the documentation (help files) that describes how to use them, and sample data.
1.1.1 1. Installing R
- The latest R Version is available here
- Based on your operating system (Linux, Mac, Windows), click Download.
- If you use Windows, click Download R for Windows: Find ‘base’ under subdirectories -> Click ‘install R for the first time’ -> Click Download R 3.6.3 (or another version) for Windows
- If you use Mac, click Download R for (Mac) OS X: Find the header ‘Latest release’ -> Click R-3.6.3.pkg (or another version)
- If you use Linux, click Download R for Linux: Choose your Linux distribution (debian/, redhat/, suse/, ubuntu/) -> Open the terminal -> Run the installation command
1.1.2 2. Installing RStudio
Download RStudio on the RStudio Website (Link). It’s free!
1.1.3 3. Installing packages
As a prerequisite for this guide, you need to have a few essential R packages installed.
- Open RStudio
- Inside RStudio, find the window named Console on the bottom left corner of your screen (it might fill the entire left side of the screen).
- We will now install a few packages using R Code. Here’s an overview of the packages, and why we need them:
Package | Description |
---|---|
lavaan | A sophisticated and user-friendly package for structural equation modeling |
ggplot2 | A flexible and user-friendly package for making graphs |
tidySEM | Plotting and tabulating the output of SEM-models |
semTools | Comparing models, establishing measurement invariance across groups |
psych | Descriptive statistics |
foreign | Loading data from SPSS ‘.sav’ files |
readxl | Loading data from Excel ‘.xslx’ files |
4. To install these packages, we use the install.packages()
function in R. One package after another, our code should look like this:
install.packages("tidySEM", dependencies = TRUE)
Don’t forget to put the package names in ""
. Otherwise, you will get an error message.
1.1.4 Get started
1.1.5 Starting a new project in Rstudio
To keep all your work organized, you should use a project. In Rstudio, click on the New project button:
In the pop-up dialog, click New directory, and again New project.
type the desired directory name in the dialog (give it a meaningful name, e.g. “TCSM_course”), and use ‘Browse’ if you need to change the directory where you store your projects. Now, in your project, click File > New file > R script. This script file works just like notepad, or the syntax editor in SPSS: You type plain text, but you can run it any time you want. Conduct all of the exercises in this script file.
1.1.6 Code conventions
Throughout the guide, a consistent set of conventions is used to refer to code:
- Functions are in a code font and followed by parentheses, like
sum()
ormean()
. - Other R objects (like data or function arguments) are in a code
font, without parentheses, like
seTE
ormethod.tau
. - Sometimes, we’ll use the package name followed by two colons, like
lavaan::sem()
. This is valid R code and will run. Thelavaan::
part indicates that the functionsem()
comes from the packagelavaan
.
1.1.7 Getting Help
As you start to apply the techniques described in this guide to your data you will soon find questions that the guide does not answer. This section describes a few tips on how to get help.
- Every function in R has documentation (a help file). To see it, select the name of the function and press F1, or run the command
?
followed by the name of the function, e.g.:?aov
. I have been using R for 10 years, and I still press F1 all the time to see how a function works. - Andy Field, the book used for our undergraduate statistics courses (Field, Miles, and Field 2012), is also available for R. Many basic analyses are explained for R in this book.
- If you get stuck, start with Google. Typically, adding “R” to a search is enough to restrict it to relevant results, e.g.: “exploratory factor analysis R.” Google is particularly useful for error messages. If you get an error message and you have no idea what it means, try googling it. Chances are that someone else has been confused by it in the past, and there will be help somewhere on the web. (If the error message isn’t in English,
run
Sys.setenv(LANGUAGE = "en")
and re-run the code; you’re more likely to find help for English error messages.) - If Google doesn’t help, try stackoverflow. Start by spending a little time searching for an existing answer; including [R] restricts your search to questions and answers that use R.
- Lastly, if you stumble upon an error (or typos!) in this guide’s text or R syntax, feel free to contact Caspar van Lissa at c.j.vanlissa@uu.nl.