[R] grid with random or clustered distribution

Sarah Goslee sarah.goslee at gmail.com
Wed Sep 9 21:27:07 CEST 2015


########################################################
### simulate landscapes with spatial autocorrelation ###
###             Sarah Goslee 2015-09-09              ###
###     Goslee 2006 PLANT ECOLOGY 187(2):203-212     ###
########################################################

library(gstat)


## parameters
abun <- 0.2
dim1 <- 20
dim2 <- 50


## setup
xy <- expand.grid(seq_len(dim1), seq_len(dim2))
names(xy) <- c("x","y")


## three sample simulations

# no spatial autocorrelation
g.dummy <- gstat(formula = z~x+y, locations = ~x+y, dummy = TRUE, beta
= 0, model = vgm(1,"Nug", 0), nmax = 50)
sim <- predict(g.dummy, newdata = xy, nsim = 1)
random.landscape.000 <- predict(g.dummy, newdata = xy, nsim = 1)
random.landscape.000[,3] <- ifelse(random.landscape.000[,3] >
quantile(random.landscape.000[,3], abun), 0, 1)

# little spatial autocorrelation
g.dummy <- gstat(formula = z~x+y, locations = ~x+y, dummy = TRUE, beta
= 0, model = vgm(1,"Exp", 5), nmax = 50)
random.landscape.005 <- predict(g.dummy, newdata = xy, nsim = 1)
random.landscape.005[,3] <- ifelse(random.landscape.005[,3] >
quantile(random.landscape.005[,3], abun), 0, 1)

# much spatial autocorrelation
g.dummy <- gstat(formula = z~x+y, locations = ~x+y, dummy = TRUE, beta
= 0, model = vgm(1,"Exp", 250), nmax = 50)
sim <- predict(g.dummy, newdata = xy, nsim = 1)
random.landscape.250 <- predict(g.dummy, newdata = xy, nsim = 1)
random.landscape.250[,3] <- ifelse(random.landscape.250[,3] >
quantile(random.landscape.250[,3], abun), 0, 1)


# plot the simulated landscapes
par(mfrow=c(1,3))
image(random.landscape.000, main="Null", xaxt="n", yaxt="n", bty="n",
xlim=c(0,dim1), ylim=c(0, dim2), col=c("lightgray", "darkgray"))
image(random.landscape.005, main="5", xaxt="n", yaxt="n", bty="n",
xlim=c(0,dim1), ylim=c(0, dim2), col=c("lightgray", "blue"))
image(random.landscape.250, main="250", sub=paste("abun =", abun),
xaxt="n", yaxt="n", bty="n", xlim=c(0,dim1), ylim=c(0, dim2),
col=c("lightgray", "darkblue"))

########################################################
###                        end                       ###
########################################################

On Wed, Sep 9, 2015 at 9:27 AM, SH <emptican at gmail.com> wrote:
> Hi Sarah,
>
> Thanks for your prompt responding.  The methodology in the publication is
> very similar to what I plan to do.  Yes, could you be willing to share the
> code if you don't mind?
>
> Thanks a lot again,
>
> Steve
>
> On Wed, Sep 9, 2015 at 9:11 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>>
>> You can use gstat, as in:
>>
>> https://www.researchgate.net/publication/43279659_Behavior_of_Vegetation_Sampling_Methods_in_the_Presence_of_Spatial_Autocorrelation
>>
>> If you need more detail, I can dig up the code.
>>
>> Sarah
>>
>> On Wed, Sep 9, 2015 at 8:49 AM, SH <emptican at gmail.com> wrote:
>> > 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)
>> >
>>
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>
>



-- 
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org



More information about the R-help mailing list