This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 + | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # 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) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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") | |