---
title: "In-Class Exercise 09"
date: "13 March 2023"
date-modified: "`r Sys.Date()`"
author: "Xinyee How"
format: html
editor: visual
execute: 
  echo: true
  eval: true
  warning: false
---

Installing packages

```{r}
pacman::p_load(olsrr, ggpubr, sf, spdep, GWmodel, tmap, tidyverse, tidymodels, SpatialML, devtools, rsample)
```

Importing data

```{r}
mdata <- read_rds("data/aspatial/mdata.rds")
```

```{r}
set.seed(1234)
resale_split <- initial_split(mdata, prop = 6.5/10,)
train_data <- training(resale_split)
test_data <- testing(resale_split)
```

Building a non-spatial MLR

```{r}
price_mlr = lm(resale_price ~ floor_area_sqm +
                 storey_order + remaining_lease_mths +
                 PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT +
                 PROX_PARK + PROX_MALL + PROX_SUPERMARKET + 
                 WITHIN_350M_KINDERGARTEN + WITHIN_350M_CHILDCARE + 
                 WITHIN_350M_BUS + WITHIN_1KM_PRISCH,
               data= train_data)
summary(price_mlr)
```

predictive model - only r-squared value important

Saving data into file

```{r}
write_rds(price_mlr, "data/aspatial/price_mlr.rds")
```

## GWR predictive method

### Converting sf dataframe to SpatialPointDataFrame

sf is a list object --\> in order for package "ranger" to run, we need to extract and convert

```{r}
train_data_sp <- as_Spatial(train_data)
train_data_sp
```

### Computing adaptive bandwidth

```{r}
#| eval: false
bw_adaptive <- bw.gwr(resale_price ~ floor_area_sqm +
                  storey_order + remaining_lease_mths +
                  PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER +
                  PROX_MRT + PROX_PARK + PROX_MALL + 
                  PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +
                  WITHIN_350M_CHILDCARE + WITHIN_350M_BUS +
                  WITHIN_1KM_PRISCH,
                  data=train_data_sp,
                  approach="CV",
                  kernel="gaussian",
                  adaptive=TRUE,
                  longlat=FALSE)
```

Preparing coordinates data

Extracting coordinates data

```{r}
coords <- st_coordinates(mdata)
coords_train <- st_coordinates(train_data)
coords_test <- st_coordinates(test_data)
```

Saving output for future use

```{r}
coords_train <- write_rds(coords_train, "data/model/coords_train.rds")
coords_test <- write_rds(coords_test, "data/model/coords_test.rds")
```

Dropping geometry field

```{r}
train_data <- train_data %>% st_drop_geometry()
```

Calibrating random forest - using Ranger

```{r}
#| eval: false
set.seed(1234)
rf <- ranger(resale_price ~ floor_area_sqm +
                 storey_order + remaining_lease_mths +
                 PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT +
                 PROX_PARK + PROX_MALL + PROX_SUPERMARKET + 
                 WITHIN_350M_KINDERGARTEN + WITHIN_350M_CHILDCARE + 
                 WITHIN_350M_BUS + WITHIN_1KM_PRISCH,
               data= train_data)
summary(rf)
```

using spatialML package

```{r}
#| eval: false
set.seed(1234)
gwRF_adaptive <- grf(formula = resale_price ~ floor_area_sqm +
                 storey_order + remaining_lease_mths +
                 PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT +
                 PROX_PARK + PROX_MALL + PROX_SUPERMARKET + 
                 WITHIN_350M_KINDERGARTEN + WITHIN_350M_CHILDCARE + 
                 WITHIN_350M_BUS + WITHIN_1KM_PRISCH,
               dframe= train_data, 
               bw=55, #need to calculate this out
               kernel="adaptive",
               coords=coords_train)
```

Saving output for future use

```{r}
#| eval: false 
write_rds(gwRF_adaptive, "data/model/gwRF_adaptive.rds")
```

## Predicting using test data

### Preparing test data

```{r}
test_data <- cbind(test_data, coords_test) %>% st_drop_geometry()
```

#### Predicting with test data

coordinates -\> only can calibrate SVY21 values

output is a vector

```{r}
#| eval: false
gwRF_pred <- predict.grf(gwRF_adaptive,
                         test_data,
                         x.var.name="X",
                         y.var.name = "Y",
                         local.w=1,
                         global.w=0)
```

Converting predicting output into data frame
