###################################################################### # heterogenous_poissonpp.txt # # # Deeper examination of inhomogenous poisson pp # Requires spatstat package and splines package # ###################################################################### par (mfrow=c(1,1)) library(spatstat) library(splines) ###################################################################### # First, we do example of linear increasing mean in x and y ###################################################################### #underlying function func=function(x,y) { (x+y)/20 } #generate grid locations x=c(1:20) y=c(1:20) z=matrix(0,20,20) for (i in 1:20){ for (j in 1:20) { z[i,j]=func(i,j) } } #view true lambda function par(mfrow=c(2,2)) image(x,y,z, main="True Underlying Lambda Function") persp(x,y,z,main="True Underlying Lambda Function") #generate random points linearpp=rpoispp(func,win=owin(c(0,20),c(0,20))) plot(linearpp, cols="blue", pch=19,main="Randomly Generated Data from Lambda Function") plot(density(linearpp),main="Kernel Smoothed Estimate of Lambda Function") ### Fit Flat CSR Model and look at residuals and model fit model1=ppm(linearpp,~1) print.ppm(model1) #Get model diagnostics values residuals(model1) par(mfrow=c(1,1)) diagnose.ppm(model1, cols="red", pch=19) #Chi-Square test of goodness of fit CHSQ=quadrat.test(model1,nx=5,ny=5) CHSQ plot(linearpp, cols="blue",pch=19, main="Chi-Square Test Assuming CSR") plot(CHSQ, add=TRUE, col="red") #Kolmogorov-Smirnov Test test1=cdf.test(model1, "x") plot(test1, lwd=3, cex=.8) test1=cdf.test(model1, "y") plot(test1) ### Now Fit Model in X and Y and look at residuals and model fit model2=ppm(linearpp,~polynom(x,y,1)) print.ppm(model2) #Get model diagnostics values par(mfrow=c(1,1)) diagnose.ppm(model2, cols="red", pch=19) #Chi-Square test of goodness of fit CHSQ2=quadrat.test(model2,nx=5,ny=5) CHSQ2 plot(linearpp, cols="blue",pch=19, main="Chi-Square Test for Linearly Increasing Model") plot(CHSQ2, add=TRUE, col="red") #Kolmogorov-Smirnov Test test2=cdf.test(model2, "y") plot(test2, lwd=3, cex=.8) test2=cdf.test(model2, "x") plot(test2) ### Now Overfit model with quadratic and look at residuals and model fit model3=ppm(linearpp,~polynom(x,y,2)) print.ppm(model3) #Get model diagnostics values par(mfrow=c(1,1)) diagnose.ppm(model2) #Chi-Square test of goodness of fit CHSQ2=quadrat.test(model3,nx=5,ny=5) CHSQ2 plot(linearpp, cols="blue",pch=19) plot(CHSQ2, add=TRUE, col="red") #Kolmogorov-Smirnov Test test2=cdf.test(model3, "y") plot(test2) test2=cdf.test(model3, "x") plot(test2) #Compare AIC of all three models AIC(model1) AIC(model2) AIC(model3) #Likelihood Ratio Test anova(model1,model2, test="LRT") anova(model2,model3, test="LRT") ###################################################################### #Next, the conbination of TWO multivariate normal distributions ###################################################################### #generate grid locations x <- 1:20 y <- 1:20 z <- matrix(0,20,20) #set means and inverses of covariance matrices mu1 <- c(3,3) mu2 <- c(16,14) sigmasq1 <- 6 sigmasq2 <- 12 Siginv <- matrix(0,2,2) Siginv[1,1] <- 1/sigmasq1 Siginv[2,2] <- 1/sigmasq2 Siginv2 <- matrix(0,2,2) Siginv2[1,1] <- 1/60 Siginv2[2,2] <- 1/35 #calculate density function height combination on a 20 x 20 grid #z[i,j] has intensity at each location. for (i in 1:20) { for (j in 1:20) { z[i,j] <- (1/(2*pi))*exp((-1/2)* ( (c(x[i],y[j]) - mu1)%*%Siginv%*%(c(x[i],y[j]) - mu1) ) )+ (1/(2*pi))*exp((-1/2)* ( (c(x[i],y[j]) - mu2)%*%Siginv2%*%(c(x[i],y[j]) - mu2) ) ) } } #set plot picture par(mfcol=c(1,3),pty='s',mar=c(2,2,2,2)) #Goal is to generate 200 points over this region - so we generate 400 points #from a Poisson distribution with intensity = max intensity over entire region #generate random locations on 20x20 grid xrand <- runif(500,min=0,max=20) yrand <- runif(500,min=0,max=20) #generate random values between 0 and max intensity test <- runif(500,min=,max=max(z)) #zvalues are approximate intensity values at 300 random generated points zval <- 1:500 for (i in 1:500) { zval[i] <- z[trunc(xrand[i])+1,trunc(yrand[i])+1] } #finally, keep 200 first points such that random number between 0 and max intensity #is less than curve height at that point plot1x <- xrand[test