---
title: "Measuring Democracy and Repression"
output:
  pdf_document: default
  html_notebook: default
  html_document:
    df_print: paged
bibliography: tutorial.bib
always_allow_html: yes

---

## Introduction

I want to talk to you about some earlier work of mine [@armstrong_measuring_2009] that highlight measurement models and measurement strategies.  This work follows from Christian's work on state repression and democracy [@Davenport2007BOOK].  The basic idea is that repression is, in part, a function of democracy.  Both democracy and repression are complex constructs that you might not be able to measure with a single variable.  Or, to put it another way, any single variable might have lots of measurement error and that by using multiple variables we could mitigate the effects of measurement error. 

In general, my work tried to figure out the functional relationship between democracy and repression, noting that... 
1. Democracy can broadly be represented by two dimension - voice and veto, and 
2. Democracy has been shown to have varied different kinds of effects: linear [@PoeTate94], quadratic [@HF95], threshold [@DavenportArmstrong2004].  

## Conceptualization.

The main conceptual work here was to figure out how to measure democracy.  There are two main dimensions of democracy: 

1. Voice - behavioral dimension of democracy (related to contestation and participation). 
2. Veto - institutional measure of democracy (related to constraints on the executive). 

The idea is to find a set of indicators that capture the various relevant pieces of the dimension.  In addition to capturing the general idea, we might want to also capture both _de jure_ and _de facto_ measures.  The variables I chose were: 

|      | Voice  |  Veto |
|------|---|---|
|_de facto_ | Electoral Competition (lgates) | Checks (log_checks) |
|          | Competetive Participation (parcomp)  | Political Constraints (polconiii)  |
|          | ACLP (aclp) | Law & Order  (laworder)|
|          | Competitive Exec Recruitment (xrcomp) |   |

|      | Voice  |  Veto |
|------|---|---|
|_de jure_ | Bernhard et al (bnr) | Executive Constraints (xconst) |
|          | Suffrage (sfrg) | Sub-federal Veto (subfed) |

## Measurement

There are a few ways that we could use the variables identified above.  

1. Take the best variable and use that.  
2. Include all variables in the model. 
3. Aggregate the data. 

Idea 1 is problematic because we don't know _a priori_ which variables is the "right" one.  Without knowing that, we are either left to make an _ad hoc_ decision or we have to run several different models and chose the highest $R^2$ model or something similar which poses problems for inference. 

Idea 2 poses a problem because it is compounding, rather than mitigating the measurement error problem.  That is to say, if all variables are measured with error, then including a lot of error-filled measures is not likely to eliminate the problem.  Also, it makes it more difficult to evaluate the hypotheses if the patterns of significance of the included variables are not simple (e.g., all or none of the included variables related to a concept are significant).  

Idea 3 has the "best" idea of the three.  This helps mitigate measurement error problems and makes the evaluation of the relevant hypotheses simpler.  

### Theoretical Model 

Just like in regression models, measurement models have statistical theoretic underpinnings and make a set of assumptions about how the variables are related to each other.  First, though, let's think about the nature of an individual variable.  

$$\text{Indicator} = f(\text{Truth} + \text{Measurement Error})$$

Each observed indicator is a function of the truth (e.g., true democracy) plus some measurement error.  The simplest model we could use is to simply treat the indicator as a linear function of the Truth: 

$$\text{Indicator} = \text{Truth} + \text{Measurement Error}$$

To be more precise, if we had $i={1, \ldots, N}$ observations and $j={1, \ldots, K}$ variables, we would write the equation as: 

$$\text{Indicator}_{ij} = \text{Truth}_i + \text{Measurement Error}_{ij}$$

Note that the indicators are inndexed by both observations (individuals, countries, etc...) and variables.  The Truth, however, is indexed only by observation because the truth doesn't vary by indicator, it is exactly the same.  Just like the indicators, measurement errors will be different for each individual and indicator variable.  We have to make a couple of important assumptions about the measurement errors for the model to work: 

1. Measurement errors are normally distributed - essentially this is an assumption that measurement errors will cancel eachother out as the number of indicator variables grows.  
2. Measurement errors are unrelated to the Truth.  
3. Measurement errors are unrelated to each other (assumption of conditional independence).  

If we can assume all of these things, then the model is called the _Summated Rating Model_.  Some important highlights of the model are

1. The model is estimated simply by adding (or alternatively averaging) the indicator variables for each observation. 2. It tends to increase level of measurement.  So long as all of the individual indicators are ordinal level, the estimate of the Truth is more like an interval-level variable.  
3. The moel assumes that each variable is an equally good indicator of the latent variable.  

If you've heard the saying - "Measure twice, cut once" - this is an appeal to the idea underlying the summated rating model.  In a way, this is also the idea behind Phil Tetlock's work on predictions - that the wisdom of crowds can ultimately be very accurate [@tetlock_superforecasting_2015]. 

If we were going to do the Summated Rating model on our democracy variables, we could do this as follows: 

```{r}
f <- url("https://quantoid.net/files/india/tutorial.rda")
load(f)
```

The code above will put a data frame in your workspae called `dat`.  We could see what variables are in the data with: 

```{r}
str(dat)
```

We could then make the voice and veto data: 

```{r, message=FALSE, warning=FALSE}
library(dplyr)
# library(plyr)
voice.dat <- dat %>% dplyr::select("lgates", "parcomp", "xrcomp", "aclp", "bnr", "sfrg")
veto.dat <- dat %>% dplyr::select("log_checks", "polconiii", "laworder", "xconst", "subfed")
```

Next, we can use the `alpha` function from the `psych` package to estimate the reliability for each variable. 

```{r, message=FALSE}
library(psych)
a.voice <- alpha(voice.dat, check.keys=TRUE)
a.voice
```

In the first returned data frame (that is $1\times 9$), look at the `std.alpha` measure which is the reliability of the scale (essentially an estimate of the $R^{2}$ between the scale and the truth) when all variables are standardized.  Next, we want to look at the second returned data frame (that is $6\times 8$).  This is the set item deletion statistics.  It give you the $\alpha$ values if each variable is deleted in turn in the `std.alpha` column.  What you want is to look for variables that could increase the $\alpha$ values if they are deleted.  In this case, suffrage could be removed and we could get a more reliable scale.  

As an aside, this is reasonable.  The Electoral Competition measure already takes into account actual voter turnout.  So, what suffrage is giving us in addition is essentially the proportion of people who could have voted, but didn't.  This doesn't prove to be a good indicator of democracy.  We could make the change and re-estimate the model: 

```{r, message=FALSE}
voice.dat <- voice.dat %>% dplyr::select(-sfrg)
a.voice <- alpha(voice.dat, check.keys=TRUE)
a.voice
```

Doing this aga, reveals that the `bnr` variable is also not as reliable.  So, we could delete it and do it again: 

```{r, message=FALSE}
voice.dat <- voice.dat %>% dplyr::select(-bnr)
a.voice <- alpha(voice.dat, check.keys=TRUE)
a.voice
```

We see that this produces a result where we couldn't remove any single variable without reducing the overall $\alpha$ value.  This has substantive implications, too.  You'll see that the two variables that were removed were the _de jure_ variables.  This suggests that the _de jure_ and _de facto_ democracy variables are not both indicators of the same underlying process.  

We could do the same thing with the veto data. 

```{r}
a.veto <- alpha(veto.dat, check.keys=TRUE)
a.veto
```

Here, we could take out the `subfed` variable: 

```{r}
veto.dat <- veto.dat %>% dplyr::select(-subfed)
a.veto <- alpha(veto.dat, check.keys=TRUE)
a.veto
```

Next, we could take out the `laworder` variable: 

```{r}
veto.dat <- veto.dat %>% dplyr::select(-laworder)
a.veto <- alpha(veto.dat, check.keys=TRUE)
a.veto
```

We could then put the scores back in our dataset and then look at the relationship between these variables and repression: 

```{r}
dat$voice_srm <- a.voice$scores
dat$veto_srm <- a.veto$scores
cor(dat[,c("voice_srm", "veto_srm", "rep1")], use="pair")
```


We could make a plot of these variables and repression, too: 

```{r, out.width="50%"}
library(ggplot2)
ggplot(dat, aes(x=voice_srm, y=rep1)) + geom_point(pch=1) + geom_smooth(method="lm") + theme_bw() + theme(aspect.ratio=1)
```

```{r, out.width="50%"}
ggplot(dat, aes(x=veto_srm, y=rep1)) + geom_point(pch=1) + geom_smooth(method="lm") + theme_bw() + theme(aspect.ratio=1)
```

You'll note in the veto plot, we get a few outliers with values bigger than 6.  This happens because some countries are only observed on a single variable.  We could remove any observation that has any missing data on the indicators as follows: 

```{r}
miss.voice <- is.na(rowSums(voice.dat))
dat$voice_srm <- ifelse(miss.voice, NA, dat$voice_srm)
miss.veto <- is.na(rowSums(veto.dat))
dat$veto_srm <- ifelse(miss.veto, NA, dat$veto_srm)
```

Now we could re-make the graphs

```{r, out.width="50%"}
ggplot(dat, aes(x=voice_srm, y=rep1)) + geom_point(pch=1) + geom_smooth(method="lm") + theme_bw() + theme(aspect.ratio=1)
```


```{r, out.width="50%"}
ggplot(dat, aes(x=veto_srm, y=rep1)) + geom_point(pch=1) + geom_smooth(method="lm") + theme_bw() + theme(aspect.ratio=1)
```


### Factor Analysis

There are two big potential problems with the summated rating model. 

1. The model assumes that each variable is equally reliable - that is, each one is an equally "good" indicator of the latent construct. 
2. This model, for reasons that we won't talk about much right now, is not a good _test_ of dimensionality.  That is, it could identify a single dimensional fit to the data even if the true underlying process is multidimensional.  For example: 

$$\text{Indicator}_{ij} = \text{True X}_{i} + \text{True Y}_{i} + \text{Measurement Error}_{ij}$$

In this case, the summated rating model cannot figure this out.  Factor analysis solves both of these problems.  The statistical model here is the following: 

$$\text{Indicator}_{ij} = \beta_{j}\text{Truth}_i + \text{Measurement Error}_{ij}$$

assuming that all indicators are centered at 0 (and often we standardize them to have $\sigma=1$.)  We can estimate the factor analysis model with the `fa` function in the `psych` package. 

The first thing we can do is include again all of the original variables in the data:

```{r}
voice.dat <- dat %>% dplyr::select("lgates", "parcomp", "xrcomp", "aclp", "bnr", "sfrg")
veto.dat <- dat %>% dplyr::select("log_checks", "polconiii", "laworder", "xconst", "subfed")
```

Next, we can look at the factor analysis solution: 

```{r}
library(psych)
fa.voice <- fa(voice.dat, nfactors=2, fm="pa")
loadings(fa.voice)
```

There is a lot to look at here.  First, look at the three rows at the bottom of the output.  Since each variable is standardized, each one has a variance equal to 1.  So, if there are six variables, then the tota variability to be explained is 6.  The `SS loadings` row gives you the amount of variace explained by each factor and the `Proportion Var` row gives you the proportion of the overall variance. Notice that the first number in the `Proportion Var` row is quite high (66% of the variance captured).  And, the second number is pretty low (3.5% of the variance).  This big discrepancy between the first and second numbers indicates that one dimension is sufficient to describe the structure underlying the indicators.  

Next, look at the numbers labelled `Loadings`.  These give you the $\beta$ coefficients relating the latent estimate of Truth to the observed indicators.  Since the data are standardized, these will be between -1 and 1.  Ideally farther from zero is better.  The first four, the ones that we used as input into the SRM are quite high.  The `bnr` variable here also has a reasonably high loading at around 0.7.  The suffrage variable, on the other hand, has a quite low loading.  This could lead to two possibilities - either leave it in the model because there is some "signal", however weak, relating to the suffrage variable.  Or, take it out of the model altogether.  As I'll show you below, this choice doesn't matter that much. 

```{r}
fa.voice <- fa(voice.dat, nfactors=1, fm="pa")
fa.voice2 <- fa(dplyr::select(voice.dat, -'sfrg'), nfactors=1, fm="pa")
cor(fa.voice$scores, fa.voice2$scores, use="pair")
```
In the output above, you can see that the two variables are nearly perfectly linearly related.  

We can undertake the same kind of analysis with the `veto` data. 

```{r}
fa.veto <- fa(veto.dat, nfactors=2, fm="pa")
loadings(fa.veto)
```

Here, we get largely the same type of thing, but both the `laworder` and `subfed` variables are better predicted by the second dimension rather than the first. Thus, we can eliminate those variables from the model and simply estimate a single dimension on the remaining variables. 

```{r}
fa.veto <- fa(dplyr::select(veto.dat, -"subfed", -"laworder"), 
      nfactors=1, fm="pa")
loadings(fa.veto)
```

So, in both cases, we explain about 80% of the variation in the indicators with the latent variable.

#### What do we do next. 

There are essentially two purposes for these kinds of models. 

1. Understand the underlying structure.  These models can be an end in themselves.  That is, these can be the final stage in the analysis.  In this case, we can answer questions about the sources of variation that underlie a set of observed data. 
2. Generating estimates of the latent variable.  These estimates can be used to stand in for the concept you're trying to measue.  These are available in the `scores` element of the object.  For example, `fa.voice$scores`.  

We could find these and put them back into the data. 

```{r}
dat$voice_fa <- fa.voice$scores[,1]
dat$veto_fa <- fa.veto$scores[,1]
dat.fa <- dat
```

<!-- Make an example with V-DEM data -->

## IRT Models

In the interest of completeness, I want to touch briefly on a model that is similar in spirit, but for different data.  The above model essentially assumes that data are measured at the interval-level.  That said, there are intersting relationships between IRT models and factor analytic models.  If we have binary data (or ordinal data), item response theory is another model that is appropriate.  We'll talk just about binary data, but the general idea can be extended to model ordinal data as well. 

The differnce here is that in what was above, we used the linear model framework to predict the observed indicators as a linear function of the latent construct.  In item response theory,  we treat the problem as one of classification.  We want to use the latent variable to classify the observed indicators.  In order to identify the model (that is, to get answers), we use a logistic regression to classify the observed indicators. 

$$\log\left(\frac{\text{Pr}(\text{Indicator}_{ij})}{1-\text{Pr}(\text{Indicator}_{ij})}\right) = \beta_{0j} + \beta_{1j}\text{Truth}_{i}$$

Or, equivalently

$$\text{Pr}(\text{Indicator}_{ij}) = \frac{1}{1+e^{-(\beta_{0j} + \beta_{1j}\text{Truth}_{i})}}$$

This is a non-linear model that takes the following general form. 

```{r}
s <- seq(-4, 4, length=100)
y <- plogis(s)
df <- data.frame(x=s, y=y)
ggplot(df, aes(x=x, y=y)) + geom_line() + theme_bw() + theme(aspect.ratio = 1) + 
  labs(x="Latent Construct", y="Pr(Indicator = 1)")
```

For each indicator, there are two parameters (this is called the two-parameter logistic or 2PL model).  

1. $\beta_{0j}$ is called the difficulty parameter.  It identifies how much of the latent trait you need to have a .5 probability of being a 1.  
2. $\beta_{1j}$ is called the discimination parameter.  It identifies how steep the curve is relating the latent construct to the observed indicators.  Steeper slopes mean that we've got a better classifier, at least on average. 

I turned all of the data from above into binary data so we could try out this idea. 

```{r}
f <- url("https://quantoid.net/files/india/tutorial_bin.rda")
load(f)
library(ltm)
voice.irt <- ltm(voice.bin ~ z1, IRT.param=FALSE)
veto.irt <- ltm(veto.bin ~ z1, IRT.param=FALSE) 
```

We can summarize these results to figure out how "difficult" each practice is and how well the latent dimension discriminates.  

```{r}
summary(voice.irt)
```

Here, we can see that all of the coefficients (the `z1.` values, which are the $\beta_{1j}$ values) are reasonably large and statistically different from zero.  It's probably easier to see the effects in what's called the Item Characteristic Curve (ICC).  This shows the functional relationship between the latent variable and the observed indicators.  


```{r, out.width="50%"}
plot(voice.irt, type="ICC", ylim=c(-.1, 1))
```

Here, you can see that suffrage is both not as good at discriminating between zeros and ones and is the "easiest" measure.  The intepretation is that if countries are going to take on value of 1 on any indicator, it's most likely to be suffrage.  The "hardest" one is the `bnr` variable.  The interpretation there is that if a country has a one on `bnr` they likely have ones on all of the other measures, too.   

We can look at the same things with respect to `veto`

```{r}
summary(veto.irt)
```

The findings are more or less similar here.  Both the `subfed` and `laworder` variables are not particularly good indicators, but their coefficients are still statistically different from zero.  We can also look at the ICC plot. 

```{r, out.width="50%"}
plot(veto.irt, type="ICC", ylim=c(-.1, 1))
```

These models can be used for the same two purposes as the factor model.  The first is to understand the structure of the underlying data.  The second is to produce a score that can stand in for the latent variable.  The procedure here is a bit more cumbersome.  We need to save the scores (which are organized by response profile) and then merge them on to the original data.  

```{r}
# make the predictions
voice.fs <- factor.scores.ltm(voice.irt)
# remove the variables Obs and Exp
voice.fs <- voice.fs$score.dat %>% dplyr::select(-"Obs", -"Exp")
# rename the latent variable and its standard deviation
names(voice.fs)[7:8] <- c("voice_irt", "voice_irt_s")
veto.fs <- factor.scores.ltm(veto.irt)
veto.fs <- veto.fs$score.dat %>% dplyr::select(-"Obs", -"Exp")
names(veto.fs)[6:7] <- c("veto_irt", "veto_irt_s")

# merge predictions with original binary data
veto.bin <- plyr:::join(veto.bin, veto.fs, type="left")
voice.bin <- plyr:::join(voice.bin, voice.fs, type="left")

# put those predictions back into original data. 
dat.fa$voice_irt <- voice.bin$voice_irt
dat.fa$veto_irt <- veto.bin$veto_irt
dat <- dat.fa
```

We could then correlate all of our latent variables estimated so far. 

```{r}
cor(dat[,c("voice_srm", "voice_fa", "voice_irt", "voice")], use="pair")
```

```{r}
cor(dat[,c("veto_srm", "veto_fa", "veto_irt", "veto")], use="pair")
```

I made the democracy data into binary indicators to show you how this works, but generally you probably wouldn't do this.  With mixed data (different levels of measurement) so long as nothing is nominal in nature, you're probably better of either

1. Sticking with normal theory factor analysis (as we discussed above with the `fa` function), or
2. Using a mixed data factor analysis model - you could do this with `MCMCmixfactanal` function in the `MCMCpack` package.  These are Bayesian models, which have lots of benefits, but also require more thorough checking for convergence. 

## Predictive Models

Ultimately, we are measuring democracy in the way we do not as an end in itself, but as a means for estimating the relationship between democracy and repression.   We can make copies of the data and rename the different types of voice and veto as simply `voice` and `veto`.  This will allow us to make a nice statistical table pretty easily.  We can then use each different copy of the data in turn to give us a different measurement for the independent variables.  I'm also including the measure I created from a slightly different, but more complicated measurement model as well.  We will also need to create the lag of repression.  We can use the `tscslag` function defined below to do that: 


```{r}
tscslag <- function(x, cs, ts, l=1){
	obs <- 1:length(x)
	lagobs <- match(paste(cs, ts-l, sep="::"), paste(cs, ts, sep="::"))
	lagx <- x[ lagobs]
	lagx
}
dat$lagrep <- tscslag(dat$rep1, dat$ccode, dat$year)
tmp1 <- tmp2 <- tmp3 <- dat
tmp1$voice <- scale(tmp1$voice_srm)
tmp2$voice <- scale(tmp2$voice_fa)
tmp3$voice <- scale(tmp3$voice_irt)
tmp1$veto <- scale(tmp1$veto_srm)
tmp2$veto <- scale(tmp2$veto_fa)
tmp3$veto <- scale(tmp3$veto_irt)

mod1 <- lm(rep1 ~ lagrep + voice*veto + log(gdppc) + logpop + cwar + iwar, data=tmp1)
mod2 <- lm(rep1 ~ lagrep + voice*veto + log(gdppc) + logpop + cwar + iwar, data=tmp2)
mod3 <- lm(rep1 ~ lagrep + voice*veto + log(gdppc) + logpop + cwar + iwar, data=tmp3)
mod4 <- lm(rep1 ~ lagrep + voice*veto + log(gdppc) + logpop + cwar + iwar, data=dat)

```

```{r, results='asis'}
library(stargazer)
stargazer(mod1,mod2,mod3,mod4, type ='html')
```

We need to consider a couple of things.  First, how well does the model fit - it explains about 97% of the variation in repression.  This sounds really high, but a lot of that explanatory power comes from the lag of repression - the fact that we are accounting in the model for what repression was like in the last year.  If we have time, we can talk about how to investigate the interactions.   I'll show the code below that produces the marginal effects plots. 

```{r, out.width="100%", message=FALSE, warning=FALSE}
library(DAMisc)
DAintfun2(mod1, c("voice", "veto"), hist=TRUE, scale.hist=.3, rug=FALSE)
```
```{r, out.width="100%"}
DAintfun2(mod2, c("voice", "veto"), hist=TRUE, scale.hist=.3, rug=FALSE)
```
```{r, out.width="100%"}
DAintfun2(mod3, c("voice", "veto"), hist=TRUE, scale.hist=.3, rug=FALSE)
```
```{r, out.width="100%"}
DAintfun2(mod4, c("voice", "veto"), hist=TRUE, scale.hist=.3, rug=FALSE)
```


## Maps

Instead of spending more time on statistical modeling, which would probably require some more foundational discussion, I thought I would discuss for a bit how you could map the data if you wanted to.  Here, we could map data from any single year for either `voice` or `veto`, or we could take the average over all years.  First, let's figure out how to manage the data and get everything in the right place. 

First, let's load the `sf` and `spData` packages.  If you have trouble installing the `sf` package, look [here](https://github.com/r-spatial/sf). 
```{r, message=FALSE, warning=FALSE}
library(sf)
library(spData)
data(world)
head(world)
```

To merge our democracy data onto the world data, we need to have a common identifier.  Generally, merging by country name is problematic for a small set of countries.  Ultimately, we'd like to merge by something like numeric country code (COW) or ISO code of some sort or another.  The `countrycode` package allows us to switch between various different standards.  So, we want to put a new variable in our democracy data that is the ISO 2-character identifier.  

```{r}
library(countrycode)
dat$iso_a2 <- countrycode(dat$ccode, "cown", "iso2c")
```

While we're not going to worry about this too much now, the world data we have is a recent version, so countries like East- and West-Germany (COW codes 260 and 265) don't exist anymore, so they won't be plotted.  Next, we can select the data we want to merge and merge it with the world data. Since we need to plot a single year, we'll use 1999. 

```{r}
tmp <- dplyr:::select(dat, "voice", "veto", "iso_a2", "year") %>% filter(year == 1999)
world <- merge(world, tmp, all.x=TRUE, all.y=FALSE)
```

Next, we can make the map. 

```{r, out.width="75%"}
library(ggplot2)
ggplot(world) + geom_sf(aes(fill=voice)) + theme_bw()
```


```{r, out.width="75%"}
ggplot(world) + geom_sf(aes(fill=veto)) + theme_bw()
```

The `tmap` package provides another alternative, with a discrete color scheme by default. 

```{r}
library(tmap)
tm1 <- tm_shape(world) + tm_fill(col="voice", 
    palette = "RdBu", title="Democracy (Voice)")   + 
    tm_borders(col="gray50", lwd=.5)
print(tm1)
```

If you want a bit of interactivity, you can use the `leaflet` package to convert your `tmap` into a leaflet map. 

```{r}
library(leaflet)
tmap_leaflet(tm1)
```

As you can see, R provides a nice interface to sophisticated graphics with relatively little knowledge required of the underlying javascript technology. 

## UIs

In the remaining time we have (if any), I thought I'd discuss the `shiny` interface for R.  This is a web-based interface that will allow you to program interactive front-ends to arbitrarily complicated R code.  If we wanted other people to be able to change our analysis, we could either give them replication code and data or we could build an interface that would make it easy to change things.  Here are a few things we might want to let users change: 

1. Variables in the measurement model. 
2. Year chosen for graph. 

There are a few different widgets we can use to generate interactivity.  You can find more information about these and other tools at the [Shiny Website](https://shiny.rstudio.com). 

1. `checkboxInput` and `checkboxGroupInput` give you access to check boxes for non-mutually exclusive choices. 
2. `selectorInput` gives you access to pick one (or potentially more) element from a list of pre-defined choices. 
3. `radioButtons` gives you access to radio buttons for mutually exclusive choices. 
4. `numericInput` allows users to input numbers that will be passed to the serve. 

### Structure of a shiny app. 

A shiny app has two components 

1. `ui` identifies what the user sees including both the choices they can make and the output that gets returned to the interface. 
2. `server` takes the input arguments and executes R functions on them to produce output.  

Before we get into building our own Shiny app for our purposes, here's a toy example that just chooses variables to plot. 

```{r, eval=FALSE}
library(shiny)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            selectInput("x", "X-variable",
                        choices = c("Education" = "education", "Income" = "income", 
                                    "Percent Women" = "women", "Occupational Prestige" = "prestige")),
            selectInput("y", "Y-variable",
                        choices = c("Education" = "education", "Income" = "income", 
                                    "Percent Women" = "women", "Occupational Prestige" = "prestige")), 
            radioButtons("smooth", "Trend Line", choices = c("None" = "none", "Linear Model"="lm", 
                        "LOESS Curve"="loess"))
            ),
        mainPanel(
            plotOutput("plot")
           )
      )
    )

server <- function(input, output){
    library(ggplot2)
    library(carData)
    data(Prestige)
    output$plot <- renderPlot({
        g <- ggplot(Prestige, aes(x=get(input$x), y=get(input$y))) + geom_point(pch=1) + theme_bw() + theme(aspect.ratio=1)
        if(input$smooth == "lm"){
            g <- g+geom_smooth(method="lm")
        }
        if(input$smooth == "loess"){
            g <- g+geom_smooth(method="loess")
        }
        print(g)
    })
}
shinyApp(ui=ui, server=server)
```

For our example, the `ui` would need to do the following: 

1. Use check boxes to allow the users choose variables for the measurement model.
2. Use a selector input to choose the year to be plotted. 
3. Show the map that gets rendered in the server file. 

The `server` will have to do the following things. 

1. Load the data and packages. 
2. Estimate the appropraite measurement model. 
3. Save the predictions into the data. 
4. Filter the data to choose the right year. 
5. Merge the measurement scores data with the country-level geographic data. 
6. Make the map. 







# References