Lecture 1 Introduction to R

40
Lecture 1 Introduction to R 732A44 Programming in R

description

Lecture 1 Introduction to R. Course structure. Self-studies of the course book 2 Lectures (1 in the beginning, 1 in the end) Labs (computer). Compulsory submission of reports-. One written final exam (computer) Course book: R Cookbook , by Paul Teetor - PowerPoint PPT Presentation

Transcript of Lecture 1 Introduction to R

Page 1: Lecture 1 Introduction  to R

732A44 Programming in R

Lecture 1Introduction to R

Page 2: Lecture 1 Introduction  to R

732A44 Programming in R

Self-studies of the course book

2 Lectures (1 in the beginning, 1 in the end)

Labs (computer). Compulsory submission of reports-.

One written final exam (computer)

Course book: R Cookbook, by Paul Teetor◦ knowledge of R at the advanced level:

The Art of R Programming by Norman Matloff.

Lab schedule: http://www.mattiasvillani.com/teaching/programming-in-r/

Course structure

Page 3: Lecture 1 Introduction  to R

732A44 Programming in R

R is an open source comprehensive statistical package, more and more used around the world.

R is a real programming language (compare to SAS or Minitab)

R is not low-level language as C or Visual Basic

Can be compared to Matlab by structure, by origin – free version of S

+ Has a lot of contributed packages covering a lot of statistical methods (also recent ones!)

- Documentation is badly structured

About R

Page 4: Lecture 1 Introduction  to R

732A44 Programming in R

Installing the R software

R project web site: http://www.r-project.org/

Find the mirror nearest to you when downloading

Page 5: Lecture 1 Introduction  to R

732A44 Programming in R

Page 6: Lecture 1 Introduction  to R

732A44 Programming in R

Start(All )ProgramRR 2.9.0

Page 7: Lecture 1 Introduction  to R

732A44 Programming in R

Important menu items

Page 8: Lecture 1 Introduction  to R

732A44 Programming in R

Important menu items

Page 9: Lecture 1 Introduction  to R

732A44 Programming in R

Important menu items

Page 10: Lecture 1 Introduction  to R

732A44 Programming in R

Notepad++: choose ”Language” ”R”

Other editors: Rstudio and Notepad++

Page 11: Lecture 1 Introduction  to R

732A44 Programming in R

Specific function◦ help(function)

Help browser◦ help.start()

Search for something in help◦ help.search(“expression”)

Quick reminder of function arguments:◦ args(function)

Examples of how to use function:◦ example(function)

If some method is not installed on the computer:◦ RSiteSearch(”expression")

Getting help

Page 12: Lecture 1 Introduction  to R

732A44 Programming in R

R is case-sensitive Separate commands by semicolon (;) Comments: #R is a very cool language!

Data assignment:

Use -> or <-a<-3;3->b;

Preliminaries

Page 13: Lecture 1 Introduction  to R

732A44 Programming in R

Assign a vector

Working with vectors

The function c() combines individual values (comma-spaced) to a vector

Printing the value on screen:

Either enter the variable or use the function print()

Note that the output begins with [1]. This is the row number, and in this case x is interpreted as a row vector

Page 14: Lecture 1 Introduction  to R

732A44 Programming in R

Listing and removing objectsListing defined objects (vectors, matrices, data frames):

Use the function ls() with no arguments

Removing objects:

Use the function rm()

Page 15: Lecture 1 Introduction  to R

732A44 Programming in R

Use either ‘: ‘ or seq()

Sequences

Page 16: Lecture 1 Introduction  to R

732A44 Programming in R

Important: In R, operations with vectors are performed element-by-element

Some operations: Element-wise: +-*/^ log exp sin cos sqrt length –number of elements sum - sum of all elements mean max min order

Logicals:TRUE or FALSE:a<-TRUE;

> >= < <= != & (and) | (or)

Operation with vectors

Page 17: Lecture 1 Introduction  to R

732A44 Programming in R

Use the function matrix()

a<-matrix(values,nrow=m,ncol=n)

values is a list of values enclosed in c(), i.e. a row vector or an already defined vector.

m is the number of rows and n is the number of columns of

the matrix. The number of values must be dividable by both m and n.

The values are entered column-wise.

The identifiers nrow= and ncol= can be omitted

Note the double indexing, first number for row and second number for column

Working with matrices

Page 18: Lecture 1 Introduction  to R

732A44 Programming in R

Positive integral indexingx[1,6] x[2:10]

Negative integral indexingx[-(1:5)] all except elements 1:5

Indexing entire row or columnx[2,] entire row 2

Finding elements satisfying specific condition:◦ x[x>mean(x)]

Indexing

Page 19: Lecture 1 Introduction  to R

732A44 Programming in R

Matrix operations

Page 20: Lecture 1 Introduction  to R

732A44 Programming in R

Matrix operators/functions:

transpose b=t(a)

b = aT

Inverse

b=solve(a)

b = a-1 (when needed)

Matrix operations

Page 21: Lecture 1 Introduction  to R

732A44 Programming in R

List is a collection of objects

Lists

Page 22: Lecture 1 Introduction  to R

732A44 Programming in R

Collecting vectors and matrices with the same number of rows in a data frameUse the function data.frame(object 1, object 2, … , object k)

Matrices need to be protected , otherwise each column of a matrix will be identified as a single object in the data frame.

Protection is made with the function I()

Data frames

Page 23: Lecture 1 Introduction  to R

732A44 Programming in R

Objects within a data frame can be called upon using the syntax

dataframe$object

Data frames

Page 24: Lecture 1 Introduction  to R

732A44 Programming in R

Vectors, lists, data frames can be entered in R:◦ edit(variable)

Example:

myframe<-data.frame();edit(myframe);

Entering or editing data

Page 25: Lecture 1 Introduction  to R

732A44 Programming in R

Names of objects within a data frame can be called, set or changed by handling the object

names()

Data frames

Page 26: Lecture 1 Introduction  to R

732A44 Programming in R

1. Save file as comma-separated file (csv)

2. Set current working directory:◦ setwd(directory)

3. Use read.csv2(filename)

Example:setwd("Z:/732A44");mydata<-read.csv2("Counties.csv");

Read from Excel file

Page 27: Lecture 1 Introduction  to R

732A44 Programming in R

if (expr) {…}else{…}

If you need to connect several conditions, use ’&’ , ’&&’, ’| ’ or ’||’

Conditional execution

Page 28: Lecture 1 Introduction  to R

732A44 Programming in R

for (name in expr1 ){…}

while (condition){…}

Loops

Page 29: Lecture 1 Introduction  to R

732A44 Programming in R

Function writing must always end with writing the value which should be returned!

You may also use ”return(value)” to show what value the function should return

Writing your own functions

Page 30: Lecture 1 Introduction  to R

732A44 Programming in R

If several arguments need to be returned, list may be used

Writing your own functions

Page 31: Lecture 1 Introduction  to R

732A44 Programming in R

Obligatory arguments and arguments by default Variables can be specified in any order when you call the

function

Writing your own functions

Page 32: Lecture 1 Introduction  to R

732A44 Programming in R

plot(x,..) plots time series

plot(x,y) scatter plot

plot(x,y) followed by points(x,y) plots several scatterplots in one coordinate system

hist(x,..) plots a hitogram

persp(x,y,z,…) creates surface plots

Graphical procedures

Page 33: Lecture 1 Introduction  to R

732A44 Programming in R

99% of all moderate-size codes contain mistakes in the first version Often not easy to find mistake debug

Way 1: debug a function step-by-step from the beginning: Use debug(function) Use undebug(function)

Example: Find a step when 1+2+4+8+… becomes equal to 500 (find mistake) !

Debugging

myfun<-function(x) { i<-1; r<-0; while (r!=x) { r<-r+i; i<-i*2; } return(i);}

debug(myfun);a<-myfun(500);#after some stepsundebug(myfun);

Page 34: Lecture 1 Introduction  to R

732A44 Programming in R

Things to do in debug mode

n or <Enter> : run next line c : continue until you exit from the loop or from the

function Any R command, for ex. find out the value of a variable where : prints stack trace Q : quit the debugger

Debugging

Page 35: Lecture 1 Introduction  to R

732A44 Programming in R

Way 2: insert browser() in the suspicious place in the code:

Debugging

myfun<-function(x) { i<-1; r<-0; while (r!=x) { r<-r+i; if(r>500) browser(); i<-i*2; } return(i);}

Page 36: Lecture 1 Introduction  to R

732A44 Programming in R

Solving a linear system of equations

Some examples

2

12

2

1

11

12

21

21

2

1

xx

xx

x

x

bxa

Page 37: Lecture 1 Introduction  to R

732A44 Programming in R

Some examples

nn

iiii

xx

xx

xx

nixxy

,2,1

2,22,1

1,21,1

,22,110

1

1

1

,,1;

X

Xy εβ

Regression model

Page 38: Lecture 1 Introduction  to R

732A44 Programming in R

Some examples

Use help(”lm”) to learn more about this function

Page 39: Lecture 1 Introduction  to R

732A44 Programming in R

Page 40: Lecture 1 Introduction  to R

732A44 Programming in R

Huge more to find out!

• Read course books

• Use PDF manuals

• Use the help function help(”function”) or ?function

• Use Google (search for “R: what you are looking for”)

Final comments