Skip to content

Instantly share code, notes, and snippets.

View rasmusab's full-sized avatar

Rasmus Bååth rasmusab

View GitHub Profile
@rasmusab
rasmusab / r_markdown_example.Rmd
Created March 25, 2014 22:19
R markdown example
Inlämningsuppgifter SM 1 & 2
========================================================
Rasmus Bååth, Sumsar Htååb
Detta är ett exempeldokumen skrivet med R markdown.
R markdown-dokumentet måste vara fristående från din nuvarande R session.
T.ex. så måste du i R markdown dokumentet alltid ladda in de paket du vill
använda även om de redan är inladdade i din nuvarande R-session:
```{r message=FALSE}
@rasmusab
rasmusab / mario_beep
Last active August 29, 2015 14:04
Defines a function that plays the Mario theme while an experssion is executing (Linux only)
# The code below will probably only work on Linux and requires VLC on the path.
library(beepr)
library(stringr)
# Plays a file with vlc and returns the vlc instance's PID
play_vlc_pid <- function(fname) {
system(paste("vlc -Idummy --no-loop --no-repeat --playlist-autostart --no-media-library --play-and-exit", fname),
ignore.stdout = TRUE, ignore.stderr=TRUE,wait = FALSE)
ps_out <- system("ps -eo pid,comm,etime| grep vlc", intern=TRUE)
### Function for generating the posterior of Robo's position ###
landscape <- c(rep("plain", 50), rep("mountain", 25), rep("forest", 45))
landscape_color <- c(mountain = "black", forest = "green", plain = "yellow")
cover_cost <- c(mountain = 10, forest = 5, plain = 1)
posterior_sample <- function(n) {
dist_i <- sample(3, n, replace = TRUE, c(18, 17, 65))
mu <- c(15, 40, 90)
@rasmusab
rasmusab / lin_reg_speed_comparison.R
Last active August 29, 2015 14:17
A Speed Comparison Between Flexible Linear Regression Alternatives in R.
library(microbenchmark)
library(arm)
library(rstan)
library(bbmle)
log_post <- function(par, y, x) {
sigma <- exp(par[1])
intercept <- par[2]
beta <- as.matrix(par[-c(1,2)])
mu <- intercept + x %*% beta
@rasmusab
rasmusab / chess_pre_analysis.R
Created June 2, 2015 00:21
Takes the matrix created by this script: https://gist.github.com/rasmusab/fb98cced046d4c675d74 and calculates some statistics and fits some models to it.
# Takes the matrix created by this script: https://gist.github.com/rasmusab/fb98cced046d4c675d74
# and calculates some statistics and fits some models to it. All this is pretty memory heavy so saving
# the interesting parts using saveRDS so the script only need to be run once.
set.seed(123)
games <- as.data.frame(readRDS("milionbase_matrix.rds"))
# Saving some info
n_games <- max(games[,"game_id"])
fullmoves_white <- games[ games[, "fullmoves"] < 100 & games[,"active_player"] == 1, "fullmoves"]
@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 / 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 / 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)
```