---
title: "Introduction to SKYNET"
author: "Filipe Teixeira"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to SKYNET}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

SKYNET is a flexible R package that allows generating bespoke air transport statistics for urban studies based on publicly available data from the Bureau of Transport Statistics (BTS) in the United States. 

## SKYNET's segments

SKYNET is effectively divided into four segments:

1. Import Data
1. Generate Air Networks
1. Plot Air Networks


## Import Data

To import data, simply type `import_db1b()` or `import_t100()` including the path to your desired file. 
Note: we recommend naming the files with a similar structure as `Ticket 2016Q1.csv` or `Coupon 2016Q1.csv` respectively.
    
```{r, eval=FALSE}
 library(skynet)
 import_db1b("folder/Coupon 2016Q1.csv", "folder/Ticket 2016Q1.csv")
 import_t100("folder/T100_2016.csv")
```

The BTS DB1B data consists of 2 sets of files, `Coupon` and `Ticket`.
They can be both downloaded at https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=289 and https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=272 respectively.

Despite being possible to download the complete zipped file, which includes all variables, due to its size, we recommend selecting the following set. 


```{r, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
knitr::kable(data.frame(Coupon = c("Itinerary ID", "Market ID", "Sequence Number", "Origin City Market ID",  
"Origin", "Year", "Quarter", "Destination City Market ID", "Destination", "Trip Break", "Operating Carrier", 
"Distance", "Gateway"),
Ticket = c("Itinerary ID", "Roundtrip", "Itinerary Yield", "Passengers",
"Itinerary Fare", "Bulkfare Indicator", "Distance Full","","","","","","")))
```

Since version 1.0.2 that the import method changed being the `netimport()` function no longer available.
When importing from the prezipped DB1B file, just add the argument `zip = TRUE` to the `import_db1b()` function. This does not apply to the T100 file which can be simply imported by typing `import_t100()`.
In order to save space, it is possible as well to import the prezipped file, and convert it to a smaller file with only the necessary variables, with the function `convert_raw()`. 

When importing files from the T100 dataset, we recommend naming the file as `T100 year mkt` for the Market dataset and `T100 year seg` for the Segment dataset.


## Create networks

SKYNET creates three types of networks and an extra option:

1. Directed Network - `make_net_dir()`
1. Undirected Network - `make_net_und()`
1. Path Network - `make_net_path()`
1. Metro Network - To be used as argument in `make_net_dir()` and `make_net_und()`
1. International Option - `make_net_int()`

When generating a network, SKYNET, creates a list which includes:

1. Dataframe from original data (example below)
1. iGraph object
1. Dataframe with nodes(airports) statistics


```{r, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(skynet)
library(dplyr)
library(kableExtra)
options(knitr.table.format = "html") 
data("OD_Sample")
rownames(OD_Sample) <- NULL
knitr::kable(head(OD_Sample, 5)) %>% kable_styling()
```

When generating a network with SKYNET, it is possible use the following arguments:  
1. `carriers` - groups OD data per carrier when `TRUE`  

To extract the backbone of the network:  
1. `cap` (to be used with `pct`) - filters the network based on a given percentage (default percentage = 10%)  
1. `disp` (to be used with `alpha`) - filters the network using the Serrano et all backbone extraction algorithm (default alpha = 0.003)  

## Create Maps

One of SKYNET's advantages is the possibility of plotting maps without having to recur to external software.

Typing `net_map(skynet_object)` plots a ggplot2 based map with OD information. When specifying the group by carrier option when generating a network, `net_map()` distinguishes carriers with different colors. The `pct` argument allow to plot only a percentage of the available data.
It is important to point the path to the dataframe created by SKYNET.

```{r, echo=FALSE, message=FALSE, warning=FALSE,dpi = 300, fig.width = 6, fig.height= 4, out.width=500, }
library(skynet)
data("OD_Sample")
test <- make_net_dir(OD_Sample)
net_map(test, pct = 10)
```

## Extra Functions

SKYNET, allows as well to perform quick searches on both airports and carriers, by their IATA code. `find_airport()`, `find_carrier()`. 

## Bootnet

With version 1.0.2, we included the option to bootstrap networks and retrieve certain network statistics.

```{r}
library(skynet)
test <- make_net_dir(OD_Sample)
boot_network(test$gDir, n = 10)
```

