[R] grid with random or clustered distribution

SH emptican at gmail.com
Wed Sep 9 14:49:48 CEST 2015


Hi R-users,

I hope this is not redundant questions.  I tried to search similar threads
relevant to my questions but could not find.  Any input would be greatly
appreciated.

I want to generate grid with binary values (1 or 0) in n1 by n2 (e.g., 100
by 100 or 200 by 500, etc.) given proportions of 1 and 0 values (e.g., 1,
5, or 10% of 1 from 100 by 100 grid).  For clustered distributed grid, I
hope to be able to define cluster size if possible.  Is there a simple way
to generate random/clustered grids with 1 and 0 values with a
pre-defined proportion?

So far, the function "EVariogram" in the "CompRandFld" package generates
clustered grid with 1 and 0.  Especially, the example #4 in the
"EVariogram" function description is a kind of what I want. Below is the
slightly modified code from the original one.  However, the code below
can't control proportion of 1 and 0 values and complicated or I have no
idea how to do it.  I believe there may be easies ways to
generate random/clustered grids with proportional 1 and 0 values.

Thank you very much in advance,

Steve


library(CompRandFld)
library(RandomFields)

x0 <- seq(1, 50, length.out=50)
y0 <- seq(1, 60, length.out=60)
d <- expand.grid(x=x0, y=y0)
dim(d)
head(d)
x <- d$x
y <- d$y
# Set the model's parameters:
corrmodel <- 'exponential'
mean <- 0
sill <- 1
nugget <- 0
scale <- 3
set.seed(1221)
# Simulation of the Binary-Gaussian random field:
data <- RFsim(x, y, corrmodel="exponential", model="BinaryGauss",
              param=list(mean=mean,sill=sill,scale=scale,nugget=nugget),
              threshold=0)$data
# Empirical lorelogram estimation:
fit <- EVariogram(data, x, y, numbins=20, maxdist=7, type="lorelogram")
# Results:
plot(fit$centers, fit$variograms, xlab='Distance', ylab="Lorelogram",
     ylim=c(min(fit$variograms), max(fit$variograms)),
     xlim=c(0, max(fit$centers)), pch=20, main="Spatial Lorelogram")
# Plotting
plot(d, type='n')
text(d, label=data)

	[[alternative HTML version deleted]]



More information about the R-help mailing list