Skip to content

Instantly share code, notes, and snippets.

View jrnold's full-sized avatar

Jeffrey Arnold jrnold

  • Discord
  • Oakland, CA
  • 02:25 (UTC -07:00)
  • LinkedIn in/jrnold
View GitHub Profile
data {
int jj_n;
int jj_idx[jj_n];
int jj_groups[jj_n];
...
}
parameters {
vector[3] beta;
...
}
@jrnold
jrnold / gist:9f21e4862a997294b245
Created May 5, 2014 23:54
Don't use periods for variable names in R
foo <- structure(rnorm(100), class = "test")
t(foo)
@jrnold
jrnold / gist:8291473
Last active January 2, 2016 10:39
instructions to install and run mathcamp code.
install.packages(c("ggplot2", "devtools", "shiny"))
library(devtools)
install_github("jrnold/pols_math_camp_2014")
library(mathcamp)
run_shiny_example("functions")
@jrnold
jrnold / server.R
Last active May 6, 2020 18:39
Shiny app comparing the t-distribution to the normal distribution
library("shiny")
x <- seq(-4, 4, by=0.01)
norm_dens <- dnorm(x)
shinyServer(function(input, output) {
output$plot <- renderPlot({
t_dens <- dt(x, df = input$df)
print(ggplot()
@jrnold
jrnold / server.R
Last active December 26, 2015 08:49
Confidence interval simulation
library("shiny")
library("ggplot2")
library("plyr")
norm_mean <- 0
norm_sd <- 1
##' Take sample from normal dist and calculate confidence interval for normal
smpl_mean_sample_ci <- function(n, p=0.95, pop_mean=0, pop_sd=1) {
smpl <- rnorm(n, norm_mean, norm_sd)
@jrnold
jrnold / server.R
Last active December 26, 2015 04:39
Sampling Distribution of Sample Means
## Copyright: Martin Berlin, Jeffrey Arnold <jeffrey.arnold@gmail.com> (2013)
##
## Adapted from original code by Martin Berlin <mno.berlin@gmail.com> http://spark.rstudio.com/berlin/stat/
library(shiny)
library(plyr)
library(ggplot2)
## Generate data
@jrnold
jrnold / gist:6799152
Last active August 13, 2025 16:04
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.
@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 / 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 / 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;