Skip to content

Instantly share code, notes, and snippets.

View rasmusab's full-sized avatar

Rasmus Bååth rasmusab

View GitHub Profile
@rasmusab
rasmusab / get_up_to_speed_with_bayes_test_script.R
Last active July 8, 2019 21:56
UseR 2019 tutorial - Get up to speed with Bayes test script
# Prior to the tutorial make sure that the script below runs without error on your R installation.
# You first need to install the follwoing packages:
# install.packages(c("rstanarm", "prophet", "CausalImpact"))
library(rstanarm)
library(prophet)
library(CausalImpact)
# This will test that rstanarm works
# Don't be alarmed if you get a warning about "divergent transitions "
@rasmusab
rasmusab / analyze_gnubg.R
Created July 4, 2018 13:09
A barely tested R script that takes a backgammon match in any format gnubg can read, analyzes the match and returns a data frame with the analysis
# A barely tested R script that takes a backgammon match in any format
# gnubg can read, analyzes the match and returns a data frame with the analysis
# It requires that gnubg is readily available on the command line.
library(tidyverse)
library(jsonlite)
library(glue)
analyze_bg_match <- function(match_fname, match_format = "auto") {
gnubg_analysis_fname = tempfile()
@rasmusab
rasmusab / ab_test.csv
Created June 7, 2018 13:43
Example ab-test data
date group n_purchases
2017-05-01 0 124
2017-05-02 0 95
2017-05-03 0 147
2017-05-04 0 116
2017-05-05 0 219
2017-05-06 0 248
2017-05-07 0 132
2017-05-08 0 110
2017-05-09 0 114
date births deaths
1841-01-01 254 37
1841-02-01 239 18
1841-03-01 277 12
1841-04-01 255 4
1841-05-01 255 2
1841-06-01 200 10
1841-07-01 190 16
1841-08-01 222 3
1841-09-01 213 4
year births deaths clinic
1841 3036 237 clinic 1
1842 3287 518 clinic 1
1843 3060 274 clinic 1
1844 3157 260 clinic 1
1845 3492 241 clinic 1
1846 4010 459 clinic 1
1841 2442 86 clinic 2
1842 2659 202 clinic 2
1843 2739 164 clinic 2
@rasmusab
rasmusab / the-probability-my-son-will-be-stung-by-a-bumblebee.R
Created August 14, 2017 12:17
R and Stan script calculating the probability that my son will be stung by a bumblebee.
library(tidyverse)
library(purrr)
library(rstan)
### Defining the data ###
#########################
bumblebees <- c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0)
toddler_steps <- c(26, 16, 37, 101, 12, 122, 90, 55, 56, 39, 55, 15, 45, 8)
@rasmusab
rasmusab / fuzzycolor.R
Created June 18, 2016 10:11
A function that takes a vector of color names and matches it against the xkcdcolors list of color names using edit distance.
# fuzzycolor() takes a vector of color names and matches it against the
# xkcdcolors list of color names using edit distance. fuzzycolor() always
# returns a vector of hex color strings, perhaps the ones you wanted...
fuzzycolor <- function(color_names) {
library(xkcdcolors)
names_distance <- adist(color_names, xcolors(), ignore.case = TRUE, partial = TRUE)
xkcd_colors <- xcolors()[ apply(names_distance, 1, which.min) ]
hex_colors <- name2color(xkcd_colors)
names(hex_colors) <- xkcd_colors
hex_colors
@rasmusab
rasmusab / pystan_test_script.R
Created January 18, 2016 21:10
A short script you can use to test if pystan is installed and working correctly.
# Prior to the tutorial make sure that the script below runs without error on your python installation.
# What you need is a working installation of Stan: http://mc-stan.org/ .
# For installation instructions, see here:
# http://mc-stan.org/interfaces/pystan.html
# After installation you should be able to run this script which should output
# some summary statistics and some pretty plots, :)
# Fitting a simple binomial model using Stan
@rasmusab
rasmusab / rstan_test_script.R
Created January 18, 2016 20:54
A short script you can use to test if rstan is installed and working correctly.
# Prior to the tutorial make sure that the script below runs without error on your R installation.
# What you need is a working installation of Stan: http://mc-stan.org/ .
# For installation instructions, see here:
# https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
# After installation you should be able to run this script which should output
# some summary statistics and some pretty plots, :)
# Generating some fake data
set.seed(123)
@rasmusab
rasmusab / tensorflow_in_r_using_rpython.R
Last active April 25, 2018 02:35
An example of building a TensorFlow model from R using rPython
### An example of building a TensorFlow model from R using rPython ###
# For this script you need to
# 1. Have python 2.7 installed.
# 2. Install the rPython package in R.
# 3. Install Google's TensorFlow library as per these instructions:
# http://www.tensorflow.org/get_started/os_setup.md#binary_installation
### Here is how to setup and run a trivial TensorFlow model ###
# Load TensorFlow (I couldn't get this to work without setting sys.argv... )