Available properties

Column

Calendar View

Column

Share in Week

Time Series

Unavailable properties

Column

Calendar View

Column

Share in Week

Time Series

Map View

Column

AirBnB Lets in Dublin - Source: http://insideairbnb.com

Column

Totally Unavailable

Distribution of Availability

Basic Data

Column

List View

---
title: "AirBnB Letting Patterns"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
    social: menu
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(ggplot2)
library(lubridate)
library(ggthemes)
library(ggcal)
library(plotly)
library(sf)
library(tmap)
library(DT)

read_csv('calendar.csv.gz') %>% 
  mutate(available = available == 't',
         price = as.numeric(str_extract(price,'[0-9.]+'))) -> 
  calendar 
read_csv("listings.csv.gz") %>% 
  st_as_sf(coords=c('longitude','latitude'),crs=4326) %>%
  mutate(FullyBooked=availability_365==0) -> 
  listings

avail_length <- function(lseq) {
  rl <- rle(lseq)
  rl <- rl$lengths[rl$values]
  rl <- mean(rl)
  ifelse(is.nan(rl),0,rl)
}

calendar %>% group_by(listing_id) %>% 
  summarise(al=avail_length(available)) -> avail

listings %>% 
  left_join(avail,by=c("id"="listing_id")) ->
  listings

calendar %>%
  mutate(dow=wday(date,label=TRUE)) %>% 
  group_by(listing_id,dow) %>% 
  summarise(ct=sum(available)) %>% 
  group_by(listing_id) %>% 
  filter(sum(ct)!=0) %>% 
  mutate(dowprop = ct/sum(ct)) %>% 
  select(-ct) %>% 
  spread(key=dow,value=dowprop)  %>% 
  {. ->> tmp} %>% 
  .[,-1] %>% 
  prcomp %>%
  .$x %>% 
  as_data_frame %>% 
  mutate(listing_id=tmp$listing_id) %>%
  left_join(tmp) -> week_use
# listings %>% left_join(week_use,by=c('id'='listing_id')) %>% filter(!is.na(PC1)) -> onk
# tm_shape(onk) + tm_dots(col='PC1',size=0.25)
# 
```


Available properties
=====================================  

Column {data-width=750}
-----------------------------------------------------------------------

### Calendar View 

```{r}
calendar %>% count(date,wt=available) %>% 
  rename(`Available Properties`=n,Date=date) %>%
  mutate(`Week Day`=wday(Date,label=TRUE),Week=week(Date))  -> res 
ggcal(res$Date,res$`Available Properties`) + scale_fill_viridis_c()
```

Column {data-width=350}
-----------------------------------------------------------------------

### Share in Week

```{r}
res %>% 
  filter(Week < 53) %>%
  ggplot(aes(x=Week,y=`Available Properties`,fill=`Week Day`)) +    
  geom_col(position='fill') + scale_fill_hue()
```

### Time Series

```{r}
res %>% ggplot(aes(x=Date,y=`Available Properties`)) + geom_line(col='red')
```


Unavailable properties
=====================================  

Column {data-width=750}
-----------------------------------------------------------------------

### Calendar View

```{r}
calendar %>% count(date,wt=!available) %>% 
  rename(`Unavailable Properties`=n,Date=date) %>%
  mutate(`Week Day`=wday(Date,label=TRUE),Week=week(Date))  -> res 
ggcal(res$Date,res$`Unavailable Properties`) + scale_fill_viridis_c() 
```

Column {data-width=350}
-----------------------------------------------------------------------

### Share in Week

```{r}
res %>% 
  filter(Week < 53) %>%
  ggplot(aes(x=Week,y=`Unavailable Properties`,fill=`Week Day`)) +    
  geom_col(position='fill') + scale_fill_hue() 
```

### Time Series

```{r}
res %>% ggplot(aes(x=Date,y=`Unavailable Properties`)) + geom_line(col='red')
```

Map View
=======================================
Column {data-width=750}
-----------------------------------------------------------------------

### AirBnB Lets in Dublin - Source: http://insideairbnb.com

```{r}
tmap_mode('view')
tm_shape(listings) + tm_dots(col="al",title="Mean Availability Interval",style='fixed',breaks=c(0,1,35,106,232,366))
```



Column {data-width=350}
-----------------------------------------------------------------------

### Totally Unavailable

```{r}
tmap_mode('view')
tm_shape(listings) + tm_dots(col="FullyBooked",title="Unavailable 365 Days")
```

### Distribution of Availability

```{r}
avail %>% 
  rename(`Mean Availability Length`=al) %>%
  ggplot(aes(x=`Mean Availability Length`)) + 
  geom_dotplot(binwidth=1,
               method='histodot',
               stackdir="center",
               col='darkblue')
```

Basic Data
=======================================

Column {data-width=750}
-----------------------------------------------------------------------

### List View

```{r} 
listings %>% filter(row_number()<50) %>% as.data.frame %>% select(name,host_name,neighbourhood,room_type,price,minimum_nights,availability_365) %>% as_data_frame -> l2
datatable(l2)
```