If not already, R is well on its way to becoming the lingua franca of statistical analysis. It is open source, free, and extraordinarily powerful. Most importantly, more and more are being contributed to core . As of today there are at least 21,365 packages that add to the functionality of R. More packages are added daily. R is a data analysis system that is both open source and is also extensible. Open source means that the actual computer code behind all operations is available to anyone to examine and to reuse, within the constraints of the GPL 2.0. It is free software in the meaning of free speech in that everyone can use it, everyone can examine the code, everyone can distribute it, and everyone can add to it. Psychologists around the world are learning to take advantage of R for their research and this course will allow you to do so as well.

These notes and homework assignments are part of a course Psychology 350: Special Topics: An introduction to R for psychological research at Northwestern University.

The syllabus for this course is available at http://personality-project.org/courses/350.syllabus.pdf. Lecture notes and readings are available there.

Homework for week 1

Although R can be used from several different interfaces, a recommended one is to use RStudio. Thus, the homework for the first week will be to download R and RStudio and practice using Markdown and some very baby steps in R.

Instructions for downloading R and RStudio

First, download R

R is hosted on mirror servers around the world. One of the most convenient ones is probably the one hosted by Rstudio in (on) the cloud:

https://cloud.r-project.org

(note that the RStudio web site is now called Posit.)

Select your operating system, and then go to the download page.

Select the most recent release (currently R-4.3.3) and click to install. Thats it!

Then, download RStudio (desktop)

Go to Rstudio.com https://www.rstudio.com/products/rstudio/download/ and select your installer (e.g. Rstudio Version 2023.12.1+402 )

Now open Rstudio and start using it

RStudio and RMarkdown

RStudio is not R, but rather an interface to ‘R’ but it is a convenient way to write up notes (and eventually run your R code). The first example actually does run a small R function and shows the output.

Choose new file from the file menu

Select R markdown as your file

The default R Markdown script

When you ask for a new file, of type Markdown, it will look like this:

https://personality-project.org/courses/350/first.Rmd

Which can rendered into an HTML or PDF document by choosing the `Knit” option.

https://personality-project.org/courses/350/first.html.

This is all very nice, but we actually want to do something useful. So we modify that script.

We can take the default script and modify it, or completely erase it and start with a brand new window where we paste what we want.

A new window will open which will look like this https://personality-project.org/courses/350/350.homework-1.Rmd which produces output which looks like this: https://personality-project.org/courses/350/350.homework-1.html

(It is possible that you need to copy this file from the server and paste into R Markdown. I am still playing with this and we will work through this in class. )

Select the Knit button and you have just run your first R code, as well as making your first R markdown example.

Lets make it more powerful: Install the psych package

You can do this from R studio by selecting the Install Packages and then entering psych to be installed. Install psychTools as well.

This should be version 2.4.3

Or you can install it using the install.packages command

Now, change your markdown script file to do something interesting

We want to

make psych active (using library)

make psychTools active (using library)

run a simple function (describe)

Show some nice output (pairs.panels)

Here is the code to do that

library(psych)
library(psychTools)
sessionInfo()  #to see what packages are active
## R version 4.3.3 (2024-02-29)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Sonoma 14.2.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: America/Chicago
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] psychTools_2.4.3 psych_2.4.3     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.31     R6_2.5.1          fastmap_1.1.1     xfun_0.41         lattice_0.22-5   
##  [6] cachem_1.0.7      parallel_4.3.3    knitr_1.45        foreign_0.8-86    htmltools_0.5.5  
## [11] rmarkdown_2.21    cli_3.6.1         R.methodsS3_1.8.2 grid_4.3.3        sass_0.4.5       
## [16] jquerylib_0.1.4   mnormt_2.1.1      compiler_4.3.3    R.oo_1.25.0       rstudioapi_0.14  
## [21] tools_4.3.3       rtf_0.4-14.1      nlme_3.1-164      evaluate_0.20     bslib_0.4.2      
## [26] yaml_2.3.7        rlang_1.1.0       jsonlite_1.8.4
describe(iris)  # a built in data set from Fisher
##              vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## Sepal.Length    1 150 5.84 0.83   5.80    5.81 1.04 4.3 7.9   3.6  0.31    -0.61 0.07
## Sepal.Width     2 150 3.06 0.44   3.00    3.04 0.44 2.0 4.4   2.4  0.31     0.14 0.04
## Petal.Length    3 150 3.76 1.77   4.35    3.76 1.85 1.0 6.9   5.9 -0.27    -1.42 0.14
## Petal.Width     4 150 1.20 0.76   1.30    1.18 1.04 0.1 2.5   2.4 -0.10    -1.36 0.06
## Species*        5 150 2.00 0.82   2.00    2.00 1.48 1.0 3.0   2.0  0.00    -1.52 0.07
pairs.panels(iris) #a graphic display