Skip to content

Instantly share code, notes, and snippets.

View rasmusab's full-sized avatar

Rasmus Bååth rasmusab

View GitHub Profile
@rasmusab
rasmusab / chat-gtp-api-call.R
Last active May 8, 2025 08:17
How to call the ChatGTP API from R (in 2023-03-01)
# How to call the new (as of 2023-03-01) ChatGTP API from R
# Get your API key over here: https://platform.openai.com/
api_key <- "sk-5-your-actual-api-key-Fvau6" # Don't share this! 😅
library(httr)
library(stringr)
# Calls the ChatGTP API with the given promps and returns the answer
ask_chatgtp <- function(prompt) {
response <- POST(
@rasmusab
rasmusab / botty-app.html
Last active February 16, 2025 20:15
Bötty: a fun and friendly chatbot UI that uses the ChatGPT API.
<!--
This is a fun and friendly chatbot UI that uses the ChatGPT API.
It features
* A retro-styled CRT screen UI
* A ASCII face that changes based on the bot's current emotion
* Some light sound effects when the bot is responding
* Persistent chat history using localStorage
* All in one single HTML file
To get going you need a ChatGPT API key to assign to the `openai_api_key` variable.
@rasmusab
rasmusab / hello.Rnw
Created October 13, 2024 11:22
"Hello world" in R Sweave
\documentclass{article}
\begin{document}
<<echo=FALSE,results=tex>>=
cat('\\section{hi}\n')
@
<<echo=FALSE>>=
library(xtable)
@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 / make-iba-cocktails-pdf-gif-datatable.R
Created March 12, 2023 23:04
A couple of quick hacks that can be done with a scraped IBA cocktails dataset
# This script takes the scraped IBA cocktails dataset found here:
# https://github.com/rasmusab/iba-cocktails
# And produces a four page PDF with all the IBA cocktails and animated GIFs that
# loops through these, as well.
library(tidyverse)
library(glue)
library(ggtext)
library(gridExtra)
@rasmusab
rasmusab / bowling-t-test.R
Created February 20, 2023 16:45
A much improved bowling T-test in R
bowling_animation_urls <- c(
"0.01" = "https://i.imgur.com/Kn8CQbj.gif",
"0.05" = "https://i.imgur.com/HFTKdDL.gif",
"0.1" = "https://i.imgur.com/8Sw54Mz.gif",
"1" = "https://i.imgur.com/kjROdhj.gif"
)
# We need to download the animations to the session temp dirercorty to display
# them in the Rstudio Viever pane
bowling_animation_html <- sapply(bowling_animation_urls, function(url) {
@rasmusab
rasmusab / introduction_to_bayes_test_script.R
Last active May 2, 2023 17:16
Test script for "Introduction to Bayesian data analysis with R"
# 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 JAGS: http://mcmc-jags.sourceforge.net/
# and the rjags R package that can be installed from R by running
# install.packages("rjags")
# Generating some fake data
set.seed(123)
y <- rbinom(30, size = 1, prob = 0.2015)
# Fitting a simple binomial model using JAGS
# Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
1 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
2 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False
3 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False
3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False
4 Charmander Fire 309 39 52 43 60 50 65 1 False
5 Charmeleon Fire 405 58 64 58 80 65 80 1 False
6 Charizard Fire Flying 534 78 84 78 109 85 100 1 False
6 CharizardMega Charizard X Fire Dragon 634 78 130 111 130 85 100 1 False
6 CharizardMega Charizard Y Fire Flying 634 78 104 78 159 115 100 1 False
@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 / corr_jags.R
Created April 6, 2014 21:18
Correlation in rJAGS
library(rjags)
library(mvtnorm) # to generate correlated data with rmvnorm.
library(car) # To plot the estimated bivariate normal distribution.
set.seed(31415)
mu <- c(10, 30)
sigma <- c(20, 40)
rho <- -0.7
cov_mat <- rbind(c( sigma[1]^2 , sigma[1]*sigma[2]*rho ),
c( sigma[1]*sigma[2]*rho, sigma[2]^2 ))