Apple mobility trends data visualization

Version 0.9
Anton Antonov
​MathematicaForPrediction at WordPress​
​SystemModeling at GitHub​
April 2020
May 2020
December 2020

Introduction

I this notebook we ingest and visualize the mobility trends data provided by Apple, [APPL1].
We take the following steps:
1
.
Download the data
2
.
Import the data and summarise it
3
.
Transform the data into long form
4
.
Partition the data into subsets that correspond to combinations of geographical regions and transportation types
5
.
Make contingency matrices and corresponding heat-map plots
6
.
Make nearest neighbors graphs over the contingency matrices and plot communities
7
.
Plot the corresponding time series

Data description

From Apple’s page https://www.apple.com/covid19/mobility

About This Data​
The CSV file and charts on this site show a relative volume of directions requests per country/region or city compared to a baseline volume on January 13th, 2020.
We define our day as midnight-to-midnight, Pacific time. Cities represent usage in greater metropolitan areas and are stably defined during this period. In many countries/regions and cities, relative volume has increased since January 13th, consistent with normal, seasonal usage of Apple Maps. Day of week effects are important to normalize as you use this data.
Data that is sent from users’ devices to the Maps service is associated with random, rotating identifiers so Apple doesn’t have a profile of your movements and searches. Apple Maps has no demographic information about our users, so we can’t make any statements about the representativeness of our usage against the overall population.

Observations

The observations listed in this subsection are also placed under the relevant statistics in the following sections and indicated with “Observation”.
◼
  • The directions request volumes reference date for normalization is 2020-01-13 : all the values in that column are
    100
    .
  • ◼
  • From the community clusters of the nearest neighbor graphs (derived from the time series of the normalized driving directions requests volume) we see that countries and cities are clustered in expected ways. For example, in the community graph plot corresponding to “{city, driving}” the cities Oslo, Copenhagen, Helsinki, Stockholm, and Zurich are placed in the same cluster. In the graphs corresponding to “{city, transit}” and “{city, walking}” the Japanese cities Tokyo, Osaka, Nagoya, and Fukuoka are clustered together.
  • ◼
  • In the time series plots the Sundays are indicated with orange dashed lines. We can see that from Monday to Thursday people are more familiar with their trips than say on Fridays and Saturdays. We can also see that on Sundays people (on average) are more familiar with their trips or simply travel less.
  • Load packages

    In[]:=
    Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/DataReshape.m"]​​Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/HeatmapPlot.m"]

    Data ingestion

    Apple mobile data was provided in this WWW page: https://www.apple.com/covid19/mobility , [APPL1]. (The data has to be download from that web page -- there is an “agreement to terms”, etc.)
    In[]:=
    dsAppleMobility=ResourceFunction["ImportCSVToDataset"]["~/Downloads/applemobilitytrends-2021-01-15.csv"]
    Out[]=
    geo_type
    region
    transportation_type
    alternative_name
    sub-region
    country
    2020-01-13
    2020-01-14
    2020-01-15
    2020-01-16
    country/region
    Albania
    driving
    100.0
    95.3
    101.43
    97.2
    country/region
    Albania
    walking
    100.0
    100.68
    98.93
    98.46
    country/region
    Argentina
    driving
    100.0
    97.07
    102.45
    111.21
    country/region
    Argentina
    walking
    100.0
    95.11
    101.37
    112.67
    country/region
    Australia
    driving
    AU
    100.0
    102.98
    104.21
    108.63
    country/region
    Australia
    transit
    AU
    100.0
    101.78
    100.64
    99.58
    country/region
    Australia
    walking
    AU
    100.0
    101.31
    101.82
    104.52
    country/region
    Austria
    driving
    Österreich
    100.0
    101.14
    104.24
    112.21
    country/region
    Austria
    walking
    Österreich
    100.0
    101.55
    105.59
    112.24
    country/region
    Belgium
    driving
    België|Belgique
    100.0
    101.19
    107.49
    107.67
    country/region
    Belgium
    transit
    België|Belgique
    100.0
    98.67
    105.01
    105.87
    country/region
    Belgium
    walking
    België|Belgique
    100.0
    101.46
    110.44
    118.86
    country/region
    Brazil
    driving
    Brasil
    100.0
    99.71
    100.9
    101.88
    country/region
    Brazil
    transit
    Brasil
    100.0
    102.45
    104.28
    100.2
    country/region
    Brazil
    walking
    Brasil
    100.0
    106.3
    104.75
    99.05
    country/region
    Bulgaria
    driving
    България
    100.0
    102.56
    104.73
    104.35
    country/region
    Bulgaria
    walking
    България
    100.0
    101.9
    99.61
    100.06
    country/region
    Cambodia
    driving
    ព្រះរាជាណាចក្រកម្ពុជា
    100.0
    100.75
    99.33
    96.0
    country/region
    Cambodia
    walking
    ព្រះរាជាណាចក្រកម្ពុជា
    100.0
    98.06
    99.7
    98.0
    country/region
    Canada
    driving
    100.0
    102.91
    99.74
    105.17
    rows 1–20 of 4691
    columns 1–10 of 375
    Unable to save data.
    Observation: The directions requests volumes reference date for normalization is 2020-01-13 : all the values in that column are
    100
    .
    Data dimensions:
    In[]:=
    Dimensions[dsAppleMobility]
    Out[]=
    {4691,375}
    Data summary:
    In[]:=
    Magnify[ResourceFunction["RecordsSummary"][dsAppleMobility],0.6]
    Out[]=
    Number of unique “country/region” values:
    In[]:=
    Length[Union[Normal[dsAppleMobility[Select[#["geo_type"]=="country/region"&],"region"]]]]
    Out[]=
    63
    Number of unique “city” values:
    In[]:=
    Length[Union[Normal[dsAppleMobility[Select[#["geo_type"]=="city"&],"region"]]]]
    Out[]=
    295
    All unique geo types:
    In[]:=
    lsGeoTypes=Union[Normal[dsAppleMobility[All,"geo_type"]]]
    Out[]=
    {city,country/region,county,sub-region}
    All unique transportation types:
    In[]:=
    lsTransportationTypes=Union[Normal[dsAppleMobility[All,"transportation_type"]]]
    Out[]=
    {driving,transit,walking}

    Data transformation

    It is better to have the data in long form (narrow form). For that I am using the package "DataReshape.m", [AAp1].
    In[]:=
    (*lsIDColumnNames={"geo_type","region","transportation_type"};*)(*FortheinitialdatasetofApple'smobilitydata.*)​​lsIDColumnNames={"geo_type","region","transportation_type","alternative_name","sub-region","country"};​​dsAppleMobilityLongForm=ToLongForm[dsAppleMobility,lsIDColumnNames,Complement[Keys[dsAppleMobility〚1〛],lsIDColumnNames]];​​Dimensions[dsAppleMobilityLongForm]
    Out[]=
    {1730979,8}
    Remove the rows with “empty” values:
    In[]:=
    dsAppleMobilityLongForm=dsAppleMobilityLongForm[Select[#Value≠""&]];​​Dimensions[dsAppleMobilityLongForm]
    Out[]=
    {1709416,8}
    Rename the column “Variable” to “Date” and add a related “DateObject” column:
    In[]:=
    AbsoluteTiming[​​dsAppleMobilityLongForm=dsAppleMobilityLongForm[All,Join[KeyDrop[#,"Variable"],<|"Date"#Variable,"DateObject"DateObject[#Variable]|>]&];​​]
    Out[]=
    {714.062,Null}
    Add “day name” (“day of the week”) field:
    In[]:=
    AbsoluteTiming[​​dsAppleMobilityLongForm=dsAppleMobilityLongForm[All,Join[#,<|"DayName"->DateString[#DateObject,{"DayName"}]|>]&];​​]
    Out[]=
    {498.026,Null}
    Here is sample of the transformed data:
    In[]:=
    SeedRandom[3232];​​RandomSample[dsAppleMobilityLongForm,12]
    Out[]=
    geo_type
    region
    transportation_type
    alternative_name
    sub-region
    country
    Value
    Date
    DateObject
    DayName
    county
    Colbert County
    driving
    Alabama
    United States
    162.3
    2020-06-23
    Tue 23 Jun 2020
    Tuesday
    county
    Weld County
    walking
    Colorado
    United States
    131.14
    2021-01-14
    Thu 14 Jan 2021
    Thursday
    sub-region
    Canton of Basel-Stadt
    driving
    Kanton Basel-Stadt|Canton de Bâle-Ville
    Switzerland
    55.72
    2020-03-17
    Tue 17 Mar 2020
    Tuesday
    city
    Orlando
    driving
    Florida
    United States
    118.09
    2020-02-26
    Wed 26 Feb 2020
    Wednesday
    county
    Suffolk County
    driving
    New York
    United States
    118.04
    2020-10-29
    Thu 29 Oct 2020
    Thursday
    county
    Aguadilla Municipio
    driving
    Puerto Rico
    United States
    34.11
    2020-05-03
    Sun 3 May 2020
    Sunday
    county
    Suffolk County
    driving
    Massachusetts
    United States
    75.82
    2020-06-11
    Thu 11 Jun 2020
    Thursday
    sub-region
    Mazovia Province
    walking
    Województwo Mazowieckie
    Poland
    49.65
    2020-05-04
    Mon 4 May 2020
    Monday
    city
    Cincinnati
    driving
    Ohio
    United States
    108.95
    2020-10-04
    Sun 4 Oct 2020
    Sunday
    county
    Bulloch County
    walking
    Georgia
    United States
    68.88
    2020-10-18
    Sun 18 Oct 2020
    Sunday
    county
    Blaine County
    driving
    Oklahoma
    United States
    79.44
    2020-03-17
    Tue 17 Mar 2020
    Tuesday
    city
    Okayama
    driving
    岡山市
    Okayama Prefecture
    Japan
    139.26
    2020-02-07
    Fri 7 Feb 2020
    Friday
    Here is summary:
    Partition the data into geo types × transportation types:

    Basic data analysis

    We consider relative volume o directions requests for the last date only. (The queries can easily adjusted for other dates.)
    Here we plot histograms and Pareto principle adherence:

    Heat-map plots

    We can visualize the data using heat-map plots. Here we use the package "HeatmapPlot.m", [AAp2].
    Remark: Using the contingency matrices prepared for the heat-map plots we can do further analysis, like, finding correlations or nearest neighbors. (See below.)
    Cross-tabulate dates with regions:
    Make a heat-map plot by sorting the columns of the cross-tabulation matrix (that correspond to countries):
    (We use Rasterize in order to reduce the size of the notebook.)
    Here we take closer look to one of the plots:

    Nearest neighbors graphs

    Graphs overview

    Here we create nearest neighbor graphs of the contingency matrices computed above and plot cluster the nodes:

    Closer look into the graphs

    Here we endow each nearest neighbors graph with appropriate vertex labels:
    Here we plot the graphs with clusters:
    Observation: From the community clusters of the nearest neighbor graphs (derived from the time series of the normalized driving directions requests volume) we see that countries and cities are clustered in expected ways. For example in the community graph plot corresponding to “{city, driving}” the cities Oslo, Copenhagen, Helsinki, Stockholm, and Zurich are placed in the same cluster. In the graphs corresponding to “{city, transit}” and “{city, walking}” the Japanese cities Tokyo, Osaka, Nagoya, and Fukuoka are clustered together.

    Time series analysis

    Time series

    In this section for each date we sum all cases over the region-transportation pairs, make a time series, and plot them.
    Remark: In the plots the Sundays are indicated with orange dashed lines.
    Here we make the time series:
    Here we plot them:
    Observation: In the time series plots the Sundays are indicated with orange dashed lines. We can see that from Monday to Thursday people are more familiar with their trips than say on Fridays and Saturdays. We can also see that on Sundays people (on average) are more familiar with their trips or simply travel less.

    “Forecast”

    He we do “forecast” for code-workflow demonstration purposes -- the forecasts should not be taken seriously.
    Fit a time series model to the time series:
    Plot data and forecast:

    References

    [APPL1] Apple Inc., Mobility Trends Reports, (2020), apple.com.
    [AA1] Anton Antonov, "NY Times COVID-19 data visualization", (2020), SystemModeling at GitHub.
    [AAp1] Anton Antonov, Data reshaping Mathematica package, (2018), MathematicaForPrediciton at GitHub.
    [AAp2] Anton Antonov, Heatmap plot Mathematica package, (2018), MathematicaForPrediciton at GitHub.