What we do:
load(url("http://github.com/mgimond/Spatial/raw/master/Data/ppa.RData"))
starbucks # A ppp point layer of Starbucks stores in Massachusetts;
ma # An owin polygon layer of Massachusetts boundaries;
pop # An im raster layer of population density distribution.
library(rgdal)
library(maptools)
library(raster)
# Load an MA.shp polygon shapefile
s <- readOGR(".", "MA") # Don't add the .shp extension
ma <- as(s, "owin")
# Load a starbucks.shp point feature shapefile
s <- readOGR(".","starbucks") # Don't add the .shp extension
starbucks <- as(s, "ppp")
# Load a pop_sqmile.img population density raster layer
img <- raster("pop_sqmile.img")
pop <- as.im(img)
library(spatstat)
marks(starbucks) <- NULL
plot(starbucks, main=NULL, cols=rgb(0,0,0,.2), pch=20) # before specifying the boundary
Window(starbucks) <- ma # define the boundary
plot(starbucks, main=NULL, cols=rgb(0,0,0,.2), pch=20) # after specifying the boundary
hist(pop, main=NULL, las=1)
pop.lg <- log(pop)
hist(pop.lg, main=NULL, las=1)
Q <- quadratcount(starbucks, nx= 6, ny=3)
plot(starbucks, pch=20, cols="grey70", main=NULL) # Plot points
plot(Q, add=TRUE) # Add quadrat grid
Q.d <- intensity(Q)
plot(intensity(Q, image=TRUE), main=NULL, las=1) # Plot density raster, las?:http://rfunction.com/archives/1302
plot(starbucks, pch=20, cex=0.6, col=rgb(0,0,0,.5), add=TRUE) # Add points
spatstat::rescale
- rescale the length unit from m
to km
starbucks.km <- rescale(starbucks, 1000, "km")
ma.km <- rescale(ma, 1000, "km")
pop.km <- rescale(pop, 1000, "km")
pop.lg.km <- rescale(pop.lg, 1000, "km")
Q <- quadratcount(starbucks.km, nx= 6, ny=3)
Q.d <- intensity(Q)
plot(intensity(Q, image=TRUE), main=NULL, las=1) # Plot density raster
plot(starbucks.km, pch=20, cex=0.6, col=rgb(0,0,0,.5), add=TRUE) # Add points
brk <- c( -Inf, 4, 6, 8 , Inf) # Define the breaks
Zcut <- cut(pop.lg.km, breaks=brk, labels=1:4) # Classify the raster
E <- tess(image=Zcut) # Create a tesselated surface
plot(E, main="", las=1) # no main title
Q <- quadratcount(starbucks.km, tess = E) # Tally counts
Q.d <- intensity(Q) # Compute density
plot(intensity(Q, image=TRUE), las=1, main=NULL)
plot(starbucks.km, pch=20, cex=0.6, col=rgb(1,1,1,.5), add=TRUE)
cl <- interp.colours(c("lightyellow", "orange" ,"red"), E$n)
plot(intensity(Q, image=TRUE), las=1, col=cl, main=NULL)
plot(starbucks.km, pch=20, cex=0.6, col=rgb(0,0,0,.5), add=TRUE)
density
computes an isotropic kernel intensity estimate of the point patternK1 <- density(starbucks.km) # Using the default bandwidth
plot(K1, main=NULL, las=1)
contour(K1, add=TRUE)
# with a 50 km bandwidth
K2 <- density(starbucks.km, sigma=50) # Using a 50km bandwidth
plot(K2, main=NULL, las=1)
contour(K2, add=TRUE)
# change the kernel to a disc function type
K3 <- density(starbucks.km, kernel = "disc", sigma=50) # Using a 50km bandwidth
plot(K3, main=NULL, las=1)
contour(K3, add=TRUE)
rho <- rhohat(starbucks.km, pop.lg.km, method="ratio")
# Generate rho vs covariate plot: non-parametric fitting curve
plot(rho, las=1, main=NULL)
pred <- predict(rho)
cl <- interp.colours(c("lightyellow", "orange" ,"red"), 100) # Create color scheme
plot(pred, col=cl, las=1, main=NULL)
PPM1 <- ppm(starbucks.km ~ pop.lg.km)
## Warning: Values of the covariate 'pop.lg.km' were NA or undefined at
## 0.57% (4 out of 699) of the quadrature points. Occurred while executing:
## ppm.ppp(Q = starbucks.km, trend = ~pop.lg.km, data = NULL, interaction =
## NULL)
plot(effectfun(PPM1, "pop.lg.km", se.fit=TRUE), main=NULL, las=1)
PPM1
To be continued