install.packages(c("tidyverse", "devtools", "testthat", "rmarkdown"))HW0: Installing R
Why Use R?
You could, in principle, do all of our data analysis by hand. But once data sets grow beyond a handful of rows and the formulas get more involved, hand calculation becomes slow, tedious, and error prone. (Imagine computing a variance for a million survey responses with a pencil.) A computer is the right tool for this job, and specifically we’ll use the statistical programming language R. Why R?
R is the perfect first programming language for statisticians because it emphasizes elegant, functional solutions: you focus on WHAT you want to compute, not HOW the computer should carry it out, which frees you to think about complicated ideas in simple ways.
R is completely free to download and use.
R is used in industry and academia by data analysts, statisticians, and economists everywhere.
You may be wondering: Why not Python? Why not Excel?
Python is a fine language, and most everything you can do in R, you can also do in Python. The difference is emphasis: Python doesn’t share R’s focus on functional programming, where you describe WHAT you want to compute and let the language handle the details. That style is exactly what makes R a good place to learn to think about data.
Excel is handy for eyeballing a small table. The trouble is that spreadsheet analysis lives in clicks and hidden formulas instead of a script. Excel struggles to be reproducible: another person can’t take your raw data, follow your steps by running a script, and arrive at the same result. In R, you don’t work with the spreadsheet itself, but with a script that reads in the data and then follows a series of steps to do your analysis. There’s a famous cautionary tale here: two prominent Harvard economists published a paper in 2010 on economic growth and public debt that helped justify austerity measures in the US and Europe after the 2008 financial crisis. But in 2013 when a graduate student tried to replicate the results for a class assignment, they discovered the results hinged on an Excel formula that silently dropped five countries from an average. If the authors were using a programming language instead of Excel, that error would have been much easier to spot before it was published.
So: R it is. Your assignment for today is simply to get R running on your own computer, following the instructional video below. If you run into trouble, come to office hours or email to make an appointment.
Here’s a video showing you how to do steps 1 and 2 on windows, here’s a video for macs.
Step 1: Install R
Go here: https://cran.r-project.org/ and follow the instructions to download R for your Linux, Windows, or Mac. You should download the latest release. If you are a Mac user, first check to see whether you have an Apple silicon mac or an older Intel mac (Top left apple symbol > About this mac > Chip will say M1, M2, … or Intel). Finish the installation by opening the installer and agreeing to the software license agreement. Click through until you have confirmation it was successful.
Step 2: Install RStudio IDE
Go here https://posit.co/products/open-source/rstudio and find the RStudio IDE version for you. Mac users will need to drag the RStudio icon into their applications folder.
Step 3: Install Four Useful Packages
Open RStudio. In your Console (lower left hand pane), run this line of code to install four packages we’ll use throughout the term: the tidyverse, devtools, testthat, and rmarkdown.
Step 4: Prove your workspace is ready to go!
Prove everything works by compiling a report:
Download hw00_checkpoint.R and open it in RStudio.
Replace YOUR NAME HERE with your name (keep the quotes).
Hit File > Render Document (or Compile Report) to render the script into an HTML document. RStudio runs the whole script from top to bottom in a fresh session, so if any piece of your installation is missing, compilation stops with an error instead of producing a document. A finished document is proof that everything works.
Find
hw00_checkpoint.htmlin the same folder where you saved the script (likely Downloads), and upload it to Canvas.
The compiled document shows the versions you installed, a passing test from the testthat package (the same machinery that will grade your koan homework), and your very first plot, made with ggplot2. We’ll spend several weeks on plots like it later in the term.