library(rgdal)
library(GISTools)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(classInt)
library(spatstat)
library(aspace)
library(sp)
library(raster)
library(splancs)
library(ggtern)
library(spdep)
setwd("D:/PROJ/election/data")
#VOTE=read.csv("VOTE3.csv")
#VOTE$STATIONCODE=as.character(VOTE$STATIONCODE)
VSHP=readOGR(dsn="VOTE_TWN.SHP",layer="VOTE_TWN")
VSHP$STATIONC=as.character(VSHP$STATIONC)
TWN=readOGR(dsn="TAIWAN_VILLAGE_VOTE.SHP",layer="TAIWAN_VILLAGE_VOTE")
TWN.TOWN=readOGR(dsn="TAIWAN_TOWN_MAP.SHP",layer="TAIWAN_TOWN_MAP")
MRT=readOGR(dsn="MRT.shp",layer="MRT")
Choose the Town hvaing an MRT station within 1 km
dd= gDistance(TWN.TOWN, MRT,byid=T)
min.dd= apply(dd,2,min)
TWN.M= TWN.TOWN[min.dd<=1000,]
Choose the Voting station in the chosen Town
VSHP$TOWNCODE= substring(VSHP$STATIONC,1,8)
twnmy=TWN.M$TOWNCODE
V.M= VSHP[VSHP$TOWNCODE %in% twnmy,]
plot(TWN.M,col="cornsilk",border="white")
plot(TWN.TOWN,col="grey",border="white",add=T)
plot(TWN.M,col="cornsilk",border="gold",add=T)
plot(VSHP,pch=19,cex=0.7,col="black",add=T)
plot(V.M,pch=19,cex=0.7,col="chocolate4",add=T)
plot(MRT,pch=19,cex=1,col="red",add=T)
Select GREEN and BLUE Voting stations
PG.pt= sort(V.M$P_G_pt)
PG.75= quantile(PG.pt,0.75)
V.G75= V.M[V.M$P_G_pt>PG.75,]
PB.pt= sort(V.M$P_B_pt)
PB.75= quantile(PB.pt,0.75)
V.B75= V.M[V.M$P_B_pt>PB.75,]
plot(TWN.M,col="cornsilk",border="white")
plot(TWN.TOWN,col="grey",border="white",add=T)
plot(TWN.M,col="cornsilk",border="gold",add=T)
plot(VSHP,pch=19,cex=0.7,col="black",add=T)
plot(V.M,pch=19,cex=0.7,col="darkgray",add=T)
plot(V.G75,pch=19,cex=0.7,col="green3",add=T)
plot(V.B75,pch=19,cex=0.7,col="dodgerblue",add=T)
plot(MRT,pch=10,cex=1,col="red",add=T)
MRT$COL2=as.character(MRT$COL2)
MRT@data[is.na(MRT$COL2),]$COL2=""
M.blu= MRT[MRT$COL=="blu"|MRT$COL2=="blu",] #23
M.red= MRT[MRT$COL=="red"|MRT$COL2=="red",] #28
M.grn= MRT[MRT$COL=="grn"|MRT$COL2=="grn",] #20
M.org= MRT[MRT$COL=="org"|MRT$COL2=="org",] #26
M.brn= MRT[MRT$COL=="brn"|MRT$COL2=="brn",] #24
plot(TWN.M,col="cornsilk",border="white")
plot(TWN.TOWN,col="grey",border="white",add=T)
plot(TWN.M,col="cornsilk",border="gold",add=T)
plot(VSHP,pch=19,cex=0.7,col="black",add=T)
plot(V.M,pch=19,cex=0.7,col="darkgray",add=T)
plot(V.G75,pch=19,cex=0.7,col="green3",add=T)
plot(V.B75,pch=19,cex=0.7,col="dodgerblue",add=T)
plot(M.org,pch=10,cex=1,col="darkorange",add=T)
plot(M.brn,pch=10,cex=1,col="brown",add=T)
plot(M.red,pch=10,cex=1,col="red",add=T)
plot(M.grn,pch=10,cex=1,col="green",add=T)
plot(M.blu,pch=10,cex=1,col="blue",add=T)
no-weight and weighted by votes
bnd=c(c( 279623.3,323663.6), c( 2750544.9,2792921.8))
masker=poly.outer(as.points(TWN.M@bbox[1,],TWN.M@bbox[2,]),TWN.M)
## Warning in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td,
## unaryUnion_if_byid_false, : spgeom1 and spgeom2 have different proj4
## strings
kde= kde2d(V.B75@coords[,1],V.B75@coords[,2],1500,200,lims=bnd)
image(kde,asp=1,col= brewer.pal(7, "Blues" ))
add.masking(masker,col="white")
plot(TWN.M,add=T,border="grey")
plot(M.org,pch=10,cex=1,col="darkorange",add=T)
plot(M.brn,pch=10,cex=1,col="brown",add=T)
plot(M.red,pch=10,cex=1,col="red",add=T)
plot(M.grn,pch=10,cex=1,col="green",add=T)
plot(M.blu,pch=10,cex=1,col="blue",add=T)
kde= kde2d.weighted(V.B75@coords[,1],V.B75@coords[,2],1500,200,w=V.B75$P_B,lims=bnd)
image(kde,asp=1,col= brewer.pal(7, "Blues" ))
add.masking(masker,col="white")
plot(TWN.M,add=T,border="grey")
plot(M.org,pch=10,cex=1,col="darkorange",add=T)
plot(M.brn,pch=10,cex=1,col="brown",add=T)
plot(M.red,pch=10,cex=1,col="red",add=T)
plot(M.grn,pch=10,cex=1,col="green",add=T)
plot(M.blu,pch=10,cex=1,col="blue",add=T)
kde= kde2d(V.G75@coords[,1],V.G75@coords[,2],1500,200,lims=bnd)
image(kde,asp=1,col= brewer.pal(7, "Greens" ))
add.masking(masker,col="white")
plot(TWN.M,add=T,border="grey")
plot(M.org,pch=10,cex=1,col="darkorange",add=T)
plot(M.brn,pch=10,cex=1,col="brown",add=T)
plot(M.red,pch=10,cex=1,col="red",add=T)
plot(M.grn,pch=10,cex=1,col="green",add=T)
plot(M.blu,pch=10,cex=1,col="blue",add=T)
kde= kde2d.weighted(V.G75@coords[,1],V.G75@coords[,2],1500,200,w=V.G75$P_G,lims=bnd)
image(kde,asp=1,col= brewer.pal(7, "Greens" ))
add.masking(masker,col="white")
plot(TWN.M,add=T,border="grey")
plot(M.org,pch=10,cex=1,col="darkorange",add=T)
plot(M.brn,pch=10,cex=1,col="brown",add=T)
plot(M.red,pch=10,cex=1,col="red",add=T)
plot(M.grn,pch=10,cex=1,col="green",add=T)
plot(M.blu,pch=10,cex=1,col="blue",add=T)
F(d)
change SpatialPoint to ppp
MRTbox= bbox(MRT)
MRTppp= ppp(MRT@coords[,1],MRT@coords[,2],MRTbox[1,],MRTbox[2,])
M.blubox= bbox(M.blu)
M.bluppp= ppp(M.blu@coords[,1],M.blu@coords[,2],M.blubox[1,],M.blubox[2,])
M.redbox= bbox(M.red)
M.redppp= ppp(M.red@coords[,1],M.red@coords[,2],M.redbox[1,],M.redbox[2,])
M.grnbox= bbox(M.grn)
M.grnppp= ppp(M.grn@coords[,1],M.grn@coords[,2],M.grnbox[1,],M.grnbox[2,])
M.orgbox= bbox(M.org)
M.orgppp= ppp(M.org@coords[,1],M.org@coords[,2],M.orgbox[1,],M.orgbox[2,])
M.brnbox= bbox(M.brn)
M.brnppp= ppp(M.brn@coords[,1],M.brn@coords[,2],M.brnbox[1,],M.brnbox[2,])
V.G75box= bbox(V.G75)
V.G75ppp= ppp(V.G75@coords[,1],V.G75@coords[,2],V.G75box[1,],V.G75box[2,])
V.B75box= bbox(V.B75)
V.B75ppp= ppp(V.B75@coords[,1],V.B75@coords[,2],V.B75box[1,],V.B75box[2,])
k=1
nnrc= nncross(V.G75ppp,M.bluppp,k=1)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.redppp,k=1)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.grnppp,k=1)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.orgppp,k=1)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.brnppp,k=1)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=1", xlab="distance", cex=0,verticals=T, xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(V.G75ppp,M.rdmppp)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=2
nnrc= nncross(V.G75ppp,M.bluppp,k=2)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.redppp,k=2)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.grnppp,k=2)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.orgppp,k=2)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(V.G75ppp,M.brnppp,k=2)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=2",xlab="distance", cex=0,verticals=T, xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(V.G75ppp,M.rdmppp,k=2)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=1
nnrc= nncross(V.B75ppp,M.bluppp,k=1)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.redppp,k=1)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.grnppp,k=1)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.orgppp,k=1)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.brnppp,k=1)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=1",xlab="distance", cex=0,verticals=T, xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(V.B75ppp,M.rdmppp)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=2
nnrc= nncross(V.B75ppp,M.bluppp,k=2)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.redppp,k=2)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.grnppp,k=2)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.orgppp,k=2)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(V.B75ppp,M.brnppp,k=2)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=2",xlab="distance", cex=0,verticals=T, xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(V.B75ppp,M.rdmppp,k=2)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
blue line
nnrc= nncross(M.bluppp,V.G75ppp,k=1)
nnrcblu= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
nnrc= nncross(M.bluppp,V.G75ppp,k=3)
nnrcblu= ecdf(nnrc[,1])
lines(nnrcblu,cex=0,verticals=T,col="blue")
nnrc= nncross(M.bluppp,V.G75ppp,k=5)
nnrcblu= ecdf(nnrc[,1])
lines(nnrcblu,cex=0,verticals=T,col="blue")
nnrc= nncross(M.bluppp,V.G75ppp,k=8)
nnrcblu= ecdf(nnrc[,1])
lines(nnrcblu,cex=0,verticals=T,col="blue")
orange line
nnrc= nncross(M.orgppp,V.G75ppp,k=1)
nnrcorg= ecdf(nnrc[,1])
plot(nnrcorg,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="orange")
nnrc= nncross(M.orgppp,V.G75ppp,k=3)
nnrcorg= ecdf(nnrc[,1])
lines(nnrcorg,cex=0,verticals=T,col="orange")
nnrc= nncross(M.orgppp,V.G75ppp,k=5)
nnrcorg= ecdf(nnrc[,1])
lines(nnrcorg,cex=0,verticals=T,col="orange")
nnrc= nncross(M.orgppp,V.G75ppp,k=8)
nnrorg= ecdf(nnrc[,1])
lines(nnrcorg,cex=0,verticals=T,col="orange")
green line
nnrc= nncross(M.grnppp,V.G75ppp,k=1)
nnrcgrn= ecdf(nnrc[,1])
plot(nnrcgrn,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="green3")
nnrc= nncross(M.grnppp,V.G75ppp,k=3)
nnrcgrn= ecdf(nnrc[,1])
lines(nnrcgrn,cex=0,verticals=T,col="green3")
nnrc= nncross(M.grnppp,V.G75ppp,k=5)
nnrcgrn= ecdf(nnrc[,1])
lines(nnrcgrn,cex=0,verticals=T,col="green3")
nnrc= nncross(M.grnppp,V.G75ppp,k=8)
nnrcgrn= ecdf(nnrc[,1])
lines(nnrcgrn,cex=0,verticals=T,col="green3")
red line
nnrc= nncross(M.redppp,V.G75ppp,k=1)
nnrcred= ecdf(nnrc[,1])
plot(nnrcred,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="red")
nnrc= nncross(M.redppp,V.G75ppp,k=3)
nnrcred= ecdf(nnrc[,1])
lines(nnrcred,cex=0,verticals=T,col="red")
nnrc= nncross(M.redppp,V.G75ppp,k=5)
nnrcred= ecdf(nnrc[,1])
lines(nnrcred,cex=0,verticals=T,col="red")
nnrc= nncross(M.redppp,V.G75ppp,k=8)
nnrcred= ecdf(nnrc[,1])
lines(nnrcred,cex=0,verticals=T,col="red")
brown line
nnrc= nncross(M.brnppp,V.G75ppp,k=1)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcbrn,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="brown")
nnrc= nncross(M.brnppp,V.G75ppp,k=3)
nnrcbrn= ecdf(nnrc[,1])
lines(nnrcbrn,cex=0,verticals=T,col="brown")
nnrc= nncross(M.brnppp,V.G75ppp,k=5)
nnrcbrn= ecdf(nnrc[,1])
lines(nnrcbrn,cex=0,verticals=T,col="brown")
nnrc= nncross(M.brnppp,V.G75ppp,k=8)
nnrcbrn= ecdf(nnrc[,1])
lines(nnrcbrn,cex=0,verticals=T,col="brown")
randomly select 23 MRT stations as simulation
k=1
nnrc= nncross(M.bluppp,V.G75ppp,k=1)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.G75ppp,k=1)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.G75ppp,k=1)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.G75ppp,k=1)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.G75ppp,k=1)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=1",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.G75ppp,k=1)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=3
nnrc= nncross(M.bluppp,V.G75ppp,k=3)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.G75ppp,k=3)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.G75ppp,k=3)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.G75ppp,k=3)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.G75ppp,k=3)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=3",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.G75ppp,k=3)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=5
nnrc= nncross(M.bluppp,V.G75ppp,k=5)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.G75ppp,k=5)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.G75ppp,k=5)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.G75ppp,k=5)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.G75ppp,k=5)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=5",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.G75ppp,k=5)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=8
nnrc= nncross(M.bluppp,V.G75ppp,k=8)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.G75ppp,k=8)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.G75ppp,k=8)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.G75ppp,k=8)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.G75ppp,k=8)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.G75ppp,k=8)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
blue line
nnrc= nncross(M.bluppp,V.B75ppp,k=1)
nnrcblu= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
nnrc= nncross(M.bluppp,V.B75ppp,k=3)
nnrcblu= ecdf(nnrc[,1])
lines(nnrcblu,cex=0,verticals=T,col="blue")
nnrc= nncross(M.bluppp,V.B75ppp,k=5)
nnrcblu= ecdf(nnrc[,1])
lines(nnrcblu,cex=0,verticals=T,col="blue")
nnrc= nncross(M.bluppp,V.B75ppp,k=8)
nnrcblu= ecdf(nnrc[,1])
lines(nnrcblu,cex=0,verticals=T,col="blue")
orange line
nnrc= nncross(M.orgppp,V.B75ppp,k=1)
nnrcorg= ecdf(nnrc[,1])
plot(nnrcorg,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="orange")
nnrc= nncross(M.orgppp,V.B75ppp,k=3)
nnrcorg= ecdf(nnrc[,1])
lines(nnrcorg,cex=0,verticals=T,col="orange")
nnrc= nncross(M.orgppp,V.B75ppp,k=5)
nnrcorg= ecdf(nnrc[,1])
lines(nnrcorg,cex=0,verticals=T,col="orange")
nnrc= nncross(M.orgppp,V.B75ppp,k=8)
nnrorg= ecdf(nnrc[,1])
lines(nnrcorg,cex=0,verticals=T,col="orange")
green line
nnrc= nncross(M.grnppp,V.B75ppp,k=1)
nnrcgrn= ecdf(nnrc[,1])
plot(nnrcgrn,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="green3")
nnrc= nncross(M.grnppp,V.B75ppp,k=3)
nnrcgrn= ecdf(nnrc[,1])
lines(nnrcgrn,cex=0,verticals=T,col="green3")
nnrc= nncross(M.grnppp,V.B75ppp,k=5)
nnrcgrn= ecdf(nnrc[,1])
lines(nnrcgrn,cex=0,verticals=T,col="green3")
nnrc= nncross(M.grnppp,V.B75ppp,k=8)
nnrcgrn= ecdf(nnrc[,1])
lines(nnrcgrn,cex=0,verticals=T,col="green3")
red line
nnrc= nncross(M.redppp,V.B75ppp,k=1)
nnrcred= ecdf(nnrc[,1])
plot(nnrcred,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="red")
nnrc= nncross(M.redppp,V.B75ppp,k=3)
nnrcred= ecdf(nnrc[,1])
lines(nnrcred,cex=0,verticals=T,col="red")
nnrc= nncross(M.redppp,V.B75ppp,k=5)
nnrcred= ecdf(nnrc[,1])
lines(nnrcred,cex=0,verticals=T,col="red")
nnrc= nncross(M.redppp,V.B75ppp,k=8)
nnrcred= ecdf(nnrc[,1])
lines(nnrcred,cex=0,verticals=T,col="red")
brown line
nnrc= nncross(M.brnppp,V.B75ppp,k=1)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcbrn,main="F(d) k=1,3,5,8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="brown")
nnrc= nncross(M.brnppp,V.B75ppp,k=3)
nnrcbrn= ecdf(nnrc[,1])
lines(nnrcbrn,cex=0,verticals=T,col="brown")
nnrc= nncross(M.brnppp,V.B75ppp,k=5)
nnrcbrn= ecdf(nnrc[,1])
lines(nnrcbrn,cex=0,verticals=T,col="brown")
nnrc= nncross(M.brnppp,V.B75ppp,k=8)
nnrcbrn= ecdf(nnrc[,1])
lines(nnrcbrn,cex=0,verticals=T,col="brown")
k=1
nnrc= nncross(M.bluppp,V.B75ppp,k=1)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.B75ppp,k=1)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.B75ppp,k=1)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.B75ppp,k=1)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.B75ppp,k=1)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=1",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.B75ppp,k=1)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=3
nnrc= nncross(M.bluppp,V.B75ppp,k=3)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.B75ppp,k=3)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.B75ppp,k=3)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.B75ppp,k=3)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.B75ppp,k=3)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=3",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.B75ppp,k=3)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=5
nnrc= nncross(M.bluppp,V.B75ppp,k=5)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.B75ppp,k=5)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.B75ppp,k=5)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.B75ppp,k=5)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.B75ppp,k=5)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=3",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.B75ppp,k=5)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
k=8
nnrc= nncross(M.bluppp,V.B75ppp,k=8)
nnrcblu= ecdf(nnrc[,1])
nnrc= nncross(M.redppp,V.B75ppp,k=8)
nnrcred= ecdf(nnrc[,1])
nnrc= nncross(M.grnppp,V.B75ppp,k=8)
nnrcgrn= ecdf(nnrc[,1])
nnrc= nncross(M.orgppp,V.B75ppp,k=8)
nnrcorg= ecdf(nnrc[,1])
nnrc= nncross(M.brnppp,V.B75ppp,k=8)
nnrcbrn= ecdf(nnrc[,1])
plot(nnrcblu,main="F(d) k=8",xlab="distance", cex=0,verticals=T,xlim=c(0,1500),ylim=c(0,1),col="blue")
for (i in seq(1:50)){
rdm= sample(1:108,23)
M.rdm= MRT[rdm,]
M.rdmbox= bbox(M.rdm)
M.rdmppp= ppp(M.rdm@coords[,1],M.rdm@coords[,2],M.rdmbox[1,],M.rdmbox[2,])
nnrc=nncross(M.rdmppp,V.B75ppp,k=8)
nnrf=ecdf(nnrc[,1])
lines(nnrf,cex=0,verticals=T,col="grey")
}
plot(nnrcblu,cex=0,verticals=T,add=T,col="blue")
plot(nnrcred,cex=0,verticals=T,add=T,col="red")
plot(nnrcgrn,cex=0,verticals=T,add=T,col="green3")
plot(nnrcorg,cex=0,verticals=T,add=T,col="orange")
plot(nnrcbrn,cex=0,verticals=T,add=T,col="brown")
M.buf= gBuffer(MRT,width=1000,byid=T)
Mbrn.buf= gBuffer(M.brn,width=1000,byid=T)
Mred.buf= gBuffer(M.red,width=1000,byid=T)
Morg.buf= gBuffer(M.org,width=1000,byid=T)
Mgrn.buf= gBuffer(M.grn,width=1000,byid=T)
Mblu.buf= gBuffer(M.blu,width=1000,byid=T)
t=c(M.buf,Mbrn.buf,Mred.buf,Morg.buf,Mgrn.buf,Mblu.buf)
countV=list()
countG=list()
countB=list()
countBV=list()
countGV=list()
for (i in 1:6){
countV[[i]]= poly.counts(V.M,t[[i]])
countG[[i]]= poly.counts(V.G75,t[[i]])
countB[[i]]= poly.counts(V.B75,t[[i]])
countBV[[i]]=countB[[i]]/countV[[i]]
countGV[[i]]=countG[[i]]/countV[[i]]
}
BV=c();GV=c()
for (i in 1:6){
BV[i]=mean(countBV[[i]])
GV[i]=mean(countGV[[i]])
}
count= data.frame(BV,GV)
count=data.frame(t(count))
colnames(count)= c("MRT","Brown","Red","Orange","Green","Blue")
count=count[c("MRT","Brown","Green","Red","Blue","Orange")] #變換column order
countt=as.matrix(count)
barplot(countt,col=c("blue","green3"),beside=T,ylim=c(0,1))
abline(h=0.25)
abline(h=0.5)
blue line
Mblu.VG= apply(nncross(M.bluppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mblu.BG= apply(nncross(M.bluppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mblu.VG,Mblu.BG,alternative="greater",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mblu.VG and Mblu.BG
## t = 1.0248, df = 7.4819, p-value = 0.1687
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## -138.4773 Inf
## sample estimates:
## mean of x mean of y
## 943.5154 776.8635
Mblu.VG= apply(nncross(M.bluppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mblu.BG= apply(nncross(M.bluppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mblu.VG,Mblu.BG,alternative="less",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mblu.VG and Mblu.BG
## t = 1.0248, df = 7.4819, p-value = 0.8313
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf 471.7811
## sample estimates:
## mean of x mean of y
## 943.5154 776.8635
Mblu.VG= apply(nncross(M.bluppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mblu.BG= apply(nncross(M.bluppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mblu.VG,Mblu.BG,alternative="two.sided",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mblu.VG and Mblu.BG
## t = 1.0248, df = 7.4819, p-value = 0.3374
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -212.9095 546.2132
## sample estimates:
## mean of x mean of y
## 943.5154 776.8635
green line
Mgrn.VG= apply(nncross(M.grnppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mgrn.BG= apply(nncross(M.grnppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mgrn.VG,Mgrn.BG,alternative="greater",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mgrn.VG and Mgrn.BG
## t = 7.5724, df = 5.0295, p-value = 0.0003104
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## 1065.428 Inf
## sample estimates:
## mean of x mean of y
## 1913.1862 462.1239
Mgrn.VG= apply(nncross(M.grnppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mgrn.BG= apply(nncross(M.grnppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mgrn.VG,Mgrn.BG,alternative="less",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mgrn.VG and Mgrn.BG
## t = 7.5724, df = 5.0295, p-value = 0.9997
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf 1836.696
## sample estimates:
## mean of x mean of y
## 1913.1862 462.1239
orange line
Morg.VG= apply(nncross(M.orgppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Morg.BG= apply(nncross(M.orgppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Morg.VG,Morg.BG,alternative="less",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Morg.VG and Morg.BG
## t = -10.501, df = 5.6475, p-value = 3.222e-05
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf -1099.94
## sample estimates:
## mean of x mean of y
## 834.2439 2187.4064
brown line
Mbrn.VG= apply(nncross(M.brnppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mbrn.BG= apply(nncross(M.brnppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mbrn.VG,Mbrn.BG,alternative="greater",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mbrn.VG and Mbrn.BG
## t = 9.5039, df = 4.6917, p-value = 0.0001532
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## 1621.876 Inf
## sample estimates:
## mean of x mean of y
## 2555.973 489.521
red line
Mred.VG= apply(nncross(M.redppp,V.G75ppp, k=1:5),2,FUN=mean)[1:5]
Mred.BG= apply(nncross(M.redppp,V.B75ppp, k=1:5),2,FUN=mean)[1:5]
t.test(Mred.VG,Mred.BG,alternative="two.sided",paired=F,var.equal=F,conf.level=0.95)
##
## Welch Two Sample t-test
##
## data: Mred.VG and Mred.BG
## t = 0.96775, df = 7.9999, p-value = 0.3615
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -221.3839 541.5657
## sample estimates:
## mean of x mean of y
## 1117.5878 957.4969