BASC0005 Quantitative Methods 2: Data Science and Visualisation
Ollie Ballinger
Outline
Dirty Data
Data Acquisition
Data Types
1. Dirty Data
Dirty Data Kills
Understanding and cleaning data properly may seem boring, but is (literally) of vital importance
“Growth in a Time of Debt”, 2010
A paper written by Harvard professors Carmen Reinhart and Kenneth Rogoff
Argued that austerity (cutting public spending) during a recession is necessary to achieve growth.
A review of the paper found that its conclusions resulted from spreadsheet error;
“When properly calculated, the average real GDP growth rate for countries carrying a public-debt-to-GDP ratio of over 90 percent is actually 2.2 percent, not −0.1 percent as published in Reinhart and Rogoff”.
Dirty Data Kills
Even though the paper was retracted, it was cited thousands of times including by European politicians during the Eurozone crisis
George Osborne cited it directly in a speech in Parliament.
It was a key piece of justification used by the International Monetary Fund to impose austerity on Greece
“Researchers reported that government health spending fell sharply and that the causes of death that increased most were largely those that healthcare could have addressed. Greece’s reduced spending, required as part of the austerity measures, had been criticized for omitting measures to protect the country’s National Health System.”
2. Data Acquisition
Ways to gather online data
How to get data generated, published or hosted online:
Download a packaged dataset
API (Application Programming Interface): using a prebuilt set of functions developed by a company to access their services. Often pay to use. For example: Google Map API, Facebook API, Twitter API
Web scraping: using software, scripts or by-hand extracting data from what is displayed on a page or what is contained in the HTML file (often in tables).
Download
This is the simplest form of data acquisition. Many organizations provide access to data as CSV files(Comma Separated Values)
SERVER: takes the “address” field out of the URL, matches it with the corresponding location data, and sends the result back to the application as JSON data
Web scraping
Why do it?
Older government or smaller news sites might not have APIs for accessing data, or publish RSS feeds or have databases for download.
Or, you don’t want to pay to use the API or the database.
In these cases, the data you need isn’t being distributed in a structured way. You need to go out and get it.
BUT– be careful, you could get into trouble. Scraping is often a violation of terms and conditions. Use wisely.
Key acronyms
Data retrieval methods
API
Application Programming Interface
RSS
Really Simple Syndication
Data formats
CSV
Comma Separated Values
JSON
JavaScript Object Notation
Languages
XML
Extensible Markup Language
HTML
HyperText Markup Language
3. Data Types
Types of data
What kind of values are in your data (data types)?
Simple/atomic:
Numeric: integers, floats
Boolean: binary or true false values
Strings: sequence of symbols
Data storage
How is your data represented and stored (data format)?
SemistructuredData: not all records are represented by the same set of keys or some data records are not represented using the key-value pair structure.
Structured Data: each data record is presented in a form of a [possibly complex and multi-tiered] dictionary (json, xml, etc.)
Tabular Data: a dataset that is a two-dimensional table, where each row typically represents a single data record, and each column represents one type of measurement (csv, dat, xlsx, etc.).
Types of Data
We’ll see later that it’s important to distinguish between classes of variables or attributes based on the type of values they can take on.
Quantitative variable: is numerical and can be either:
discrete - a finite number of values are possible in any bounded interval. For example: “Number of siblings” is a discrete variable
continuous - an infinite number of values are possible in any bounded interval. For example: “Height” is a continuous variable
Categorical variable: no inherent order among the values For example: “What kind of pet you have” is a categorical variable
Pandas Data Types (dtypes)
Common Issues
Common issues with data:
Missing values: how do we fill in?
Wrong values: how can we detect and correct?
Messy format
Not usable: the data cannot answer the question posed
Panel Data
What if we were measuring deliveries over time in multiple grocery stores?
Data that involves repeated observations of the same groups over time is called panel data
Many country-level indicators (GDP, HDI, etc.), surveys, and censuses are in panel format.
The best way to organize this data is to have each row represent a unique combination of group and time. (e.g. country x year)
Example: GDP data
This is GDP data as provided by the World Bank
It’s in “wide” format
It’s relatively efficient
There are 12 cells
There is no repetition
But how would you add another variable (e.g. unemployment rate) to this data set?
Country
2019
2020
2021
Angola
69.3
53.6
72.5
Brazil
1873.2
1448.5
1608.9
Colombia
323.1
270.2
314.3
Example: GDP data
This is the same data in long format
It’s less efficient
There are now 27 cells
There’s repetition in the Country and Year columns
But it’s better structured
Adding further variables is straightforward: just add a column
Country
Year
GDP
Angola
2019
69.3
Angola
2020
53.6
Angola
2021
72.5
Brazil
2019
1873.2
Brazil
2020
1448.5
Brazil
2021
1608.9
Colombia
2019
323.1
Colombia
2020
270.2
Colombia
2021
314.3
Tabular data is best data
Common causes of messiness are:
Column headers are values, not variable names
Variables are stored in both rows and columns
Multiple variables are stored in one column/entry
Multiple types of experimental units stored in same table
In general, we want each file to correspond to a dataset, each column to represent a single variable and each row to represent a single observation.
We want to tabularize the data.
Recap
Acquire data
Download, use an API, scrape a website
Clean/format the data so that:
Each row represents a single object/observation/entry
Each column represents an attribute/property/feature of that entry
Values are numeric whenever possible
Columns contain atomic properties that cannot be further decomposed