---
title: "Electoral Volatility"
author: "Miguel Rodriguez"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Volatilidad Electoral}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Introduction.

Another of the electoral dimensions that acquires special relevance in the electoral studies is the one known as **volatility**. Basically, what it tries to measure is how the electoral behavior of the voters changes from one electoral process to another and, in short, the level of fidelity of the voters in relation to their democratically elected representatives. Once the concept is defined, the difficulty comes in the way of measuring it, given the wide field that we want to cover with this dimension.

In 1979 it was Pederson who proposed the formula that allows its calculation and whose mathematical expression is the following:

$$VT=\frac{1}{2}\sum_{i=1}^{n}\left|\triangle p_{i}\right| $$
Where $\triangle p_{i}$ denotes the increase or decrease of electoral support between two elections expressed as a percentage of the vote. 

Although there are other different approaches that seek to measure that magnitude as well. In this sense, the individual measurement of the change in the vote of each voter can also be done [^1](In this case, a totally disaggregated volatility would be calculated where the changes in the tendency of the electorate would be clearly manifested. The difficulty in this case would come from the impossibility of obtaining this information at this level of detail, since to achieve this objective it would be necessary to carry out a complete census, requiring a disproportionate effort to achieve the set objective). To get closer to this ideal situation, it would be possible to perform surveys that would give us an approximation of the problem. One of the statistical studies that could provide us with this information can be found in the opinion barometers that the Spanish Sociological Research Centre (CIS) periodically publishes ([Spanish Sociological Research Centre](http://www.cis.es/cis/opencms/ES/index.html){target="_blank"} ), which usually include a question about the informant's voting intention and which political party he/she voted for in previous elections. With sufficient caution, and even if it is only to obtain an estimated value of the reality to be measured, it would be possible to cross these two variables and in this way obtain an approximation of the volatility of the electorate at individual level.

As explained by Oñate and Ocaña (1999), **aggregate volatility (VT)** has subsequently been broken down into two other components, which clarify and complement the possible conclusions that can be drawn from aggregate volatility. On the one hand, there is the **volatility between blocks (VB)**, which tries to measure how the vote has changed between two ideological blocks (such as right-left, or even center-periphery). The measurement of this magnitude is performed by means of the following formula:

$$VB=\frac{\left|\triangle p_{i}+...+\triangle p_{k}\right|}{2}+\frac{\left|\triangle p_{x}+...\triangle p_{z}\right|}{2} $$
Indicating with i...k the parties classified within a block and with x...z those corresponding to the other block.

The other component of the total or aggregate volatility is called **Intrablock Volatility (VIB)**, which attempts to measure the changes in electoral or parliamentary support within each ideological block considered. The way to calculate its value is through the expression:
$$ VIB = VT -VB $$

# Volatility calculation.

The computation of these indicators is not easy to obtain and, since it involves the comparison of two electoral processes, it is necessary to have data that allows the comparison between the two processes considered. In order to establish this relationship between the two processes, the following aspects must be taken into account:

* The parties or groupings of parties must be linked between the two time periods.
* There may be parties that are only present in one period, either because they disappear or are newly created.
* A party or coalition may disaggregate into other groups between the two time periods.

In the *Relectoral* package the function called *volatilidad()* has been implemented, which has a parameter called *enlace* and it is the one that refers to a data.frame that contains the data that will allow to make comparisons between the two different electoral processes, whose structure is going to be described later.

The parameters to be provided to the *volatilidad()* function are the following:

1.- A data.frame made up of two columns. The first contains the names of the parties and the second the votes/seats. This information refers to the first comparison period. If votes are provided, the electoral volatility will be obtained, and if seats are provided, the parliamentary volatility will be obtained.

2.- A second data.frame made up of two columns. The first contains the names of the parties and the second the votes/seats. The data will correspond to the results obtained in the second election under comparison.

3.- A third data.frame that will serve as a link between the two previous ones. Its generic name is *enlace*, and since its structure is more complex, we will now proceed to give a more detailed description of it.

To try to explain better how the structure of the data.frame referenced in the *enlace* parameter should be, the following figure has been created, which aims to clarify completely how it should look like.

![Fichero de enlace](./img/enlace.PNG){ width=100% }

As it can be see, the format of this data.frame can be divided into four sections. The first section consists of the names of the parties or electoral groups. The second section is made up of 10 columns with "p1_i" names for i=1,2,...10. The third section is also made up of other columns with the names 'p2_i' for i=1,2,...,10, and the last section is made up of a single column with the name 'bloque' and must contain the values 'D' (first letter of the word 'Right' in Spanish) or 'I' (first letter of the word 'Left' in Spanish) to indicate that the party in the first column belongs to the ideological right-wing group ('D') or left-wing group ('I').

The two central sections are those that identify and relate the votes/seats of both electoral processes, for the same political party or group of parties in the following way. In the left zone, the number(s) of the records of the first data.frame that are going to be taken into account to link with the numbers of the records that appear in the right zone for the same row. When the 10 columns enabled for this purpose are not required, the remaining columns are filled with zeros. 

For example, in the figure above the first row, corresponding to the "PSOE" party, indicates that the first row of the first data.frame must be linked to the first row of the second data.frame. In the case of the "Cs" party, it is indicated that the third row of the first data.frame must be linked to the fifth row of the second data.frame. The case of "PODEMOS_EU" indicates a special treatment, as it is indicated that the votes/seats in rows 12 and 25 of the first data.frame are added together, and then compared with the data in row 14 of the second data.frame.

As at the end of these links not all the parties will be linked, a kind of "Others" group will be created with two records added and called, for example, "resto_I" and "resto_D" (these would be the names to be placed in the first column), in such a way that the first one includes parties of ideology from block ´"I" and the second party of ideologies from block "D". Then, in each information section, the numbers of the records of these parties not linked in the way indicated above would be pointed out and in this way these unlinked of parties would be grouped, which on the other hand will have little or no relevance for the calculation of the volatility sought, since the votes obtained would be few, and therefore the number of seats would be zero.

# Example of use.

This example will calculate the estimated volatility between the April and May elections held in Spain to choose the 350 deputies that make up the Spanish Congress.

To obtain these results, the first thing we must have is the total number of votes or seats of 
each of the parties that took part in that election. To obtain this data, it has been used the
resources provided free of charge by the Spanish Ministry of Home Office (MIR). This data can be 
downloaded via this link [http://www.infoelectoral.mir.es/infoelectoral/min/](http://www.infoelectoral.mir.es/infoelectoral/min/){target="_blank"}. In that page you must choose each of the two elections indicated above.

This way, two Excell files will be downloaded (in order to be able to read them in R, they have been converted to files with extension .xlsx), which can be imported into R with the following commands:

```{r}
if (!require("readxl")) install.packages("readxl");require("readxl")
dat1<-read_xlsx("../inst/data_raw/volatilidad/abril_19.xlsx",col_names = TRUE)
dat2<-read_xlsx("../inst/data_raw/volatilidad/noviembre_19.xlsx",col_names = TRUE)
```

And if we look at the first data in one of these files, we can see the following:

```{r}
head(dat1)
```

Now with each of these files, you must obtain the total votes of each party (if you want the electoral version) or the total number of seats obtained (if you prefer the parliamentary version). In the case of the **electoral version**, the following data will be obtained:

Ahora con cada uno de estos ficheros, se deben obtener el total de votos de cada partido (si se quiere la versión electoral), o el total de escaños conseguidos ( si se quiere la versión parlamentaria ). Para el caso de la **versión electoral**, se obtendrán los siguientes datos:

```{r}
dat1_electoral<-dat1[,c(1,3)]
dat2_electoral<-dat2[,c(1,3)]
```

In case you want to get the parliamentary version of the index, the following data should be taken into account:

```{r}
dat1_parlamentaria<-dat1[,c(1,7)]
dat2_palamentaria<-dat2[,c(1,7)]
```

And now it remains to generate the file called *enlace*, with the structure that has already been commented on in the previous section. The truth is that this is the most delicate part when calculating this indicator. In this specific case, the file that has been taken into account for this example is shown below, and for purely didactic reasons, it is shown entirely.

```{r}
enlace <- read.csv("../inst/data_raw/volatilidad/abril_noviembre_2019.csv",
                   header = TRUE,sep=";", stringsAsFactors = FALSE)
enlace
```

To help in the generation of this .csv file, what it has been done first is to build this structure in an Excel spreadsheet and then export it to this csv file.

Now all the ingredients are in place to calculate the volatilities. We start with the electoral volatility and for this purpose the following code has been executed.

```{r}
# Loading Relectoral package
if (!require("Relectoral")) install.packages("Relectoral"); require("Relectoral")
s<-volatilidad(dat1_electoral,dat2_electoral,enlace)
```

Then, the total volatility will be:

```{r}
s$Total
```

While the volatility between blocks will be the following:

```{r}
s$Entre
```

Finally, the intrablock volatility, as indicated above, will be obtained from the difference of these two figures.

For the calculation of parliamentary volatility, the process is similar and would be as follows:

```{r}
s1<-volatilidad(dat1_parlamentaria,dat2_palamentaria,enlace)
s1$Total
s1$Entre
```



