R for Spatial Statistics

 

Generating Data

It's important to be able to generate data in a variety of ways for testing functions and for Monte Carlo methods.

Generating Curves

The code below will generate a normal curve. Note that this is not data from a normal distribution (that is in the next section).

First, we'll generate a vector containing a sequence of 200 numbers, equally spaced, from -4 to 4 and store it in x.

 x=seq(-4,4,length=200)

Then, we can compute a normal value for each x value along a normal curve.

 y=1/sqrt(2*pi)*exp(-x^2/2)

Now, we can plot x against y in a scatter plot. the "type" is a "line" with a "line width" of 2 and a "color" of red.

plot(x,y,type="l",lwd=2,col="red") 

Try this code and make sure it works in R. Now try some other functions and try some different options for the "plot()" function.

This example is taken from: http://msenux.redwoods.edu/math/R/normal.php

Generating Random Data

"runif()" will generate random numbers in a uniform (flat) distribution. Run the code below and then examine the vector "x" which will contain 100 values from 0 to 1.

x=runif(100) # get the random numbers

You can histogram the values to check their distribution with the "hist()" function.

 hist(x,probability=TRUE,col=gray(.9),main="Random Numbers from 0 to 1") 

Now, lets create a vector of 100 values from 0 to 100.

 y =seq(0,10,length=100)

We can multiple these vectors together to create a linearly increasing vector with some random noise.

z=x+y

Go ahead and plot the result against x.

plot(z,y)

Now, try other values for the random sequence and for the deterministic sequence.

Other Resources

The Uniform Distribution

The Normal Distribution