R365: Day 37 – Random numbers

One of my friends is working on stochastic and deterministic modeling, so I thought that I would go a bit into stochastic modeling for the next few posts.

What is stochastic/chaotic/deterministic? Stochastic means truly random, where the events at one time point do not affect events in the future. There are lots of ways to generate random numbers, including some really awesome books, or using atmospheric noise. Chaotic systems are not random! Chaotic systems are deterministic, but you need to know the initial conditions to make accurate predictions. Weather is considered a chaotic system. Deterministic models means that the model progresses in a predictable fashion. That does not necessarily mean linear, but these models tend to be stable and (over a very long time period) tend to come to some sort of equilibrium, even if that means oscillating into infinity.

I first ran across these terms while taking a population biology class. We would analyze populations of endangered species and determine if their populations were likely to be deterministic (which in that context meant doomed to extinction). (bummer). After entering grad school I took a class on dynamic models in biology using this book as a textbook and working our way through life tables, matrices, ODEs and attractors.

The heart of any stochastic modeling is the ability to generate randomness. There are lots of different ways to create random numbers, but some of the simplest are the use of runif() or sample(). Both of them will give you a random number from a uniform distribution. If you are interested in another distribution, there are lots of probability distributions, one of them might suit you better. For instance, if you just want to generate a coin tossing random integer, you can use

rbinom(100,1,prob=0.5)

Which will generate 100 random digits of either 0 or 1. If you are interested in an uneven split, you can easily adjust the prob= portion to suit your needs. If what you need is an actual number, then sample() is a good tool. Sample() can be used with or without replacement, so that if you wanted to sample something from a group only once, you could. Sample() also lets you sample from either a vector or a positive integer. So if you had a vector of things like names (like a list of states), then you could sample randomly from those as well. For instance,

a=sample(state.name, 10, replace=FALSE)
 [1] "New Mexico" "Nevada" "Colorado" "Nebraska" "Hawaii" "California" "Tennessee" "Delaware" 
 [9] "West Virginia" "Connecticut"

or you could combine this with {maps} to create

Rplot08

a=sample(state.name, 10, replace=FALSE)
map('state', interior = FALSE)
map('state', boundary = FALSE, lty = 5, add = TRUE)
map('state',region=a,fill=TRUE,col=heat.colors(10),add=TRUE)

 This post was pretty useful for looking into random numbers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s