Skip to content

Instantly share code, notes, and snippets.

View jrnold's full-sized avatar

Jeffrey Arnold jrnold

View GitHub Profile
@jrnold
jrnold / gist:3886051
Created October 13, 2012 20:32
Create S4 subclass of data.frame with constraints on columns
## Check whether columns could be a key to a data.frame
##
## @param x \code{data.frame} to check
## @param i \code{character} or \code{integer} Column names
## @return \code{logical}. \code{TRUE} if no duplicated values
## of \code{i} in \code{x}.
is_key <- function(x, i) {
!any(duplicated(x[ , i]))
}
@jrnold
jrnold / gist:3889496
Created October 14, 2012 19:00
Better print display for themes and elements in ggplot2
library(ggplot2)
library(plyr)
library(grid)
notnull <- function(x) !is.null(x)
as.character.element <- function(x) {
.f <- function(x) {
if (class(x) %in% c("rel", "unit")) {
as.character(x)
@jrnold
jrnold / gist:3890358
Created October 15, 2012 01:16
Generic functions for dlm
setOldClass("dlm")
setOldClass("dlmFiltered")
##' Mean Squared Error Generic function
mse <- function(x, na.rm=FALSE, ...) {
mean((x - mean(x, na.rm=na.rm))^2, na.rm=na.rm)
}
##' @export
setGeneric("mse")
@jrnold
jrnold / gist:3890361
Created October 15, 2012 01:19
Bayesian lm
##' Bayesian normal linear regression with reference prior
##'
##' Given an lm object, simulate the Bayesian posterior.
##'
##' @export
bayeslm <- function(..., niter=100) {
llik <- function(y, b, sigma2, X) {
sum(dnorm(y, X %*% b, sqrt(sigma2), log = TRUE))
}
@jrnold
jrnold / gist:4327373
Created December 18, 2012 11:53
Run multiple Stan chains with PowerShell
For ($i=1; $i -le 4; $i++) {
model --refresh=0 --seed=12345 --chain_id=$i --samples=samples$i.csv
}
@jrnold
jrnold / globaltemp.R
Created February 3, 2013 03:06
Kalman filter in Stan
library(KFAS)
library(rstan)
data(GlobalTemp)
model_dlm1a <- stan_model("../stan/dlm1a.stan")
y <- as.matrix(GlobalTemp)
data <-
within(list(),
{
@jrnold
jrnold / foo.stan
Last active December 12, 2015 10:39
github bug report stan-dev/stan
data {
int n;
int m;
row_vector[m] alpha;
}
parameters {
row_vector[m] beta[n-1];
}
transformed parameters {
matrix[n, m] alpha_beta;
@jrnold
jrnold / server.R
Last active December 24, 2015 00:19
library("stringr")
library("ggplot2")
shinyServer(function(input, output) {
output$main_plot <-
renderPlot({
x <- as.numeric(str_split(input$data, ",")[[1]])
xseq <- seq(min(x), max(x), length.out=100)
xvar <- var(x)
@jrnold
jrnold / median.factors.R
Last active December 24, 2015 06:49
Median methods for ``factor`` and ``ordered`` classes in R.
quantile.ordered <- function(x, probs = seq(0, 1, 0.25)) {
tab <- table(x)
cdf <- cumsum(tab / sum(tab))
idx <- sapply(probs, function(p) min(which(cdf >= p)))
levels(x)[idx]
}
quantile.factor <- quantile.ordinal
median.ordered <- function(x) quantile(x, 0.5)
median.factor <- median.ordered
@jrnold
jrnold / gist:6799152
Last active August 18, 2021 13:44
Create a plot of the normal distribution with an area shaded in. Useful for teaching z-scores and stuff like that.
library("ggplot2")
#' Draw Normal Distribution Density with an area shaded in.
#'
#' @param lb Lower bound of the shaded area. Use \code{-Inf} for a left tail.
#' @param ub Upper bound of the shaded area. Use \code{Inf} for a right tail.
#' @param mean Mean of the normal distribution
#' @param sd Standard deviation of the normal distribution
#' @param limits Lower and upper bounds on the x-axis of the area displayed.
#' @return ggplot object.