Part II — Computer Simulations · R

Introduction to R

What Is R?

R is a free, open-source programming language and environment designed specifically for statistical computing and graphics. It is widely used in academic research, data science, and program evaluation. The computer simulation exercises in this workbook use R in place of the MINITAB package used in earlier versions.

R has several advantages for these exercises: it is freely available on all major platforms, it has an enormous library of statistical packages, and the scripts you write are self-documenting — you can share, reproduce, or modify any analysis simply by sharing the .R file.

Installing R and RStudio

You will need two pieces of software: R itself (the computing engine) and RStudio (a friendly interface for working with R). Both are free.

  1. Install R — Download from cran.r-project.org. Choose the version for your operating system (Windows, macOS, or Linux) and follow the installer instructions.
  2. Install RStudio — Download the free Desktop version from posit.co. Install it after R is already installed.

Once both are installed, open RStudio. You will see four panes: a script editor (top left), a console (bottom left), an environment/history viewer (top right), and a files/plots/help panel (bottom right).

Getting Started in RStudio

The best way to run the simulation exercises is to open the downloaded .R script in RStudio's script editor, then run lines or sections by pressing Ctrl+Enter (Windows/Linux) or Cmd+Enter (macOS). Output appears in the Console; plots appear in the Plots panel.

Each script in this workbook is self-contained — it generates all its own data and produces all analyses from scratch. Because the data are randomly generated, your results will differ slightly from any example output shown here, but the patterns will be consistent across runs.

Installing Required Packages

The simulation exercises use several R packages beyond the base installation. You only need to install each package once. Copy and run the following commands in the RStudio Console (or from a script) before running the simulations for the first time:

install.packages("psych") install.packages("ggplot2") install.packages("psychTools") install.packages("lessR") install.packages("lavaan") install.packages("GPArotation")

After installation, each package is loaded within the script that needs it using library() — for example, library(psych). You do not need to reinstall packages between sessions.

Running the Simulations

Each simulation exercise has a downloadable .R script. To run an exercise:

  1. Click the download button on the exercise page to save the .R file.
  2. Open the file in RStudio (File → Open File, or drag it onto RStudio).
  3. Read through the script from top to bottom. The comments (lines starting with #) explain what each section does.
  4. Run the script one section at a time using Ctrl+Enter to execute the selected line or block. Observe the output in the Console and the Plots panel as you go.
  5. After completing the main script, try the variations suggested in the "Reflections & Variations" section at the end of each exercise.

Because the data are randomly generated each time you run a script, you can run any exercise multiple times and observe how results vary across samples while staying consistent with the patterns built into the simulation model.

Getting Help

If you encounter an unfamiliar R function, type ?function_name or help(function_name) in the Console to open its documentation. For broader questions, the R community is large and active — searching for any error message or task on the web almost always returns a useful answer.

The Quick-R reference site provides concise examples for most common statistical tasks. The ggplot2 documentation is particularly helpful for understanding the graphics commands used in these exercises.

← Back to Simulation Home