Skip to content

Instantly share code, notes, and snippets.

View rasmusab's full-sized avatar

Rasmus Bååth rasmusab

View GitHub Profile
@rasmusab
rasmusab / create_chess_plots.R
Created June 2, 2015 00:27
Creates some plots from the model fits created by this script: https://gist.github.com/rasmusab/b29bb53cfc3fe25f3f80
library(ggplot2)
library(plyr)
n_games <- readRDS("n_games.Rdata")
n_positions <- readRDS("n_positions.Rdata")
fullmoves_white <- readRDS("fullmoves_white.Rdata")
fits <- readRDS("chess_fits.Rdata")
fit <- fits$fit
fit <- fit
# From https://aschinchon.wordpress.com/2015/06/23/the-2d-harmonograph-in-shiny/
# All code by Antonio S. Chinchón (@aschinchon), just rearranged so that you can directly
# run it by copy and pasting it into an R console.
library(shiny)
CreateDS = function () {
f=jitter(sample(c(2,3),4, replace = TRUE))
d=runif(4,0,1e-02)
p=runif(4,0,pi)
xt = function(t) exp(-d[1]*t)*sin(t*f[1]+p[1])+exp(-d[2]*t)*sin(t*f[2]+p[2])
@rasmusab
rasmusab / easy-bayesian-bootstrap-in-r.Rmd
Created July 14, 2015 10:56
The R markdown file behind my blog post on "Easy Bayesian Bootstrap in R": http:///www.sums.net/blog/2015/07/easy-bayesian-bootstrap-in-r/
---
title: "Easy Bayesian Bootstrap in R"
author: "Rasmus"
date: "04/06/2015"
output:
html_document:
keep_md: yes
self_contained: no
---
@rasmusab
rasmusab / MCMCDEMO.BAS
Created July 20, 2015 15:54
An implementation of the Metropolis-Hastings algorithm and a Bayesian model in Microsoft QBasic 1.1 (but written in a more old-school BASIC style). More info at http://www.sumsar.net
0 CLS
10 REM SETTING UP THE VARIABLES AND DATA
20
30 REM NO OF PUPS PER DEN FROM A SAMPLE OF 16 WOLF DENS
40 DATA 5, 8, 7, 5, 3, 4, 3, 9, 5, 8, 5, 6, 5, 6, 4, 7
50
60 NDATA = 16
70 NSAMPLES = 1000
75 NBURNIN = 250
80 NPARAMS = 2
@rasmusab
rasmusab / probability_of_pregnancy.R
Created November 5, 2015 19:46
A script that implements a Bayesian model calculating the probability that a couple is fertile and is going to be pregnant.
# A Bayesian model that calculates a probability that a couple is fertile
# and pregnant. Please use this for fun only, not for any serious purpose
# like *actually* trying to figure out whether you are pregnant.
# Enter your own period onsets here:
period_onset <- as.Date(c("2014-07-02", "2014-08-02", "2014-08-29", "2014-09-25",
"2014-10-24", "2014-11-20", "2014-12-22", "2015-01-19"))
# If you have no dates you can just set days_between_periods to c() instead like:
# days_between_periods <- c()
days_between_periods <- as.numeric(diff(period_onset))
@rasmusab
rasmusab / a-bayesian-model-to-calculate-whether-my-wife-is-pregnant.Rmd
Last active February 12, 2017 00:57
The R Markdown file that was the basis of my blog post: A Bayesian Model to Calculate whether my Wife is Pregnant or Not
---
title: "A Bayesian Model to Calculate Whether My Wife is Pregnant or Not"
author: "Rasmus Bååth"
output: html_document
---
```{r echo = FALSE}
library(knitr)
options(digits=2)
```
@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... )
@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 / 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 / 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