################################################## Exercise sheet 1 ##### Exercise 1 - Exploring a spatial point pattern # load package splancs library(splancs) # load dataset southlancs contained in the package data(southlancs) head(southlancs) # extract larynx cases and their locations larynx<-southlancs[southlancs[,3]==1,] loclarynx<-cbind(larynx$x,larynx$y) # plot their locations plot(loclarynx,xlab="eastings",ylab="northings",main="Locations of larynx cancer cases",pch="+",col="red") # borders of the study area are given as a polygon polymap(southlancs.bdy,border="grey") # estimate kernel density - different bandwidths k1<-kernel2d(pts=loclarynx,poly=southlancs.bdy,h0=100,nx=100,ny=100) k2<-kernel2d(pts=loclarynx,poly=southlancs.bdy,h0=500,nx=100,ny=100) k3<-kernel2d(pts=loclarynx,poly=southlancs.bdy,h0=1000,nx=100,ny=100) k4<-kernel2d(pts=loclarynx,poly=southlancs.bdy,h0=2000,nx=100,ny=100) # plot the densities, using contour or image par(mfrow=c(2,2)) contour(k1,nlevels=15,main="Bandwidth=100") contour(k2,nlevels=15,main="Bandwidth=500") contour(k3,nlevels=15,main="Bandwidth=1000") contour(k4,nlevels=15,main="Bandwidth=2000") par(mfrow=c(2,2)) image(k1,col=grey(seq(0.8,0,-0.1)),main="Bandwidth=100") image(k2,col=grey(seq(0.8,0,-0.1)),main="Bandwidth=500") image(k3,col=grey(seq(0.8,0,-0.1)),main="Bandwidth=1000") image(k4,col=grey(seq(0.8,0,-0.1)),main="Bandwidth=2000") # add location of the old incinerator to the plot old.incinerator image(k4,col=grey(seq(0.8,0,-0.1)),main="Bandwidth=2000") points(old.incinerator,pch="+",col="red") ####### Exercise 2 - Distance to an incinerator, Poisson regression analysis # read in dataset newyork.txt newyork<-read.table("newyork.txt",header=TRUE) names(newyork) # location of the population centroids locnewyork<-cbind(newyork[,1],newyork[,2]) plot(locnewyork,xlab="xcoord",ylab="ycoord",main="Population Centroids",pch="+") # add incinerator to the plot points(-0.14,-67.19,col="red") # compute distance from each population centroid to the incinerator using rdist library(fields) monarch<-matrix(c(-0.14,-67.19),nrow=1,ncol=2) # to use rdist(), this must be a matrix dm<-rdist(locnewyork,monarch) #(*) write a function which calculates the Euklidean distance between two locations euklid<-function(loc1,loc2){ d<-sqrt(sum((loc1-loc2)^2)) return(d)} disteuklid<-rep(0,790) for (i in 1:790){ disteuklid[i]<-euklid(locnewyork[i,],monarch)} # poisson regression # calculate the inverse distance invdist<-1/dm poisson.glm<-glm(newyork$ncases~invdist+offset(log(newyork$npopulation)),family="poisson") summary(poisson.glm) ########### Exercise 3 - Mapping shapefiles in R # load the library maptools library(maptools) # read in the shapefile Switzerland swissmap<-readShapeSpatial("switzerland/switzerland",IDvar="BEZIRKSNR") # plot a map of all Swiss regions plot(swissmap) # define a vector with the ID's of the Swiss lakes lake<-c(100,200,300,500,1000,1400,1700,1900,2000,2100,2200,2300,2400) # define a color vector assining different colors to lakes and regions colors<-ifelse(swissmap$BEZIRKSNR%in%lake,"blue","yellow") # plot the Swiss map with regions and lakes plot(swissmap,col=colors)