Skip to content

Instantly share code, notes, and snippets.

View khakieconomics's full-sized avatar

Jim khakieconomics

View GitHub Profile
data {
int N; // number of rows
int T; // number of inidvidual-choice sets/task combinations
int I; // number of Individuals
int P; // number of covariates
vector<lower = 0, upper = 1>[N] choice; // binary indicator for choice
matrix[N, P] X; // product attributes
int task[T]; // index for tasks
data {
int N; // number of rows
int T; // number of inidvidual-choice sets/task combinations
int I; // number of Individuals
int P; // number of covariates
vector<lower = 0, upper = 1>[N] choice; // binary indicator for choice
matrix[N, P] X; // product attributes
int task[T]; // index for tasks
@khakieconomics
khakieconomics / classic_iv.stan
Created October 13, 2017 19:33
Classic instrumental variables regression model for Stan
data {
int N;
int PX; // dimension of exogenous covariates
int PN; // dimension of endogenous covariates
int PZ; // dimension of instruments
matrix[N, PX] X_exog; // exogenous covariates
matrix[N, PN] X_endog; // engogenous covariates
matrix[N, PZ] Z; // instruments
vector[N] Y_outcome; // outcome variable
int<lower=0,upper=1> run_estimation; // simulate (0) or estimate (1)
library(rstan); library(dplyr); library(ggplot2); library(reshape2)
options(mc.cores = parallel::detectCores())
set.seed(49)
# Observations
N <- 2000
# Draw errors
rho <- 0.5
Sigma <- matrix(c(1, rho, rho, 1), 2, 2)
functions {
real binormal_cdf(real z1, real z2, real rho) {
if (z1 != 0 || z2 != 0) {
real denom = fabs(rho) < 1.0 ? sqrt((1 + rho) * (1 - rho)) : not_a_number();
real a1 = (z2 / z1 - rho) / denom;
real a2 = (z1 / z2 - rho) / denom;
real product = z1 * z2;
real delta = product < 0 || (product == 0 && (z1 + z2) < 0);
return 0.5 * (Phi(z1) + Phi(z2) - delta) - owens_t(z1, a1) - owens_t(z2, a2);
}
@khakieconomics
khakieconomics / truncnorm_ng.stan
Created August 15, 2017 20:55
Unconstrained quantile to truncated normal conversion
functions {
// lower bound is a, upper bound is b, rv is x, mean is mu, sd is sigma
vector xi(vector x, real mu, real sigma) {
return((x - mu)./sigma);
}
real alpha(real a, real mu, real sigma) {
real out;
out = (a==negative_infinity())? negative_infinity(): (a - mu)/sigma;
return(out);
}
@khakieconomics
khakieconomics / ppd_plot.R
Created May 19, 2017 18:27
Plotting posterior predictive density
library(ggplot2); library(dplyr)
aa <- data_frame(a = rnorm(30, 2, .1), b = rnorm(30, .5, .05), sigma = rnorm(30, 1, .1))
g <- ggplot(data.frame(x = c(-1, 5.5)), aes(x))
for(i in 1:nrow(aa)) {
g <- g +
stat_function(fun = dnorm, args = list(mean = c(aa$a[i] + aa$b[i]), sd = aa$sigma[i]), alpha = 0.3)
}
g +
data {
int N; // number of observations
matrix[N, 6] Y; // a matrix of observations for each measure. I've encoded missing values as -9
vector[6] inits; // a vector of centers for the initial values of the data
}
parameters {
// the state
matrix[N, 6] X;
// cholesky factor of the correlation matrix of the innovations to the state
cholesky_factor_corr[6] L_omega;
@khakieconomics
khakieconomics / adding_up_imputation.R
Created March 1, 2017 01:52
Imputing values that add up to (known) totals when totals are known for all.
# load the Stan library and set it up to use all cores
library(rstan)
options(mc.cores = parallel::detectCores())
# Create some data
softmax <- function(x) exp(x)/sum(exp(x))
N <- 50
P <- 5
theta <- rnorm(P)
@khakieconomics
khakieconomics / lca_salary_distributions.R
Created February 17, 2017 20:40
Scrapes, saves and plots the salary rates for LCA applications for a few quantitative fields in NY state.
library(tidyr); library(ggplot2)
library(rvest); library(dplyr); library(ggthemes)
session <- html_session("http://visadoor.com/")
form <- html_form(session)[[1]]
# Add job titles to the character list below
jobs <- c("data scientist", "economist", "actuary", "consultant", "management consultant", "statistician")