Skip to content

Instantly share code, notes, and snippets.

View potterzot's full-sized avatar

Nicholas Potter potterzot

View GitHub Profile
@potterzot
potterzot / simulation.R
Last active September 2, 2020 02:25
Replication of Lewbel, Schennach, and Zhang (2020): Identification of a Triangular Two Equation System Without Instruments
# This script attempts to replicate a potion of Lewbel et al (2020)
# Working paper: https://drive.google.com/file/d/1UHWtjK81Cd0f3ksg6pv9B-xg1TGWNRQ1/view
# We focus on the simulation design #2 described on pg. 15
# See also Appendix B on pg. 28 for a clear description of estimation approach
# Results are in Table 2 on pg. 33
# Number of observations and true coefficient values
set.seed(15)
N <- 100
beta <- 1
@potterzot
potterzot / r_datatable.Rmd
Last active April 15, 2020 18:55
Introduction to data.table
---
title: An Introduction to data.table using geographic data
author: Nicholas A Potter
output: html_document
---
```{r, echo = FALSE}
```
@potterzot
potterzot / dc.R
Last active October 11, 2019 19:04
R script for data carpentry lesson
################################################################
# EVERYTHING HERE IS THE CODE THE LESSON IS BASED OFF OF
# IN THE NEXT SECTION IS CODE ACTUALLY WRITTEN DURING THE LESSON
################################################################
install.packages("tidyverse")
install.packages(c("here", "reprex"))
### Resources
# Stack overflow: stackoverflow.com
@potterzot
potterzot / get_nass.R
Created May 28, 2019 20:07
Fetching PCT PLANTED and YIELD for corn from USDA-NASS QuickStats
### Fetch % planted and yield from NASS QUICKSTATS
#
# Requires the rnassqs `R` package: https://github.com/potterzot/rnassqs
# Need to get an API KEY here: https://quickstats.nass.usda.gov/api
api_key <- Sys.getenv("NASSQS_TOKEN")
## First get PCT PLANTED
# Define the parameters to fetch PCT PLANTED
pct_planted_params <- list(
@potterzot
potterzot / extract_spei.Rmd
Created March 28, 2018 18:29
Using tidync versus ncdf4
This documents the ease with which I used `tidync` to extract data from the drought index database [SPEI](http://spei.csic.es/database.html#p3) compared with using `ncdf4` directly.
### Setup necessary for both methods
```{r}
# Establish dimensions ------------------------------------------------------------------
### Time limits.
time_first_idx <- as.integer(difftime(as.Date("1990-01-01"), as.Date("1900-01-01"))) + 15
@potterzot
potterzot / latlon2shape.R
Created December 19, 2017 21:07
Map a lat/lon point to a county or other geography
#' Get county/state name from lat/long pair.
#'
#' Maps a lat/long pair to a geography.
#'
#' @importFrom maps map
#' @importFrom maptools map2SpatialPolygons
#' @importFrom sp over CRS SpatialPoints
#' @export
#'
#' @param lat list of numbers or number of the latitude in degrees.
@potterzot
potterzot / reprex_testthat_output.md
Last active October 19, 2017 02:55
Output from running tests on reprex
test_that("reprex: clipboard input works", {
  skip_if_no_clipboard()
  clipr::write_clip("1:5") #<- this line hangs
  expect_match(reprex(render = FALSE), "^1:5$", all = FALSE)
})

Output from tests testthat results ================================================================

@potterzot
potterzot / Makefile
Last active October 10, 2017 18:11
Using Make to compile multiple Rmd files in a directory
# This lets you type `make`
# and will pandoc all *.Rmd files in the letters directory
# into pdfs stored in the output directory
all: readme letters
#generate all letters
SRC=$(wildcard letters/*.Rmd)
OUT=$(patsubst letters/%.Rmd, output/%.pdf, $(SRC))
letters: $(OUT)
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

1. "Liking more of everything"

Given $$X = \mathbb{R}^L$$, for every two bundles $$x, y \in X$$, $$x \succeq y$$ if and only if $$x_k \ge y_k \forall k$$.

a) Preferences are complete if for every two bundles $$x, y \in X$$, either $$x \succeq y$$ or $$x \preceq y$$ or $$x \sim y$$. Consider two bundles $$x,y$$, with $$x_i &gt; y_i$$ and $$x_j &lt; y_j$$. Then $$x \npreceq y$$ and $$x \nsucceq y$$, so they preferences are not complete.

b) Preferences are transitive if for any bundles $$x,y,z \in X$$, if $$x \succeq y$$ and $$y \succeq z$$, then $$x \succeq z$$.

Assume that $$x \succeq y$$ and $$y \succeq z$$. Then by the preference relation above, $$x_k \ge y_k \forall k$$ and $$y_k \ge z_k \forall k$$. Substituting, we can see that $$x_k \ge y_k \ge z_k \Rightarrow x_k \ge z_k \forall k$$, hence $$x \succeq z$$.

@potterzot
potterzot / plot_multi_libraries.md
Last active August 4, 2016 17:34
Plotting with multiple library options in R

Plotting with Multiple Library Options in R

This is how I offer a single plot function with options to plot in different libraries. The motivation is that for research projects, sometimes we want an interactive graphic for web pages, but we might also want the same graphic in journal-quality png,svg,jpeg format.

It works well I think, but is there a better way of doing this?

The basic idea is a wrapper function that takes an argument that determines which plotting library to use, something like this:

#' Example plot for this question