Lab1: Introduction to R (and More!)


Getting Started with R

Click on the R icon

If no R icon, go to Start and then All Programs to find R

To quit R:

> q()

What does it ask you? What does this mean?

For R HELP, for example to get help about the function rnorm ,

> help(rnorm)

For R HELP in html format

If you go to Help on the R toolbar and select R language (html), you will get the html version of help.


Example 1: Let the games begin.

Generate a sample of 100 N(0,1) random variables.

> help(rnorm)

> temp <- rnorm(100)

What is in the object temp?

What is the length of the oject temp?

Make a informative plot of temp?


Example 2: The Nonparametric Bootstrap

Let's get our own bootsrap function by:

> source("http://www.rohan.sdsu.edu/~babailey/bridges10/bootstrap.r") Here is the function: bootstrap.r

There is a help file available: bootstrap.help

Let's boostrap the mean of data. Let's make it simple: 1,2,3

> data <- c(1,2,3)

> results <- bootstrap(x=data,nboot=100,theta=mean)

Let's make a histogram of the 100 boostrap means:

> hist(results$thetastar)

How could you construct a CI?

> quantile(results$thetastar, c(0.05, 0.95))


Example 3: Trees

Here is: Information on the South African Heart Disease Data

Let's get the South African Heart Disease Data into R:

> sahd <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data", sep=",",head=T,row.names=1)

We can make a scatterplot matrix by:

> pairs(sahd)

Before we grow a gree we have to load the R package rpart .

Go to the toolbar under Packages select Load Packages and click on rpart from the list and load.

Let's look at the help function:

> help(rpart)

Let's grow a tree and look at the tree diagram:

> sahdtree <- rpart(as.factor(chd)~., data=sahd)
> plot(sahdtree)
> text(sahdtree)


Example 4: Random Forest

Before we grow a Random Forest we have to load the R package randomForest .

Let's look at the help function:

> help(randomForest)

Let's grow a Random Forest:

> sahdrf <- randomForest(as.factor(chd)~., data=sahd, importance=TRUE)
> print(sahdrf)
> varImpPlot(sahdrf, type=1)