Statistical Methods with Application to Finance

Introduction to R

Roberta Pappadà

April 1, 2026

What is R?

R is a free software environment for statistical analysis and graphics:

  • it is open source
  • it includes a wide set of tools for the statistical analysis of data
  • it is an object-oriented language
  • it can be easily extended by the user

R Studio is a open source integrated development environment for R

Installing Base-R and RStudio

-> Obtain R via the Comprehensive R Archive Network (CRAN):

-> RStudio:

www.rstudio.com/products/rstudio/download/

Instructions for software installation are available at

How to Install R on Windows, Mac OS X, and Ubuntu

Getting started

To open RStudio, simply double-click the RStudio icon on your desktop

The Four RStudio Windows:

  • Source
  • Console
  • Environment / History
  • Files / Plots / Packages / Help

Writing Code

R Console
  • Click inside the Console pane
  • After the prompt > type a command (for instance, 1 + 2)
  • Press Enter
Source file

collect the code into the R Script, then run selection with

  • Run button
  • Command + Enter on Mac
  • Control + Enter on Windows

  • '#' for comments

Command Line and Writing Shortcuts

Basic Data Types

  • character :

    "a"
    
  • numeric:

    2, 5.2
    
  • integer:

    2L
    
  • logical:

    TRUE,FALSE 
    
  • complex:

    2+4i
    

What kind of object is it?

class( ), typeof( ), mode( ), str( ), is.numeric( )

Basic Data Structures

  • Vector

  • Matrix

  • Array

  • List

  • Factor

  • Data frame

c( ), vector( ), seq( )

matrix( )

array()

list()

factor()

data.frame()

Some basic functions

Data visualization

  • categorical variables: pie( ), barplot( )
  • numerical variables: hist( ), boxplot( )

Use plot( ) to produce a scatter plot

  x<-c(0.5, 0.5, 2, 4, 6, 0.2, 5, 4.1)
  y<-c(0.5, 1.5, 3.2, 8, 6, 1.2, 3, 3.5)
  plot(x,y, pch=21, bg="red")
plot of chunk unnamed-chunk-1

plot of chunk unnamed-chunk-1

Installing R packages

R packages for basic functions are already available when R is installed.

Other packages can be obtained from CRAN and its mirrors. For example, to install the forecast package, start R and type

install.packages("quantmod")

In RStudio you can use “Install” from the Packages tab. Then type the command

library(quantmod)

You can also click the box next to the package name.

Online resources