About Me Blog
A tutorial on tidy cross-validation with R Analyzing NetHack data, part 1: What kills the players Analyzing NetHack data, part 2: What players kill the most Building a shiny app to explore historical newspapers: a step-by-step guide Classification of historical newspapers content: a tutorial combining R, bash and Vowpal Wabbit, part 1 Classification of historical newspapers content: a tutorial combining R, bash and Vowpal Wabbit, part 2 Curly-Curly, the successor of Bang-Bang Dealing with heteroskedasticity; regression with robust standard errors using R Easy time-series prediction with R: a tutorial with air traffic data from Lux Airport Exporting editable plots from R to Powerpoint: making ggplot2 purrr with officer Fast food, causality and R packages, part 1 Fast food, causality and R packages, part 2 For posterity: install {xml2} on GNU/Linux distros Forecasting my weight with R From webscraping data to releasing it as an R package to share with the world: a full tutorial with data from NetHack Get text from pdfs or images using OCR: a tutorial with {tesseract} and {magick} Getting data from pdfs using the pdftools package Getting the data from the Luxembourguish elections out of Excel Going from a human readable Excel file to a machine-readable csv with {tidyxl} Historical newspaper scraping with {tesseract} and R How Luxembourguish residents spend their time: a small {flexdashboard} demo using the Time use survey data Imputing missing values in parallel using {furrr} Intermittent demand, Croston and Die Hard Looking into 19th century ads from a Luxembourguish newspaper with R Making sense of the METS and ALTO XML standards Manipulate dates easily with {lubridate} Manipulating strings with the {stringr} package Maps with pie charts on top of each administrative division: an example with Luxembourg's elections data Missing data imputation and instrumental variables regression: the tidy approach Modern R with the tidyverse is available on Leanpub Objects types and some useful R functions for beginners Pivoting data frames just got easier thanks to `pivot_wide()` and `pivot_long()` R or Python? Why not both? Using Anaconda Python within R with {reticulate} Searching for the optimal hyper-parameters of an ARIMA model in parallel: the tidy gridsearch approach Some fun with {gganimate} Split-apply-combine for Maximum Likelihood Estimation of a linear model Statistical matching, or when one single data source is not enough The best way to visit Luxembourguish castles is doing data science + combinatorial optimization The never-ending editor war (?) The year of the GNU+Linux desktop is upon us: using user ratings of Steam Play compatibility to play around with regex and the tidyverse Using Data Science to read 10 years of Luxembourguish newspapers from the 19th century Using a genetic algorithm for the hyperparameter optimization of a SARIMA model Using cosine similarity to find matching documents: a tutorial using Seneca's letters to his friend Lucilius Using linear models with binary dependent variables, a simulation study Using the tidyverse for more than data manipulation: estimating pi with Monte Carlo methods What hyper-parameters are, and what to do with them; an illustration with ridge regression {disk.frame} is epic {pmice}, an experimental package for missing data imputation in parallel using {mice} and {furrr} Building formulae Functional peace of mind Get basic summary statistics for all the variables in a data frame Getting {sparklyr}, {h2o}, {rsparkling} to work together and some fun with bash Importing 30GB of data into R with sparklyr Introducing brotools It's lists all the way down It's lists all the way down, part 2: We need to go deeper Keep trying that api call with purrr::possibly() Lesser known dplyr 0.7* tricks Lesser known dplyr tricks Lesser known purrr tricks Make ggplot2 purrr Mapping a list of functions to a list of datasets with a list of columns as arguments Predicting job search by training a random forest on an unbalanced dataset Teaching the tidyverse to beginners Why I find tidyeval useful tidyr::spread() and dplyr::rename_at() in action Easy peasy STATA-like marginal effects with R Functional programming and unit testing for data munging with R available on Leanpub How to use jailbreakr My free book has a cover! Work on lists of datasets instead of individual datasets by using functional programming Method of Simulated Moments with R New website! Nonlinear Gmm with R - Example with a logistic regression Simulated Maximum Likelihood with R Bootstrapping standard errors for difference-in-differences estimation with R Careful with tryCatch Data frame columns as arguments to dplyr functions Export R output to a file I've started writing a 'book': Functional programming and unit testing for data munging with R Introduction to programming econometrics with R Merge a list of datasets together Object Oriented Programming with R: An example with a Cournot duopoly R, R with Atlas, R with OpenBLAS and Revolution R Open: which is fastest? Read a lot of datasets at once with R Unit testing with R Update to Introduction to programming econometrics with R Using R as a Computer Algebra System with Ryacas

Exploring NACE codes

A quick one today. If you work with economic data, you’ll be confronted to NACE code sooner or later. NACE stands for Nomenclature statistique des Activités économiques dans la Communauté Européenne. It’s a standard classification of economic activities. It has 4 levels, and you can learn more about it here.

Each level adds more details; consider this example:

C - Manufacturing
C10 - Manufacture of food products
C10.1 - Processing and preserving of meat and production of meat products
C10.1.1 - Processing and preserving of meat
C10.1.2 - Processing and preserving of poultry meat
C10.1.3 - Production of meat and poultry meat products

So a company producing meat and poultry meat products would have NACE code level 4 C10.1.3 with it. Today for work I had to create a nice visualisation of the hierarchy of the NACE classification. It took me a bit of time to find a nice solution, so that’s why I’m posting it here. Who knows, it might be useful for other people. First let’s get the data. Because finding it is not necessarily very easy if you’re not used to navigating Eurostat’s website, I’ve put the CSV into a gist:

library(tidyverse)
library(data.tree)
library(igraph)
library(GGally)
nace_code <- read_csv("https://gist.githubusercontent.com/b-rodrigues/4218d6daa8275acce80ebef6377953fe/raw/99bb5bc547670f38569c2990d2acada65bb744b3/nace_rev2.csv")
## Parsed with column specification:
## cols(
##   Order = col_double(),
##   Level = col_double(),
##   Code = col_character(),
##   Parent = col_character(),
##   Description = col_character(),
##   `This item includes` = col_character(),
##   `This item also includes` = col_character(),
##   Rulings = col_character(),
##   `This item excludes` = col_character(),
##   `Reference to ISIC Rev. 4` = col_character()
## )
head(nace_code)
## # A tibble: 6 x 10
##    Order Level Code  Parent Description `This item incl… `This item also…
##    <dbl> <dbl> <chr> <chr>  <chr>       <chr>            <chr>           
## 1 398481     1 A     <NA>   AGRICULTUR… "This section i… <NA>            
## 2 398482     2 01    A      Crop and a… "This division … This division a…
## 3 398483     3 01.1  01     Growing of… "This group inc… <NA>            
## 4 398484     4 01.11 01.1   Growing of… "This class inc… <NA>            
## 5 398485     4 01.12 01.1   Growing of… "This class inc… <NA>            
## 6 398486     4 01.13 01.1   Growing of… "This class inc… <NA>            
## # … with 3 more variables: Rulings <chr>, `This item excludes` <chr>,
## #   `Reference to ISIC Rev. 4` <chr>

So there’s a bunch of columns we don’t need, so we’re going to ignore them. What I’ll be doing is transforming this data frame into a data tree, using the {data.tree} package. For this, I need columns that provide the hierarchy. I’m doing this with the next chunk of code. I won’t explain each step, but the idea is quite simple. I’m using the Level column to create new columns called Level1, Level2, etc. I’m then doing some cleaning:

nace_code <- nace_code %>%
  select(Level, Code)

nace_code <- nace_code %>%
  mutate(Level1 = ifelse(Level == 1, Code, NA)) %>%
  fill(Level1, .direction = "down") %>%  
  mutate(Level2 = ifelse(Level == 2, Code, NA)) %>%
  fill(Level2, .direction = "down") %>%  
  mutate(Level3 = ifelse(Level == 3, Code, NA)) %>%
  fill(Level3, .direction = "down") %>%  
  mutate(Level4 = ifelse(Level == 4, Code, NA)) %>%  
  filter(!is.na(Level4))

Let’s take a look at how the data looks now:

head(nace_code)
## # A tibble: 6 x 6
##   Level Code  Level1 Level2 Level3 Level4
##   <dbl> <chr> <chr>  <chr>  <chr>  <chr> 
## 1     4 01.11 A      01     01.1   01.11 
## 2     4 01.12 A      01     01.1   01.12 
## 3     4 01.13 A      01     01.1   01.13 
## 4     4 01.14 A      01     01.1   01.14 
## 5     4 01.15 A      01     01.1   01.15 
## 6     4 01.16 A      01     01.1   01.16

I can now create the hierarchy using by creating a column called pathString and passing that data frame to data.tree::as.Node(). Because some sections, like C (manufacturing) are very large, I do this separately for each section by using the group_by()-nest() trick. This way, I can create a data.tree object for each section. Finally, to create the plots, I use igraph::as.igraph() and pass this to GGally::ggnet2(), which takes care of creating the plots. This took me quite some time to figure out, but the result is a nice looking PDF that the colleagues can now use:

nace_code2 <- nace_code %>%
  group_by(Level1, Level2) %>%
  nest() %>%
  mutate(nace = map(data, ~mutate(., pathString = paste("NACE2",
                                       Level1,
                                       Level2,
                                       Level3,
                                       Level4,
                                       sep = "/")))) %>%
  mutate(plots = map(nace, ~as.igraph(as.Node(.)))) %>%
  mutate(plots = map(plots, ggnet2, label = TRUE))


pdf("nace_maps.pdf")
pull(nace_code2, plots)
dev.off()

Here’s how the pdf looks like:

If you want to read more about {data.tree}, you can do so here and you can also read more about the ggnet2() here.

Hope you enjoyed! If you found this blog post useful, you might want to follow me on twitter for blog post updates and buy me an espresso or paypal.me, or buy my ebook on Leanpub.

Buy me an EspressoBuy me an Espresso