No more ascii-art on Linux

I have just come across Will Lowe’s excellent post on using pretty Latex tables when you’re still working on the analysis.

I had to make a tiny modification to get this run on GNU/Linux: changing the “open” command to the more specific “evince” (i.e. the PDF viewer). All credit should go to Will Lowe.

as_pdf <- function(x){
require(tools)

fname <- tempfile(pattern = "texview-", tmpdir = tempdir(),
fileext = ".tex")

header <- "\\documentclass{article}
\\usepackage[margin=10pt,font=small,labelformat=empty,
labelsep=none]{caption}
\\usepackage{dcolumn}
\\thispagestyle{empty}
\\begin{document}"
footer <- "\\end{document}"

cat(header, file=fname, sep='\n')
cat(x, file=fname, append=TRUE)
cat(footer, file=fname, append=TRUE, sep='\n')

newfile <- paste0(file_path_sans_ext(fname), ".pdf")
cropfile <- paste0(file_path_sans_ext(fname), "-crop.pdf")

origdir <- getwd()
tryCatch({
setwd(tempdir()) ## next to the tex file
texi2pdf(fname, clean=TRUE)
system2("pdfcrop", args=c("--margins", "10", newfile, cropfile),
stdout=FALSE)
}, finally=setwd(origdir))

system2("evince", args=c(cropfile))
## shell.exec(file_path_as_absolute(cropfile)) ## for Windows, probably
}

One observation: The leading comment line in the stargazer package breaks the function in its current version (the Latex code is not broken down into lines). All works fine with the apsrtable package.

Leik’s Measure of Ordinal Consensus

In 1966 Robert K. Leik introduced a measure of ordinal consensus based on cumulative frequency distributions. It can be used to express agreement or polarization, just like Cees van der Eijk‘s measure of agreement “A”, and its derived measure of polarization. A difference exists in that in Leik’s measure, an equal distribution of frequencies – all categories equally common – does not always give the same value. Leik defends this, arguing that an equal distribution should only be considered the mid-point between agreement and polarization if the number of categories is very large. With a small number of categories, polarization may simply be a result of chance.

Here’s a graphical summary of how Leik’s measure of ordinal dispersal behaves with increasing numbers of categories (consensus is defined as 1 minus dispersal), as outlined in table 3 of the article.
leik1

Leik’s measure of ordinal dispersion is available in the latest version of the package agrmt (version 0.27, not yet on CRAN)

Leik, R. 1966. ‘A measure of ordinal consensus’. Pacific Sociological Review 9 (2): 85–90.

Calculating Polarization

How can we enumerate the polarization of a party system, or the polarization of opinions? Polarization exists when the population are divided in their opinions. If we measure these opinions on an ordered scale (as is common place), we’re looking at peaks in two non-adjacent positions. An ideal type would be 50% for an issue, and 50% against it.

The opposite ideal type can help us formulate what we mean by polarization. If all positions are equally popular, we cannot really speak of polarization, but it is not the logical opposite. The opposite of polarization is agreement: everyone has the same position on an issue.

To enumerate polarization, we can work backwards from Cees van der Eijk‘s (2001) measure of agreement: inverting it. I’ve written up a few functions to do this in R.

Van der Eijk, C. 2001. “Measuring agreement in ordered rating scales.” Quality and Quantity 35(3): 325-341.

Plotting Connected Lines with Missing Values

When we plot data with missing values, R does not connect them. This is probably the correct behaviour, but what if we really want to gloss over missing data points?

plot(variable.name[country=="UK"], type="b") gives me something like the following. I used type="b", since type="l" will give an empty plot – generally not very useful.

miss_plain

What if we simply leave out the missing values? plot(na.omit(variable.name[country=="UK"]), type="b") kind of works, but we lose the correct spacing on the x-axis:

miss_omit

So what we can do is the following. In a first step we identify for which points we have data. Next we plot, but only these. In contrast to the above method, the spacing on the x-axis remains intact.

miss <- !is.na(variable.name[country=="UK"])
plot(which(miss), variable.name[country=="UK" & miss], col="red", type="b", lwd=2)

miss_miss

It is important to include an xlim argument if we add multiple lines on the same plot. Typically I draw the axes separately, as this gives me more control over them, especially the labels on the x-axis.

miss <- !is.na(variable.name[country=="UK")
plot(which(miss), variable.name[country=="UK" & miss], col="red", type="b", lwd=2, axes=FALSE, xlim=c(1,16))
axis(2)
axis(1,at=c(1,6,11,16), labels=c("1995","2000","2005","2010"))

miss_typical

Quotas in Costa Rica

National legislature differ in the extent to which they include women. For many reasons it is desirable to have inclusive legislatures, and quotas are one means to increase the proportion of women in legislatures. Are they effective? Pamela Paxton, Melanie Hughes and Matthew Painter included the example of Costa Rica in their EJPR article.

The example is useful to illustrate that just requiring parties to have 40% women on their lists does not make a big difference. Parties can comply with this rule while placing most of the women at the bottom of the list, resulting in few (if any) more women elected to the legislature. Costa Rica later introduced placement mandates, making the quota law much more effective.

Costa Rica

Here I replicate (and extend) their figure 1. I have used the latest figures from the IPU, which differ slightly for 1994 and 2002 (red dots = data cited in article). We can see that there was stagnation after the increase associated with the placement mandates. As far as I can tell, this is typical. My take on this is that attitudes need to catch up before we see further increases (dashed line, assuming a gradual change over time). Indeed, the quotas possibly were made possible by the low proportion of women in legislature lagging attitudes on what the population deems desirable. A big question is whether these additional women in the legislature have an impact on policy and the lives of ordinary women (and men) beyond what attitudes in society suggest.

Paxton, P., M. Hughes, and M. Painter. 2010. ‘Growth in women’s political representation: A longitudinal exploration of democracy, electoral system and gender quotas’. European Journal of Political Research 49(1):25–52.