Introduction to RMarkdown

Before we dive into our first coding session, let’s become a bit more familiar with the programming tools used in this course.

We will write our annotated R code using Markdown.

Markdown is a simple formatting syntax to generate HTML or PDF documents. In combination with R, it will generate a document that includes the comments, the R code, and the output of running such code.

You can embed R code in chunks like this one:

1 + 1
## [1] 2

You can run each chunk of code one by one, by highlighting the code and clicking Run (or pressing Ctrl + Enter in Windows or command + enter in OS X). You can see the output of the code in the console right below, inside the RStudio window.

Alternatively, you can generate (or knit) an html document with all the code, comment, and output in the entire .Rmd file by clicking on Knit HTML.

You can also embed plots and graphics, for example:

x <- c(1, 3, 4, 5)
y <- c(2, 6, 8, 10)
plot(x, y)

If you run the chunk of code, the plot will be generated on the panel on the bottom right corner. If instead you knit the entire file, the plot will appear after you view the html document.

Using R + Markdown has several advantages: it leaves an “audit trail” of your work, including documentation explaining the steps you made. This is helpful to not only keep your own progress organized, but also make your work reproducible and more transparent. You can easily correct errors (just fix them and run the script again), and after you have finished, you can generate a PDF or HTML version of your work.

We will be exploring R through R Markdown over the next few modules. For more details and documentation see http://rmarkdown.rstudio.com.

Make sure R and RStudio are installed

Follow the instructions in the class material and install R and RStudio. If you feel more comfortable using the basic R terminal, skip the step of installing RStudio and the corresponding chunk.

Now run the following code to make sure that you have the current version of R.

version$version.string
## [1] "R version 4.1.0 (2021-05-18)"

This chunk should return something like "R version 4.1.0 (2021-05-18)".

rstudioapi::versionInfo()$version

This chunk should print something like [1] ‘1.4.1717’.

If they do not, then try to get as close to the current versions as possible!

Basic regression operations in R

For this we will use the automobile dataset from the James et. al. text. This can be found in the ISLR package in R.

Start by loading this package:

library("ISLR")

Now we can regress miles-per-gallon on the weight of the vehicle and the number of cylinders.

data(Auto)
with(Auto, lm(mpg ~ weight + cylinders))
## 
## Call:
## lm(formula = mpg ~ weight + cylinders)
## 
## Coefficients:
## (Intercept)       weight    cylinders  
##   46.292310    -0.006347    -0.721378

Why did we need the with() wrapper?