```{r include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, 
                      fig.width = 9, fig.height = 6, fig.align = "center", dev.args = list(bg = "#F0F0F0"))
```

```{r setup, message=FALSE, warning=FALSE, cache=FALSE, echo=FALSE}
library(tidyverse)
library(tidygeocoder)
library(kableExtra)
library(ggtext)

theme_set(ggthemes::theme_fivethirtyeight() + 
            theme(plot.title.position = "plot"))
```

```{r get-last-scraped-file}
last_housing_file <- list.files(path = "files", pattern = "housing_listings_(.*).csv", full.names = TRUE) %>% 
  as_tibble() %>% 
  rename(file = value) %>% 
  mutate(date = str_extract(file, "2021-[0|1][0-9]-[0-3][0-9]"),
         date = as.Date(date)) %>% 
  arrange(rev(date)) %>% 
  slice(1) %>% 
  pull(file)

last_file_date <- last_housing_file %>% 
  str_extract_all("[0-9]+-[0-9]+-[0-9]+") %>% 
  unlist() %>% 
  as.Date() %>% 
  format(format = "%d/%m/\'%y")
```

```{r load-data}
data <- read_csv(glue::glue(last_housing_file), col_types = cols()) %>% 
  mutate(location = str_to_title(location),
         location = str_remove_all(location,"#"),
         location = str_trim(location),
         price_per_m2 = price / bruksareal,
         title = str_to_sentence(title))
```

## Calculate the averages for current listings on Finn

### Report generated on `r format(Sys.Date(), format = "%d %B %y")` for data from date `r last_file_date`

```{r calculate-summary}
data_summ <- data %>% 
  summarise(across(c(contains("price"),bruksareal), list(mean = mean, median = median, sem = normentR::sem), na.rm = TRUE),
            across(contains("price"), ~ quantile(.x, probs = c(0.4,0.6), na.rm = TRUE)),
            bruksareal_40 = quantile(bruksareal, probs = 0.4, na.rm = TRUE),
            bruksareal_60 = quantile(bruksareal, probs = 0.6, na.rm = TRUE)) %>% 
  mutate(price_60 = lead(price),
         price_per_m2_60 = lead(price_per_m2)) %>% 
  rename(price_40 = price, 
         price_per_m2_40 = price_per_m2) %>% 
  slice(1) %>% 
  glimpse()
```

Listings within "average" range. This range is defined as the space between the 40th and 60th percentile for price, price per m2, and floor space.

```{r get-most-average-listings}
data %>% 
  filter(price > data_summ$price_40 & price < data_summ$price_60,
         price_per_m2 > data_summ$price_per_m2_40 & price_per_m2 < data_summ$price_per_m2_60,
         bruksareal > data_summ$bruksareal_40 & bruksareal < data_summ$bruksareal_60) %>% 
  select(id,location,address,price,soverom,bruksareal,etasje,price_per_m2,date_changed,ad_url) %>% 
  mutate(price = format(price, big.mark = ".", decimal.mark = ","),
         price_per_m2 = format(price_per_m2, big.mark = ".", decimal.mark = ","),
         bruksareal = glue::glue("{bruksareal}m2"),
         address = str_replace_all(address,"`","'")) %>% 
  arrange(price) %>% 
  kbl() %>%
  kable_styling(bootstrap_options = c("hover", "condensed"), font_size = 14) %>% 
  scroll_box(width = "100%")
```

## Get listings below average price

Listings with price below 40th percentile and price per m2 below 60th percentile.

```{r get-affordable}
data %>% 
  filter(price < data_summ$price_40,
         price_per_m2 < data_summ$price_per_m2_60,
         str_detect(location, "Majorstu[en|a]|Frogner|Fagerborg|Bislett|Marienlyst|Holmenkollen|
                    Sagene|Bjølsen|Tåsen|Grü")) %>%
  select(id,location,address,price,soverom,bruksareal,etasje,price_per_m2,date_changed,ad_url) %>% 
  mutate(price = format(price, big.mark = ".", decimal.mark = ","),
         price_per_m2 = format(price_per_m2, big.mark = ".", decimal.mark = ","),
         bruksareal = glue::glue("{bruksareal}m2"),
         address = str_replace_all(address,"`","'")) %>% 
  arrange(price) %>% 
  kbl() %>%
  kable_styling(bootstrap_options = c("hover", "condensed"), font_size = 14) %>% 
  scroll_box(width = "100%")
```


```{r load-sf-data, eval=FALSE}
roads_load <- sf::read_sf(dsn = "~/Downloads/norway-latest-free.shp/gis_osm_roads_free_1.shp", 
                          layer = "gis_osm_roads_free_1", as_tibble = TRUE)
water_load <- sf::read_sf(dsn = "~/Downloads/norway-latest-free.shp/gis_osm_water_a_free_1.shp",
                          layer = "gis_osm_water_a_free_1", as_tibble = TRUE)

save(roads_load, water_load, file = "files/Oslo_sf_data.RData")
```


```{r process-sf-data}
load("files/Oslo_sf_data.RData")

roads_oslo <- sf::st_crop(roads_load, 
                          xmin = 0.99 * min(data$long, na.rm = TRUE), 
                          xmax = 1.01 * max(data$long, na.rm = TRUE), 
                          ymin = 0.9995 * min(data$lat, na.rm = TRUE), 
                          ymax = 1.0005 * max(data$lat, na.rm = TRUE))
water_oslo <- sf::st_crop(water_load, 
                          xmin = 0.99 * min(data$long, na.rm = TRUE), 
                          xmax = 1.01 * max(data$long, na.rm = TRUE), 
                          ymin = 0.9995 * min(data$lat, na.rm = TRUE), 
                          ymax = 1.0005 * max(data$lat, na.rm = TRUE))
```

```{r create-location-plot, out.width="100%"}
data %>% 
  ggplot(aes(x = long, y = lat)) + 
  #geom_map(data = no_map, map = no_map,
  #         aes(x = long, y = lat, map_id = id),
  #         fill = "#7f7f7f", inherit.aes = FALSE) +
  geom_sf(data = water_oslo,
          fill = "#cedded",  
          color = "#cedded",
          inherit.aes = FALSE) +
  geom_sf(data = roads_oslo,
          size = 0.1, color = "grey70",
          show.legend = "point",
          inherit.aes = FALSE) + 
  geom_polygon(
    data = fhimaps::oslo_ward_map_b2020_default_dt,
    mapping = aes(x = long, y = lat, group = group),
    color = "black",
    fill = "transparent",
    size = 0.2, inherit.aes = FALSE
  ) +
  geom_point(aes(color = log10(price)), alpha = 0.5, stroke = 0) + 
  #geom_bin2d(alpha = 0.5) +
  labs(x = NULL,
       y = NULL,
       color = "log<sub>10</sub>-transformed housing price") + 
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  scale_color_gradient(low = "green", high = "red",
                       guide = guide_colorbar(title.position = "top", title.hjust = 0.5, 
                                              barwidth = 20, barheight = 0.5, ticks = FALSE)) +
  coord_sf() +
  theme(legend.title = element_markdown(),
        axis.ticks = element_line(),
        panel.grid.major = element_blank(),
        plot.background = element_rect(color = "transparent", fill = "#F0F0F0"),
        panel.background = element_rect(color = "transparent", fill = "#F0F0F0", size = 0))
```

