*初步環境建置與讀取檔案
library(splancs);library(GISTools);library(rgdal);library(raster);library(ggtern);library(spatstat);library(aspace) setwd("D:/1072SA/Mid2/data") TS=readOGR(dsn = ".", layer = "toshin",encoding = "utf8") JR=readOGR(dsn = ".", layer = "JR",encoding = "utf8") MRT.pt=read.csv("metro.csv") MRT_all=SpatialPoints(MRT.pt[,1:2],TS@proj4string) MRT_all=spTransform(MRT_all,JR@proj4string) TS=spTransform(TS,JR@proj4string)
Q1
par(family="JH") MRT=gIntersection(MRT_all,TS,byid=T) plot(TS,col='#FFFF0055') calc_sde(points=MRT@coords,verbose = F) plot_sde(plotnew=F, plotcentre=T,sde.col='red',sde.lwd=1.5, centre.col='red',titletxt="東京都心六區內的地下鐵點位分布",plotpoints=T,points.col='#996633', points.pch=16,cex=0.8)
Q2
MRT.ppp=ppp(MRT@coords[,1],MRT@coords[,2],as.owin(TS)) m=mean(nndist(MRT.ppp)) sprintf("最鄰近車站距離的平均值為%.2f公尺",m)
## [1] "最鄰近車站距離的平均值為563.58公尺"
\(設定顯著水準為{\alpha=0.05}\)
\(H_0:都心內地鐵站不為均勻分布\)
\(H_a:都心內地鐵站為均勻分布\)r_sim=c() for(i in 1:999) r_sim[i]=mean(nndist(rpoint(length(MRT),win=as.owin(TS)))) r_sim=sort(r_sim) if(m>r_sim[950]) cat("落在隨機模擬的95%CI之外,都心內地鐵站為均勻分布") else cat("落在隨機模擬的95%CI之中,都心內地鐵站不為均勻分布")
## 落在隨機模擬的95%CI之外,都心內地鐵站為均勻分布
Q3
par(family="JH") plot(c(0,2000),c(0,1),type='n',xaxs = "i", yaxs ="i",xlab="距離",ylab='G',main='高階鄰近車站距離關係圖') for(i in 4:1) lines(ecdf(nndist(MRT.ppp,k=i)),col=i,cex=0,lwd=3,verticals=T) legend(1500,0.4,c("第一鄰近","第二鄰近","第三鄰近","第四鄰近"),pch=15,col=1:4,box.col=NA)
s=mean(nndist(MRT.ppp,k=3)<1000)*100 sprintf("%.2f%%的地鐵站,在一公里內可以到達另外三個車站",s)
## [1] "69.72%的地鐵站,在一公里內可以到達另外三個車站"
Q4
par(family="JH") MRT.KDE=raster(kde.points(MRT,2000,n=500, lims=TS)) JR.KDE =raster(kde.points( JR,2000,n=500, lims=TS)) DUAL.KDE=MRT.KDE-JR.KDE image(DUAL.KDE,asp=1,bty='n',xaxt='n',yaxt ='n',xlab="",ylab="",col=brewer.pal(9,'RdYlGn'),main='地下鐵與JR的Dual KDE地圖') masker=poly.outer(DUAL.KDE,TS) add.masking(masker, col="white") plot(TS,add=T)
plot(DUAL.KDE, axes=F,box=F,legend=F,col=brewer.pal(9,'RdYlGn'),main='地下鐵與JR的Dual KDE地圖') add.masking(masker, col="white") plot(TS,add=T)
image(DUAL.KDE,asp=1,bty='n',xaxt='n',yaxt ='n',xlab="",ylab="") add.masking(masker, col="white") plot(TS,add=T)
plot(DUAL.KDE, axes=F,box=F,legend=F) add.masking(masker, col="white") plot(TS,add=T)