Homework #1: Set up R and RStudio
Due Wed., Jan 7
PDF version
Homework
Preliminary Information
Set up software: R, RStudio, R development environment, and R packages for Bayesian statistics.
Homework Exercises:
Install R
If you don’t have R installed on your computer, download and install R. If you have a version of R older than 4.5, you should update it to 4.5 or higher.
To install or upgrade R, download R version 4.5.2 from The Comprehensive R Archive Network
- Windows:
- Download and install the “base distribution” of R 4.5.2.
- MacOS:
- Download and install R version 4.5.2
- Linux:
- You should be able to install R from your Linux distribution’s package
manager:
sudo apt-get install r-base r-base-devfor Debian or Ubuntusudo yum install Rorsudo dnf install gitfor Fedora, Red Hat, and related distributions.- If you are using Ubuntu, there are advanced instructions for configuring the package management system to use the very latest versions of R and its associated packages at https://cran.rstudio.org/bin/linux/ubuntu/fullREADME.html. This is optional and the default version of R should be fine for this class, as long as it is version 4.5 or higher.
- You should be able to install R from your Linux distribution’s package
manager:
- Windows:
Install RStudio
RStudio is a free editing and software development environment that makes it much easier to work with R. I strongly recommend that you install RStudio for this course.
Go to the download page for the free desktop edition of RStudio and download the installer for your operating system. Windows, MacOS, and the Debian, Ubuntu, Fedora, RedHat, and openSUSE editions of Linux are all supported.
There are other versions of RStudio (an expensive professional edition and a server edition). You want the free desktop edition. As of January 2026, the latest version is RStudio Desktop version 2025.09.2+418 (Cucumberleaf Sunflower)
Run the installer.
After the installer finishes running, run RStudio.
When RStudio starts up, the lower left part of the screen should have a window that displays the R version, saying something like this:
R version 4.5.2 (2025-10-31 ucrt) -- "[Not] Part in a Rumble" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.The details will be different depending on your operating system, but if you see something like this, RStudio correctly found R on your computer.
Configuring RStudio for additional optional software. There are a few other pieces of software that you may want to use this semester, but these are not required.
If have installed the Git revision control system or the LaTeX typesetting software, you should configure RStudio to use them:
If you will use Git for software revision control with R, you should open the “Tools” menu, and click on the “Global Options” choice.
- Go to the “Git/SVN” tab and click “enable version control interface for RStudio projects”. If RStudio can find the git program on your computer, it will appear in the “git executable” field. If RStudio can’t find it, you can help it by browsing to the git program.
If you have installed LaTeX on your computer, click on the SWeave tab, and select “knitr” for weaving
.Rnwfiles, and choosepdfLaTeXfor typesetting LaTeX files into PDF.
Install the
cmdstanrpackage for RAfter you have installed R and RStudio, you will need to install a number of software packages that extend R for Bayesian data analysis.
The trickiest package to install is
cmdstanr. I recommend that you follow the detailed installation instructions on thecmdstanrweb site.After you install
cmdstanr, you will need to use it to install thecmdstansoftware. For this, you will need to install a C++ compiler and related tools before you can installcmdstan.(Don’t worry, you won’t need to do any programming in C++. Stan writes the C++ code for you, based on your description of the statistical problem.)
Follow the instructions at the
cmdstanweb page, https://mc-stan.org/docs/cmdstan-guide/installation.html. There are instructions for installing the C++ toolchain for Windows, MacOS, and Linux. If you are using Windows, the simplest thing to do is install the Rtools4.5 software from the same place you installed R, or from https://cran.rstudio.org/bin/windows/Rtools/rtools45/rtools.htmlAfter you’ve done this, you can go to the main
cmdstanrinstallation instructions and continue from there.The basic steps are to do the following at the R console in RStudio:
First, check whether the C++ toolchain is set up correctly:
library(cmdstanr) check_cmdstan_toolchain()If you get an error message, then run
check_cmdstan_toolchain(fix = TRUE)to fix the toolchain configuration
Next, install
cmdstanoptions(mc.cores = parallel::detectCores() / 2 - 1) install_cmdstan()The line beginning
options(...is optional. It tells R to use multiple processor cores to installcmdstan, which can speed up the installation considerably if you have a powerful processor in your computer.
If you run into difficulty, let me know and I will help you.
If you are having trouble installing
cmdstan, don’t worry. We won’t usecmdstanuntil late in the semester, so go ahead and install the other things, and there will be plenty of time for me to help you getcmdstaninstalled and working on your computer.Install other R packages
This will be a lot easier. Open RStudio, go to the “Console” window in RStudio, and type
install.packages(c("devtools", "pacman")) library(pacman) for (p in c("tidyverse", "ape", "bayesplot", "brms", "broom", "coda", "dagitty", "flextable", "ggdag", "ggformula", "ggmcmc", "ggrepel", "ggthemes", "knitr", "loo", "mvtnorm", "patchwork", "posterior", "psych", "rcartocolor", "Rcpp", "rmarkdown", "rprojroot", "sf", "shiny", "shinystan", "statebins", "tidybayes", "viridis", "viridisLite", "wesanderson")) { p_install(p, character.only = TRUE, force = FALSE) } remotes::install_github("rmcelreath/rethinking") remotes::install_github("mjskay/tidybayes.rethinking")This may take some time to run, but when it is done, you should be good to go.
Notes on Homework:
This homework assignment is just to set up the software on your computer. There is nothing for you to turn in. Please ask me for help if you have trouble getting the software installed.