--- title: "R course - Exercice 2" author: "First Name - Last Name" date: "Master 2 Statistics and Econometrics" output: pdf_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## Problem The objective is to compare two mechanisms of election in a fictitious electoral problem. The context is as follows: we consider a country divided into 3 states, where each state contains the same number of voters $n$, where $n$ is assumed to be odd. There are two candidates for the position of president: a candidate $Py$ and a candidate $R$. We consider the following two mechanisms: * Direct Mechanism (M1): The candidate with the most votes in the country wins the election. * Indirect Mechanism (M2): Each state elects a representative $Py$ or $R$ (the one with the highest number of votes in the state). The party with the most representatives chooses its president. ## Which criteria can be used for comparing the quality of an election? We focus here on the probability that an elector is pivot. An elector is said to be pivotal if, by changing his vote, he also changes the result of the election. **Remark:** a pivot is a voter who necessarily voted for the winner. **Example :** We consider the following situation with 3 voters per state: * in the 1st state, 2 people vote for Py and 1 votes for R * in the 2nd state, 2 people vote for Py and 1 votes for R * in the 3rd state, 1 people votes for Py and 2 vote for R **By considering M1**, there were 5 votes for Py and 4 votes for R; in other words the election is won by Py. Are there any voters who are pivotal? Yes, the five voters who voted for Py are all pivotal, since if one had voted for the other side, it would have changed the outcome of the election. **Consequence for the algorithm:** according to the previous example, we deduce that in the case of **M1**, we are in the presence of pivotal voters if and only if the winner of the election won with a difference of +1. In this case, the number of pivots equals $\frac{3n + 1}{2}$. **By considering M1**, 1st state was won by Py, 2nd state was won by Py, 3rd state was won by R. In other words, Py won the election with 2 representatives against 1 alone for the other side. Are there any voters who are pivotal? Yes, there are 4. In 1st state, if the voters who voted for Py would have voted for R, the state would have been won by R and R would then have won the election. In state 2, there are also 2 pivotal voters. On the other hand, in state 3, there are no pivotal voters because none of these voters could have changed the outcome of the poll by changing their vote. **Consequence for the algorithm:** according to the previous example, we deduce that in the case of **M2**, it is necessary that the winner has gained exactly two states out of three possible. Moreover, it is necessary that in the states won by the winner, there was a difference of +1 vote. The number of pivots is thus equal to $\frac{n + 1}{2}$ multiplied by the number of states won by the winner with a gap vote. ## How to simulate the choice of an elector? * **IC** case : a voter of the country chooses his candidate according to a Bernoulli of parameter $p = 1/2$. * **IAC**$^*$ case: in each state $k$ ($k = 1, ..., 3$), an elector chooses his candidate according to a Bernoulli of parameter $p_k$, where each $p_k$ is simulated according to a Uniform law $[0, 1]$. **Remark:** here, we are more interested in knowing the number of voters who voted *Py* or *R* within a state. Thus, instead of simulating in each state $n$ Bernoulli, we can directly simulate the number of people who voted for $Py$ using a Binomial of parameter $(n, p)$ (function *rbinom()*) where $p$ depends on the model above. ## Objective Create in **R** the function *simul_elec()*, having for input arguments: * an integer **n** corresponding to the number of electors in a state, * a string **case** which gives the simulation model of the choice of a voter, * an integer **B** which gives the number of replications. Inside the function, we will replicate **B** times the following simulation process: * simulate according to **case**, the choice of voters in each state. The goal is to obtain a vector of size 3 (a value per state), containing the number of people who voted for one of the two candidates, choose $Py$ for example, within each state. This can be done with the function $rbinom()$. * determine the winner of the election according to the mechanism **M1** or **M2**. Here, one always chooses $Py$ as a reference, in other words one looks at whether or not $Py$ won the election. * keep in mind the following information for each mechanism: how many people are pivots in **M1** and **M2** The function will return for each mechanism, the average percentage on the $B$ simulations of people satisfied by the election. In addition, it will check if possible, that the input arguments are adequate to the problem (**n** must be an odd and **case** a character equals to "IC" or "IAC_star") ```{r, echo = F} ### Include your code here ``` We will take the following example to verify that the function works well: ```{r, eval = F} res_IC <- simul_elec(n = 5, cas = "IC", B = 100000) res_IAC_star <- simul_elec(n = 5, cas = "IAC_star", B = 100000) ``` ## Notation You will have to return the exercice in *.pdf* or *.html* format, which would have been done with **R** Markdown if possible. It should contain the lines of code used to answer the questions, but you should also explain what you are doing. **Remark:** try to use the least amount of command lines per question asked. The idea of this exercice is also to make you look for and find the most "elegant" and simple solutions to answer a given question. In particular, it will be possible to use *replicate()*, *switch()*, *rowMeans()*, *stopifnot()* functions.