Moving Averages in R

To the best of my knowledge, R does not have a built-in function to calculate moving averages. Using the filter function, however, we can write a short function for moving averages:

mav <- function(x,n=5){stats::filter(x,rep(1/n,n), sides=2)}

We can then use the function on any data: mav(data), or mav(data,11) if we want to specify a different number of data points than the default 5; plotting works as expected: plot(mav(data)).

In addition to the number of data points over which to average, we can also change the sides argument of the filter functions: sides=2 uses both sides, sides=1 uses past values only.

No Syntax Highlighting for R in Notepad++

Notepad++ is a versatile text editor for Windows. Unfortunately in the current version, some themes do not offer syntax highlighting for R scripts. This can be fixed relatively easily by adding the relevant entry from a theme that does include it. In Windows > Program Files > Notepad++, open the theme that does not work (e.g. vim Dark Blue.xml), and one that does work (e.g. the stylers.model.xml). Simply copy over the relevant bits, and change the colours to match those of your theme.

You might have to start Notepad++ as administrator (right-click on the program entry in the start menu). Let’s hope the next release has this bug fixed…

Individual Representation

My article on individual representation has now been published. In the article I develop a new measure of political representation. It is based on a new conceptualization, complementing existing approaches. The focus is on the individual (in society), and an individual representation score is calculated to express how well the individual is represented by the legislature. These scores involve a double comparison, first with the positions of other individuals in society, and then with the positions of the members of parliament. R-code to calculate these individual representation scores is available from R-Forge.

Representation of Women in National Legislatures

After a considerable time as an on-line paper, my article on the political representation of women in national legislatures finally appeared in print. I use a large cross-national sample of all free and partly-free countries (according to Freedom House). Like some recent contributions, I find that attitudes toward women as political leaders are a powerful predictor for the share of women in the national legislature. This link was already established by Pippa Norris and Ronald Inglehart, amongst others, but in this article I also consider the role of gender quotas. Once controlling for regional or cultural/attitudinal differences, voluntary party quotas and legislative quotas do not appear to be significant. Obviously there are often implementation issues, but we need to think more carefully about the underlying mechanisms: I argue that cultural variables are probably behind both the share of women in legislatures and the (successful) implementation of quotas.

Norris, P., and R. Inglehart. 2001. “Cultural obstacles to equal representation.” Journal of Democracy 12(3): 126–40.

Agresti & Finlay Statistics Exercises

A few years ago Ken Okamoto-Kaminski and I did the exercises in Agresti & Finlay “Statistical Methods for the Social Sciences“. We did the exercises for which there is a key, and the document shows how we solved all these exercises. These are my notes, and everything is handwritten. The edition we used had a misprint in the answer for exercise 3.5.f (I’ve checked this with Agresti, so the misprint should be fixed in newer editions).

Agresti & Finlay Statistics Exercises