## Calculating Agreement, Consensus, Polarization in R

I have just uploaded a new version of the R package agrmt to R-Forge. The package implements various measures to enumerate the degree of agreement, consensus, or polarization among respondents. Apart from van der Eijk’s Agreement “A”, there are a range of other measures proposed in the literature.

## Same Explanatory Variables, Multiple Dependent Variables in R

I needed to run variations of the same regression model: the same explanatory variables with multiple dependent variables. In R, we can do this with a simple `for()` loop and `assign()`.

First I specify the dependent variables:

`dv <- c("dv1", "dv2", "dv3")`

Then I create a for() loop to cycle through the different dependent variables:

`for(i in 1:length(dv)){`

Within this loop, I need to create an object to hold the models. I need a separate object for each model, so I create one with `paste()`. For the first dependent variable, this will be `model1`; for the second dependent variable `model2`, and so on.

`model <- paste("model",i, sep="")`

With this object to hold the model in place, I can run the model: the ith dependent variable is used. It is stored in an object called `m`.

```m <- lm(as.formula(paste(dv[i],"~ ev1 + ev2")), data=mydata) ```

Now, I assign the model `m` to the `model` object created above: model1 for the first dependent variable, etc. That’s also the end of the `for()` loop.

`assign(model,m)}`

We can now look at the results:

`summary(model1); summary(model2); summary(model3)`

or, more practical to compare models:

```library(memisc) mtable(model1, model2, model3)```

## Barplots across variables in R Here’s a good example of how useful `sapply `can be. I have some data from Qualtrics, and each response is coded in its own variable. Let’s say there is a question on what kind of organization respondents work in, with 10 response categories. Qualtrics produces 10 variables, each with 1 if the box was ticked, and empty otherwise (structure shown just below). With the default CSV import, these blank cells are turned into NA. Here’s a simple way to produce a barplot in this case (in R, of course). ```types = sapply(1:10, function(i) sum(get(paste("Q1_",i,sep="")), na.rm=TRUE)) barplot(types)```

Let’s take this step by step. To count frequencies, we simply use `sum()`, with the argument `na.rm=TRUE` because the variables only contain 1 and NA. `get()` is used to find the variable specified by a string; the string is created with `paste()`. In this case, the variable names are Q1_1, Q1_2, Q1_3, … Q1_9, Q1_10. By using `paste()`, we combine the “Q1_” part with the counter variable i, with no separation (`sep=""`).

The whole thing is then wrapped up in `sapply()`, with the counter variable i defined to take values from 1 to 10; the `function(i)` part is there so that the counter variable is applied to the sum. So `sapply()` takes each value of the counter variable, and applies it to the function we specified, which calculates the sum for one variable Q1_i at a time.

Now I can simply do a boxplot, and add the `names.arg` argument to specify the labels.

(Here I specified the colours: `barplot(types, col=rainbow(10))` to have a catchy image at the top of this post, albeit one where colours have no meaning: so-called chart-junk).