# Example for the use of the K2-index # developed under R-version 2.5.1 # Katja Schiffers, Frank M. Schurr, Katja Tielboerger, # Carsten Urbach, Kirk Moloney, and Florian Jeltsch library(spatstat) library(spatial) source("Funktionen/K2.R") # generating pattern with positive autocorrelation on scale 0.9 with intensity 0.5 # and negative autocorrelation on scale 0.6 with intensity 0.95 twoscalepp <- function(n, attsc=attsc, attint=attint, repsc=repsc, repint=repint) { loc <- matrix(NA, nrow=n, ncol=2) set <- FALSE loc[1,1]<- runif(1, min=0, max=20) loc[1,2]<- runif(1, min=0, max=20) N <- 1 att <- runif(1, min=0, max=1) rep <- runif(1, min=0, max=1) while(N < n) { x <- runif(1, min=0, max=20) y <- runif(1, min=0, max=20) for(i in 1:N){ if(set==FALSE){ dist <- sqrt(((loc[i,1]-x)^2)+((loc[i,2]-y)^2)) if((dist < attsc) && (att < attint)) set <- TRUE if((dist > attsc) && (att > attint)) set <- TRUE } } for(j in 1:N){ dist <- sqrt(((loc[j,1]-x)^2)+((loc[j,2]-y)^2)) if((dist < repsc) && (rep < repint)) set <- FALSE } if(set==TRUE){ att <- runif(1, min=0, max=1) rep <- runif(1, min=0, max=1) N <- N+1 loc[N,1] <- x loc[N,2] <- y cat(N, " ") if(N%%10==0) cat("\n") set <- FALSE } } pp <- ppp(loc[,1], loc[,2], window=owin(c(0,20), c(0,20))) return(pp) } two <- twoscalepp(150, attsc=0.9, attint=0.95, repsc=0.6, repint=0.75) plot(two, pch=20, cex=0.8, xlab="", ylab="") # analysing pattern with Ripley's K-function and plotting the L-function twoK <- envelope(two, fun=Kest) plot(twoK, sqrt(./pi)-r ~ r, xlab="scale(r)",ylab="L(r)", main="Ripley's K") # analysing pattern with pair correlation function with kernel width parameter 'stoyan' = 0.15 twoPCF <- envelope(two, fun=pcf.ppp, stoyan=0.15) plot(twoPCF, xlab="scale(r)", ylab="g(r)", main="Pair correlation function") # analysing pattern with K2-index with kernel width parameter 'stoyan' = 0.15 twoK2 <- K2(two, nsim=99, stoyan=0.15) plot(twoK2, main="K2-index", xlab="scale(r)", ylab="K2(r)")