R Markdown for Lecture 1 exercise.

Load the flights dataset.

Calculate the mean delay in arrival for Delta Airlines (DL) (use filter())

Calculate the associated 95% confidence interval.

Do the same for United Airlines (UA) and compare the two. Do their confidence intervals overlap?

Calculate the mode for the delay in arrival for at JFK airport.

save a dataset as .sav with only departing flights from JFK airport.

Load the flights data.

setwd("~/Dropbox/Teaching_MRes_Northumbria/Lecture1")
library(nycflights13)
flights<-nycflights13::flights

Select just the delta flights

require(dplyr)
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
delta<-filter(flights, carrier=="DL")

Means

#remove the missings
# chose to work in same dataset. (for safety reasons you could make a new one!)
delta<-filter(delta, arr_delay!='NA')
mean(delta$arr_delay)
## [1] 1.644341
# store it
mean_delta<-mean(delta$arr_delay)

95% CI

First get the ‘se’

se_delta<-sd(delta$arr_delay)/sqrt(length(delta$arr_delay))
se_delta
## [1] 0.2033937

Now calculate 95%CI.

UL_delta<- (mean_delta + 1.96*se_delta)
LL_delta<- (mean_delta - 1.96*se_delta)
UL_delta
## [1] 2.042993
LL_delta
## [1] 1.245689

United airlines.

All in one go

require(dplyr)
united<-filter(flights, carrier=="UA")
united<-filter(united, arr_delay!='NA')
mean(united$arr_delay)
## [1] 3.558011
# store it
mean_united<-mean(united$arr_delay)
se_united<-sd(united$arr_delay)/sqrt(length(united$arr_delay))
se_united
## [1] 0.1704989
UL_united<- (mean_united + 1.96*se_united)
LL_united<- (mean_united - 1.96*se_united)
UL_united
## [1] 3.892189
LL_united
## [1] 3.223833

Conclusion: Delta vs. United.

The 95%CI’s do not overlap. United [3.22 to 3.89] is significantly slower in terms of arrival time than Delta [1.25 to 2.04].

JFK airport

Make a dataset.

jfk<- filter(flights, origin=="JFK")
# remove the missings.
jfk<- filter(jfk, arr_delay!='NA')

Calculate the mode.

library(modeest)
mlv(jfk$arr_delay,  method='mfv')
## [1] -13

The mode is -13. The most common value in the dataset is thus 13 minutes early!

Write away the data.

require(haven)
## Loading required package: haven
write_sav(jfk, 'jfk.sav')

The end.

sessionInfo()
## R version 4.3.2 (2023-10-31)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.4
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: Europe/London
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] haven_2.5.3        modeest_2.4.0      dplyr_1.1.3        nycflights13_1.0.2
## 
## loaded via a namespace (and not attached):
##  [1] statip_0.2.3        jsonlite_1.8.7      highr_0.10         
##  [4] compiler_4.3.2      fBasics_4032.96     rpart_4.1.21       
##  [7] tidyselect_1.2.0    stable_1.1.6        cluster_2.1.4      
## [10] jquerylib_0.1.4     timeSeries_4031.107 rmutil_1.1.10      
## [13] yaml_2.3.7          fastmap_1.1.1       R6_2.5.1           
## [16] generics_0.1.3      knitr_1.45          forcats_1.0.0      
## [19] tibble_3.2.1        spatial_7.3-17      timeDate_4022.108  
## [22] bslib_0.5.1         pillar_1.9.0        rlang_1.1.1        
## [25] utf8_1.2.4          stabledist_0.7-1    cachem_1.0.8       
## [28] xfun_0.41           sass_0.4.7          cli_3.6.1          
## [31] magrittr_2.0.3      digest_0.6.33       rstudioapi_0.15.0  
## [34] hms_1.1.3           lifecycle_1.0.3     clue_0.3-65        
## [37] vctrs_0.6.4         evaluate_0.23       glue_1.6.2         
## [40] fansi_1.0.5         rmarkdown_2.25      tools_4.3.2        
## [43] pkgconfig_2.0.3     htmltools_0.5.7