intro to r - umiacsusers.umiacs.umd.edu/.../lect20_rintro/rintro.pdf · •plotting: there are...

6
Intro to R Héctor Corrada Bravo CMSC858B Spring 2012 University of Maryland Computer Science http://www.nytimes.com/2009/01/07/technology/business-computing/07program.html?_r=2&pagewanted=1 http://www.forbes.com/forbes/2010/0524/opinions-software-norman-nie-spss-ideas-opinions.html http://www.theregister.co.uk/2010/05/06/revolution_commercial_r/ Some history John Chambers and others started developing the “S” language in 1976 Version 4 of the language definition (currently in use) was settled in 1998 That year, “S” won the ACM Software System Award Commercially available as “S-PLUS” Some history Ihaka and Gentleman (of NYTimes fame) create R in 1991 They wanted lexical scoping (see NYTimes pic) Released under GNU GPL in 1995 Maintained by R Core Group since1997

Upload: others

Post on 06-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to R - UMIACSusers.umiacs.umd.edu/.../lect20_Rintro/Rintro.pdf · •Plotting: there are three graphics system in R: • graphics: the base system (which we’ll use today)

Intro to RHéctor Corrada Bravo

CMSC858B Spring 2012University of Maryland

Computer Sciencehttp://www.nytimes.com/2009/01/07/technology/business-computing/07program.html?_r=2&pagewanted=1

http://www.forbes.com/forbes/2010/0524/opinions-software-norman-nie-spss-ideas-opinions.html

http://www.theregister.co.uk/2010/05/06/revolution_commercial_r/

Some history

• John Chambers and others started developing the “S” language in 1976

• Version 4 of the language definition(currently in use) was settled in 1998

• That year, “S” won the ACM Software System Award

• Commercially available as “S-PLUS”

Some history

• Ihaka and Gentleman (of NYTimes fame) create R in 1991

• They wanted lexical scoping (see NYTimes pic)

• Released under GNU GPL in 1995

• Maintained by R Core Group since1997

Page 2: Intro to R - UMIACSusers.umiacs.umd.edu/.../lect20_Rintro/Rintro.pdf · •Plotting: there are three graphics system in R: • graphics: the base system (which we’ll use today)

Languages used in Kaggle (prediction competition site)

Currently• Freely available: http://www.r-project.org/

• IDEs:

• [cross-platform] http://rstudio.org/

• [Windows and Linux] http://www.revolutionanalytics.com/

• Also bindings for emacs [http://ess.r-project.org/] and plugin for eclipse [http://www.walware.de/goto/statet]

• Resources:

• Manuals from r-project http://cran.r-project.org/manuals.html

• Chambers (2008) Software for Data Analysis, Springer.

• Venables & Ripley (2002) Modern Applied Statistics with S, Springer.

• List of books: http://www.r-project.org/doc/bib/R-books.html

• Programming language features:

• Conceptually, it is very similar to Scheme (functional, lexical scope, lists are basic data structure) with saner syntax.

• Syntax is nice for numerical computation (similar to matlab)

• Many language features there to support statistical modeling (formula syntax, data.frames)

• Objects (we’ll see that next time with Bioconductor)

• Fairly clean C interface (non-base package Rcpp provides awesome interface to C++)

• Interactive (REPL), but also scripting available

Page 3: Intro to R - UMIACSusers.umiacs.umd.edu/.../lect20_Rintro/Rintro.pdf · •Plotting: there are three graphics system in R: • graphics: the base system (which we’ll use today)

• Documentation system:

> help(“sapply”)# bring up help page

> ?sapply # shortcut

> ??sapply # search for string in docs

> help.start()# open doc index

• Uses a package framework (similar to Python)

• Divided into two parts

• base: what you get when you download R (base package, and other packages like stats, graphics, utils, Matrix, boot,codetools)

• everything else:

• [http://cran.r-project.org/]

@Yankees Follower Map

Asia 4Africa 1N. America 79S. America 7Australia/N.Z. 1Europe 17

Created by @simplystats twitterMap

• loading packages

> library(twitteR)

> require(twitteR)

• installing packages

> install.packages(“twitteR”)

Page 4: Intro to R - UMIACSusers.umiacs.umd.edu/.../lect20_Rintro/Rintro.pdf · •Plotting: there are three graphics system in R: • graphics: the base system (which we’ll use today)

Function definition

twitterMap <- function(userName,userLocation=NULL, #default valuefileName="twitterMap.pdf",nMax = 1000)

{<body>

}

Function call

twitterMap("Yankees", userLocation="New York", nMax=500,file="yanks.pdf")

• Lists are basic data structure (like scheme)

# creating a list (with names)

> list(users=followers, location=followersLocation)

# accessing named element

> tmp$users

# accessing element by index

> followers[[1]]

# slicing list

> followers[1:3]

Functional language

followersLocation = sapply(followers,location)

Equivalent (really bad idea in general)

followersLocation = c()for (i in 1:length(followers)) {followersLocation[i]=location(followers[[i]])}

vectors

# creatingvec = c(1,10,20)vec = 1:100vec = seq(1,100,by=2)vec = rnorm(100)

# indexingvec[1]vec[1:10]

# operationssum(vec)mean(vec)vec/10crossprod(vec)tcrossprod(vec)

Page 5: Intro to R - UMIACSusers.umiacs.umd.edu/.../lect20_Rintro/Rintro.pdf · •Plotting: there are three graphics system in R: • graphics: the base system (which we’ll use today)

Matrices

# creatingmat = matrix(c(1,10,20,30), nrow=2, ncol=2)mat = matrix(rnorm(100), nrow=20, ncol=5)

# indexingmat[1,1] # element in row 1 column 1mat[,1] # column 1

# operationssum(mat) # sum of all entriescolSums(mat) # column-wise sumapply(mat,2,sum) # same thing

rowMeans(vec)# row-wise means

# operations with vectors and scalarsmat/10 # divide all entries by scalar

vec = runif(20)mat/vec # divide each column by vec

vec = rnorm(5)sweep(mat,2,vec,”/”) # divide each row by vec

Data frames: a hybrid of matrix and list

# creating (looks like a named list)distDF=data.frame(dist=c(yanksLL[,3], natsLL[,3]),team=rep(c(“yanks”,”nats”),

c(nrow(yanksLL), nrow(natsLL)))

# accessing# like a listdistDF[[1]] # the first element (column)distDF$dist # a named element (column)names(distDF) # the names of elements (columns)

# like a matrixdistDF[1,1] # the first value in first columndistDF[,1] # the entire first column

Formula syntax: statistical modeling is part of the language

# a linear regression model# fit = lm (dist~team, data=distDF)

# which you can get information aboutsummary(fit)

formula

• Plotting: there are three graphics system in R:

• graphics: the base system (which we’ll use today)

• lattice: a very flexible system (uses statistical model syntax we’ll see later)

• ggplot2: very pretty, very extensible (grammar of graphics)

• R graph gallery: http://addictedtor.free.fr/graphiques/

Page 6: Intro to R - UMIACSusers.umiacs.umd.edu/.../lect20_Rintro/Rintro.pdf · •Plotting: there are three graphics system in R: • graphics: the base system (which we’ll use today)

Statistical tests available out of the box as objects you can compute on

# perform a KS test for differences in# distribution of distancestestRes=ks.test(log(subset(distDF,team=="yankees")$dist+1),

log(subset(distDF,team=="nationals")$dist+1), alternative="greater")

# print resulttestRes

# get value of test statistictestRes$stat

# get P-value for testtestRes$p.value