Generating sets of independent random variables

These routines are easily used in R to generate a file of independent random parameter sets for use as an input to a model. One way of doing this is to use the data.frame construct to produce a file with each parameter set as a row in the data.frame. Thus to generate 250 samples of 3 parameters (Szm, Ksat, Chv) from normal, lognormal and uniform distributions respectively, the following script will produce a data.frame called params

num <- 250;

Szm <- rnorm(num, 0.025,0.005);

Ksat <- rlnorm(num,1,0.5);

Chv <- runif(num, 1000,3000);

params <- data.frame(Szm,Ksat,Chv);

write.table(params, file=”filename”,quote=FALSE);

Alternatively, the file may be written in comma separated variable format (.csv) for easy input into, for example, Excel, by using write.csv.

A table can be read back into R by using the functions

params <- read.table(file=”filename”)

or

params <- read.csv(file=”filename”)

Information on other parameters for the read.table / write.table or other functions can be obtained by using the R help system.

Plotting different distributions.

The following routines written in R allow the user to plot any number of random samples from selected distributions (see Figure 3.2 in Environmental Modelling: An Uncertain Future?). The mean and standard deviations of samples are plotted, together with the sample PDF and CDF, are plotted.

xuniform (num, min, max)

xnormal (num, mean, sd)

xlognorm(num, min, logmean, logsd)

xexp(num, min, rate)

xgamma(num, min, shape, rate)

xbeta (num, min, max, shape1, shape2)

xtriangle (num, min, peak, max)

xtrapezoid (num, min, peak1, peak2, max)

These can be used to explore the effects of changing sample size (num), distribution function, and distribution parameters (min, max, mean, sd,…..etc).