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
CRS=rpoint(100, win=TN)
NND=nncross(X=CRS,Y=SH)
Fd=ecdf(NND$dist)
F_sim=matrix(nrow=99,ncol=100)
for(i in 1:99) F_sim[i,]=nncross(CRS,rpoint(n, win=TN))$dist
F_sim_sort=apply(t(apply(F_sim,1,sort)),2,sort)
plot(Fd,col="red",cex=0,main="F function",xlab="distance",ylab="F(d)",xlim=c(0,4000))
for(i in 1:99) lines(ecdf(F_sim[i,]),col="grey",verticals=T,cex=0)
lines(ecdf(F_sim_sort[5,]),verticals=T,col='blue',cex=0,lwd=1.5)
lines(ecdf(F_sim_sort[95,]),verticals=T,col='blue',cex=0,lwd=1.5)
lines(Fd,col="red",verticals=T,cex=0,lwd=3)
Comparing with the result of envelope (school.ppp, fun=Fest)
CI=envelope(SH, Fest, nsim=99, nrank=5)
plot(CI)