Summarizing a data-frame with xtable

Today I had to pretty-print a summary of a R data-frame composed of lots of factors. I thought I would use LaTeX and the xtable package.

Unfortunately, if you try the natural

xtable(summary(data))

you get a table that is very, very wide. Since there’s no easy way in LaTeX to split wide tables over multiple pages, that approach won’t work.

Here, instead is my trick for pretty-printing such summaries (basically shoving everything into a matrix and using xtable on that). This won’t work if you have a period in any of your variable names. Kludgy, I know.

foo<-sapply(data,summary) ## get summary of each var.
bar<-matrix(nrow=length(unlist(foo)),ncol=3)

bar[,1]<-gsub(”\\..*”,”",names(unlist(foo))) ## first column has variable names
bar[,1][duplicated(bar[,1])]<-”" ## don’t repeat variable names
bar[,2]<-gsub(”.*?\\.”,”",names(unlist(foo))) ## second column has factor levels
bar[,3]<-as.numeric(unlist(foo))
bar[,3][is.na(bar[,3])]<-0 ## in case anything has been coerced

colnames(bar)<-c(”Item”,”Response Category”,”Number”)
rownames(bar)<-rep(”",nrow(bar))
bar<-xtable(bar)
align(bar)<-c(”l”,”l”,”p{7cm}”,”r”) ## make sure factor levels don’t take up more than 7cm
print(bar,tabular.environment = “longtable”,include.rownames=FALSE,size=”footnotesize”,floating=FALSE, #
hline.after=NULL, #
add.to.row=list(pos=list(-1,0, nrow(bar)), #
command=c(’\\toprule ‘, # use nice booktabs formatting
‘\\midrule \\endhead ‘, # with running headers from longtable
‘\\bottomrule ‘)))

Remember to declare that you’re using booktabs and longtable. Here’s an example of data-frame summary.

How the 2001 result would have been with the current electoral law

After our article on Italian electoral reform was published, I was asked how the results of the Italian election of 2001 would have turned out if the current electoral system had been used. Here’s my very rough answer, which ignores the complexities (too many to be counted) of the ‘porcata’. Continue Reading »

Openparlamento.it

From the same people who brought you openpolis, openparlamento.it went live today.

I’m tremendously pleased that sites like this are starting to happen in Italy. The Camera actually has quite well-formated XML files for each of the plenary sessions, and all it needed was for someone to put that to good use.

So wonderful is the site that I feel churlish to pick on them. But just note that their graph of parliamentarians’ positions is based on multidimensional scaling, and as such is going to fall prey to the same problems as optimal classification analysis — extreme deputies in the governing coalition who rebel are going to be depicted as centrists, when they’re not.

Still, nice visualization.

Openparlamento visualization

Italy Today: The Sick Man of Europe

Still no comment on the EP elections (shocked and distraught by BNP success in the UK), but here’s a plug for a book edited by Andrea Mammone and Giuseppe Veltri, to which I’ve contributed a chapter:

This volume represents one of the most comprehensive analyses of contemporary Italy. It is a provocative and an innovative collection of essays that aims to highlight the current ‘crisis’ of the country through an analysis of several different ‘dark shadows’ of contemporary Italian society.

From the new emerging mafias to the predicaments of the political system, from the ongoing presence of Fascism to the failed reconciliation of the years of terrorism, from the missed opportunity of the EU funding to the increasing regional economic gap: these are only a few of the critical issues that affect the country of la dolce vita and that have been here analyzed in an unprecedented effort. I

taly already had a long history of ‘unsolved’ issues, several chronic problems and contradictions that have been ignored for a very long time, during which they have assumed dramatic proportions and gravity. The peninsula has henceforth become the ‘Sick Man of Europe’, a country facing a veritable decline also caused by apparent incapacity and difficulties of the ruling economic, political and social elites.

See the Routledge site for the details of the chapters

Article on Italian electoral reform out

My article with Alan Renwick and David Hine is out now in Electoral Studies’ “in press” section. Abstract:

In December 2005, Italy’s mixed-member electoral system was replaced with a system of bonus-adjusted proportional representation. The reform conformed with rational-choice models in that it was imposed by the ruling coalition, which sought to bolster its own power interests. But the case illustrates the impossibility of reducing such power-based motivation to a single goal, such as seat maximization. Power is shaped by many factors, and electoral systems influence many of these. This article develops a theoretical framework for understanding the various power-oriented considerations that may operate in electoral reform. It then analyses the role these played in Italy. It argues, in particular, for the need to take account of coalition dynamics when studying such processes.

Mapping EUI network drives on Linux

[Of parochial interest only]

I’m using the EUI’s Citrix farm for some high-demand computing. Instead of emailing the data files back and forth, I finally mapped my G: drive. Here’s how I did it:

  1. sudo apt-get install smbfs
  2. sudo mkdir /media/euishare
  3. sudo mount -t cifs //RESSRV1/username$ /media/euishare -o username=username,password=******,iocharset=utf8,file_mode=0777,dir_mode=0777

Replace username with your username, and the asterisks with your own password. If you’re not a SPS, HEC or Law researcher, RESSRVC1 is not your server: check this list for your server.

Italian polling for EP elections (2)

The campaign for the European parliament elections continues to lack lustre. There’s been no noticeable decline in the electoral fortunes of the PdL, despite Silvio Berlusconi’s unclear relationship with an 18-year old Neapolitan girl, although the PD continues its (very slight) uptick.

Continue Reading »

Latvian public broadcaster about to run out of money

Budget is 30% down on last year. They say they can keep going until September.

Italian polling for EP elections

Last year I aggregated the polling data for the general election.

That was fun.

This year, I’m aggregating the polling data for the European Parliament election — the Italian part, anyway.

This is less fun.

As everyone knows, European Parliament elections are second-order elections. Voters use them not to vote on the issues — they don’t know about the issues — but instead to give the government a progress report.

And whilst progress reports are interesting, your view of government performance might not be rocked that much by campaign events. And that’s when there is a campaign — so far, the parties have promised to spend less “in light of the current economic climate”.

With that out of the way, I present you with some flat lines: Continue Reading »

Freedom House rankings for 2009

The Freedom House rankings for press freedom for 2009 are now out [h/t Michele].

Italy drops to 73rd place, loses ‘Free’ status. The headline for the press release says that this is the first time Italy has lost ‘Free’ status. Not so This is the second time Italy has suffered this fate– it was also judged ‘partly free’ from 2002 to 2004, again with Berlusconi in office.