Skip to content

Instantly share code, notes, and snippets.

View khakieconomics's full-sized avatar

Jim khakieconomics

View GitHub Profile

Keybase proof

I hereby claim:

  • I am khakieconomics on github.
  • I am javage (https://keybase.io/javage) on keybase.
  • I have a public key ASCOfa5tiUHOtFlCuiaEWO_BO1GQwq0-7ugZRerDYO_p7go

To claim this, I am signing this object:

@khakieconomics
khakieconomics / Predocs_tracking_and_DiD.R
Last active October 9, 2019 21:08
File to ping Github's API, get the commit numbers for applicants to our predoc program, and run a simple DiD on their daily commits before and after the program.
# A simple script to grab github commits for a list of users and plot
# Group average commits per day for two groups
# To use this script, you'll need to set up a yaml file with credentials
# for github, and set up a google sheet with columns `Github handle` and
#`Attended` (with values "Yes" or "No")
# Author: Jim Savage, Schmidt Futures
# Load libraries
library(tidyverse); library(httr); library(yaml);library(jsonlite);
@khakieconomics
khakieconomics / albert_chib_style_trivariate.stan
Created January 30, 2019 00:54
Trivariate model with a binary margin
data {
int N;
int N_neg;
int N_pos;
int P;
matrix[N, P] X;
matrix[N, 3] Y_raw; // first column is 0s and 1s
int pos_neg[N_neg];
int pos_pos[N_pos];
}
@khakieconomics
khakieconomics / matrix_completion.stan
Created November 27, 2018 01:02
A simple matrix completion model for fixed K in Stan
data {
int N; // number of individuals
int T; // number of time periods
matrix[N, T] Y; // outcome matrix; missing entries set to -9.0
int K; // rank of matrix
}
parameters {
matrix[N, K] M; // individual loadings
matrix[T, K] U; // time factors
real<lower = 0> sigma; // error scale
functions {
matrix make_pairwise_logit_mat(vector theta) {
matrix[rows(theta), rows(theta)] out;
for(r in 1:rows(out)) {
for(c in 1:r) {
if(r==c) {
out[r, c] = 0;
} else {
out[r, c] = exp(theta[c])/(exp(theta[c]) + exp(theta[r]));
@khakieconomics
khakieconomics / stan_stan.stan
Created September 23, 2018 14:52
Censored time and group random effects normal linear model.
data {
int N; // number of observations
int T; // number of time periods
int I; // number of groups
int P; // number of controls
int<lower = 1, upper = T> time[N]; // time index
int<lower = 1, upper = I> group[N]; // group index
vector[N] Y;
matrix[N, P] X; // you should include group/time means in X
}
@khakieconomics
khakieconomics / demo_for_andrew.Rmd
Created August 28, 2018 19:52
Simple R dashboard with plotly mouse-over effects
---
title: "Demo of mouse-over"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
@khakieconomics
khakieconomics / deep_loops_in_stan.R
Created August 8, 2018 22:52
Write your deep loops in Stan, not R
library(tidyverse); library(rstan)
# Some fake data
Individuals <- 1000
Sims <- 1000
Months <- 20
initial_values <- data.frame(customer_id = 1:Individuals, initial_value = rnorm(Individuals))
library(tidyverse); library(lfe)
some_data <- expand.grid(time = 1:10, individual = 1:200) %>%
left_join(data_frame(time = 1:10, time_effects= rnorm(10))) %>%
left_join(data_frame(individual = 1:200, individual_effects= rnorm(200))) %>%
mutate(treatment = sample(0:1, n(), replace = T),
outcome = time_effects + individual_effects + 1 * treatment + rnorm(n())) %>%
group_by(time) %>%
mutate(demeaned_outcome = outcome - mean(outcome),
demeaned_treatment = treatment - mean(treatment)) %>%
library(rstan); library(tidyverse)
# A utility function
extract_pars <- function(mle_fit, pars) {
unlist(lapply(pars, function(n) mle_fit$par[grepl(pattern = n, x = names(mle_fit$par))]))
}
# Simulate fake data ------------------------------------------------------