---
title: "Using `dogesr` to work with Venetian doges tenures"
author: "JJ Merelo"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using `dogesr` to work with Venetian doges tenures}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
bibliography: ../inst/doges.bib
---

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

## Introduction

Since Venetian doges were elected for life, it was apparently a conscious design by the electoral college (composed, after a series of lot drawings and designations of 41 nobles from the Maggior Consiglio) to choose them in such a way that their terms would not last too long [@smith2021long]. Using data from `dogesr` [@dogesr], we will, in this vignette, see to what extent that happened and if there was some evolution during the time the Republic of Venice existed

## Set up

```{r load}
library(dogesr)
data("doges")
```

This will import the data from the `dogesr` package into the `data.doges` data frame. We are interested in the `Doges` and `Years` variables. Let's create a data frame out of that:

```{r years}
doges.all.years <- data.frame( doge = data.doges$Doge, years = data.doges$Years)
```

Several doges only were able to stay alive during the same year

```{r same.year}
knitr::kable(doges.all.years[doges.all.years$years == 0,]$doge, col.names=NULL)
```

And quite a few during one year or less

```{r one.year.or.less}
doges.one.year <- unique(doges.all.years[doges.all.years$years <= 1,])
knitr::kable(doges.one.year[order(doges.one.year$years),],col.names=NULL)
```

So it effectively looks like most doges effectively had a short shelf life. As a matter of fact, the doge that staid in power the longest was

```{r longest.doge}
knitr::kable(doges.all.years[which.max(doges.all.years$years),])
```

Francesco Foscari was elected when he was 50, defeating Pietro Loredan. After being in power for 34 years, he was the only doge forced to abdicate after the *serrata* or new rules electing the doges.

We can see whether this has any kind of influence using now several functions provided by the `dogesr` package next.

## Analyzing the evolution of the effective terms of the doges using `doges.years`

This can be analyzed a but further with a function provided by `dogesr`, `doges.years`. This filters part of the original data, leaving only data needed to work with the doges' terms (or years)

```{r using.doges.years}
data("doges.years")
knitr::kable(head(doges.years[order(-doges.years$Years),],10))
```

First thing we can note is that, among the top 10, there is only one, Leonardo Loredan, who was elected after the aforementioned Francesco Foscari. This was, according to everyone, an unexpected event [@loredan], and he was chosen by the minimum needed to get elected. So he was really an outlier. The distribution in bins of 5 years, below, shows how infrequent was a term of more than 10 years.

> In fact, doges' years of ruling were as short as the life of the common dog.

```{r distribution, fig.cap="This histogram reproduces, with a fixed bin size, Figure 1 in [@smith2021long]."}
library(ggplot2)
library(ggthemes)
ggplot(doges.years, aes(x=Years))+geom_histogram(binwidth=5)+theme_tufte()
```

The distribution is skewed because it includes the first years of the institution. This is the distribution by centuries

```{r boxplot}
doges.years$Century <- as.factor(doges.years$Century)
ggplot(doges.years, aes(x=Century,y=Years))+geom_boxplot()+theme_tufte()
```

There seems to be a clear difference before and after the thirteenth century; there is also a return to longer tenures by the end of the Republic, in the 18th century. Was there a real difference?

```{r serrata}
doges.years$pre.serrata <- TRUE
doges.years[doges.years$Start >= 1297,]$pre.serrata <- FALSE
means <- aggregate(Years ~  pre.serrata, doges.years, mean)
medians <- aggregate(Years ~  pre.serrata, doges.years, median)
```

This difference is significant, as indicated by the Wilcoxon test

```{r wilcox}
wilcox.test(doges.years[doges.years$pre.serrata == T,]$Years,   doges.years[doges.years$pre.serrata == F,]$Years )
```

## Conclusions

As indicated by [@smith2021long], there seems to be evidence that supports the existence of "informal"  limits on the terms of doges of the the Republic of Venice. In their paper they indicate 1172 as a departure point for the term "limits", when the Maggior Consiglio was formed. According to the figure above, there does not seem to be a significant difference between the XII and XIII century in terms of tenure. However, 1297 does seem to be a watershed event, with all terms after that being significantly different to terms prior to it. We can conclude, then, that the Serrata, closing of the Maggior Consiglio to all but a few patrician families, was in fact the event that marked the shift to electing older doges.

This vignette also shows how, using `dogesr`, it becomes significantly easier for everyone with the skill to work with the R language to reproduce or work upon results related to life terms and other Venetian doges data.

## References
