R Markdown
library(data.table)
library(ggplot2)
library(sp)
library(rgdal)
library(ggmap)
library(spatstat)
library(maptools)
tenders <- fread("tabula-tender-bids-from-mar-2012-to-jan-2017.csv", header = F, fill = T)
colnames(tenders) <- c("centre", "stall", "area", "trade", "bid", "month")
tenders <- tenders[centre != "",]
tenders <- tenders[!1135,]
tenders[1:1134, type:="cooked",]
tenders[1135:nrow(tenders),type:="lockup"]
tenders[,bidNum:=as.numeric(gsub(bid,pattern="\\$|,",replacement = "")),]
tenders[,date:=as.Date(paste0("01-",month),"%d-%b-%Y"),]
tenders[,priceM2:=bidNum/as.numeric(area),]
ggplot(tenders[,list(price=mean(priceM2)),by=centre],aes(centre,price))+geom_bar(stat="identity")+theme(axis.text = element_text(angle=60,hjust=1))+xlab("Hawker centre address")+ylab("Price")+ggtitle("Average price of hawker centre stalls in Singapore 2012-2017")

ggplot(tenders[,list(price=mean(priceM2)),by=trade],aes(trade,price))+geom_bar(stat="identity")+theme(axis.text = element_text(angle=45,hjust=1))+xlab("Type of stall")+ylab("Price")+ggtitle("Average bid price per meter square in hawker centres in Singapore 2012-2017")

ggplot(tenders[,list(count=length(stall)),by=centre],aes(centre,count))+geom_bar(stat="identity")+theme(axis.text = element_text(angle=60,hjust=1))+xlab("Hawker centre address")+ggtitle("Total number of bids in each hawker centre 2012-2017")

h<- readOGR("HAWKERCENTRE.kml","HAWKERCENTRE")
## OGR data source with driver: KML
## Source: "HAWKERCENTRE.kml", layer: "HAWKERCENTRE"
## with 116 features
## It has 2 fields
h.t <- data.frame(toupper(h$Name),h@coords[,1:2])
colnames(h.t) <- c("name","lon","lat")
tenders.sp <- merge(tenders,h.t,by.x="centre",by.y="name",all.x=T)
centres <- tenders[,list(count=.N),by=centre]
centres[,location:=paste0(centre, ", Singapore"),]
#g <- geocode(centres[,location,], output ="latlon", source = "google", sensor = F)
centres <- read.csv("centres-geocoded.csv")
tenders.sp <- merge(tenders, centres, by.x="centre", by.y="centre", all.x = T)
tenders.sp$year <- substr(tenders.sp$month,5,6)
ggplot(tenders.sp, aes(x=lon, y=lat, size=priceM2,color=type)) + geom_point(alpha=0.3) + coord_fixed()+ facet_wrap(~year)+theme(axis.text = element_text(angle=90, hjust=1)) + labs(x="Latitude",y ="Longitude", size="Price/meter square", color="Stall type")+ggtitle("Number of bids per year (20 ~)")

ggplot(tenders.sp, aes(x=lon, y=lat, size=priceM2)) + geom_point(alpha=0.3) + coord_fixed()+ facet_wrap(~trade)+theme(axis.text = element_text(angle=90, hjust=1)) + labs(x="Latitude", y="Longitude", size="Price/meter square")+ggtitle("Number of bids for hawker centre stalls 2012-2017 Singapore")

centres.sp <- tenders.sp[lat > 0,list(lon=lon[1], lat=lat[1], price=mean(priceM2), count=.N),by=centre]
centres.sp[is.na(price),price:=0,] #NA to 0
coordinates(centres.sp) <- c('lon', 'lat')
centres.ppp <- unmark(as.ppp(centres.sp))
sg <- readOGR(".", "sg-all")
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "sg-all"
## with 1 features
## It has 13 fields
sg.window <- as.owin(sg)
centres.ppp <- centres.ppp[sg.window]
plot(Kest(centres.ppp))

plot(density(centres.ppp, 0.02))

contour(density(centres.ppp, 0.02))

pop <- as.im(readGDAL("sg-pop.tif"))
## sg-pop.tif has GDAL driver GTiff
## and has 37 rows and 58 columns
plot(rhohat(centres.ppp, pop))

plot(rhohat(centres.ppp, pop, weights=centres.sp$price))

plot(pop)
plot(centres.ppp, add=T)

cat("\n Reflection: As clearly shown from the plot above, the outliers are mainly located at Yew Tee and Sengkang, which are residential areas. There are less hawker centres located in those areas because there will be no much population in the day time for people need to go for work or school. Moreover, it is very likely that at night people will go back home to eat dinner with families. Therefore hawker centres tend to open in places with high population flow during day time, such as town area or business parks etc.\n")
##
## Reflection: As clearly shown from the plot above, the outliers are mainly located at Yew Tee and Sengkang, which are residential areas. There are less hawker centres located in those areas because there will be no much population in the day time for people need to go for work or school. Moreover, it is very likely that at night people will go back home to eat dinner with families. Therefore hawker centres tend to open in places with high population flow during day time, such as town area or business parks etc.