Skip to content

Instantly share code, notes, and snippets.

View khakieconomics's full-sized avatar

Jim khakieconomics

View GitHub Profile
library(KFAS)
library(rstan)
data(GlobalTemp)
model_dlm1a <- stan_model("../stan/dlm1a.stan")
y <- as.matrix(GlobalTemp)
data <-
within(list(),
{
library(readr); library(dplyr); library(lubridate)
dat_2 <- read_csv("Datathon WC Data Games 11-20.csv")
glimpse(dat_2)
dat_3 <- read_csv("Datathon WC Data Games 21-30.csv")
as_datetime <- function(x){
# Some fake data
library(dplyr); library(rstan)
# Write out the data generation with known parameters
# Set the number of individuals
n_ind <- 50
# Generate fake data
dgp <- "data {
int<lower=1> N;
real x[N];
real rho;
real sigma;
}
transformed data {
vector[N] mu;
library(MASS); library(rstan)
# Sample size
N <- 5000
# Generate two uncorrelated covariates (means 1 and 5, both with standard deviations of 1)
X <- mvrnorm(N, c(1, 5), matrix(c(1, 0, 0, 1), 2, 2))
plot(X)
# Loadings matrix
Gamma <- matrix(c(0.5, 1,
# In this demonstration, I estimate a discrete-time NGARCH model.
# I first simulate the model with known parameters, then try to
# recover the parameters from simulated data. Then I apply the
# model to weekly returns for Google.
# The model seems to be able to recover most of the parameters
# fairly well, with inference for alpha, beta fairly week. gamma
# does not seem to be well identified.
# jim savage, james@lendable.io
@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")
@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)
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 / 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 +