Lab 01 - Hello R!

Learning goals

  • Get acquainted with R and RStudio, which we will be using throughout the course to analyze data as well as to learn the statistical concepts discussed in the course.
  • Appreciate the value of visualization in exploring the relationship between variables.
  • Start using R for building plots and calculating summary statistics.

Terminology

We’ve already thrown around a few new terms, so let’s define them before we proceed.

  • R: Name of the programming language we will be using throughout the course.
  • RStudio: An integrated development environment for R. In other words, a convenient interface for writing and running R code.

I like to think of R as the engine of the car, and RStudio is the dashboard.

Starting slow

As the labs progress, you are encouraged to explore beyond what the labs dictate; a willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.

And to make versioning simpler, this is a solo lab. Additionally, we want to make sure everyone gets a significant amount of time at the steering wheel.

Getting started

1 Download R

If you don’t have R installed.

Go to the CRAN and download R, make sure you get the version that matches your operating system.

If you have R installed

If you have R installed run the following code

R.version
##                _                           
## platform       x86_64-apple-darwin20       
## arch           x86_64                      
## os             darwin20                    
## system         x86_64, darwin20            
## status                                     
## major          4                           
## minor          3.1                         
## year           2023                        
## month          06                          
## day            16                          
## svn rev        84548                       
## language       R                           
## version.string R version 4.3.1 (2023-06-16)
## nickname       Beagle Scouts

This should tell you what version of R you are currently using. If your R version is lower then 4.3.0 I would strongly recommend updating. In general it is a good idea to update your R version, unless you have a project right now that depend on a specific version of R.

1.2 Download RStudio

We recommend using RStudio as your IDE if you don’t already have it installed. You can go to the RStudio website to download and install the software. Once it is installed, open RStudio and use it to complete the rest of this lab.

Hello RStudio!

RStudio is comprised of four panes.

  • On the bottom left is the Console, this is where you can write code that will be evaluated. Try typing 2 + 2 here and hit enter, what do you get?

  • On the bottom right is the Files pane, as well as other panes that will come handy as we start our analysis.

  • If you click on a file, it will open in the editor, on the top left pane.

  • Finally, the top right pane shows your Environment. If you define a variable it would show up there. Try typing x <- 2 in the Console and hit enter, what do you get in the Environment pane?

Start a new Project

RStudio Projects are a great way to stay organized and keep all of your work on a particular topic in one place. Project files keep track of things like the R objects you are using and which files you have open, so that you can quickly jump in and out of different work environments.

In the top right of the RStudio window, you should see a drop-down menu that says “Project: (None)”. Click on this and then “New Project…”, which will open up a dialogue box. If you have already created a directory (folder) for you Lab 1 materials, you can associate this Project with an existing directory, otherwise use the dialogue box to create a new directory. We’ll use the generic Project type, “New Project”. Then give it a name like “Lab 1”, choose where you want to save it on your computer, and click “Create Project”. Now in the top right, you should see “Lab 1” next to the R Project logo.

Create a Quarto document

We will use Quarto documents a lot in this course because they are reproducible and allow us to seamlessly integrate code and text.

In the top left, you will see a “New File” icon that leads to a drop-down menu. Click on this and then “Quarto document…”, which will open up a dialogue box. You can leave most of the settings on their defaults for now, just give your document a title and author (yourself) and click “Create”. RStudio may ask you if you would like to install a package that is required, and if so, click “Install”.

This will open the default Quarto document, which already contains some example content. Read through it, then remove this content and set up new sections titled “Question 1” through “Question 5”. Under each section title, add an R code chunk via “Insert…”, “Executable Cell”, “R”. This document will serve as the template for your responses to this lab.

Save your Quarto markdown (qmd) file as lab-01-hello-r.qmd and see what happens when you click “Render”.

Packages

R is an open-source language, and developers contribute functionality to R via packages. In this lab we will work with three packages: datasauRus which contains the dataset, and tidyverse which is a collection of packages for doing data analysis in a “tidy” way.

Load these packages by running the following in the Console.

library(tidyverse) 
library(datasauRus)

If you haven’t installed these packages yet and R complains, then you can install these packages by running the following command. (Note that R package names are case-sensitive)

install.packages(c("tidyverse", "datasauRus"))

Note that the packages are also loaded with the same commands in your Quarto document.

Warm up

Before we introduce the data, let’s warm up with some simple exercises.

The top portion of your Quarto file (between the three dashed lines) is called YAML. It stands for “YAML Ain’t Markup Language”. It is a human-friendly data serialization standard for all programming languages. All you need to know is that this area is called the YAML (we will refer to it as such) and that it contains meta information about your document.

YAML

Open the Quarto (qmd) file in your project, change the author name to your name, and Render the document.

Data

The data frame we will be working with today is called datasaurus_dozen and it’s in the datasauRus package. Actually, this single data frame contains 13 datasets, designed to show us why data visualisation is important and how summary statistics alone can be misleading. The different datasets are marked by the dataset variable.

To find out more about the dataset, type the following in your Console: ?datasaurus_dozen. A question mark before the name of an object will always bring up its help file. This command must be run in the Console.

Question 1

  1. Based on the help file, how many rows and how many columns does the datasaurus_dozen file have? What are the variables included in the data frame? Add your responses to your lab report.

Let’s take a look at what these datasets are. To do so we can make a frequency table of the dataset variable:

datasaurus_dozen %>%
  count(dataset)
## # A tibble: 13 × 2
##    dataset        n
##    <chr>      <int>
##  1 away         142
##  2 bullseye     142
##  3 circle       142
##  4 dino         142
##  5 dots         142
##  6 h_lines      142
##  7 high_lines   142
##  8 slant_down   142
##  9 slant_up     142
## 10 star         142
## 11 v_lines      142
## 12 wide_lines   142
## 13 x_shape      142
# table(datasaurus_dozen$dataset)

The original Datasaurus (dino) was created by Alberto Cairo in this great blog post. The other Dozen were generated using simulated annealing and the process is described in the paper Same Stats, Different Graphs: Generating Datasets with Varied Appearance and Identical Statistics through Simulated Annealing by Justin Matejka and George Fitzmaurice. In the paper, the authors simulate a variety of datasets that the same summary statistics to the Datasaurus but have very different distributions.

Data visualization and summary

Question 2

  1. Plot y vs. x for the dino dataset. Then, calculate the correlation coefficient between x and y for this dataset.

Below is the code you will need to complete this exercise. Basically, the answer is already given, but you need to include relevant bits in your Rmd document and successfully knit it and view the results.

Start with the datasaurus_dozen and pipe it into the filter function to filter for observations where dataset == "dino". Store the resulting filtered data frame as a new data frame called dino_data.

dino_data <- datasaurus_dozen %>%
  filter(dataset == "dino")
# dino_data <- datasaurus_dozen[datasaurus_dozen$dataset == 'dino', ]

There is a lot going on here, so let’s slow down and unpack it a bit.

First, the pipe operator: %>%, takes what comes before it and sends it as the first argument to what comes after it. So here, we’re saying filter the datasaurus_dozen data frame for observations where dataset == "dino".

Second, the assignment operator: <-, assigns the name dino_data to the filtered data frame.

Next, we need to visualize these data. We will use the ggplot function for this. Its first argument is the data you’re visualizing. Next we define the aesthetic mappings. In other words, the columns of the data that get mapped to certain aesthetic features of the plot, e.g. the x axis will represent the variable called x and the y axis will represent the variable called y. Then, we add another layer to this plot where we define which geometric shapes we want to use to represent each observation in the data. In this case we want these to be points,m hence geom_point.

ggplot(data = dino_data, mapping = aes(x = x, y = y)) +
  geom_point()

# plot(dino_data$x, dino_data$y)

If this seems like a lot, it is. And you will learn about the philosophy of building data visualizations in layers in detail in the upcoming class. For now, follow along with the code that is provided.

For the second part of these exercises, we need to calculate a summary statistic: the correlation coefficient. Correlation coefficient, often referred to as \(r\) in statistics, measures the linear association between two variables. You will see that some of the pairs of variables we plot do not have a linear relationship between them. This is exactly why we want to visualize first: visualize to assess the form of the relationship, and calculate \(r\) only if relevant. In this case, calculating a correlation coefficient really doesn’t make sense since the relationship between x and y is definitely not linear – it’s dinosaurial!

But, for illustrative purposes, let’s calculate correlation coefficient between x and y.

Start with dino_data and calculate a summary statistic that we will call r as the correlation between x and y.

dino_data %>%
  summarize(r = cor(x, y))
# cor(dino_data$x, dino_data$y)

Question 3

  1. Plot y vs. x for the star dataset. You can (and should) reuse code we introduced above, just replace the dataset name with the desired dataset. Then, calculate the correlation coefficient between x and y for this dataset. How does this value compare to the r of dino?

Question 4

  1. Plot y vs. x for the circle dataset. You can (and should) reuse code we introduced above, just replace the dataset name with the desired dataset. Then, calculate the correlation coefficient between x and y for this dataset. How does this value compare to the r of dino?

Facet by the dataset variable, placing the plots in a 3 column grid, and don’t add a legend.

Question 5

  1. Finally, let’s plot all datasets at once. In order to do this we will make use of facetting.
ggplot(datasaurus_dozen, aes(x = x, y = y, color = dataset))+
  geom_point()+
  facet_wrap(~ dataset, ncol = 3) +
  theme(legend.position = "none")
# layout(matrix(1:16, ncol=4))
# for(i in 1:length(unique(datasaurus_dozen$dataset))){
#   dset_name <- unique(datasaurus_dozen$dataset)[i]
#   subset <- datasaurus_dozen[datasaurus_dozen$dataset == dset_name, ]
#   plot(subset$x, subset$y, main = dset_name, col = i)
# }
# layout(1)

And we can use the group_by function to generate all correlation coefficients.

datasaurus_dozen %>%
  group_by(dataset) %>%
  summarize(r = cor(x, y))
# sapply(unique(datasaurus_dozen$dataset), function(ds){
#     subset <- datasaurus_dozen[datasaurus_dozen$dataset == ds, ]
#     cor(subset$x, subset$y)
# })

You’re done with the data analysis exercises, but we’d like you to do two more things:

Resize your figures:

Add the fields fig-width and fig-height to the YAML header of your document. These will allow you to specify the size (in inches) of any figures generated by the code chunks in your report.

You can also use different figure sizes for different figures. If you are in the Visual editor mode, switch to Source mode. Notice that each R chunk starts and ends with three backticks. Click on the gear icon in the top right of a code chunk and select “Use custom figure size” in the pop-up menu. Set the height and width of the figures and hit Apply when done. Then, render your document and see how you like the new sizes. Change and render again and again until you’re happy with all the figure sizes. Note that changing the figure sizes added new options to these chunks: fig.width and fig.height. You can change them by defining different values directly in your Quarto document as well.

Change the look of your report:

If you have time you can explore the different ways you can add styling to your document. Try adding a theme field to the YAML header and see if you can find valid names of different themes.

Here is a Quarto cheatsheet and a general markdown cheatsheet


This set of lab exersixes have been adopted from Mine Çetinkaya-Rundel’s class Introduction to Data Science.