{
  "id": 3560,
  "title": "R code to create test set from training",
  "url": "/competitions/flight/discussion/3560",
  "author_name": "",
  "post_date": "2013-01-09T06:55:01.500Z",
  "votes": 2,
  "comment_count": 3,
  "views": 3518,
  "content": "<p><br class=\"x_Apple-interchange-newline\">\r\n<span>cutoff_days &lt;- read.csv (paste (cv_path, 'days.csv', sep='/'), header=TRUE, stringsAsFactors = FALSE)</span><br>\r\n<span>cutoff_days$cutoff_time &lt;- as.POSIXct (cutoff_days$selected_cutoff_time, tz=&quot;UTC&quot;, format = &quot;%Y-%m-%d %H:%M:%S&quot;, origin=&quot;1970-1-1&quot; )</span><br>\r\n<br>\r\n<span>#myNms &lt;- c (&quot;flight_history_id&quot;, &quot;departure_airport_code&quot;, &quot;arrival_airport_code&quot;, &quot;published_departure&quot;, &quot;published_arrival&quot;, &quot;scheduled_gate_departure&quot;, &quot;scheduled_gate_arrival&quot;, &quot;scheduled_runway_departure&quot;, &quot;scheduled_runway_arrival&quot;, &quot;actual_gate_arrival&quot;,\r\n &quot;actual_runway_arrival&quot;, &quot;actual_gate_arrival_mins&quot;, &quot;actual_runway_arrival_mins&quot;, &quot;airline_icao_code&quot;,&quot;departure_airport_icao_code&quot;, &quot;arrival_airport_icao_code&quot;, &quot;creator_code&quot;, &quot;scheduled_aircraft_type&quot;, &quot;actual_aircraft_type&quot;, &quot;scheduled_air_time&quot;, &quot;actual_gate_departure&quot;,\r\n &quot;actual_runway_departure&quot;)</span><br>\r\n<br>\r\n<span>myNms &lt;- c (&quot;flight_history_id&quot;, &quot;departure_airport_code&quot; , &quot;arrival_airport_code&quot;, &quot;published_departure&quot; , &quot;published_arrival&quot;, &quot;scheduled_gate_departure&quot; , &quot;scheduled_gate_arrival&quot;, &quot;scheduled_runway_departure&quot;,</span><br>\r\n<span>&quot;scheduled_runway_arrival&quot;)</span><br>\r\n<br>\r\n<span>for (myDate in test_dates) {</span><br>\r\n<span>print (paste (&quot;for myDate&quot;, myDate, sep=&quot;:&quot;))</span><br>\r\n<br>\r\n<span>base_day_path &lt;- paste (cv_path,myDate, sep=&quot;/&quot;)</span><br>\r\n<span>myList &lt;- summarize_flight_history_day (base_day_path, test_day_path = NA)</span><br>\r\n<span>flighthistory &lt;- myList [[1]]</span><br>\r\n<br>\r\n<span># remove flights that do not arrive and depart both in the US</span><br>\r\n<span>flighthistory_departs_from_us &lt;- flighthistory$departure_airport_icao_code %in% us_icao_codes$icao_code</span><br>\r\n<span>flighthistory_arrives_in_us &lt;- flighthistory$arrival_airport_icao_code %in% us_icao_codes$icao_code</span><br>\r\n<span>myRows &lt;- which ((which(flighthistory_arrives_in_us == TRUE) &amp; which(flighthistory_departs_from_us == TRUE)) == TRUE)</span><br>\r\n<span>flighthistory &lt;- flighthistory[myRows,]</span><br>\r\n<br>\r\n<span># remove flights that have an invalid form of datetime - either arrival or departure date</span><br>\r\n<span>ignoreRows &lt;- c(which (is.na (flighthistory$actual_gate_departure) == TRUE)</span><br>\r\n<span>, which (is.na (flighthistory$actual_gate_arrival) == TRUE)</span><br>\r\n<span>, which (is.na (flighthistory$actual_runway_arrival) == TRUE)</span><br>\r\n<span>, which (is.na (flighthistory$actual_runway_departure) == TRUE))</span><br>\r\n<span>ignoreRows &lt;- unique (ignoreRows)</span><br>\r\n<br>\r\n<span>flighthistory &lt;- flighthistory [-ignoreRows,]</span><br>\r\n<br>\r\n<span># only flights that are in air at that time</span><br>\r\n<span>flighthistory &lt;- flighthistory [!is.na(flighthistory$actual_gate_arrival),]</span><br>\r\n<span>flighthistory &lt;- flighthistory [!is.na(flighthistory$actual_runway_arrival),]</span><br>\r\n<br>\r\n<br>\r\n<span># remove flights whose gate_departure time is after runway_departure_time OR whose gate arrival is before runway arrival</span><br>\r\n<span>ignoreRows &lt;- c(which (flighthistory$actual_gate_departure &gt; flighthistory$actual_runway_departure), which (flighthistory$actual_runway_arrival &gt; flighthistory$actual_gate_arrival))</span><br>\r\n<span>ignoreRows &lt;- unique (ignoreRows)</span><br>\r\n<br>\r\n<span>flighthistory &lt;- flighthistory [-ignoreRows,]</span><br>\r\n<br>\r\n<br>\r\n<br>\r\n<span>flighthistory$cutoff_time &lt;- as.POSIXct (cutoff_days [cutoff_days$folder_name == myDate, &quot;selected_cutoff_time&quot;], tz=&quot;UTC&quot;, format = &quot;%Y-%m-%d %H:%M:%S&quot;, origin=&quot;1970-1-1&quot; )</span><br>\r\n<br>\r\n<span>testflighthistory &lt;- flighthistory [flighthistory$actual_gate_arrival &gt;= flighthistory$cutoff_time,]</span><br>\r\n<br>\r\n<br>\r\n<br>\r\n<span>write.csv (x=testflighthistory[,myNms] ,file=paste (cv_path, myDate, &quot;test_flights_new.csv&quot;, sep= &quot;/&quot;), row.names = FALSE)</span><br>\r\n<br>\r\n</p>",
  "messages": [
    {
      "id": "19122",
      "postDate": "01/09/2013 06:55:01",
      "content": "<p><br class=\"x_Apple-interchange-newline\">\r\n<span>cutoff_days &lt;- read.csv (paste (cv_path, 'days.csv', sep='/'), header=TRUE, stringsAsFactors = FALSE)</span><br>\r\n<span>cutoff_days$cutoff_time &lt;- as.POSIXct (cutoff_days$selected_cutoff_time, tz=&quot;UTC&quot;, format = &quot;%Y-%m-%d %H:%M:%S&quot;, origin=&quot;1970-1-1&quot; )</span><br>\r\n<br>\r\n<span>#myNms &lt;- c (&quot;flight_history_id&quot;, &quot;departure_airport_code&quot;, &quot;arrival_airport_code&quot;, &quot;published_departure&quot;, &quot;published_arrival&quot;, &quot;scheduled_gate_departure&quot;, &quot;scheduled_gate_arrival&quot;, &quot;scheduled_runway_departure&quot;, &quot;scheduled_runway_arrival&quot;, &quot;actual_gate_arrival&quot;,\r\n &quot;actual_runway_arrival&quot;, &quot;actual_gate_arrival_mins&quot;, &quot;actual_runway_arrival_mins&quot;, &quot;airline_icao_code&quot;,&quot;departure_airport_icao_code&quot;, &quot;arrival_airport_icao_code&quot;, &quot;creator_code&quot;, &quot;scheduled_aircraft_type&quot;, &quot;actual_aircraft_type&quot;, &quot;scheduled_air_time&quot;, &quot;actual_gate_departure&quot;,\r\n &quot;actual_runway_departure&quot;)</span><br>\r\n<br>\r\n<span>myNms &lt;- c (&quot;flight_history_id&quot;, &quot;departure_airport_code&quot; , &quot;arrival_airport_code&quot;, &quot;published_departure&quot; , &quot;published_arrival&quot;, &quot;scheduled_gate_departure&quot; , &quot;scheduled_gate_arrival&quot;, &quot;scheduled_runway_departure&quot;,</span><br>\r\n<span>&quot;scheduled_runway_arrival&quot;)</span><br>\r\n<br>\r\n<span>for (myDate in test_dates) {</span><br>\r\n<span>print (paste (&quot;for myDate&quot;, myDate, sep=&quot;:&quot;))</span><br>\r\n<br>\r\n<span>base_day_path &lt;- paste (cv_path,myDate, sep=&quot;/&quot;)</span><br>\r\n<span>myList &lt;- summarize_flight_history_day (base_day_path, test_day_path = NA)</span><br>\r\n<span>flighthistory &lt;- myList [[1]]</span><br>\r\n<br>\r\n<span># remove flights that do not arrive and depart both in the US</span><br>\r\n<span>flighthistory_departs_from_us &lt;- flighthistory$departure_airport_icao_code %in% us_icao_codes$icao_code</span><br>\r\n<span>flighthistory_arrives_in_us &lt;- flighthistory$arrival_airport_icao_code %in% us_icao_codes$icao_code</span><br>\r\n<span>myRows &lt;- which ((which(flighthistory_arrives_in_us == TRUE) &amp; which(flighthistory_departs_from_us == TRUE)) == TRUE)</span><br>\r\n<span>flighthistory &lt;- flighthistory[myRows,]</span><br>\r\n<br>\r\n<span># remove flights that have an invalid form of datetime - either arrival or departure date</span><br>\r\n<span>ignoreRows &lt;- c(which (is.na (flighthistory$actual_gate_departure) == TRUE)</span><br>\r\n<span>, which (is.na (flighthistory$actual_gate_arrival) == TRUE)</span><br>\r\n<span>, which (is.na (flighthistory$actual_runway_arrival) == TRUE)</span><br>\r\n<span>, which (is.na (flighthistory$actual_runway_departure) == TRUE))</span><br>\r\n<span>ignoreRows &lt;- unique (ignoreRows)</span><br>\r\n<br>\r\n<span>flighthistory &lt;- flighthistory [-ignoreRows,]</span><br>\r\n<br>\r\n<span># only flights that are in air at that time</span><br>\r\n<span>flighthistory &lt;- flighthistory [!is.na(flighthistory$actual_gate_arrival),]</span><br>\r\n<span>flighthistory &lt;- flighthistory [!is.na(flighthistory$actual_runway_arrival),]</span><br>\r\n<br>\r\n<br>\r\n<span># remove flights whose gate_departure time is after runway_departure_time OR whose gate arrival is before runway arrival</span><br>\r\n<span>ignoreRows &lt;- c(which (flighthistory$actual_gate_departure &gt; flighthistory$actual_runway_departure), which (flighthistory$actual_runway_arrival &gt; flighthistory$actual_gate_arrival))</span><br>\r\n<span>ignoreRows &lt;- unique (ignoreRows)</span><br>\r\n<br>\r\n<span>flighthistory &lt;- flighthistory [-ignoreRows,]</span><br>\r\n<br>\r\n<br>\r\n<br>\r\n<span>flighthistory$cutoff_time &lt;- as.POSIXct (cutoff_days [cutoff_days$folder_name == myDate, &quot;selected_cutoff_time&quot;], tz=&quot;UTC&quot;, format = &quot;%Y-%m-%d %H:%M:%S&quot;, origin=&quot;1970-1-1&quot; )</span><br>\r\n<br>\r\n<span>testflighthistory &lt;- flighthistory [flighthistory$actual_gate_arrival &gt;= flighthistory$cutoff_time,]</span><br>\r\n<br>\r\n<br>\r\n<br>\r\n<span>write.csv (x=testflighthistory[,myNms] ,file=paste (cv_path, myDate, &quot;test_flights_new.csv&quot;, sep= &quot;/&quot;), row.names = FALSE)</span><br>\r\n<br>\r\n</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "19141",
      "postDate": "01/10/2013 03:01:42",
      "content": "<p>Hi Black Magic,</p>\r\n<p>I wanted to point out a helpful date package: lubridate. It's a tool created by Hadley Wickham, the author of plyr, ggplot2, and stringr. It's pretty handy for working with dates.</p>\r\n<p>Also, I see you are doing a check of ICAO against some list. I'm pretty sure that all continential US ICAOs beging with K. I don't think it makes much difference computationally, but the rules are pretty clear about disallowing outside data sources.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "19143",
      "postDate": "01/10/2013 03:19:10",
      "content": "<p>Hi Ricardo,</p>\r\n<p>[quote=ricardo sachez;19141]&nbsp;the rules are pretty clear about disallowing outside data sources.[/quote]</p>\r\n<p>A list of ICAO_code has been provided (see Reference/usairporticaocodes).</p>\r\n<p>Best,</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "19145",
      "postDate": "01/10/2013 04:27:15",
      "content": "<p>yes I am using same ICAO codes.<br>\r\nProblem is that traditional Cross validation is not working here. I have lined up some good algos but I need CV to work to optimize</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 19141,
      "author_name": "rickyars",
      "author_url": "",
      "post_date": "01/10/2013 03:01:42",
      "content": "<p>Hi Black Magic,</p>\r\n<p>I wanted to point out a helpful date package: lubridate. It's a tool created by Hadley Wickham, the author of plyr, ggplot2, and stringr. It's pretty handy for working with dates.</p>\r\n<p>Also, I see you are doing a check of ICAO against some list. I'm pretty sure that all continential US ICAOs beging with K. I don't think it makes much difference computationally, but the rules are pretty clear about disallowing outside data sources.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 19143,
      "author_name": "jay670790",
      "author_url": "",
      "post_date": "01/10/2013 03:19:10",
      "content": "<p>Hi Ricardo,</p>\r\n<p>[quote=ricardo sachez;19141]&nbsp;the rules are pretty clear about disallowing outside data sources.[/quote]</p>\r\n<p>A list of ICAO_code has been provided (see Reference/usairporticaocodes).</p>\r\n<p>Best,</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 19145,
      "author_name": "rkirana",
      "author_url": "",
      "post_date": "01/10/2013 04:27:15",
      "content": "<p>yes I am using same ICAO codes.<br>\r\nProblem is that traditional Cross validation is not working here. I have lined up some good algos but I need CV to work to optimize</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "19122": "",
    "19141": "",
    "19143": "",
    "19145": ""
  },
  "source": "meta"
}