New data source for political science researchers

Political Data Yearbook Interactive is a new source for data on election results, turnout and government composition for all EU and some non-European countries. It is basically an online version of the yearbooks that ECPR printed as part of the European Journal for Political Research for many years now.

The interactive online tool has some (limited) visualization options and can export data in several formats.

Music Network Visualization

Note: probably of interest only to the intersection of the readers who are into niche music genres and those interested in network visualization.

My music interests have always been rather, hmm…, eclectic. Somehow IDM, ambient, darkwave, triphop, acid jazz, bossa nova, qawali, Mali blues and other more or less obscure genres have managed to happily co-exist in my music collection. The sheer diversity always invited the question whether there is some structure to the collection, or each genre is an island of its own. Sounds like a job for network visualization!

Now, there are plenty of music network viz applications on the web. But they don’t show my collection, and just seem unsatisfactory for various reasons. So I decided to craft my own visualization using R and igraph.

As a first step I collected for all artists in my last.fm library the artists that the site classifies as similar. So I piggyback on last.fm for the network similarity measures. I also get info on the most-often used tag for the artist and the number of plays it has on the site. The rest is pretty straightforward as can be seen from the code.

# Load the igraph and foreign packages (install if needed)
require(igraph)
require(foreign)
lastfm<-read.csv("http://www.dimiter.eu/Data_files/lastfm_network_ad.csv", header=T,  encoding="UTF-8") #Load the dataset

lastfm$include<-ifelse(lastfm$Similar %in% lastfm$Artist==T,1,0) #Index the links between artists in the library
lastfm.network<-graph.data.frame(lastfm, directed=F) #Import as a graph

last.attr<-lastfm[-which(duplicated(lastfm$Artist)),c(5,3,4) ] #Create some attributes
V(lastfm.network)[1:106]$listeners<-last.attr[,2]
V(lastfm.network)[107:length(V(lastfm.network))]$listeners<-NA
V(lastfm.network)[1:106]$tag<-last.attr[,3]
V(lastfm.network)[107:length(V(lastfm.network))]$tag<-NA #Attach the attributes to the artist from the library (only)
V(lastfm.network)$label.cex$tag<-ifelse(V(lastfm.network)$listeners>1200000, 1.4, 
                                    (ifelse(V(lastfm.network)$listeners>500000, 1.2,
                                            (ifelse(V(lastfm.network)$listeners>100000, 1.1,
                                                   (ifelse(V(lastfm.network)$listeners>50000, 1, 0.8))))))) #Scale the size of labels by the relative popularity

V(lastfm.network)$color<-"white" #Set the color of the dots
V(lastfm.network)$size<-0.1 #Set the size of the dots
V(lastfm.network)$label.color<-NA
V(lastfm.network)[1:106]$label.color<-"white" #Only the artists from the library should be in white, the rest are not needed

E(lastfm.network)[ include==0 ]$color<-"black" 
E(lastfm.network)[ include==1 ]$color<-"red" #Color edges between artists in the library red, the rest are not needed

fix(tkplot) #Add manually to the function an argument for the background color of the canvas and set it to black (bg=black)

tkplot(lastfm.network, vertex.label=V(lastfm.network)$name, layout=layout.fruchterman.reingold,
       canvas.width=1200, canvas.height=800) #Plot the graph and adjust as needed

I plot the network with the tkplot command which allows for the manual adjustments necessary because many artist names get on top of each other in the initial plot. Because the export options of tkplot are limited I just took a print screen ( I know, I know, that’s kind of cheating ;-)), added the tittle in Photoshop and, voila, it’s done!

[click to enlarge and explore]
my-music-netowrk

Knowing intimately the artists in the graph, I can certify that the network definitely makes a lot of sense. I love the small clusters (Flying Louts, Andy Stott, Extrawelt and Claro Intelecto [minimal/dub], or Anouar Brahem and Rabih Abou-Khalil [ethno jazz]) loosely connected to the rest of the network. And I love the fact that the boundary spanners are immediately obvious (e.g. Pink Martini between acid jazz and world music [what a stupid label by the way!], or Cesaria Evora between African and Caribbean music, or Portishead between brit-pop, trip-hop and darkwave, or Amon Tobin between trip-hop, electro and IDM). Even the different world music genres are close to each other but still unconnected. And somehow Banco De Gaya, the most ethno of all electronica in the library, ended up closest to the world/ethno clusters. There are a few problems, like Depeche Mode, which get to be pulled from the opposite sides of the graph, but these are very few.

Altogether, I have to admit I feel like a teenage dream of mine has finally been realized. But I realize the network is a rather personal thing (as it was meant to be) so I don’t expect many to get overly excited about it. Still, I would be glad to hear your comments or suggestions for extensions and improvements. And, if you were a good boy/girl during the year, I could also consider visualizing your last.fm network as a present for the new year!

Network visualization in R with the igraph package

In this post I showed a visualization of the organizational network of my department. Since several people asked for details how the plot has been produced, I will provide the code and some extensions below. The plot has been done entirely in R (2.14.01) with the help of the igraph package. It is a great package but I found the documentation somewhat difficult to use, so hopefully this post can be a helpful introduction to network visualization with R. Here we go:

# Load the igraph package (install if needed)

require(igraph)

# Data format. The data is in 'edges' format meaning that each row records a relationship (edge) between two people (vertices).
# Additional attributes can be included. Here is an example:
#	Supervisor	Examiner	Grade	Spec(ialization)
#	AA		BD		6	X	
#	BD		CA		8	Y
#	AA		DE		7	Y
#	...		...		...	...
# In this anonymized example, we have data on co-supervision with additional information about grades and specialization. 
# It is also possible to have the data in a matrix form (see the igraph documentation for details)

# Load the data. The data needs to be loaded as a table first: 

bsk<-read.table("http://www.dimiter.eu/Data_files/edgesdata3.txt", sep='t', dec=',', header=T)#specify the path, separator(tab, comma, ...), decimal point symbol, etc.

# Transform the table into the required graph format:
bsk.network<-graph.data.frame(bsk, directed=F) #the 'directed' attribute specifies whether the edges are directed
# or equivelent irrespective of the position (1st vs 2nd column). For directed graphs use 'directed=T'

# Inspect the data:

V(bsk.network) #prints the list of vertices (people)
E(bsk.network) #prints the list of edges (relationships)
degree(bsk.network) #print the number of edges per vertex (relationships per people)

# First try. We can plot the graph right away but the results will usually be unsatisfactory:
plot(bsk.network)

Here is the result:

Not very informative indeed. Let’s go on:

 
#Subset the data. If we want to exclude people who are in the network only tangentially (participate in one or two relationships only)
# we can exclude the by subsetting the graph on the basis of the 'degree':

bad.vs<-V(bsk.network)[degree(bsk.network)<3] #identify those vertices part of less than three edges
bsk.network<-delete.vertices(bsk.network, bad.vs) #exclude them from the graph

# Plot the data.Some details about the graph can be specified in advance.
# For example we can separate some vertices (people) by color:

V(bsk.network)$color<-ifelse(V(bsk.network)$name=='CA', 'blue', 'red') #useful for highlighting certain people. Works by matching the name attribute of the vertex to the one specified in the 'ifelse' expression

# We can also color the connecting edges differently depending on the 'grade': 

E(bsk.network)$color<-ifelse(E(bsk.network)$grade==9, "red", "grey")

# or depending on the different specialization ('spec'):

E(bsk.network)$color<-ifelse(E(bsk.network)$spec=='X', "red", ifelse(E(bsk.network)$spec=='Y', "blue", "grey"))

# Note: the example uses nested ifelse expressions which is in general a bad idea but does the job in this case
# Additional attributes like size can be further specified in an analogous manner, either in advance or when the plot function is called:

V(bsk.network)$size<-degree(bsk.network)/10#here the size of the vertices is specified by the degree of the vertex, so that people supervising more have get proportionally bigger dots. Getting the right scale gets some playing around with the parameters of the scale function (from the 'base' package)

# Note that if the same attribute is specified beforehand and inside the function, the former will be overridden.
# And finally the plot itself:
par(mai=c(0,0,1,0)) 			#this specifies the size of the margins. the default settings leave too much free space on all sides (if no axes are printed)
plot(bsk.network,				#the graph to be plotted
layout=layout.fruchterman.reingold,	# the layout method. see the igraph documentation for details
main='Organizational network example',	#specifies the title
vertex.label.dist=0.5,			#puts the name labels slightly off the dots
vertex.frame.color='blue', 		#the color of the border of the dots 
vertex.label.color='black',		#the color of the name labels
vertex.label.font=2,			#the font of the name labels
vertex.label=V(bsk.network)$name,		#specifies the lables of the vertices. in this case the 'name' attribute is used
vertex.label.cex=1			#specifies the size of the font of the labels. can also be made to vary
)

# Save and export the plot. The plot can be copied as a metafile to the clipboard, or it can be saved as a pdf or png (and other formats).
# For example, we can save it as a png:
png(filename="org_network.png", height=800, width=600) #call the png writer
#run the plot
dev.off() #dont forget to close the device
#And that's the end for now.

Here is the result:

Still not perfect, but much more informative and aesthetically pleasing.

Additional information can be found on this guide to igraph which is in development, the examples here, and the official CRAN documentation of the package. Especially useful is this list of the plot attributes that can be tweaked. The plots can also be adjusted interactively using the tkplot function instead of plot, but the options for saving the resulting figure are limited.

Have fun with your networks!

Scatterplots vs. regression tables (Economics professors edition)

I have always considered scatterplots to be the best available device to show relationships between variables. But it must be even better to have the regression table and a full description of the results in addition, right? Not so fast:

A new paper shows that professional economists make largely correct inferences about data when looking at a scatterplot, but get confused when they are shown the details of the regressions next to the scatterplot, and totally mess it up when they are shown only the numbers without the plot! Wow! If you needed any more persuasion that graphing your data and your results are more important than those regression tables with zillions of numbers, now you have it.

P.S. The authors of this research could have done a better job themselves in communicating visually their findings…

[via Felix Salmon]

 

The illusion of predictability: How regression statistics mislead experts
Emre Soyer& Robin M. Hogarth
Abstract
Does the manner in which results are presented in empirical studies affect perceptions of the predictability of the outcomes? Noting the predominant role of linear regression analysis in empirical economics, we asked 257 academic economists to make probabilistic inferences given different presentations of the outputs of this statistical tool. Questions concerned the distribution of the dependent variable conditional on known values of the independent variable. Answers based on the presentation mode that is standard in the literature led to an illusion of predictability; outcomes were perceived to be more predictable than could be justified by the model. In particular, many respondents failed to take the error term into account. Adding graphs did not improve inferences. Paradoxically, when only graphs were provided (i.e., no regression statistics), respondents were more accurate. The implications of our study suggest, inter alia, the need to reconsider how to present empirical results and the possible provision of easy-to-use simulation tools that would enable readers of empirical papers to make accurate inferences.

New tool for discourse network analysis

EJPR has just published an article introducing a new tool for ‘discourse network analysis’. Using the tool, you can measure and visualize political discourses and the networks of actors affiliated to each discourse. One can study the actor congruence networks (based on the number of statements actors share), concept congruence networks (based on whether statements are used by an actor in the same way) and trace the evolution of both over time.

Here is a graph taken from the paper which illustrates the actor congruence networks for the issue of software patents in the EU (click to enlarge):

The discourse networks analysis tool is free and available from the website of Philip Leifeld, one of the co-authors of the article. I can’t wait to get my hands on the program and try it out for myself. The tool promises to be an interesting alternative to evolutionary factor analysis – another new method for studying policy frames and discourses that I recently discussed – with the added benefit of being able to present actors and frames in an integrated analysis.  

Here is the abstract of the EJPR article (there are more resources at this website):

In 2005, the European Parliament rejected the directive ‘on the patentability of computer-implemented inventions’, which had been drafted and supported by the European Commission, the Council and well-organised industrial interests, with an overwhelming majority. In this unusual case, a coalition of opponents of software patents prevailed over a strong industry-led coalition. In this article, an explanation is developed based on political discourse showing that two stable and distinct discourse coalitions can be identified and measured over time. The apparently weak coalition of software patent opponents shows typical properties of a hegemonic discourse coalition. It presents itself as being more coherent, employs a better-integrated set of frames and dominates key economic arguments, while the proponents of software patents are not as well-organised. This configuration of the discourse gave leeway for an alternative course of political action by the European Parliament. The notion of discourse coalitions and related structural features of the discourse are operationalised by drawing on social network analysis. More specifically, discourse network analysis is introduced as a new methodology for the study of policy debates. The approach is capable of measuring empirical discourses both statically and in a longitudinal way, and is compatible with the policy network approach.

Visualizing left-right government positions

How does the political landscape of Europe change over time? One way to approach this question is to map the socio-economic left-right positions of the governments in power. So let’s plot the changing ideological  positions of the governments using data from the Manifesto project! As you will see below, this proved to be a more challenging task than I imagined, but the preliminary results are worth sharing nonetheless.

First, we need to extract the left-right positions from the Manifesto dataset. Using the function described here, this is straightforward:

lr2000<-manifesto.position('rile', start=2000, end=2000)

This compiles the (weighted) cabinet positions for the European countries for the year 2000. Next, let’s generate a static map. We can use the new package rworldmap for this purpose. Let’s also build a custom palette that maps colors to left-right values. Since in Europe red traditionally is the color of the political left (the socialists), the palette ranges from dark red to gray to dark blue (for the right-wing governments).

library (rworldmap)
op <- palette(c('red4','red3','red2','red1','grey','blue1', 'blue2','blue3', 'blue4'))

After recoding the name of the UK, we are ready to bind our data and plot the map. You can save the map as a png file.

library(car)
lr2000$State<-recode(lr$State, "'Great Britain'='United Kingdom'")

lrmapdata <- joinCountryData2Map( lr2000,joinCode = "NAME", nameJoinColumn = "State", mapResolution='medium')

par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
png(file='LR2000map.png', width=640,height=480)
mapCountryData( lrmapdata, nameColumnToPlot="position",colourPalette=op, xlim=c(-9,31), ylim=c(36,68), mapTitle='2000', aspect=1.25,addLegend=T )
dev.off()

The limits on on the x- and y-axes center the map on Europe. It is a process of trial and error till you get it right, and the limits need to be co-ordinated with the aspect and the width and height of the png file so that the map looks reasonably well-proportioned. Here  is the result (click to see in full resolution):

It looks a bit chunky but not too bad. Next, we have to find a way to show developments over time. We could show several plots for different years on one page, but this is not very effective:

A much better way would be to make the maps dynamic, or, in other words, to animate them. But this is easier said than done. After searching for a few days for tools that can accomplish the job, I settled for producing individual maps for each month, importing the series into Adobe Flash, and exporting a simple animation movie. The R code to produce  the individual  maps:

lr<-manifesto.position('rile', start=1948, end=2008, period='month')
lr$State<-recode(lr$State, "'Great Britain'='United Kingdom'")
u.c<-unique(lr$Year.month)
for (i in 1:length(u.c)){
     lr.temp<-subset(lr, lr$Year.month==u.c[i])
     lrmapdata <- joinCountryData2Map( lr.temp,joinCode = "NAME", nameJoinColumn = "State", mapResolution='medium')
     plot.name<-paste('./maps/map',i,'.png', sep='') 

     par(mai=c(0,0,0.2,0),xaxs="i",yaxs="i")
     png(file=plot.name, width=640,height=480)
     mapCountryData( lrmapdata, nameColumnToPlot="position",colourPalette=op, xlim=c(-9,31), ylim=c(36,68), mapTitle=u.c[i], aspect=1.25,addLegend=T )
     dev.off() }

And here is the result (opens outside the post):

Flash video of Left-Right positions (slow)

It kind of works, it has buttons for navigation, but it has one major flow – it is damn slow. It should be 12 frames (maps) per second, and it is 12 fps inside Flash, but once exported, the frame rate goes down (probably because my laptop’s processor is too slow). In fact, I can export a fast version, but only if I get rid of the control buttons. Here it is (right-click and press play to start):

Flash video of Left-Right positions (fast)

You can also play the animation as an AVI video (uploaded on YouTube), but somehow, through the mysteries of video-processing, a crisp slideshow of 8mb ended up as a low-res movie of 600mb.


The results resemble my initial idea, although none is perfect. Ideally, I would want a fast movie with controls and a time-slider, but my Flash programming skills (and my computer) need to be upgraded for that. Meanwhile, the Manifesto project could also update their data on which the animation is based.

Altogether, the experience of creating the visualization has been much more painful than I anticipated. First, there doesn’t seem to be an easy way to get a map of Europe (or, more precisely, of the European Union territories) for use in R. The available options are  either too low resolution, or too outdated (e.g. featuring Czechoslovakia), or require centering a world-map using ylim and xlim which is a problem because these coordinates are connected to the dimensions and the resolution of the output plot. For the US, and for individual European states, there are tons of slick and easy-to-find maps (shapefiles), but for Europe I couldn’t find anything that doesn’t feature huge tracts of land east to the Urals, which are irrelevant and remain empty with political data (which is usually available for the EU+ states only). Any pointers to good, relatively high-res maps (shapefiles) of the EU will be much appreciated.

Second, producing an animation out of the individual maps is rather difficult. Currently, Google Charts offer dynamic plots and static maps, I hope in the future they include dynamic maps as well. Especially because the googleVis package makes it possible to build Google charts from within R. I also found a new tool called StatPlanet which seems relevant and rather cool, but still relies on Adobe Flash and has no packaged Europe/EU maps. The big guns in visualization software are most probably up to the task but Tableau is prohibitively expensive and Processing is said to have a steep learning curve. Again, any help in identifying solutions that do not require proprietary software to produce animated maps would be much appreciated. I hope to be able to post an update on the project soon.