### Introducing Monte Carlo Methods with R [book]

Christian Robert, George Casella
2010
Monte Carlo Methods with R: Basic R Programming  Chapter 1: Basic R Programming "You're missing the big picture," he told her. "A good album should be more than the sum of its parts." Ian Rankin Exit Music This Chapter ◮ We introduce the programming language R ◮ Input and output, data structures, and basic programming commands ◮ The material is both crucial and unavoidably sketchy Monte Carlo Methods with R: Basic R Programming  Basic R Programming Introduction ◮ This is a quick
more » ... s a quick introduction to R ◮ There are entire books devoted to R ⊲ R Reference Card ⊲ available at http://cran.r-project.org/doc/contrib/Short-refcard.pdf ◮ Take Heart! ⊲ The syntax of R is simple and logical ⊲ The best, and in a sense the only, way to learn R is through trial-and-error ◮ Embedded help commands help() and help.search() ⊲ help.start() opens a Web browser linked to the local manual pages Monte Carlo Methods with R: Basic R Programming  Basic R Programming Why R ? ◮ There exist other languages, most (all?) of them faster than R, like Matlab, and even free, like C or Python. ◮ The language combines a sufficiently high power (for an interpreted language) with a very clear syntax both for statistical computation and graphics. ◮ R is a flexible language that is object-oriented and thus allows the manipulation of complex data structures in a condensed and efficient manner. ◮ Its graphical abilities are also remarkable ⊲ Possible interfacing with L A T E Xusing the package Sweave. Monte Carlo Methods with R: Basic R Programming  Basic R Programming Why R ? ◮ R offers the additional advantages of being a free and open-source system ⊲ There is even an R newsletter, R-News ⊲ Numerous (free) Web-based tutorials and user's manuals ◮ It runs on all platforms: Mac, Windows, Linux and Unix ◮ R provides a powerful interface ⊲ Can integrate programs written in other languages ⊲ Such as C, C++, Fortran, Perl, Python, and Java. ◮ It is increasingly common to see people who develop new methodology simultaneously producing an R package ◮ Can interface with WinBugs Monte Carlo Methods with R: Basic R Programming  Basic R Programming Getting started ◮ Type 'demo()' for some demos; demo(image) and demo(graphics) ◮ 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. ◮ Type 'q()' to quit R. ◮ Additional packages can be loaded via the library command, as in > library(combinat) # combinatorics utilities > library(datasets) # The R Datasets Package ⊲ There exist hundreds of packages available on the Web. > install.package("mcsm") ◮ A library call is required each time R is launched Monte Carlo Methods with R: Basic R Programming  Basic R Programming R objects ◮ R distinguishes between several types of objects ⊲ scalar, vector, matrix, time series, data frames, functions, or graphics. ⊲ An R object is mostly characterized by a mode ⊲ The different modes are -null (empty object), -logical (TRUE or FALSE), -numeric (such as 3, 0.14159, or 2+sqrt (3) ), -complex, (such as 3-2i or complex(1,4,-2)), and -character (such as "Blue", "binomial", "male", or "y=a+bx"), ◮ The R function str applied to any R object will show its structure. Monte Carlo Methods with R: Basic R Programming  Basic R Programming Interpreted ◮ R operates on those types as a regular function would operate on a scalar ◮ R is interpreted ⇒ Slow ◮ Avoid loops in favor of matrix mainpulations Monte Carlo Methods with R: Basic R Programming  Basic R Programming -The vector class > a=c(5,5.6,1,4,-5) build the object a containing a numeric vector of dimension 5 with elements 5, 5.6, 1, 4, -5 > a display the first element of a > b=a[2:4] build the numeric vector b of dimension 3 with elements 5.6, 1, 4 > d=a[c(1,3,5)] build the numeric vector d of dimension 3 with elements 5, 1, -5 > 2*a multiply each element of a by 2 and display the result > b%%3 provides each element of b modulo 3 Monte Carlo Methods with R: Basic R Programming  Basic R Programming More vector class > e=3/d build the numeric vector e of dimension 3 and elements 3/5, 3, -3/5 > log(d*e) multiply the vectors d and e term by term and transform each term into its natural logarithm > sum(d) calculate the sum of d > length(d) display the length of d Monte Carlo Methods with R: Basic R Programming  Basic R Programming Even more vector class > t(d) transpose d, the result is a row vector > t(d)*e elementwise product between two vectors with identical lengths > t(d)%*%e matrix product between two vectors with identical lengths > g=c(sqrt(2),log(10)) build the numeric vector g of dimension 2 and elements √ 2, log(10) > e[d==5] build the subvector of e that contains the components e[i] such that d[i]=5 > a[-3] create the subvector of a that contains all components of a but the third. > is.vector(d) display the logical expression TRUE if a vector and FALSE else Monte Carlo Methods with R: Basic R Programming  Basic R Programming Comments on the vector class ◮ The ability to apply scalar functions to vectors: Major Advantage of R. ⊲ > lgamma(c(3,5,7)) ⊲ returns the vector with components (log Γ(3), log Γ(5), log Γ (7) ). ◮ Functions that are specially designed for vectors include sample, permn, order,sort, and rank ⊲ All manipulate the order in which the components of the vector occur. ⊲ permn is part of the combinat library ◮ The components of a vector can also be identified by names. ⊲ For a vector x, names(x) is a vector of characters of the same length as x Monte Carlo Methods with R: Basic R Programming  Basic R Programming The matrix, array, and factor classes ◮ The matrix class provides the R representation of matrices. ◮ A typical entry is > x=matrix (vec,nrow=n,ncol=p) ⊲ Creates an n × p matrix whose elements are of the dimension np vector vec ◮ Some manipulations on matrices ⊲ The standard matrix product is denoted by %*%, ⊲ while * represents the term-by-term product. ⊲ diag gives the vector of the diagonal elements of a matrix ⊲ crossprod replaces the product t(x)%*%y on either vectors or matrices ⊲ crossprod(x,y) more efficient ⊲ apply is easy to use for functions operating on matrices by row or column Monte Carlo Methods with R: Basic R Programming