Skip to content

Instantly share code, notes, and snippets.

@christophergandrud
Last active March 30, 2016 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophergandrud/eee2d2c2dc5d78239e27063f3fae948c to your computer and use it in GitHub Desktop.
Save christophergandrud/eee2d2c2dc5d78239e27063f3fae948c to your computer and use it in GitHub Desktop.
Simple example of creating a missingness map with country-year data
# ---------------------------------------------------------------------------- #
# Create a missingness map to examine patterns of missing values across variables
# Christopher Gandrud
# MIT LICENSE
# ---------------------------------------------------------------------------- #
# Load required packages
library(WDI)
library(dplyr)
library(Amelia)
# Download 5 variables from the World Bank Development Indicators ----------
indicators <- c('EN.ATM.METH.KT.CE', 'EG.USE.ELEC.KH.PC', 'EN.ATM.CO2E.PC',
'SP.POP.GROW', 'EG.USE.COMM.CL.ZS')
wdi <- WDI(indicator = indicators, start = 1990, end = 2014)
# Rename indicators and drop iso2c variable
wdi <- wdi %>% rename(methane_emissions = EN.ATM.METH.KT.CE) %>%
rename(electricity_use = EG.USE.ELEC.KH.PC) %>%
rename(co2_emissions = EN.ATM.CO2E.PC) %>%
rename(population_growth = SP.POP.GROW) %>%
rename(alternative_energy = EG.USE.COMM.CL.ZS) %>%
select(-iso2c)
# Subset the data (just to make the illustration easier to understand)
wdi_sub <- wdi %>% filter(country %in% c('Afghanistan', 'Belgium', 'China',
'Colombia', 'France', 'Germany',
'South Africa', 'Zambia'))
# Create missingness map ---------------------------------------------------
missmap(obj = wdi_sub,
csvar = 'country', # country identifying variable
tsvar = 'year', # time identifying variable
y.cex = 0.75, # decrease y-axis labels so they fit
x.cex = 0.55 # decrease x-axis labels so they fit
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment