400 likes | 514 Views
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
E N D
Lecture1Introduction to R 732A44 Programming in 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 • 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/ 732A44 Programming in R
About 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 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
Start(All )ProgramRR 2.9.0 732A44 Programming in R
Important menu items 732A44 Programming in R
Important menu items 732A44 Programming in R
Important menu items 732A44 Programming in R
Other editors: Rstudio and Notepad++ Notepad++: choose ”Language” ”R” 732A44 Programming in R
Getting help • Specific function • help(function) • Help browser • help.start() • Search for something in help • help.search(“expression”) • Quick reminderoffunction arguments: • args(function) • Examples ofhowtousefunction: • example(function) • If somemethod is not installed on the computer: • RSiteSearch(”expression") 732A44 Programming in R
Preliminaries • R is case-sensitive • Separate commands by semicolon (;) • Comments: #R is a very cool language! Data assignment: Use -> or <- a<-3; 3->b; 732A44 Programming in R
Working with vectors • Assign a vector 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 objects Listing defined objects (vectors, matrices, data frames): Use the function ls() with no arguments Removing objects: Use the function rm() 732A44 Programming in R
Sequences • Use either ‘: ‘ or seq() 732A44 Programming in R
Operation with vectors Important: In R, operations with vectors are performed element-by-element Some operations: • Element-wise: +-*/^ • log exp sin cossqrt • length –number of elements • sum - sum of all elements • mean • max min order Logicals: TRUE or FALSE: a<-TRUE; > >= < <= != & (and) | (or) 732A44 Programming in R
Working with matrices 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 732A44 Programming in R
Indexing • Positive integral indexing x[1,6] x[2:10] • Negative integral indexing x[-(1:5)] all except elements 1:5 • Indexing entire row or column x[2,] entire row 2 • Finding elements satisfying specific condition: • x[x>mean(x)] 732A44 Programming in R
Matrix operations 732A44 Programming in R
Matrix operations • Matrix operators/functions: • transpose b=t(a) b = aT • Inverse b=solve(a) b = a-1 (when needed) 732A44 Programming in R
Lists • List is a collection of objects 732A44 Programming in R
Data frames Collecting vectors and matrices with the same number of rows in a data frame Use 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() 732A44 Programming in R
Data frames Objects within a data frame can be called upon using the syntax dataframe$object 732A44 Programming in R
Entering or editing data • Vectors, lists, data framescan be entered in R: • edit(variable) • Example: myframe<-data.frame(); edit(myframe); 732A44 Programming in R
Data frames Names of objects within a data frame can be called, set or changed by handling the object names() 732A44 Programming in R
Read from Excel file • Save file as comma-separatedfile (csv) • Set currentworking directory: • setwd(directory) • Useread.csv2(filename) Example: setwd("Z:/732A44"); mydata<-read.csv2("Counties.csv"); 732A44 Programming in R
Conditionalexecution if (expr) { … } else{ … } If you need to connectseveralconditions, use ’&’ , ’&&’, ’| ’ or ’||’ 732A44 Programming in R
Loops for (name in expr1 ) { … } while (condition) { … } 732A44 Programming in R
Writing your ownfunctions • Functionwriting must alwaysend with writing the valuewhichshould be returned! • You mayalsouse ”return(value)” to show whatvalue the functionshouldreturn 732A44 Programming in R
Writing your ownfunctions • Ifseveral arguments need to be returned, list may be used 732A44 Programming in R
Writing your ownfunctions • Obligatory arguments and arguments by default • Variables can be specified in any order when you call the function 732A44 Programming in R
Graphicalprocedures • plot(x,..) plots time series • plot(x,y) scatterplot • plot(x,y) followed by points(x,y) plotsseveralscatterplots in onecoordinate system • hist(x,..) plots a hitogram • persp(x,y,z,…) createssurfaceplots 732A44 Programming in R
Debugging • 99% of all moderate-sizecodescontainmistakes in the first version • Often not easytofindmistake debug Way 1: debug a function step-by-step from the beginning: • Usedebug(function) • Useundebug(function) Example: Find a step when 1+2+4+8+… becomesequalto 500(findmistake) ! 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 steps undebug(myfun); 732A44 Programming in R
Debugging Thingsto do in debug mode • n or <Enter> : runnextline • c: continueuntilyou exit from the loop or from the function • Any R command, for ex. findout the valueof a variable • where : prints stack trace • Q : quit the debugger 732A44 Programming in R
Debugging Way 2: insertbrowser() in the suspiciousplace in the code: 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
Someexamples • Solving a linear system of equations 732A44 Programming in R
Someexamples Regression model 732A44 Programming in R
Someexamples Use help(”lm”) to learn more about this function 732A44 Programming in R
Final comments 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”) 732A44 Programming in R