---
title: "A potentially useful R Markdown document"
author: "Andy Eggers"
date: "HT 2018"
header-includes:
    - \usepackage{multirow,xcolor}
output: 
  pdf_document
---

# 1. Introduction 

This is a hastily-assembled document with content from the first few weeks of the course. The idea is to help you learn to use R Markdown e.g.\ for problem set submissions. 

Don't expect the content to be in any reasonable order! The point is just to show some formatting and code.

# 2. Some text and some math

By definition, war is inefficient if there is at least one feasible peaceful allocation that is as good as war for both players and better than war for one player. Consider the peaceful allocation where $x = w_1$. $w_1$ is defined as player 1's war payoff, so this is as good as war for player 1. The peaceful allocation is better than war for the second player, because if $w_1 + w_2 < 1$ (as assumed), then $1 - w_1 > w_2$, i.e. 2's peaceful allocation is better than her war payoff. Thus war is inefficient: it is Pareto inferior to at least one peaceful outcome.

Assume $\eta_{ig}$ is distributed uniformly on $\bigg[ \frac{-1}{2 \omega_g}, \frac{1}{2 \omega_g}\bigg]$.

Rearranging, voter $i$ in group $g$ votes for party $A$ if $$\eta_{ig} < v(y_g + t_{gA}) - v(y_g + t_{gB}).$$

Let $\eta_{ig}^* \equiv v(y_g + t_{gA}) - v(y_g + t_{gB})$. 

# A $2\times2$ table 

And then you can put in a 2 $\times$ 2 game like this: 

 \renewcommand{\arraystretch}{2}
 \begin{tabular}{c|c|c|c|}
\multicolumn{2}{c}{}&\multicolumn{2}{c}{Player 2}\tabularnewline
\cline{3-4}\multicolumn{2}{l}{}&\multicolumn{1}{c}{ \textcolor{white}{BB} A \textcolor{white}{BB}} & \multicolumn{1}{c}{\textcolor{white}{BB} B \textcolor{white}{BB}}\tabularnewline
\cline{3-4}\multirow{2}{*}{Player 1}&\textcolor{white}{BB} A \textcolor{white}{BB}&1, 0 & 0, 1 \tabularnewline
\cline{3-4}&B& 0, 1 & 1, 0\tabularnewline
\cline{3-4}
\end{tabular}


# Bullet points, bold text, italic text

- **Case 1**: status quo *better* for 2 than fighting 
- **Case 2**: status quo *worse* for 2 than fighting 


# Embedding \texttt{R} code 

This is a little over-elaborate, but I can write a function to make a plot. 

```{r echo=FALSE, eval = TRUE}
bowles.plot = function(d, main = NULL, xlim = c(0,5), ylim = c(0,5), text.pos = NULL, arrow = T){
  plot(d, pch = 19, xlab = "", ylab = "", main = main, xlim = xlim, ylim = ylim, axes = F)
  if(is.null(text.pos)){pos = 4}else{pos = text.pos}
  the.col = rgb(.9, .05, .05, alpha = .5)
  the.col.2 = rgb(.05, .05, .9, alpha = .5)
  text(d, labels = c("(C,C)", "(D,C)", "(C,D)", "(D,D)"), pos = pos, col = rgb(.1, .1, .1, alpha = .8))
  if(arrow){
    for(j in c(1,3)){
      arrows(x0 = d[j, 1], y0 = d[j, 2], x1 = d[j+1, 1], y1 = d[j+1, 2], col = the.col, code = 1 + as.integer(d[j + 1,1] > d[j,1]), length = .15, lwd = 2) # connecting C,C (row 1) and D,C (row 2), or C,D (row 3) and D,D (row 4)
    }
    for(j in c(1,2)){
      arrows(x0 = d[j, 1], y0 = d[j, 2], x1 = d[j+2, 1], y1 = d[j+2, 2], col = the.col.2, code = 1 + as.integer(d[j+2,2] > d[j,2]), lty = 1, lwd = 2, length = .15) # connecting C,C (row 1) and C,D (row 3), or D,C (row 2) and D,D (row 4)
    }
  }else{
    lines(d[1:2, 1], d[1:2, 2], col = the.col) # connecting C,C and D,C
    lines(d[3:4, 1], d[3:4, 2], col = the.col) # connecting C,D and D,D
    lines(d[c(1,3), 1], d[c(1,3), 2], col = the.col, lty = 2, lwd = 2) # connecting C,C and C,D
    lines(d[c(2,4), 1], d[c(2,4), 2], col = the.col, lty = 2, lwd = 2) # connecting D,C and D,D
   }
  mtext(text = substitute(u[1]), side = 1, las = 1, line = 1)
  mtext(text = substitute(u[2]), side = 2, las = 1, line = 1)
  axis(1, at = c(-100, 100)) # at = c(0,1), pos = 0)
  axis(2, at = c(-100, 100)) # at = c(0,1), pos = 0)
}
```

And then I can use that code in two figures I put in the document.

\begin{minipage}[c]{0.5\textwidth}
```{r echo=FALSE, eval = TRUE, out.width = "80%", fig.width = 5, fig.height = 5, fig.align = "center"}
d = rbind(c(4,4), c(3,1), c(1,3), c(2,2))
bowles.plot(d, "Assurance game")
```
\end{minipage}
\begin{minipage}[c]{0.5\textwidth}
```{r echo=FALSE, eval = TRUE, out.width = "80%", fig.width = 5, fig.height = 5, fig.align = "center"}
d = rbind(c(4,3), c(2,2), c(2,2), c(3,4))
bowles.plot(d, "Coordination game", text.pos = c(4, 4, 1, 4))
```
\end{minipage}

# Including an image

The other option for including graphics is to generate an image file (e.g.\ pdf, jpeg, png) with some other program, and then include it in your RMD file. 

So, step 1: 

- I write \texttt{R} code that outputs a PDF
- I find a JPEG image on the web
- I take a screen shot that produces a png

Step 2, I include the image with some code in my RMD file. 

<!-- this is a comment. it doesn't get processed. the purpose of this comment is to point out that \\ is a "newline", and using it below prevents the picture and the caption from appearing on the same line --> 

\begin{center}
\includegraphics[width=0.5\textwidth]{multidimensional_comp_in_a_grid_5.pdf}  \\   
\footnotesize{An image from lecture, made in \texttt{R}.}
\end{center} 

\begin{center}
\includegraphics[width=0.75\textwidth]{spinal_tap.jpg}  \\
\footnotesize{An image from the web.}
\end{center}

\begin{center}
\includegraphics[width=0.75\textwidth]{screen_shot.png}  \\
\footnotesize{A screenshot from my computer, from before I realized that I needed to include newlines between the pictures and the captions.}
\end{center}

