-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdesc_analysis.Rmd
217 lines (159 loc) · 5.14 KB
/
desc_analysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
title: "Air Traffic Challenge"
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
toc: true
number_sections: true
df_print: paged
---
# Data manipulation (daily stats)
```{r,warning=FALSE, message=FALSE}
library(dplyr)
library(ggplot2)
library(sf)
library(varhandle)
library(tidyr)
library(ggplot2)
library(hrbrthemes)
library(lubridate)
twist_zrh_cleaned <-readRDS("twist_zrh_cleaned.RDS")
# flights per day, median / mean delays per day, precipitation etc.
twist_daily <-twist_zrh_cleaned %>%
group_by(date,start_landing) %>%
summarize(flights_n=n(),
mean_delay=as.numeric(mean(diff_in_secs)),
median_delay=as.numeric(median(diff_in_secs)),
precip=sum(precip),
temp_avg=mean(temp_avg),
precip=sum(precip),
lightnings_hour_f=sum(lightnings_hour_f))
```
# Descriptive Analysis
```{r,warning=FALSE, message=FALSE}
# delay (mean)
ggplot(twist_daily, aes(as.Date(date), mean_delay))+
geom_line()+
facet_wrap(~start_landing)+
theme_ipsum()
# delay (median)
ggplot(twist_daily, aes(as.Date(date), median_delay))+
geom_line()+
geom_smooth(se=F)+
facet_wrap(~start_landing)+
theme_ipsum()
# flights per day
ggplot(twist_daily, aes(as.Date(date),flights_n))+
geom_line()+
geom_smooth(se=F)+
facet_wrap(~start_landing)+
theme_ipsum()+
labs(title="Number of Flights")
# Temperature
ggplot(twist_daily, aes(as.Date(date),temp_avg))+
geom_line()+
geom_smooth(se=F)+
theme_ipsum()+
labs(title="Temperature (avg)")
```
# Visualization of Mean Delay per Flight Routes
```{r}
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr,sf,lwgeom,ggplot2,maps,maptools,rgeos)
#read in flight data
flight_sf <- readRDS("flight_sf.RDS")
#read in flight data
#filter flights for a single date
vizdata_del <- flight_sf %>%
group_by(origin_destination,start_landing) %>%
summarize(mean_delay=mean(diff/60),n=n())
#create a new geometry : lines connecting Zurich Airport and the destination/origin of the flight
vizdata_del$flightline <- sf::st_union(vizdata_del$geometry, st_as_sfc("POINT(8 47)",crs=st_crs("+proj=longlat +datum=WGS84 +no_defs"))) %>%
st_cast("LINESTRING")
#get worldmap
world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
#plot on top (new ggplot2 2.3.0 version needed!)
ggplot(vizdata_del)+
geom_sf(aes(geometry=flightline))+
geom_sf(data=world1)
#we can now set the "flightline"-geometry as the main geometry in our dataset
st_geometry(vizdata_del) = "flightline"
#transform worldmap and flights into spheric projection
world2 <- sf::st_transform(
world1,
"+proj=laea +y_0=0 +lon_0=8 +lat_0=47 +ellps=WGS84 +no_defs"
)
flights2 <- sf::st_transform(
vizdata_del,
"+proj=laea +y_0=0 +lon_0=8 +lat_0=47 +ellps=WGS84 +no_defs"
)
#Plot!
ggplot() +
geom_sf(data=world2, color="white",size=0.2)+
geom_sf(data=flights2 %>% filter(n>=100), aes(color=as.numeric(mean_delay), alpha=0.8),size=0.2,show.legend = "line")+
theme_void()+
scale_color_viridis_c(name="")+
guides(alpha=F)+
coord_sf(ndiscr=1000)+
theme(plot.background = element_rect(fill="#f5f5f2"))+
facet_wrap(~start_landing)+
labs(title="Mean Delay per Flight Route",subtitle="Routes with >=100 Flights, 2017\n\n")
```
# Flights by Night (23:30-6:00)
```{r}
#dummy for nightflights
twist_zrh_night <- readRDS("twist_zrh_cleaned.RDS") %>%
mutate(hour=hour(effective_time)+minute(effective_time)/60) %>%
mutate(night=ifelse(hour<6 |hour>=23.5,1,0)) %>%
group_by(date,start_landing, night) %>%
summarize(flights_n=n())%>%
mutate(freq =flights_n / sum(flights_n)*100)
twist_zrh_night %>%
group_by(start_landing, night) %>%
summarize(n=sum(flights_n))
# ----
devtools::install_github("jayjacobs/ggcal")
library(ggcal)
cal <- twist_zrh_night %>% filter(night==1)
```
# Flights between 23:30-6:00
## Number per day
```{r}
ggcal(cal$date,cal$flights_n)
```
## In proportion of daily flights
```{r}
ggcal(cal$date,cal$freq)
```
#
```{r}
```
```{r}
```
<!-- # Modelling with xgboost --- -->
<!-- ```{r} -->
<!-- data <- twist_zrh_cleaned %>% mutate(delay=ifelse(diff_in_secs>1800,1,0)) %>% -->
<!-- select(-geometry) -->
<!-- install.packages("xgboost", repos="http://dmlc.ml/drat/", type = "source") -->
<!-- library(xgboost) -->
<!-- library(magrittr) -->
<!-- library(dplyr) -->
<!-- library(Matrix) -->
<!-- # Partition data -->
<!-- set.seed(1234) -->
<!-- ind <- sample(2, nrow(data), replace = T, prob = c(0.8, 0.2)) -->
<!-- train <- data[ind==1,] -->
<!-- test <- data[ind==2,] -->
<!-- # Create matrix - One-Hot Encoding for Factor variables -->
<!-- trainm <- sparse.model.matrix(delay ~ .-1, data = train) -->
<!-- head(trainm) -->
<!-- train_label <- train[,"delay"] -->
<!-- train_matrix <- xgb.DMatrix(data = as.matrix(trainm), -->
<!-- label = train_label) -->
<!-- # -->
<!-- testm <- sparse.model.matrix(admit~.-1, data = test) -->
<!-- test_label <- test[,"delay"] -->
<!-- test_matrix <- xgb.DMatrix(data = as.matrix(testm), label = test_label) -->
<!-- #### WORK IN PROGRESS -->
<!-- ``` -->