---
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 `netImport()` 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)
 netImport("/folder1/Coupon 2016Q1.csv", "/folder/Ticket 2016Q1.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","","","","","","")))
```

However, when importing from the prezipped DB1B file, just add the argument `zip = TRUE` to the `netImport()` function. This does not apply to the T100 file which can be simply imported by typing `netImport()`.
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 `convertRaw()`. 

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.netDir()`
1. Undirected Network - `make.netUnd()`
1. Path Network - `make.netPath()`
1. Metro Network - To be used as argument in `make.netDir()` and `make.netUnd()`
1. International Option - `make.netInt()`

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 `netMap(list$netDir)` plots a ggplot2 based map with OD information. When specifying the group by carrier option when generating a network, `netMap` 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.netDir(OD_Sample)
netMap(test$netDir, pct = 10)
```

## Extra Functions

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

