---
title: "Lab 8"
author: "Jeffrey Arnold"
date: "Friday, May 22, 2015"
---

```{r echo = FALSE,results='hide'}
knitr::opts_chunk$set(cache = TRUE, autodep = TRUE,
               tidy = TRUE)
```


# Setup

This lab uses the following libraries
```{r message = FALSE}
library("dplyr")
library("ggplot2")
```


# Saving to Files

## Data

Use a dataset as an example and

- save to csv
- save to RData
- save to dta

## Plots

You can save plots from within RStudio in the *Plots* pane with the *Export* menu item.

You generally want to export to a vector format such as PDF or SVG if possible.
Otherwise, use PNG. 
You do not want to use JPEG since that is a lossy compression format.

You can also use R commands to save a plot to a file.
The default way to do this in R is to using R's low level graphics functions: `pdf`, `png`.
```{r eval = FALSE}
pdf("carplot.pdf")
ggplot(mtcars, aes(wt, mpg)) +
  geom_point()
dev.off()
```
Note that the file does not save until you close the device using `dev.off()`.
This is to allow devices to work with base R graphics which often require several commands to create the plot.

The `dev` functions works for all types of `ggplot2` For **ggplot2** objects, you can use the function `ggsave`:
```{r eval=FALSE}
mtcars_plot <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point()
ggsave(filename = "mtcars_plot.pdf", plot = mtcars_plot)
```
`ggsave()` will determine the file format of the file to save from the extension of the `filename` argument.
There are options for adjusting the height, width, dpi, etc. 
See the [documentation](http://docs.ggplot2.org/0.9.2.1/ggsave.html) for more information.

*Important* when you run an R markdown file, plots are saved to `{filename}_files`. 
So you an use them without manually 

# Merging

```{r}
rossoil <-read.csv("http://UW-POLS503.github.io/pols_503_sp15/data/rossoildata.csv", na.strings="")
democracy <- read.csv("http://UW-POLS503.github.io/pols_503_sp15/data/democracy.csv", header=TRUE, stringsAsFactors=FALSE, na.strings=".")
```

Merge dataframes, keeping all countries in each even if no match in the other.
Because our data is organised by country-year, include each

```{r}
new_data <- merge(rossoil, democracy,
                  by.x = c("cty_name","year"),
                  by.y = c("CTYNAME", "YEAR"), 
                  all.x=TRUE, all.y=TRUE)
```
how do the original and merged datasets compare?
```{r}
dim(rossoil)
dim(democracy)
dim(new_data)
ncol(democracy)+ncol(rossoil)-2
```
```{r results = 'hide'}
summary(new_data)
```

We can also, keep all dataframe 1
```{r}
new_data_allx <- merge(rossoil, democracy,
                  by.x = c("cty_name","year"),
                  by.y = c("CTYNAME", "YEAR"), 
                  all.x=TRUE, all.y=FALSE)
```
Let's check what it did
```{r}
filter(new_data_allx, cty_name=="Algeria") %>% tbl_df() %>% head()
filter(rossoil, cty_name=="Algeria") %>% tbl_df() %>% head()
filter(democracy, CTYNAME=="Algeria") %>% tbl_df() %>% head()
```

```{r}
new_data_ally <- merge(rossoil, democracy,
                  by.x = c("cty_name","year"),
                  by.y = c("CTYNAME", "YEAR"), 
                  all.x=FALSE, all.y=TRUE)
```
Let's check what it did
```{r}
filter(new_data_ally, cty_name=="Algeria") %>% tbl_df() %>% head()
filter(rossoil, cty_name=="Algeria") %>% tbl_df() %>% head()
filter(democracy, CTYNAME=="Algeria") %>% tbl_df() %>% head()
```

**dplyr** has its own merge functions described [here](http://cran.r-project.org/web/packages/dplyr/vignettes/two-table.html), 

# Outliers

**Challenge** Replicate the analysis in Fox Chapte 11.2 Conduct outlier diagnostics for the regression of the prestige of occuptions in Canada in 1971 on income, education, percent women, and type (white collar, blue collar, professional).
Are there any outliers? Consider hat values, Studentized residuals, and Cook's distance. Which observation has the largest influence on the regression? 
How does the regression line change if you drop that observation?

```{r}
library("car")
data("Prestige")
mod_prestige <- lm(prestige ~ income + education + women + type, data = Prestige)
```



# Multiple Imputation

For this part we will use the **Amelia** package which implements a multiple imputation method.
```{r message=FALSE}
library("Amelia")
```

We will use the Ross oil data that we've used throughout this course.
```{r}
rossoil <- read.csv("http://UW-POLS503.github.io/pols_503_sp15/data/rossoildata.csv") %>%
   arrange(id1, year) %>%
   group_by(id1) %>%
   mutate(oilL5 = lag(wdr123, 5) / 100,
          metalL5 = lag(wdr313, 5) / 100,
          GDPpcL5 = lag(wdr135, 5) / 100,
          islam = islam / 100)
rossoil1980 <- rossoil %>% filter(year == 1980)
```


**Challenge** Estimate the the following regression of regime type in 1980 with (1) listwise deletion, and (2) multiple imputation. How do the coefficients and standard errors of the regression coefficients differ?
```{r}
model2 <- lm(regime1 ~ log(GDPcap) + metalL5 + oilL5 + oecd + islam, data = rossoil)
```

Note, it would be better to both estimate this model as a panel using all available data and to impute the data as a TSCS. See the Amelia [vignette](http://cran.r-project.org/web/packages/Amelia/vignettes/amelia.pdf) for examples of how to do that.

# Sources

- <http://staff.washington.edu/csjohns/503/lab6.r>

