::p_load(sf, spdep, GWmodel, SpatialML, tidyverse, tmap, ggpubr, olsrr, devtools,
pacman tidymodels, rsample)
In Class Exericse 9
Preparing Data
Reading data file to rds
<- read_rds("Data/Aspatial/mdata.rds") mdata
Data Sampling
set.seed(1234)
<- initial_split(mdata,prop = 6.5/10,)
resale_split <- training(resale_split)
train_data <- testing(resale_split) test_data
write_rds(train_data, "Data/rds/train_data.rds")
write_rds(train_data, "Data/rds/test_data.rds")
Building a non-spatial multiple linear regression
<- lm(resale_price ~ floor_area_sqm +
price_mlr + remaining_lease_mths +
storey_order + PROX_ELDERLYCARE + PROX_HAWKER +
PROX_CBD + PROX_PARK + PROX_MALL +
PROX_MRT + WITHIN_350M_KINDERGARTEN +
PROX_SUPERMARKET + WITHIN_350M_BUS +
WITHIN_350M_CHILDCARE
WITHIN_1KM_PRISCH,data=train_data)
summary(price_mlr)
write_rds(price_mlr, "Data/rds/price_mlr.rds")
GWR
<- as_Spatial(train_data)
train_data_sp train_data_sp
Preparing Coordinate Data
Extracting Coordinates Data
<- st_coordinates(mdata)
coords <- st_coordinates(train_data)
coords_train <- st_coordinates(test_data) coords_test
<- write_rds(coords_train, "Data/rds/coords_train.rds")
coords_train <- write_rds(coords_test, "Data/rds/coords_test.rds") coords_test
Dropping the geometry field
<- train_data %>%
train_data st_drop_geometry()
Calibrating Random Forest
set.seed(1234)
<- ranger(resale_price ~ floor_area_sqm +
rf + remaining_lease_mths +
storey_order + PROX_ELDERLYCARE + PROX_HAWKER +
PROX_CBD + PROX_PARK + PROX_MALL +
PROX_MRT + WITHIN_350M_KINDERGARTEN +
PROX_SUPERMARKET + WITHIN_350M_BUS +
WITHIN_350M_CHILDCARE
WITHIN_1KM_PRISCH,data=train_data)
print(rf)
set.seed(1234)
<- grf(resale_price ~ floor_area_sqm +
gwRF_adaptive + remaining_lease_mths +
storey_order + PROX_ELDERLYCARE + PROX_HAWKER +
PROX_CBD + PROX_PARK + PROX_MALL +
PROX_MRT + WITHIN_350M_KINDERGARTEN +
PROX_SUPERMARKET + WITHIN_350M_BUS +
WITHIN_350M_CHILDCARE
WITHIN_1KM_PRISCH,dframe=train_data,
bw = 55,
kernel = "adaptive",
coords=coords_train)
<- cbind(test_data, coords_test) %>%
test_data st_drop_geometry()
<- predict.grf(gwRF_adaptive,
gwRF_pred
test_data,x.var.name="X",
y.var.name="Y",
local.w=1,
global.w=0)
<- as.data.frame(gwRF_pred) gwRF_pred_df