1 R Markdown

This website was created using a tool called R Markdown. R Markdown is an extension of the R programming language that you will use in your statistics courses. It allows you to embed your statistical calculations within a document, such as a website, a PDF file, or even a word processing file like a Microsoft Word .docx file. This is exciting and important, because it gives you the ability to create your own little lab notebook, a resource that binds together all the information from a research project.

For example, you could write a literature review that includes web links to the original papers.

Perhaps the most exciting research ever published, Trinkaus’ (1994) observational study of baseball cap directionality has had significant influence in a range of other areas, from sport to politics.

Additionally, you can embed important figures, which can be helpful if you are writing and document that describes the methods of prior studies and their results.

Stimuli and results from Yurowsky et al. (2017), Experiment 1

Stimuli and results from Yurowsky et al. (2017), Experiment 1

And you can create elegant, easy-to-read tables with minimal difficulty.

Condition Age Verbal Score Non-Verbal Score
Autism Spectrum 12;3 81 120
Age Control 12;4 123 130
Language Matched Control 10;2 81 100

2 You can also include R code!

I know that this might not seem very exciting right now, but believe me that it will be extremely useful in the future once you are comfortable using R. For example, imagine that you have analysed a data set and want to have a meeting with your supervisor to show her the results. If you write your analysis in R Markdown, you can take your supervisor through it step to step, ensuring that they understand what you have done, and that they catch any errors.

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
cars.correlation <- cor.test(cars$speed,cars$dist)
print(cars.correlation)
## 
##  Pearson's product-moment correlation
## 
## data:  cars$speed and cars$dist
## t = 9.464, df = 48, p-value = 1.49e-12
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6816422 0.8862036
## sample estimates:
##       cor 
## 0.8068949

2.1 You can also embed plots

plot(cars$speed,cars$dist)
abline(lm(cars$dist~cars$speed))

2.2 Including pretty plots

But you’ll need to make sure that your computer has downloaded the relevant R packages.

ggplot(cars, aes(x=speed,y=dist)) +
  geom_point()+
  geom_smooth()
## `geom_smooth()` using method = 'loess'

2.3  And finally, you can embed your R code in text

This is very useful for writing results sections: You can print the results of a statistical test in the text. For instance, above, we saw that there was a significant correlation r=0.81, t(48)= 9.46, p<.001.

The numbers in this section were generated using R code

... there was a significant correlation *r*=`r round(cars.correlation$estimate,2)`, *t*(`r cars.correlation$parameter`)= `r round(cars.correlation$statistic,2)`, *p*`r ifelse(cars.correlation$p.value<0.001,"<.001",paste("=",round(cars.correlation$p.value,2),sep=""))`.

3 Conclusions

I use R Markdown all the time, and find it not only helps me to understand my results in context, but helps me explain my results and ideas to others. You can see some further examples here, here, and here. Happy R Markdown use! It will serve you very well.

4 Instructions for this lab

4.1 Create a folder structure for this class and lab

On your remote drive, create a new directory for this class, and a sub-directory for this lab. You’ll probably want to create it in your Documents folder, > Classes > RM_DCS > Lab1

4.2  Download the R Markdown (RMD) file from Learn

Place it in your Lab1 directory.

4.3 Open R Studio

Navigate to your Lab1 directory (... button on the Files toolbar), and make it your Working Directory (More button on the toolbar).

4.4 Open the RMD file

By clicking on it. You’ll see that the document is a combination of strangely formatted text and R code. Take a quick read through and see if you can figure out what is going on. We’ll be learning all about this language next time.

4.5  Knit the file

Knitting translates the Markdown document into a filetype of your choice. You should see the Knit button up top; make sure to choose HTML as your output. Does the Knit succeed? Can you fiugre out why not from the error message? Where in the document is a mistake being made?

4.6 Download the needed Figure

You can download the Figure from Learn, and then you’ll need to put it in the Lab 1 directory, in its own folder called Figures. Try to knit the document now.