Part B1. Sampling Data

Import Data

If your R Markdown is NOT in the same folder as your data, please set your working directory using setwd() first. Here is an example if you are using Medusa setwd("\\medusa\StudentWork\(Your UTOR ID)\GGR276\Lab1") and if you are using your own computer ‘setwd(C:/)’. You will need to change the code to reflect your personal directory. Then load your data. When loading in the data try to get your ‘Data variable name’ short and easy to type! If you change it from what is listed in the rmarkdown instructions you will need to change it.

We are loading the same aviation facilities dataset as Assignment 1.

Once the dataset is loaded, you will need to investigate the dataset to determine if you have any NA values. It is also recommended you determine the descriptive statistics before proceeding. We are building on the skills you learned in lab 1. This includes summary statistics and filtering your data. In statistics, each step builds on the previous.

setwd("~/UTM/Year 5/Summer/Term 2/GGR276/Assignments/Assignment 2")
aviation_facilities <- read.csv("aviationfacilities.csv", sep = ",", header = TRUE)

View your dataframe and determine the distribution of the all the variables in this dataframe; are you missing any data?:

#ToDo
summary(aviation_facilities)
##       EFF_DATE        SITE_NO        SITE_TYPE_CODE      STATE_CODE   
##  Length   :19707   Min.   :  103   Length   :19707   Length   :19707  
##  N.unique :    1   1st Qu.: 5251   N.unique :    6   N.unique :   60  
##  N.blank  :    0   Median :13294   N.blank  :    0   N.blank  :  147  
##  Min.nchar:   10   Mean   :15633   Min.nchar:    1   Min.nchar:    0  
##  Max.nchar:   10   3rd Qu.:23404   Max.nchar:    1   Max.nchar:    2  
##                    Max.   :90970                                      
##                                                                       
##       ARPT_ID             CITY          COUNTRY_CODE      REGION_CODE   
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique :19707   N.unique : 8044   N.unique :   16   N.unique :   10  
##  N.blank  :    0   N.blank  :    0   N.blank  :    0   N.blank  :  147  
##  Min.nchar:    3   Min.nchar:    3   Min.nchar:    2   Min.nchar:    0  
##  Max.nchar:    4   Max.nchar:   35   Max.nchar:    2   Max.nchar:    3  
##                                                                         
##                                                                         
##       ADO_CODE         STATE_NAME       COUNTY_NAME    COUNTY_ASSOC_STATE
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707   
##  N.unique :   23   N.unique :   60   N.unique : 1802   N.unique :   73   
##  N.blank  : 3753   N.blank  :  147   N.blank  :    0   N.blank  :    0   
##  Min.nchar:    0   Min.nchar:    0   Min.nchar:    3   Min.nchar:    2   
##  Max.nchar:    3   Max.nchar:   17   Max.nchar:   21   Max.nchar:    2   
##                                                                          
##                                                                          
##      ARPT_NAME     OWNERSHIP_TYPE_CODE FACILITY_USE_CODE    LAT_DEG     
##  Length   :19707   Length   :19707     Length   :19707   Min.   : 5.00  
##  N.unique :18487   N.unique :    6     N.unique :    2   1st Qu.:33.00  
##  N.blank  :    0   N.blank  :    0     N.blank  :    0   Median :39.00  
##  Min.nchar:    2   Min.nchar:    2     Min.nchar:    2   Mean   :38.48  
##  Max.nchar:   50   Max.nchar:    2     Max.nchar:    2   3rd Qu.:42.00  
##                                                          Max.   :76.00  
##                                                                         
##     LAT_MIN         LAT_SEC          LAT_HEMIS      LAT_DECIMAL    
##  Min.   : 0.00   Min.   : 0.00   Length   :19707   Min.   :-14.33  
##  1st Qu.:15.00   1st Qu.:14.36   N.unique :    2   1st Qu.: 33.97  
##  Median :30.00   Median :29.94   N.blank  :    0   Median : 39.06  
##  Mean   :29.68   Mean   :29.56   Min.nchar:    1   Mean   : 38.98  
##  3rd Qu.:44.00   3rd Qu.:44.89   Max.nchar:    1   3rd Qu.: 42.77  
##  Max.   :59.00   Max.   :60.00                     Max.   : 76.53  
##                                                                    
##     LONG_DEG         LONG_MIN        LONG_SEC         LONG_HEMIS   
##  Min.   : 14.00   Min.   : 0.00   Min.   : 0.00   Length   :19707  
##  1st Qu.: 82.00   1st Qu.:14.00   1st Qu.:14.30   N.unique :    2  
##  Median : 91.00   Median :29.00   Median :29.96   N.blank  :    0  
##  Mean   : 94.98   Mean   :29.13   Mean   :29.72   Min.nchar:    1  
##  3rd Qu.:102.00   3rd Qu.:44.00   3rd Qu.:45.00   Max.nchar:    1  
##  Max.   :177.00   Max.   :59.00   Max.   :59.99                    
##                                                                    
##   LONG_DECIMAL     SURVEY_METHOD_CODE      ELEV        ELEV_METHOD_CODE
##  Min.   :-177.38   Length   :19707    Min.   : -223   Length   :19707  
##  1st Qu.:-102.13   N.unique :    1    1st Qu.:  275   N.unique :    3  
##  Median : -91.81   N.blank  :    0    Median :  755   N.blank  :  466  
##  Mean   : -94.93   Min.nchar:    1    Mean   : 1189   Min.nchar:    0  
##  3rd Qu.: -82.48   Max.nchar:    1    3rd Qu.: 1273   Max.nchar:    1  
##  Max.   : 174.11                      Max.   :12442                    
##                                                                        
##     MAG_VARN          MAG_HEMIS     MAG_VARN_YEAR       TPA        
##  Min.   : 0.000   Length   :19707   Min.   :1904   Min.   : 130.0  
##  1st Qu.: 3.000   N.unique :    3   1st Qu.:1985   1st Qu.: 800.0  
##  Median : 7.000   N.blank  : 5314   Median :1985   Median : 800.0  
##  Mean   : 8.384   Min.nchar:    0   Mean   :1993   Mean   : 883.4  
##  3rd Qu.:13.000   Max.nchar:    1   3rd Qu.:1995   3rd Qu.: 968.5  
##  Max.   :31.000                     Max.   :2030   Max.   :4500.0  
##  NAs    :5314                       NAs    :5438   NAs    :19169   
##      CHART_NAME    DIST_CITY_TO_AIRPORT   DIRECTION_CODE     ACREAGE       
##  Length   :19707   Min.   : 0.000       Length   :19707   Min.   :    0.0  
##  N.unique :   58   1st Qu.: 1.000       N.unique :   17   1st Qu.:   10.0  
##  N.blank  :  437   Median : 3.000       N.blank  :  207   Median :   52.0  
##  Min.nchar:    0   Mean   : 3.888       Min.nchar:    0   Mean   :  290.8  
##  Max.nchar:   26   3rd Qu.: 5.000       Max.nchar:    3   3rd Qu.:  195.0  
##                    Max.   :96.000                         Max.   :55000.0  
##                    NAs    :152                            NAs    :10046    
##    RESP_ARTCC_ID      COMPUTER_ID        ARTCC_NAME     FSS_ON_ARPT_FLAG
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique :   35   N.unique :   35   N.unique :   35   N.unique :    3  
##  N.blank  :    0   N.blank  :    0   N.blank  :    0   N.blank  : 8898  
##  Min.nchar:    3   Min.nchar:    3   Min.nchar:    4   Min.nchar:    0  
##  Max.nchar:    4   Max.nchar:    3   Max.nchar:   25   Max.nchar:    1  
##                                                                         
##                                                                         
##        FSS_ID           FSS_NAME          PHONE_NO        TOLL_FREE_NO  
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique :   86   N.unique :   86   N.unique :   19   N.unique :   19  
##  N.blank  :    0   N.blank  :    0   N.blank  :18797   N.blank  :   85  
##  Min.nchar:    3   Min.nchar:    4   Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:    4   Max.nchar:   17   Max.nchar:   12   Max.nchar:   14  
##                                                                         
##                                                                         
##      ALT_FSS_ID       ALT_FSS_NAME    ALT_TOLL_FREE_NO      NOTAM_ID    
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique :    4   N.unique :    4   N.unique :    4   N.unique : 1849  
##  N.blank  :19489   N.blank  :19489   N.blank  :19489   N.blank  :13968  
##  Min.nchar:    0   Min.nchar:    0   Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:    3   Max.nchar:    9   Max.nchar:   14   Max.nchar:    4  
##                                                                         
##                                                                         
##      NOTAM_FLAG     ACTIVATION_DATE     ARPT_STATUS    FAR_139_TYPE_CODE
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique :    3   N.unique : 1057   N.unique :    3   N.unique :   11  
##  N.blank  : 6623   N.blank  :  929   N.blank  :    0   N.blank  :19189  
##  Min.nchar:    0   Min.nchar:    0   Min.nchar:    1   Min.nchar:    0  
##  Max.nchar:    1   Max.nchar:    7   Max.nchar:    2   Max.nchar:    5  
##                                                                         
##                                                                         
##  FAR_139_CARRIER_SER_CODE ARFF_CERT_TYPE_DATE     NASP_CODE    
##  Length   :19707          Length   :19707     Length   :19707  
##  N.unique :    3          N.unique :  142     N.unique :  119  
##  N.blank  :19189          N.blank  :19189     N.blank  :16220  
##  Min.nchar:    0          Min.nchar:    0     Min.nchar:    0  
##  Max.nchar:    1          Max.nchar:    7     Max.nchar:    7  
##                                                                
##                                                                
##  ASP_ANLYS_DTRM_CODE     CUST_FLAG      LNDG_RIGHTS_FLAG   JOINT_USE_FLAG 
##  Length   :19707     Length   :19707   Length   :19707   Length   :19707  
##  N.unique :    5     N.unique :    3   N.unique :    3   N.unique :    3  
##  N.blank  :  463     N.blank  :15474   N.blank  :15502   N.blank  :15326  
##  Min.nchar:    0     Min.nchar:    0   Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:   13     Max.nchar:    1   Max.nchar:    1   Max.nchar:    1  
##                                                                           
##                                                                           
##    MIL_LNDG_FLAG   INSPECT_METHOD_CODE   INSPECTOR_CODE   LAST_INSPECTION 
##  Length   :19707   Length   :19707     Length   :19707   Length   :19707  
##  N.unique :    3   N.unique :    6     N.unique :    4   N.unique : 1510  
##  N.blank  :15242   N.blank  : 4044     N.blank  :    0   N.blank  :14066  
##  Min.nchar:    0   Min.nchar:    0     Min.nchar:    1   Min.nchar:    0  
##  Max.nchar:    1   Max.nchar:    1     Max.nchar:    1   Max.nchar:   10  
##                                                                           
##                                                                           
##  LAST_INFO_RESPONSE     FUEL_TYPES    AIRFRAME_REPAIR_SER_CODE
##  Length   :19707    Length   :19707   Length   :19707         
##  N.unique : 3540    N.unique :   78   N.unique :    4         
##  N.blank  : 5936    N.blank  :15809   N.blank  :13660         
##  Min.nchar:    0    Min.nchar:    0   Min.nchar:    0         
##  Max.nchar:   10    Max.nchar:   18   Max.nchar:    5         
##                                                               
##                                                               
##  PWR_PLANT_REPAIR_SER  BOTTLED_OXY_TYPE   BULK_OXY_TYPE        LGT_SKED    
##  Length   :19707      Length   :19707   Length   :19707   Length   :19707  
##  N.unique :    4      N.unique :    5   N.unique :    5   N.unique :    3  
##  N.blank  :13661      N.blank  :15695   N.blank  :15743   N.blank  :13078  
##  Min.nchar:    0      Min.nchar:    0   Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:    5      Max.nchar:    8   Max.nchar:    8   Max.nchar:    7  
##                                                                            
##                                                                            
##     BCN_LGT_SKED     TWR_TYPE_CODE   SEG_CIRCLE_MKR_FLAG   BCN_LENS_COLOR 
##  Length   :19707   Length   :19707   Length   :19707     Length   :19707  
##  N.unique :    3   N.unique :    6   N.unique :    4     N.unique :    8  
##  N.blank  :15310   N.blank  :    0   N.blank  : 5006     N.blank  :15208  
##  Min.nchar:    0   Min.nchar:    4   Min.nchar:    0     Min.nchar:    0  
##  Max.nchar:    7   Max.nchar:   11   Max.nchar:    3     Max.nchar:    3  
##                                                                           
##                                                                           
##    LNDG_FEE_FLAG    MEDICAL_USE_FLAG  ARPT_PSN_SOURCE  POSITION_SRC_DATE
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique :    3   N.unique :    3   N.unique :   19   N.unique : 5342  
##  N.blank  :10951   N.blank  :16697   N.blank  : 6053   N.blank  : 6345  
##  Min.nchar:    0   Min.nchar:    0   Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:    1   Max.nchar:    1   Max.nchar:   16   Max.nchar:   10  
##                                                                         
##                                                                         
##   ARPT_ELEV_SOURCE ELEVATION_SRC_DATE  CONTR_FUEL_AVBL  TRNS_STRG_BUOY_FLAG
##  Length   :19707   Length   :19707    Length   :19707   Length   :19707    
##  N.unique :   19   N.unique : 5445    N.unique :    3   N.unique :    3    
##  N.blank  : 8428   N.blank  : 8637    N.blank  :19589   N.blank  :19576    
##  Min.nchar:    0   Min.nchar:    0    Min.nchar:    0   Min.nchar:    0    
##  Max.nchar:   16   Max.nchar:   10    Max.nchar:    1   Max.nchar:    1    
##                                                                            
##                                                                            
##  TRNS_STRG_HGR_FLAG TRNS_STRG_TIE_FLAG   OTHER_SERVICES   WIND_INDCR_FLAG 
##  Length   :19707    Length   :19707    Length   :19707   Length   :19707  
##  N.unique :    3    N.unique :    3    N.unique :  441   N.unique :    4  
##  N.blank  :16733    N.blank  :14939    N.blank  :16483   N.blank  : 4676  
##  Min.nchar:    0    Min.nchar:    0    Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:    1    Max.nchar:    1    Max.nchar:   54   Max.nchar:    3  
##                                                                           
##                                                                           
##       ICAO_ID        MIN_OP_NETWORK    USER_FEE_FLAG          CTA       
##  Length   :19707   Length   :19707   Length   :19707   Length   :19707  
##  N.unique : 2706   N.unique :    2   N.unique :    2   N.unique :   45  
##  N.blank  :17002   N.blank  :    0   N.blank  :19644   N.blank  :19516  
##  Min.nchar:    0   Min.nchar:    1   Min.nchar:    0   Min.nchar:    0  
##  Max.nchar:    4   Max.nchar:    1   Max.nchar:   26   Max.nchar:    4  
##                                                                         
## 

Now that we have looked at the dataframe and know some of the summary statistics, we want to filter the data so that we only keep facilities located in continental USA. We also want to remove NA values from Acreage and Elevation so we can use those variables later for sampling and inferential statistics.

# Filtering the data to subset the dataset:
# We only want facilities where STATE_CODE is in continental USA
# We also remove records with NA values for ACREAGE or ELEV (needed later in the lab)
#ToDo

filtered_states <- subset(
  aviation_facilities,
  (STATE_CODE %in% c("AL","AZ","AR","CA","CO","CT","DE","FL","GA",
  "ID","IL","IN","IA","KS","KY","LA","ME","MD",
  "MA","MI","MN","MS","MO","MT","NE","NV","NH",
  "NJ","NM","NY","NC","ND","OH","OK","OR","PA",
  "RI","SC","SD","TN","TX","UT","VT","VA","WA","DC",
  "WV","WI","WY")) & !is.na(ACREAGE) & !is.na(ELEV)
)

nrow(filtered_states)
## [1] 9350
table(filtered_states$SITE_TYPE_CODE)
## 
##    A    B    C    G    H    U 
## 7822    8   94   24 1337   65
summary(filtered_states$ACREAGE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0    10.0    51.0   286.6   190.0 55000.0
summary(filtered_states$ELEV)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -210.0   331.1   803.5  1316.5  1420.0  9933.5

Now that we have filtered to continental USA, we will focus only on 3 facility types: Airports (A), Heliports (H), and Seaplane Bases (C).

# Use SITE_TYPE_CODE to filter the dataset to only include "A", "H", and "C".

#ToDo

filtered_states<-subset(filtered_states,(SITE_TYPE_CODE %in% c("A","H","C")) & !is.na(ACREAGE) & !is.na(ELEV))

table(filtered_states$SITE_TYPE_CODE)
## 
##    A    C    H 
## 7822   94 1337

Simple Random Sampling

First, we will randomly sample 100 records. from the filtered dataset.

random_sample <- filtered_states[sample(1:nrow(filtered_states), size = 100, replace = FALSE), ]
nrow(random_sample)
## [1] 100

Stratified Sampling

Next, we will try stratified sampling using dplyr package. In this exercise, we are only interested in facilities where Elevation is greater than 1000 (ELEV > 1000). Therefore, we create a subset first.

# Create a subset consisting of Elevation >= 1000
subset_facilities <- subset(filtered_states, ELEV >= 1000)

nrow(subset_facilities)
## [1] 3569
table(subset_facilities$SITE_TYPE_CODE)
## 
##    A    C    H 
## 3199   16  354
# Stratified Sampling
library(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
stratified_sample <- subset_facilities %>%
  group_by(SITE_TYPE_CODE) %>%
  sample_n(size = 10, replace = FALSE) %>%
  ungroup()

nrow(stratified_sample)
## [1] 30
table(stratified_sample$SITE_TYPE_CODE)
## 
##  A  C  H 
## 10 10 10

(4 marks) Q7 Now, it is your turn.

Please create a stratified sample consisting of facilities where Elevation is above the 1st quartile elevation, Acreage is above the 1st quartile acreage, and Remove NA values from Elevation and Acreage (already done in filtered_states).

For the stratified sample, you will be using the SITE_TYPE_CODE category. For each category, make the sample size equal to 20. To make the result reproducible, please add code set.seed(123) prior to your sampling.

Please indicate the number of observations in your subset and the number of observations in your stratified sample. You can use summary to find the 1st quartiles.

summary(filtered_states)
##       EFF_DATE       SITE_NO        SITE_TYPE_CODE     STATE_CODE  
##  Length   :9253   Min.   :  103   Length   :9253   Length   :9253  
##  N.unique :   1   1st Qu.: 5293   N.unique :   3   N.unique :  49  
##  N.blank  :   0   Median :13083   N.blank  :   0   N.blank  :   0  
##  Min.nchar:  10   Mean   :13812   Min.nchar:   1   Min.nchar:   2  
##  Max.nchar:  10   3rd Qu.:22890   Max.nchar:   1   Max.nchar:   2  
##                   Max.   :42289                                    
##                                                                    
##       ARPT_ID            CITY         COUNTRY_CODE     REGION_CODE  
##  Length   :9253   Length   :9253   Length   :9253   Length   :9253  
##  N.unique :9253   N.unique :5132   N.unique :   1   N.unique :   8  
##  N.blank  :   0   N.blank  :   0   N.blank  :   0   N.blank  :   0  
##  Min.nchar:   3   Min.nchar:   3   Min.nchar:   2   Min.nchar:   3  
##  Max.nchar:   4   Max.nchar:  33   Max.nchar:   2   Max.nchar:   3  
##                                                                     
##                                                                     
##       ADO_CODE        STATE_NAME      COUNTY_NAME   COUNTY_ASSOC_STATE
##  Length   :9253   Length   :9253   Length   :9253   Length   :9253    
##  N.unique :  21   N.unique :  49   N.unique :1654   N.unique :  49    
##  N.blank  :1106   N.blank  :   0   N.blank  :   0   N.blank  :   0    
##  Min.nchar:   0   Min.nchar:   4   Min.nchar:   3   Min.nchar:   2    
##  Max.nchar:   3   Max.nchar:  17   Max.nchar:  19   Max.nchar:   2    
##                                                                       
##                                                                       
##      ARPT_NAME    OWNERSHIP_TYPE_CODE FACILITY_USE_CODE    LAT_DEG     
##  Length   :9253   Length   :9253      Length   :9253    Min.   :24.00  
##  N.unique :8846   N.unique :   5      N.unique :   2    1st Qu.:33.00  
##  N.blank  :   0   N.blank  :   0      N.blank  :   0    Median :38.00  
##  Min.nchar:   2   Min.nchar:   2      Min.nchar:   2    Mean   :37.67  
##  Max.nchar:  50   Max.nchar:   2      Max.nchar:   2    3rd Qu.:42.00  
##                                                         Max.   :48.00  
##                                                                        
##     LAT_MIN         LAT_SEC          LAT_HEMIS     LAT_DECIMAL   
##  Min.   : 0.00   Min.   : 0.00   Length   :9253   Min.   :24.56  
##  1st Qu.:15.00   1st Qu.:14.51   N.unique :   1   1st Qu.:33.78  
##  Median :30.00   Median :30.00   N.blank  :   0   Median :38.58  
##  Mean   :29.81   Mean   :29.60   Min.nchar:   1   Mean   :38.17  
##  3rd Qu.:45.00   3rd Qu.:44.60   Max.nchar:   1   3rd Qu.:42.33  
##  Max.   :59.00   Max.   :59.99                    Max.   :49.00  
##                                                                  
##     LONG_DEG         LONG_MIN        LONG_SEC         LONG_HEMIS  
##  Min.   : 67.00   Min.   : 0.00   Min.   : 0.00   Length   :9253  
##  1st Qu.: 82.00   1st Qu.:15.00   1st Qu.:14.67   N.unique :   1  
##  Median : 91.00   Median :29.00   Median :30.16   N.blank  :   0  
##  Mean   : 93.18   Mean   :29.26   Mean   :29.95   Min.nchar:   1  
##  3rd Qu.:100.00   3rd Qu.:44.00   3rd Qu.:45.25   Max.nchar:   1  
##  Max.   :124.00   Max.   :59.00   Max.   :59.99                   
##                                                                   
##   LONG_DECIMAL     SURVEY_METHOD_CODE      ELEV         ELEV_METHOD_CODE
##  Min.   :-124.56   Length   :9253     Min.   :-210.0   Length   :9253   
##  1st Qu.:-100.91   N.unique :   1     1st Qu.: 332.6   N.unique :   3   
##  Median : -91.90   N.blank  :   0     Median : 805.0   N.blank  : 106   
##  Mean   : -93.68   Min.nchar:   1     Mean   :1320.5   Min.nchar:   0   
##  3rd Qu.: -82.57   Max.nchar:   1     3rd Qu.:1430.0   Max.nchar:   1   
##  Max.   : -67.01                      Max.   :9933.5                    
##                                                                         
##     MAG_VARN          MAG_HEMIS    MAG_VARN_YEAR       TPA        
##  Min.   : 0.000   Length   :9253   Min.   :1965   Min.   : 260.0  
##  1st Qu.: 3.000   N.unique :   3   1st Qu.:1985   1st Qu.: 800.0  
##  Median : 7.000   N.blank  :1593   Median :1990   Median : 800.0  
##  Mean   : 7.792   Min.nchar:   0   Mean   :1994   Mean   : 872.8  
##  3rd Qu.:12.000   Max.nchar:   1   3rd Qu.:2000   3rd Qu.: 957.8  
##  Max.   :21.000                    Max.   :2030   Max.   :4200.0  
##  NAs    :1593                      NAs    :1622   NAs    :8773    
##      CHART_NAME   DIST_CITY_TO_AIRPORT   DIRECTION_CODE    ACREAGE       
##  Length   :9253   Min.   : 0.000       Length   :9253   Min.   :    0.0  
##  N.unique :  38   1st Qu.: 2.000       N.unique :  17   1st Qu.:   10.0  
##  N.blank  :  83   Median : 3.000       N.blank  :  41   Median :   52.0  
##  Min.nchar:   0   Mean   : 3.913       Min.nchar:   0   Mean   :  288.9  
##  Max.nchar:  15   3rd Qu.: 5.000       Max.nchar:   3   3rd Qu.:  194.0  
##                   Max.   :68.000                        Max.   :55000.0  
##                   NAs    :28                                             
##    RESP_ARTCC_ID     COMPUTER_ID       ARTCC_NAME    FSS_ON_ARPT_FLAG
##  Length   :9253   Length   :9253   Length   :9253   Length   :9253   
##  N.unique :  22   N.unique :  22   N.unique :  22   N.unique :   2   
##  N.blank  :   0   N.blank  :   0   N.blank  :   0   N.blank  :2991   
##  Min.nchar:   3   Min.nchar:   3   Min.nchar:   5   Min.nchar:   0   
##  Max.nchar:   3   Max.nchar:   3   Max.nchar:  14   Max.nchar:   1   
##                                                                      
##                                                                      
##        FSS_ID          FSS_NAME         PHONE_NO       TOLL_FREE_NO 
##  Length   :9253   Length   :9253   Length   :9253   Length   :9253  
##  N.unique :  56   N.unique :  56   N.unique :   2   N.unique :   1  
##  N.blank  :   0   N.blank  :   0   N.blank  :9178   N.blank  :   0  
##  Min.nchar:   3   Min.nchar:   4   Min.nchar:   0   Min.nchar:  14  
##  Max.nchar:   3   Max.nchar:  17   Max.nchar:  12   Max.nchar:  14  
##                                                                     
##                                                                     
##      ALT_FSS_ID      ALT_FSS_NAME   ALT_TOLL_FREE_NO      NOTAM_ID   
##  Length   :9253   Length   :9253   Length   :9253    Length   :9253  
##  N.unique :   1   N.unique :   1   N.unique :   1    N.unique :1555  
##  N.blank  :9253   N.blank  :9253   N.blank  :9253    N.blank  :4776  
##  Min.nchar:   0   Min.nchar:   0   Min.nchar:   0    Min.nchar:   0  
##  Max.nchar:   0   Max.nchar:   0   Max.nchar:   0    Max.nchar:   3  
##                                                                      
##                                                                      
##      NOTAM_FLAG    ACTIVATION_DATE    ARPT_STATUS   FAR_139_TYPE_CODE
##  Length   :9253   Length   :9253   Length   :9253   Length   :9253   
##  N.unique :   3   N.unique :1027   N.unique :   3   N.unique :  11   
##  N.blank  :2068   N.blank  : 495   N.blank  :   0   N.blank  :8784   
##  Min.nchar:   0   Min.nchar:   0   Min.nchar:   1   Min.nchar:   0   
##  Max.nchar:   1   Max.nchar:   7   Max.nchar:   2   Max.nchar:   5   
##                                                                      
##                                                                      
##  FAR_139_CARRIER_SER_CODE ARFF_CERT_TYPE_DATE     NASP_CODE   
##  Length   :9253           Length   :9253      Length   :9253  
##  N.unique :   3           N.unique : 131      N.unique : 114  
##  N.blank  :8784           N.blank  :8784      N.blank  :6086  
##  Min.nchar:   0           Min.nchar:   0      Min.nchar:   0  
##  Max.nchar:   1           Max.nchar:   7      Max.nchar:   7  
##                                                               
##                                                               
##  ASP_ANLYS_DTRM_CODE     CUST_FLAG     LNDG_RIGHTS_FLAG   JOINT_USE_FLAG
##  Length   :9253      Length   :9253   Length   :9253    Length   :9253  
##  N.unique :   5      N.unique :   3   N.unique :   3    N.unique :   3  
##  N.blank  :  80      N.blank  :5674   N.blank  :5662    N.blank  :5597  
##  Min.nchar:   0      Min.nchar:   0   Min.nchar:   0    Min.nchar:   0  
##  Max.nchar:  13      Max.nchar:   1   Max.nchar:   1    Max.nchar:   1  
##                                                                         
##                                                                         
##    MIL_LNDG_FLAG  INSPECT_METHOD_CODE   INSPECTOR_CODE  LAST_INSPECTION
##  Length   :9253   Length   :9253      Length   :9253   Length   :9253  
##  N.unique :   3   N.unique :   6      N.unique :   4   N.unique :1270  
##  N.blank  :5672   N.blank  : 984      N.blank  :   0   N.blank  :4466  
##  Min.nchar:   0   Min.nchar:   0      Min.nchar:   1   Min.nchar:   0  
##  Max.nchar:   1   Max.nchar:   1      Max.nchar:   1   Max.nchar:  10  
##                                                                        
##                                                                        
##  LAST_INFO_RESPONSE     FUEL_TYPES   AIRFRAME_REPAIR_SER_CODE
##  Length   :9253     Length   :9253   Length   :9253          
##  N.unique :2280     N.unique :  62   N.unique :   4          
##  N.blank  :3966     N.blank  :5842   N.blank  :4940          
##  Min.nchar:   0     Min.nchar:   0   Min.nchar:   0          
##  Max.nchar:  10     Max.nchar:  18   Max.nchar:   5          
##                                                              
##                                                              
##  PWR_PLANT_REPAIR_SER  BOTTLED_OXY_TYPE   BULK_OXY_TYPE       LGT_SKED   
##  Length   :9253       Length   :9253    Length   :9253   Length   :9253  
##  N.unique :   4       N.unique :   5    N.unique :   5   N.unique :   3  
##  N.blank  :4937       N.blank  :5725    N.blank  :5783   N.blank  :4923  
##  Min.nchar:   0       Min.nchar:   0    Min.nchar:   0   Min.nchar:   0  
##  Max.nchar:   5       Max.nchar:   8    Max.nchar:   8   Max.nchar:   7  
##                                                                          
##                                                                          
##     BCN_LGT_SKED    TWR_TYPE_CODE  SEG_CIRCLE_MKR_FLAG   BCN_LENS_COLOR
##  Length   :9253   Length   :9253   Length   :9253      Length   :9253  
##  N.unique :   3   N.unique :   5   N.unique :   4      N.unique :   8  
##  N.blank  :5748   N.blank  :   0   N.blank  :1419      N.blank  :5699  
##  Min.nchar:   0   Min.nchar:   4   Min.nchar:   0      Min.nchar:   0  
##  Max.nchar:   7   Max.nchar:  11   Max.nchar:   3      Max.nchar:   3  
##                                                                        
##                                                                        
##    LNDG_FEE_FLAG   MEDICAL_USE_FLAG  ARPT_PSN_SOURCE POSITION_SRC_DATE
##  Length   :9253   Length   :9253    Length   :9253   Length   :9253   
##  N.unique :   3   N.unique :   3    N.unique :  17   N.unique :3822   
##  N.blank  :3839   N.blank  :8652    N.blank  :2655   N.blank  :2827   
##  Min.nchar:   0   Min.nchar:   0    Min.nchar:   0   Min.nchar:   0   
##  Max.nchar:   1   Max.nchar:   1    Max.nchar:  16   Max.nchar:  10   
##                                                                       
##                                                                       
##   ARPT_ELEV_SOURCE ELEVATION_SRC_DATE  CONTR_FUEL_AVBL TRNS_STRG_BUOY_FLAG
##  Length   :9253    Length   :9253     Length   :9253   Length   :9253     
##  N.unique :  18    N.unique :3714     N.unique :   3   N.unique :   3     
##  N.blank  :3616    N.blank  :3692     N.blank  :9188   N.blank  :9159     
##  Min.nchar:   0    Min.nchar:   0     Min.nchar:   0   Min.nchar:   0     
##  Max.nchar:  16    Max.nchar:  10     Max.nchar:   1   Max.nchar:   1     
##                                                                           
##                                                                           
##  TRNS_STRG_HGR_FLAG TRNS_STRG_TIE_FLAG   OTHER_SERVICES  WIND_INDCR_FLAG
##  Length   :9253     Length   :9253     Length   :9253   Length   :9253  
##  N.unique :   3     N.unique :   3     N.unique : 427   N.unique :   4  
##  N.blank  :6464     N.blank  :4788     N.blank  :6314   N.blank  : 902  
##  Min.nchar:   0     Min.nchar:   0     Min.nchar:   0   Min.nchar:   0  
##  Max.nchar:   1     Max.nchar:   1     Max.nchar:  54   Max.nchar:   3  
##                                                                         
##                                                                         
##       ICAO_ID       MIN_OP_NETWORK   USER_FEE_FLAG         CTA      
##  Length   :9253   Length   :9253   Length   :9253   Length   :9253  
##  N.unique :2136   N.unique :   2   N.unique :   2   N.unique :  35  
##  N.blank  :7118   N.blank  :   0   N.blank  :9190   N.blank  :9118  
##  Min.nchar:   0   Min.nchar:   1   Min.nchar:   0   Min.nchar:   0  
##  Max.nchar:   4   Max.nchar:   1   Max.nchar:  26   Max.nchar:   4  
##                                                                     
## 
subset_facilities<-subset(filtered_states,ELEV>332.6 & ACREAGE>10)
nrow(subset_facilities)
## [1] 5181
set.seed(123)
stratified_sample<-subset_facilities %>% group_by(SITE_TYPE_CODE) %>% sample_n(size=20,replace=FALSE) %>% ungroup()
nrow(stratified_sample)
## [1] 60
# ToDo
# Students can use summary() to identify the 1st quartiles (25th percentile)

Compare Sampling Technique

(4 marks) Q8 What is the difference between simple random sampling and stratified sampling? In this case, would you use simple random sampling or stratified sampling?

Type your response here Simple random sampling is choosing a specific number of observations from a dataset and choosing data values randomly.Every data value has the same probability of being selected. Stratified sampling is when a number of observations is chosen from a dataset, the values are separated into groups depending on the data, and then random values are selected from each of these groups. This ensures that the overall sample is easily represented. In this case, stratified sampling would be the better option because of the three types of facility types. If we sample this filtered data randomly, there is a good chance that one of the facility types will be underrepresented.

Part B2. Inferential Statistics

Two-sample difference of mean test

(2 marks) Q9 Does the mean elevation differ between AIRPORT and HELIPORT facilities? Write the null hypothesis and alternate hypothesis?

summary(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="H"])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   424.0   772.5  1465.0  2458.9  4255.0  6480.0
summary(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="A"])
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   335.0   757.5   941.5  1703.2  2249.4  4606.4

Type your response here Heliport facilities have a greater mean than Airport facilities, with a mean of 2458.9 and 1703.2 respectively.The null hypothesis is that μ airport=μ heliport and the alternate hypothesis is μairport!=μheliport.

(3 marks) Q10 What would happen if you used a parametric difference of mean test on non-parametric data?

Type your response here These two kinds of data are different. Parametric tests look at the actual values, while non parametric tests look at the ranking of these values. If you decide to do a parametric difference of mean test on non parametric data, the results will be unreliable because you are not looking at the values itself.

(4 marks) Q11 Please select an appropriate statistical test, and provide justifications by visualizing the distribution, using the stratified sample subset.

Type your response here Looking at the data, I would use a two sample t test because even in the stratified sample, it is a fairly large sample size, and we are also comparing two independent variables to each other, as airports and heliports are separate types of facilites. Looking at the boxplot, the distributions of data are similar, as both of them have the 1st quartile closer to the lower whisker than the higher whisker.

# ToDo
boxplot(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="A"],main="Airport Elevation",xlab="Elevation (m)")

boxplot(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="H"],main="Airport Elevation",xlab="Elevation (m)")

(6 marks) Q12 Please conduct the statistic test and interpret the results. Create 2 vectors (airport and heliport), each containing elevation values from each facility type. Then use a non-parametric difference of means test.

#TODO (2 marks)
airport<-(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="A"])
heliport<-(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="H"])
t.test(airport,heliport)
## 
##  Welch Two Sample t-test
## 
## data:  airport and heliport
## t = -1.2994, df = 32.719, p-value = 0.2029
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1939.4166   427.9466
## sample estimates:
## mean of x mean of y 
##  1703.215  2458.950
wilcox.test(airport,heliport)
## 
##  Wilcoxon rank sum exact test
## 
## data:  airport and heliport
## W = 165.5, p-value = 0.3583
## alternative hypothesis: true location shift is not equal to 0

Type the statistic results and interpretation here

Three-sample Mean Difference Test

(8 marks) Q13 Please follow the same procedure above to test whether the 1st quartile elevation differs between facility types. You need to decide which three-or-more sample test to perform.

Type your response here:

#ToDo
# Visualize Distribution (1 mark)
Q1_airport<-quantile(airport,0.25)
View(Q1_airport)
Q1_heliport<-quantile(heliport,0.25)
View(Q1_heliport)
summary(heliport)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   424.0   772.5  1465.0  2458.9  4255.0  6480.0
seaplanebase<-(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="C"])
Q1_seaplanebase<-quantile(seaplanebase,0.25)
View(Q1_seaplanebase)
boxplot(stratified_sample$ELEV[stratified_sample$SITE_TYPE_CODE=="C"],main="Airport Elevation",xlab="Elevation (m)")

class(seaplanebase)
## [1] "numeric"
kruskal.test(list(airport,heliport,seaplanebase))
## 
##  Kruskal-Wallis rank sum test
## 
## data:  list(airport, heliport, seaplanebase)
## Kruskal-Wallis chi-squared = 3.7042, df = 2, p-value = 0.1569
#ToDo
# Test (2 marks) using the filtered dataset (A/H/C)

#Type 1 and Type 2 error (4 marks) Q14 Discuss what the Type 1 and Type 2 errors would be for both your 2-sample and 3-sample difference of means tests. Why is it important to understand Type 1 and Type 2 errors?

For my two sample and 3 sample tests, type 1 errors would state that I rejected the null hypothesis when it was true. In other words, I concluded that the difference in means was significant when it was actually equal to each other. Type 2 errors state that I accepted the null hypothesis when it was false. In other words, I concluded that there was no change in the means of the variables when in reality there was. It is important to understand Type 1 and 2 errors because if you understand what these statements mean in the context of the question, it will minimize error and ensure a more accurate concluding statement.