Skip to content

Instantly share code, notes, and snippets.

View statwonk's full-sized avatar
🏠
Working from home

Christopher Peters statwonk

🏠
Working from home
View GitHub Profile
@statwonk
statwonk / server.R
Created October 10, 2015 20:43
An example of passing a json blob to the front-end from a Shiny app back-end.
library(shiny)
iris <- datasets::iris
names(iris) <- gsub('[/.]','_',tolower(names(iris)))
shinyServer(
function(input, output) {
output$json <- reactive({
paste('<script>data=',
RJSONIO::toJSON(iris[iris$species == input$species,], byrow=T, colNames=T),
';console.log(data[0]);', # print 1 data line to console
@statwonk
statwonk / server.R
Last active November 9, 2015 19:36
A DiagrammeR app
library(shiny)
library(DiagrammeR)
library(magrittr)
shinyServer(function(input, output) {
output$diagram_plot <- renderDiagrammeR({
graph <-
create_graph() %>%
set_graph_name("Boxes and Circles") %>%
set_graph_time() %>%
@statwonk
statwonk / Bayesian Survival Analysis.ipynb
Created March 20, 2016 02:56 — forked from AustinRochford/Bayesian Survival Analysis.ipynb
Bayesian Survival Analysis PyMC3 Tutorial
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@statwonk
statwonk / .getRequiredPackages2.R
Created July 6, 2016 02:09
.getRequiredPackages2
function (pkgInfo, quietly = FALSE, lib.loc = NULL, useImports = FALSE)
{
.findVersion <- function(pkg, lib.loc = NULL) {
pfile <- system.file("Meta", "package.rds", package = pkg,
lib.loc = lib.loc)
if (nzchar(pfile))
as.numeric_version(readRDS(pfile)$DESCRIPTION["Version"])
else NULL
}
.findAllVersions <- function(pkg, lib.loc = NULL) {
@statwonk
statwonk / library.R
Created July 6, 2016 02:31
R's library() function
function (package, help, pos = 2, lib.loc = NULL, character.only = FALSE,
logical.return = FALSE, warn.conflicts = TRUE, quietly = FALSE,
verbose = getOption("verbose"))
{
testRversion <- function(pkgInfo, pkgname, pkgpath) {
if (is.null(built <- pkgInfo$Built))
stop(gettextf("package %s has not been installed properly\n",
sQuote(pkgname)), call. = FALSE, domain = NA)
R_version_built_under <- as.numeric_version(built$R)
if (R_version_built_under < "3.0.0")
@statwonk
statwonk / exp_relative_likelihoods.R
Created October 23, 2017 04:14
100 exponential random variables and their relative likelihoods
suppressPackageStartupMessages({
library(dplyr)
library(ggplot2)
library(purrr)
})
exp_likelihood <- function(x, lambda) {
(lambda^length(x))*exp(-lambda*length(x)*mean(x))
}
seq_len(1e2) %>%
lapply(function(i) {

Keybase proof

I hereby claim:

  • I am statwonk on github.
  • I am statwonk (https://keybase.io/statwonk) on keybase.
  • I have a public key ASBKY0yAKx1KghVEq7DmzhrFWAIZn94O-vhQHcjU9Wwz-go

To claim this, I am signing this object:

@statwonk
statwonk / km-quantiles.R
Created April 7, 2019 13:21
A routine showing how to calculate quantiles.
library(survival)
library(Hmisc)
# 1. a weibull random number generator, see http://statwonk.com/weibull.html
rweibull_cens <- function(n, shape, scale) {
# will happen to see the death time first or censoring?
rweibull(n, shape = shape, scale = scale) -> a_random_death_time
rweibull(n, shape = shape, scale = scale) -> a_random_censor_time
pmin(a_random_censor_time, a_random_death_time) -> observed_time
@statwonk
statwonk / cdfs.R
Created April 11, 2019 18:08
CDF gist
library(tidyverse)
# Cumulative density functions
?hist
# built in datasets
str(mtcars)
head(mtcars)
@statwonk
statwonk / control-vs-interaction.R
Created May 17, 2019 17:12
A comparison of std.errors of the treatment effect from an additive DGP experiment where a pre-treatment variable is interacted maximally, only additive no interaction, or unadjusted for.
library(tidyverse)
library(lme4)
library(broom)
N <- 1e4
generate_data <- function() {
tibble(
intercept = 1,
pretreatment_var = case_when(rbinom(N, 1, p = 0.5) == 1 ~ 0.5, TRUE ~ -0.5),