# VirusPolicy README

## Overview
The results in "Polarization and Public Health: Partisan Differences in Social Distancing During the Coronavirus Pandemic" can be reproduced by completing the following steps:

1. Ensure that your computing environment satisfies the **Requirements** section below.

2. Download the data described in the **External Data** section to your local computer.

3. Complete the steps described in the **Setup** section.

4. Run ```python run_all.py``` from the root of the repository (as described in the **Build** section).

The final paper will then be produced at `paper_slides/output/paper.pdf`.

## Folder Structure
This repository is structured as follows:

* `setup/` contains programs to download required packages and otherwise assist in setup.
* `raw/` contains raw data used elsewhere in the repo.
* `data_safegraph/` contains code and output related to an initial cleaning of the SafeGraph visit dataset.
* `data/` contains code and output related to the subsequent cleaning of all other datasets.
* `descriptive/` contains code and output related to descriptive analysis of health, mobility, etc. across time and geographies.
* `event_studies/` contains code and output related to event study analysis of policy effects.
* `health_simulations/` contains code and output related to simulations of health under counterfactual policies and social distancing.
* `geo_variation/` contains code and output related to analysis of geographic variation in virus outcomes.
* `paper_slides/` contains code and output related to paper production itself, using the output produced in previous folders/modules.
* `lib/` stores programs used to facilitate running the repository, including tools to link together different modules (eg `data/` and `descriptive/`).

## Requirements
All requirements must be installed and set up for command line usage. For further detail, see the **Command Line Usage** section below.

* Python (2.7/3.7)
* pip (>=10.0)

To build the repository as-is, the following applications are additionally required:

* git-lfs
* LyX
* R
* Stata

These software are used by the scripts contained in the repository. By default, the **Setup** and **Build** instructions below will assume their usage.

## External Data
In addition to the data contained in this repository, reproduction requires access to the following pieces of data:
* **SafeGraph**: Including SafeGraph's Weekly Pattterns, Open Census, and Places datasets. For the required folder structure of an external SafeGraph data folder, see `data_safegraph/external.txt`. This can be accessed from [https://www.placekey.io/community](https://www.placekey.io/community).
* **Facteus**: Facteus data on debit transactions, provided to the SafeGraph COVID-19 consortium. See `data/external.txt` for the required folder structure.
* **Homebase**: Homebase data on small business payrolls, provided to the SafeGraph COVID-19 consortium. See [https://joinhomebase.com/data/](https://joinhomebase.com/data/) for data access. We use raw data files running through September 2, 2020. See `data/external.txt` for the required folder structure.
* **Airport Data**: Data on airport locations from the `traffic` Python package, specifically `traffic.data.airports.data`.
* **Flight Data**: Air traffic data from The OpenSky Network 2020, downloaded from [https://zenodo.org/record/3928550#.X356XpNKhTa](https://zenodo.org/record/3928550#.X356XpNKhTa). See `data/external.txt` for the required folder structure.

## Setup
**If you are using Windows, you may need to run certain bash commands in administrator mode due to permission errors. To do so, open your terminal by right clicking and selecting `Run as administrator`. To set administrator mode on permanently, refer to the [RA manual](https://github.com/gentzkow/template/wiki/Repository-Usage#Administrator-Mode).**

1. Create a `config_user.yaml` file in the root directory. A template can be found in the `setup` subdirectory. See the **User Configuration** section below for further detail.

2. Install Python dependencies listed in the `requirements.txt` file using pip. One way to do this is to run the following bash command in a terminal from the `setup` subdirectory:
   ```
   python -m pip install --user -r requirements.txt
   ```

3. Run the `check_setup.py` file. One way to do this is to run the following bash command in a terminal from the `setup` subdirectory:
   ```
   python check_setup.py
   ```

4. Install Stata dependencies using the `setup_stata.do` file. One way to do this is to use the following bash command from the `setup` subdirectory:
   ```
   stata-mp -e setup_stata.do
   ```

   If you are using a Windows, you will likely have to adjust this bash command:
   ```
   stata_executable -e setup_stata.do
   ```

   `stata_executable` refers to the name of your Stata executable. For example, if your Stata executable was located in `C:\Program Files\Stata15\StataMP-64.exe`, you would want to use the following bash command:

   ```
   StataMP-64 -e setup_stata.do
   ```

5. Install R dependencies using the `setup_r.r` file. One way to do this is to run the following bash command in a terminal from the `setup` subdirectory:
   ```
   Rscript setup_r.r
   ```
   
6. Install the [libspatialindex](https://libspatialindex.org/) library: If you use homebrew, this can be done via `brew install spatialindex`.


## Build
**If you are using Windows, you may need to run certain bash commands in administrator mode due to permission errors. To do so, open your terminal by right clicking and selecting `Run as administrator`. To set administrator mode on permanently, refer to the [RA manual](https://github.com/gentzkow/template/wiki/Repository-Usage#Administrator-Mode).**

1. Follow the *Setup* instructions above.

2. From the root of repository, run the following bash command:
   ```
   python run_all.py
   ```

## Command Line Usage

For specific instructions on how to set up command line usage for an application, refer to the [RA manual](https://github.com/gentzkow/template/wiki/Command-Line-Usage).

By default, the repository assumes the following executable names for the following applications:

```
application : executable
python      : python
git-lfs     : git-lfs
lyx         : lyx
r           : Rscript
stata       : statamp (will need to be updated if using a version of Stata that is not Stata-MP)
```

These are the standard executable names for Mac and are likely to differ on your computer if you are using Windows. Executable names for Windows will typically look like the following:

```
application : executable
python      : python
git-lfs     : git-lfs
lyx         : LyX#.# (where #.# refers to the version number)
r           : Rscript
stata       : StataMP-64 (will need to be updated if using a version of Stata that is not Stata-MP or 64-bit)
```

Default executable names can be updated in `config_user.yaml`. For further detail, see the **User Configuration** section below.

## User Configuration
`config_user.yaml` contains settings and metadata such as local paths that are specific to an individual user and thus should not be committed to Git. For this repository, this includes local paths to [external dependencies](https://github.com/gentzkow/template/wiki/External-Dependencies) as well as executable names for locally installed software.

Required applications may be set up for command line usage on your computer with a different executable name from the default. If so, specify the correct executable name in `config_user.yaml`. This configuration step is explained further in the [RA manual](https://github.com/gentzkow/template/wiki/Repository-Structure#Configuration-Files).