*初步環境建置與讀取檔案
library(sf);library(spatstat)
setwd("D:/1092SA/Data")
school=st_read("Schools.shp")
Tainan=st_read("TainanCounty.shp")
school.coord=st_coordinates(school)
TN=as.owin(Tainan)
SH=as.ppp(school.coord,TN)
n=SH$n
- Nearest Neighbor Analysis
\(H_0:點分布隨機;H_a:點分布不隨機;\alpha = 0.05\)
r_obs=mean(nndist(SH))
r_sim=sapply(1:999,function(x) mean(nndist(rpoint(n, win=TN))))
r_sim=sort(r_sim)
hist(r_sim,xlim=c(900,1500),xlab="",main="NNA")
abline(v=r_obs,col="red")
abline(v=r_sim[c(25,975)],col="blue")
觀測值落在包絡曲線之外→拒絕\(H_0\)→點分布不隨機
- K-order Nearest Neighbor Indices
\(H_0:點分布隨機;H_a:點分布不隨機;\alpha = 0.05\)
NNI_obs=colMeans(nndist(SH,k=1:50))
NNI_sim=sapply(1:999,function(x) colMeans(nndist(rpoint(n, win=TN),k=1:50)))
NNI_sim=apply(NNI_sim,1,sort)
plot(c(0,51),c(0,NNI_sim[999,50]),type="n", main="K-NNI",xlab="K-order",ylab="NNI",xaxs="i",yaxs="i")
polygon(c(1:50,50:1),c(NNI_sim[25,],rev(NNI_sim[975,])),col="#0000FF22",border =NA)
lines(1:50,NNI_sim[975,],col="blue",lty=3)
lines(1:50,NNI_sim[25,],col="blue",lty=3)
lines(1:50,NNI_sim[500,],col="blue")
lines(1:50,NNI_obs,col="red")
points(1:50,NNI_obs,col="red",pch=20,cex=1)
觀測值落在包絡曲線之外→拒絕\(H_0\)→點分布不隨機
- G Function
\(H_0:點分布隨機;H_a:點分布不隨機;\alpha = 0.1\)
G_obs=ecdf(nndist(SH))
plot(G_obs,lwd=3,cex=0,col="red",main="G(d)",xlab="distance",ylab="G(d)",xlim=c(0,5000),xaxs="i",yaxs="i")
#sapply(1:99,function(x) lines(ecdf(nndist(rpoint(n, win=TN))),cex=0))
G_sim=matrix(nrow=99,ncol=n)
for(i in 1:99) G_sim[i,]=nndist(rpoint(n, win=TN))
for(i in 1:99) lines(ecdf(G_sim[i,]),cex=0,col='grey80')
lines(G_obs,lwd=3,cex=0,col="red")
G_sim2=t(apply(G_sim,1,sort))
G_sim3=apply(G_sim2,2,sort)
lines(ecdf(G_sim3[5,]),col='blue',cex=0)
lines(ecdf(G_sim3[95,]),col='blue',cex=0)
>
> 觀測值落在包絡曲線之外→拒絕\(H_0\)→點分布不隨機