732A44 Programming in R. Self-studies of the course book 2 Lectures (1 in the beginning, 1 in the...
-
Upload
june-jefferson -
Category
Documents
-
view
214 -
download
0
Transcript of 732A44 Programming in R. Self-studies of the course book 2 Lectures (1 in the beginning, 1 in the...
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
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
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
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
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
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
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()
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
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
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
732A44 Programming in R
Matrix operators/functions:
transpose b=t(a)
b = aT
Inverse
b=solve(a)
b = a-1 (when needed)
Matrix operations
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
732A44 Programming in R
Objects within a data frame can be called upon using the syntax
dataframe$object
Data frames
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
732A44 Programming in R
Names of objects within a data frame can be called, set or changed by handling the object
names()
Data frames
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
732A44 Programming in R
if (expr) {…}else{…}
If you need to connect several conditions, use ’&’ , ’&&’, ’| ’ or ’||’
Conditional execution
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
732A44 Programming in R
If several arguments need to be returned, list may be used
Writing your own functions
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
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
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);
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
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);}
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
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