r statistical language: introduction and exercises · 2017-07-11 · r statistical language:...

17
R-project R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit¨ at Bonn WMO Verification Tutorial Berlin, May 2017 Sabrina Wahl R Introduction 1 / 16

Upload: others

Post on 10-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-project

R Statistical Language:

Introduction and Exercises

Sabrina Wahl

Meteorologisches Institut, Universitat Bonn

WMO Verification Tutorial

Berlin, May 2017

Sabrina Wahl R Introduction 1 / 16

Page 2: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

R-Project

I R is a language and environment for statistical computing and

graphics

I R provides a wide variety of statistical techniques (linear and

nonlinear modelling, classical statistical tests, time-series analysis,

classification, clustering, . . . ) and is highly extensible

I R provides a wide variety of graphical techniques to produce

well-designed publication-quality plots

I R is available as Free Software and runs on most operating systems

(Linux, Windows and MacOS)

I R is Open Source

Sabrina Wahl R Introduction 2 / 16

Page 3: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

https://www.r-project.org

Sabrina Wahl R Introduction 3 / 16

Page 4: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

R Manuals

Sabrina Wahl R Introduction 4 / 16

Page 5: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

R Contributions

Sabrina Wahl R Introduction 5 / 16

Page 6: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

R Packages

I R is a powerful high-level languages doing statistical analysis

I R has has built-in functions and contributed packages that can do

most modern statistical methods

I Currently more than 10.000 contributed packages available

I R packages are open source, have manuals and references

I Contributions from people around the world

Sabrina Wahl R Introduction 6 / 16

Page 7: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

R Packages

Sabrina Wahl R Introduction 7 / 16

Page 8: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Sample of useful R Packages

I verification, SpatialVx

I fields, maps, mapdata, spatstat

I Packages must be installed to call:

> install.packages("verification")

I Packages must be called to use:

> library("verification")

I Citing Packages: > citation("verification")

I Citing R Project: > citation()

Sabrina Wahl R Introduction 8 / 16

Page 9: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

https://cran.r-project.org/package=verification

Sabrina Wahl R Introduction 9 / 16

Page 10: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Getting help

I Manuals, Contributed documentations, Package manuals

I Help page within R (e.g. for function ”verify”):

> ?verify

> help(verify)

I Arguments

I Values

I Examples

I References

I search engines, mailing lists, ...

Sabrina Wahl R Introduction 10 / 16

Page 11: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Writing R scripts

I R script files ending with ”.r”

I R data files ending with ”.rdata”(created by > save())

I Use a text editor to write R script files (e.g. Kate)

I Use hashtag # for comments

I Use console/terminal to execute scripts from R:

> source("file.r")

> load("data.Rdata")

Sabrina Wahl R Introduction 11 / 16

Page 12: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Working directory

I Create working directory, e.g. from terminal:

home$: mkdir R-tutorial

home$: cd R-tutorial

I Start R from working directory:

home/R-tutorial$: R

I Save data files and R scripts in the working directory

Useful commands:

I get working directory: > getwd()

I change working directory: > setwd("path.to.directory")

I quit R: > q()

Sabrina Wahl R Introduction 12 / 16

Page 13: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Start with Excercises

I Download zip file: 2017-R-tutorial.zip

I Start R (from directory with zip file)

I Unzip file and create directory:

> unzip("2017-R-tutorial.zip")

I Set working directory:

> setwd("2017-R-tutorial")

I List of files in directory:

> dir()

Sabrina Wahl R Introduction 13 / 16

Page 14: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Use R as calculator

I arithmetic operations

> 2 + 3

> 2 ˆ 2 + (5-1)*4 - 3/2

I assign operator ’<-’

> a <- 3/2

> b <- a + a*2 - 3

To Do: Compute the difference between yy <- 2017 and the year

ystart you started at the university and divide this by the difference

between yy and the year you were born yborn. Multiply this with 100 to

get the percentage of your life you have spent at the university.

> p <- 100*(yy-ystart)/(yy-yborn)

Sabrina Wahl R Introduction 14 / 16

Page 15: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Use R as calculator

I arithmetic operations

> 2 + 3

> 2 ˆ 2 + (5-1)*4 - 3/2

I assign operator ’<-’

> a <- 3/2

> b <- a + a*2 - 3

To Do: Compute the difference between yy <- 2017 and the year

ystart you started at the university and divide this by the difference

between yy and the year you were born yborn. Multiply this with 100 to

get the percentage of your life you have spent at the university.

> p <- 100*(yy-ystart)/(yy-yborn)

Sabrina Wahl R Introduction 14 / 16

Page 16: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Clean up your working space

I list of names of objects in working space:

> ls()

I remove object ’x’:

> rm(x)

I clear working space (remove all objects):

> rm(list=ls())

Sabrina Wahl R Introduction 15 / 16

Page 17: R Statistical Language: Introduction and Exercises · 2017-07-11 · R Statistical Language: Introduction and Exercises Sabrina Wahl Meteorologisches Institut, Universit at Bonn WMO

R-projectR-packagesR-helpR-script

Open example scripts in Editor

I example1.r (vectors, simple plot)

I example2.r (arrays, image plot)

I example3.r (time series)

Have you already installed the packages? Then call

> library("verification")

If package is not installed yet, type

> install.packages("verification")

Sabrina Wahl R Introduction 16 / 16