Lab 1: Introduction to R and Visualizing Spatial Data


Getting Started with R

Click on the R icon

If no R icon, go to the C drive and the Programs folder to find R or search for 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?

If this were part of a homework problem, you might want to copy and paste the R code and plot into a Word Document?
Try it!

Note: To copy and paste from the graphics window,
Right click on the mouse and select Copy as metafile, then Paste.


Example 2: Coal Ash Data

We will do some EDA for Coal Ash Data. To find out more out the dataset: coal.ash.help

Let's look at the data: coal.ash.txt

Let's read the data into a R dataset:

> coal.ash <- read.table("http://www.rohan.sdsu.edu./~babailey/stat696/coal.ash.txt", header=T)

Let's look at sample code in intro1.r

To make the plots copy and past the code into R OR just source the code:

> source("http://www.rohan.sdsu.edu/~babailey/stat696/intro1.r")

What other plots would be informative?

Before we can make perspective, contour, and image plots we will need to load the R package akima .

Go to the toolbar under Packages select Load Packages and click on akima from the list and load. (we need an interp function!)

Let's look at sample code in intro2.r

To make the plots copy and past the code into R OR just source the code:

> source(""http://www.rohan.sdsu.edu/~babailey/stat696/intro2.r")

Let's look at sample code in intro3.r

To make the plots copy and past the code into R OR just source the code:

> source(""http://www.rohan.sdsu.edu/~babailey/stat696/intro3.r")

One way to look for spatial trends across rows and columns is to generate boxplots:

> boxplot(coal~y, data=coal.ash, xlab="y", ylab="Coal Ash%")

> boxplot(coal~x, data=coal.ash, xlab="x", ylab="Coal Ash%")

Let's look at sample code in intro4.s

To make the plots copy and past the code into R OR just source the code:

> source(""http://www.rohan.sdsu.edu/~babailey/stat696/intro4.r")


Example 3: Linear Models in R

The mean function m(s) in the Geostatistical model:

Z(s) = m(s) + e(s) , s in D

Here:

Large-scale variation: m(s) = E(Z(s))
Small-scale variagion: {e(s)} is a zero-mean random field associatied with C

The mean (or trend) function maybe a function of the location or of some list of covariates. It may be a linear or nonlinear function of some regression parameters, or more generally be modeled nonparametrically (ex. median polish).

If m(s,b) is a linear or nonlinear function of b, then least squares can be used to fit the data. Sometimes called trend surface analysis.

For a linear trend surface, we can use the lm function:

> help(lm)

> coal.fit1 <- lm(coal ~ x + y, data=coal.ash)

There are summary and diagnostic plots for the lm function:

> summary(coal.fit1)

> par(mfrow=c(2,2))
> plot(coal.fit1)

How well does the model fit the data?


Intro to R:

Try going through the following Intro. Lessons. The # sign is a comment, so you can copy and paste.

  • R vectors, lists and functions
  • R arithmetic and logical operators
  • R simple functions
  • R univariate random variable generators