SET WORKING DIRECTORY AND LOAD PACKAGES

setwd("/Volumes/HOANG NAM/Maps Making/Lab 9/02221-Lab9") # Set working directory
library(knitr)
library(ggplot2)  
library(data.table)
library(sp)
library(rgdal)
library(ggmap)
library(maptools)
library(plotly)

SET UP PART 1

tenders <- fread("tender-bids.csv", header = FALSE, fill =T)
colnames(tenders) <- c("centre", "stall", "area","trade", "bid", "month")

SET UP PART 2

tenders <- tenders[centre != "",] #remove empty rows
tenders <- tenders[!1135,] #remove 'lockup' row
tenders <- tenders[!1749,] #remove 'market stalls' row
tenders[1:1134,type:="cooked",] #set pre-lockup rows to type cooked
tenders[1135:1748,type:="lockup",] #set to type lockup
tenders[1749:nrow(tenders),type:="market",] #set to type market

tenders[588:636,month:=bid,] #fixing the NA
tenders[588:636,bid:=trade,]
tenders[588:636,trade:=area,]
tenders[588:636,area:=stall,]

tenders[,area:=as.numeric(gsub(area,pattern=" |\\.",replacement=""))/100]#cleanup
tenders[,bidNum:=as.numeric(gsub(bid,pattern="\\$|,", replacement = "")),] #$price to numeric
tenders[,date:=as.Date(paste0("01-",month), "%d-%b-%Y"),]#date formating
tenders <- tenders[centre != "",] # remove empty rows
tenders <- tenders[!1135,]
tenders[1:1134,type:="cooked",] ## Label Rows 1 to 821 with cooked label in 'type' column
tenders[1135:nrow(tenders),type:="lockup",] ##Label Rows 1135 till end of table with "lockup"

tenders[,bidNum:=as.numeric(gsub(bid, pattern="\\$|,", replacement = "")),] # Remove "$," from bid price and record into new column
tenders[,date:=as.Date(paste0("01-",month), "%d-%b-%Y"),] ## Format date into dd-mm-yy format
tenders[,priceM2:=bidNum/as.numeric(area),] ## Find priceM2 = (bid Price)/(area)

DATA MANIPULICATION

tenders[,list(price=mean(priceM2)),by=centre] # return a list of avg price per centre
##                            centre      price
##   1:           COOKED FOOD STALLS         NA
##   2:      AMOY STREET FOOD CENTRE  930.57088
##   3:          BEO CRESCENT MARKET   30.49304
##   4:           BERSEH FOOD CENTRE  250.04824
##   5:         BLK 665 BUFFALO ROAD  217.51288
##  ---                                        
## 100:       BLK 50A MARINE TERRACE  510.25107
## 101: BLK 511 BEDOK NORTH STREET 3 1666.66667
## 102: BLK 630 BEDOK RESERVOIR ROAD   44.63716
## 103:   BLK 82 TELOK BLANGAH DRIVE 3005.04202
## 104:   BLK 93  TOA PAYOH LORONG 4  100.76923
tenders[type=='cooked',list(price=mean(priceM2)),by=centre] # return a list avg price per centre for only cooked food
##                                centre       price
##  1:                COOKED FOOD STALLS          NA
##  2:           AMOY STREET FOOD CENTRE   930.57088
##  3:               BEO CRESCENT MARKET   149.89435
##  4:                BERSEH FOOD CENTRE   250.04824
##  5:              BLK 665 BUFFALO ROAD   284.37981
##  6:                 BLK 1 JALAN KUKOH    41.65605
##  7:     BLK 11 TELOK BLANGAH CRESCENT  2354.48029
##  8:         BLK 112 JALAN BUKIT MERAH   104.61073
##  9:          BLK 115 BUKIT MERAH VIEW   135.12743
## 10:         BLK 117 ALJUNIED AVENUE 2 23498.86494
## 11:        BLK 120 BUKIT MERAH LANE 1   177.64957
## 12:        BLK 127 TOA PAYOH LORONG 1  4742.26203
## 13:        BLK 137 TAMPINES STREET 11   222.83814
## 14:                  BLK 14 HAIG ROAD   410.02825
## 15:             BLK 159 MEI CHIN ROAD    78.00092
## 16:           BLK 16 BEDOK SOUTH ROAD   135.83423
## 17:       BLK 162 ANG MO KIO AVENUE 4   180.99548
## 18:       BLK 17 UPPER BOON KENG ROAD   194.90581
## 19:              BLK 20 GHIM MOH ROAD  5138.46154
## 20:      BLK 216 BEDOK NORTH STREET 1   174.55859
## 21:         BLK 22 TOA PAYOH LORONG 7   131.35614
## 22:      BLK 226D ANG MO KIO AVENUE 1    57.25534
## 23:     BLK 226H ANG MO KIO STREET 22  1832.25806
## 24:             BLK 22A HAVELOCK ROAD   430.37634
## 25:             BLK 22B HAVELOCK ROAD    47.00941
## 26:     BLK 254 JURONG EAST STREET 24   246.08449
## 27:             BLK 29 BENDEMEER ROAD   462.96959
## 28:         BLK 3 CHANGI VILLAGE ROAD   322.31801
## 29:            BLK 32 NEW MARKET ROAD   401.87481
## 30:               BLK 320 SHUNFU ROAD  5754.10256
## 31:              BLK 335 SMITH STREET   172.79625
## 32:         BLK 36 TELOK BLANGAH RISE    81.17021
## 33:        BLK 37A TEBAN GARDENS ROAD   232.65864
## 34:            BLK 41A CAMBRIDGE ROAD   411.76471
## 35:              BLK 44 HOLLAND DRIVE 11175.61076
## 36:     BLK 453A ANG MO KIO AVENUE 10  2963.39286
## 37:                 BLK 49 SIMS PLACE  2116.73525
## 38:                 BLK 4A JALAN BATU    99.00368
## 39:          BLK 503 WEST COAST DRIVE 34578.87719
## 40:           BLK 51 OLD AIRPORT ROAD          NA
## 41:      BLK 527 ANG MO KIO AVENUE 10          NA
## 42:       BLK 531A UPPER CROSS STREET          NA
## 43:           BLK 6 JALAN BUKIT MERAH          NA
## 44:         BLK 6 TANJONG PAGAR PLAZA   188.89338
## 45:       BLK 628 ANG MO KIO AVENUE 4   355.98991
## 46:                BLK 7 EMPRESS ROAD   201.58730
## 47:    BLK 726 CLEMENTI WEST STREET 2   492.29040
## 48:         BLK 75 TOA PAYOH LORONG 5   173.61111
## 49:               BLK 79 CIRCUIT ROAD  1613.30986
## 50:        BLK 79 TELOK BLANGAH DRIVE   306.36792
## 51:              BLK 79A CIRCUIT ROAD  1941.08701
## 52:               BLK 80 CIRCUIT ROAD  3241.66667
## 53:               BLK 85 REDHILL LANE   409.15114
## 54:               BLK 89 CIRCUIT ROAD    72.00328
## 55:              BLK 90 WHAMPOA DRIVE    58.52843
## 56:              BLK 91 WHAMPOA DRIVE   141.73077
## 57:         BLK 93 TOA PAYOH LORONG 4   934.43175
## 58:                BUKIT TIMAH MARKET   235.85086
## 59:      COMMONWEALTH CRESCENT MARKET   674.18248
## 60:                DUNMAN FOOD CENTRE  7200.26923
## 61:    EAST COAST LAGOON FOOD VILLAGE 27805.81481
## 62:              GEYLANG SERAI MARKET   160.89246
## 63:           GOLDEN MILE FOOD CENTRE  1425.31708
## 64:             KALLANG ESTATE MARKET   733.33333
## 65:         MARKET STREET FOOD CENTRE   297.63294
## 66:               MAXWELL FOOD CENTRE  3056.92193
## 67:                NEWTON FOOD CENTRE   280.92754
## 68:          NORTH BRIDGE ROAD MARKET    75.28231
## 69:           SERANGOON GARDEN MARKET   164.66147
## 70: TAMAN JURONG MARKET & FOOD CENTRE    71.19260
## 71:                TIONG BAHRU MARKET   200.58236
## 72:        ZION RIVERSIDE FOOD CENTRE   203.21152
##                                centre       price
tenders[,list(count=length(bidNum)),by=centre] # return a list of number of bids per centre
##                            centre count
##   1:           COOKED FOOD STALLS     1
##   2:      AMOY STREET FOOD CENTRE    62
##   3:          BEO CRESCENT MARKET    23
##   4:           BERSEH FOOD CENTRE   103
##   5:         BLK 665 BUFFALO ROAD    50
##  ---                                   
## 100:       BLK 50A MARINE TERRACE     8
## 101: BLK 511 BEDOK NORTH STREET 3     1
## 102: BLK 630 BEDOK RESERVOIR ROAD    19
## 103:   BLK 82 TELOK BLANGAH DRIVE    17
## 104:   BLK 93  TOA PAYOH LORONG 4     2
ggplot(tenders[,list(count=length(bidNum)),by=date], aes(date, count)) + geom_line()

QUESTIONS

Choose 2-3 questions from the list above or your own additions and include visualizations that answer those questions in your Markdown document. The plots will render within the html document automatically!

Q1: HOW DO THE BIDS CHANGE WITH TIME

g<- ggplot(tenders[,list(AverageBid=mean(bidNum)),by=date],aes(date, AverageBid))+geom_line(color = "red",size = 0.5)+ggtitle("Average Bid vs Time") 

ggplotly(g)

Q2: WHICH CENTRES HAVE HIGHER PRICE PER SQUARED METRE?

a = aggregate(priceM2 ~ centre, tenders, mean)
p=plot_ly( data = tenders, x = a$centre, y = a$priceM2, type = "bar", barmode = "group", color = ~ a$centre)  %>% layout(xaxis= list(showticklabels = FALSE))
hide_legend(p)

SPATIAL DATA

h <- readOGR("HAWKERCENTRE.kml", "HAWKERCENTRE")
## OGR data source with driver: KML 
## Source: "HAWKERCENTRE.kml", layer: "HAWKERCENTRE"
## with 116 features
## It has 2 fields
plot(h)

h.t=data.frame(toupper(h$Name),h@coords[,1:2]) ##Create simplified data frame with only name and coordinates
colnames(h.t) = c("name","lon","lat") ##Add in names for data frame columns
tenders.sp=merge(tenders,h.t, by.x="centre",by.y="name",all.x=T) ##Merge centres and h by name of hawker centres into  new data frame
tenders.sp=tenders.sp[complete.cases(tenders.sp)] #Remove incomplete data

GEOCODING HAWKER CENTRE

##Manually Geo Code each hawker center
centres = tenders[,list(count=.N),by=centre] # Find common entries with the same Hawker Centre names
centres[,location:=paste0(centre,", Singapore"),] #Concatenate values in location column with ", Singapore"
#centres=centres[!88]
#g=geocode(centres[,location,],output="latlon",source="google",sensor=F)
g = read.csv("geocoded.csv")

##Query the lat and lon values for each hawker center by googling

centres = cbind(centres,g) #Combine records by rows
tenders.sp = merge(tenders,centres,by.x="centre",by.y="centre",all.x=T) ##Merge in latlon to tenders data table

DENSITY PLOT

ggplot(tenders.sp, aes(x=lon, y=lat, size=priceM2, color=type)) + geom_point(alpha=0.3) + coord_fixed()

ggplot(tenders.sp, aes(x=lon, y=lat)) + geom_point() + geom_density2d() + coord_fixed()

ggplot(tenders.sp, aes(x=lon, y=lat)) + geom_point() + geom_hex() + coord_fixed()+ggtitle("Plot of the price per m2 of the stalls against type")

QUESTIONS

Do you think there is a specific spatial distribution with regard to the number of bids per trade or type? Or different patterns based on the year? Create a (series of) faceted plot(s) to help you answer that question and include them (and the code leading up to it) in your markdown document.

NUMBER OF BIDS PER TRADE

hide_legend(ggplot(tenders.sp, aes(x=lon, y=lat, color=trade)) + geom_point() + geom_density2d() + coord_fixed()+facet_wrap(~trade)+ggtitle("Number of bids per trade"))

All food trades can be found almost everywhere. This might be due to the fact that Singapore has mixes of people in different racial group living in the same areas.

NUMBER OF BIDS PER TYPE

hide_legend(ggplot(tenders.sp, aes(x=lon, y=lat, color=type)) + geom_point() + geom_density2d() + coord_fixed()+facet_wrap(~type)+ggtitle("Number of bids per type"))

The faceted plot based on type shows that both cooked and lockup stalls are in the same areas in the spatial distribution. This might be due to an increase in demand for food types. Cooked food stalls are heavily concentrated at a single point while lockup stalls were concentrated by in slightly more locations.

SPATIAL POINT PATERNS

library(spatstat)

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))
plot(centres.ppp)

sg <- readOGR("sg-all.shp", "sg-all")
## OGR data source with driver: ESRI Shapefile 
## Source: "sg-all.shp", 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),main = "Heat map of the density of hawker centres in Singapore")

contour(density(centres.ppp, 0.02),main = "Contour of the density of hawker centres in Singapore")

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), main = "Centres weighted by population")

plot(rhohat(centres.ppp, pop,weights=centres.sp$price), main ="Centres weighted by average tender price")

plot(pop,main = "Population density and hawker centres locations")
plot(centres.ppp, add=T)

There are 2 areas with high population but low hakwer centre density namely Choa Chu Kang and Punggol.These are the outliers which have then resulted in the dips towards the end of the population spectrum in the rhohat plots aboved. The reasons for a low hawker centre density might be due to a large number of shopping malls which also provide the same service as the hawker centres. As shown in the plot, most of the hawker centres are located at the central areas. This might be due to the fact the these places have alot of working people as well as tourists. Thus Singapore government decides to place these hawker centres there.