Skip to content

Instantly share code, notes, and snippets.

View khakieconomics's full-sized avatar

Jim khakieconomics

View GitHub Profile
functions {
matrix L_cov_exp_quad_ARD(matrix x,
real alpha,
vector rho,
real delta) {
int N = rows(x);
matrix[N, N] K;
real sq_alpha = square(alpha);
for (i in 1:(N-1)) {
K[i, i] = sq_alpha + delta;
@khakieconomics
khakieconomics / GP_IV.R
Last active July 6, 2018 21:43
File to run/visualise GP_IV.stan
# First load the libraries and define a function to create a
library(tidyverse); library(rstan)
set.seed(78)
ard_Sigma <- function(features, rho, alpha) {
noise <- 1e-8
features <- as.data.frame(features)
P <- ncol(features)
if(length(rho) != P) {
@khakieconomics
khakieconomics / b_spline_function.stan
Created July 13, 2018 15:04
Produce a matrix of b-splines for use internally within a Stan program
functions {
// B function
vector B(vector x, vector t, int i, int j_p1);
vector B(vector x, vector t, int i, int k) {
vector[rows(x)] out;
vector[rows(x)] a_i1j1;
vector[rows(x)] a_ij1;
# This script checks that the b-spline function in gist https://gist.github.com/khakieconomics/2272cd7f0d61950852622198b26a2d02
# produces something approximating the function in the splines package.
library(rstan)
expose_stan_functions("b_spline_function.stan")
probs <- runif(1000)
probs <- probs[order(probs)]
functions {
// B function
vector B(vector x, vector t, int i, int j_p1);
vector B(vector x, vector t, int i, int k) {
vector[rows(x)] out;
vector[rows(x)] a_i1j1;
vector[rows(x)] a_ij1;
functions {
vector inverse_mills(vector z) {
vector[rows(z)] out;
for(i in 1:rows(z)) {
out[i] = exp(normal_lpdf(z[i] | 0, 1)) / (Phi(z[i]));
}
return(out);
}
}
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 ------------------------------------------------------
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)) %>%
@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))
@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)