Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / README.md
Last active July 20, 2023 23:46
Bash script to create an empty Streamlit app

Bash script to create a skeleton Streamlit app

To run the script from the command line use:

curl -s https://gist.githubusercontent.com/christophergandrud/8f467beaf72a0ae1f417c3e8364c0039/raw/774b35818c72575e42aa703f472ee43cbc9d4c79/streamlit-build.sh -o script.sh && chmod +x script.sh && ./script.sh && rm script.sh
@christophergandrud
christophergandrud / war.py
Last active July 18, 2021 05:23
Create the card game "War" where the player with the most cards at the end of 1 round wins
"""
Create the card game "War"
where the player with the most cards at the end of 1 round wins
(written using GitHub Copilot)
"""
# Create a range with max from len(x)
def range_len(x) :
"""
Return the range of the length of x
#' Multi-core replicate. From the rethinking package:
#' <https://github.com/rmcelreath/rethinking/blob/3b48ec8dfda4840b9dce096d0cb9406589ef7923/R/utilities.r#L206>
#'
#' @param n integer: the number of replications.
#' @param expr the expression (a language object, usually a call) to evaluate repeatedly.
#' @param refresh status update refresh interval
#' @param mc.cores number of cores to use
#'
#' @importFrom parallel mclapply
#' @export
"""
Functions for recursively calculating mean and variance and comparing
comparing to full sample calculated mean and variance.
Functions based on:
- Chou (2021, 5) <https://arxiv.org/pdf/2102.03316.pdf>
- Chan et al (1983) <http://www.cs.yale.edu/publications/techreports/tr222.pdf>
"""
@christophergandrud
christophergandrud / covid-vaccine-growth-rates.R
Created February 20, 2021 09:45
Covid-19 Vaccination Program Growth Rates
# ---
# title: "Covid-19 Vaccination Program Growth Rates"
# author: "Christopher Gandrud"
# date: "2021-02-21"
# ---
# For hrbragg installation instructions see:
# https://git.rud.is/hrbrmstr/hrbragg#installation
xfun::pkg_attach2("hrbragg", "tsibble", "tidyverse")
@christophergandrud
christophergandrud / robustness_value.jl
Created February 4, 2021 09:14
Find the Cinelli and Hazlett (2020) robustness value for a generalised linear model.
"""
robustness_value(;fit::StatsModels.TableRegressionModel, treatment::String = "D", q::Int64 = 1)
Find the Cinelli and Hazlett (2020) robustness value for a generalised linear model.
The response will be a in the range 0 and 1. Values closer to 0 indicate that
the conclusions from the original model are highly subject to omitted variable
bias. Values closer to 1 indicate that the original model is less likely to be
caused by omitted variable bias.
"""
function robustness_value(;fit::StatsModels.TableRegressionModel,
"""
ebt_sampler(total_questions::Int = 310)
Create question set to practice for the Deutscher Einbürgerungstest.
The official question set can be found at: <http://oet.bamf.de/pls/oetut/f?p=514:1:329473569276328:::::>.
The question set has 310 questions, but who knows, maybe this could change.
Adjust the total question set with `total_questions::Int`.
"""
function ebt_sampler(total_questions::Int = 310)
i::Int = -9; u::Int = 0
out = zeros(Int, 0)
@christophergandrud
christophergandrud / create_lags_diffs.R
Created March 28, 2020 17:05
Create lag and difference variables for a grouped time series (grouped by a variable called "country")
# Create lag and difference variables for a grouped time series (grouped by a variable called "country")
#
# @param df a data frame containing the variable to lag and lead
# @param x a column name in `df` to lag and lead
# @param lag integer period to lag and difference
#
# @importFrom tsibble difference
# @importFrom dplyr %>% lag
create_lags_diffs <- function(df, x, lag = 1) {
@christophergandrud
christophergandrud / simulate_power_distributed_discrete.R
Created January 12, 2020 10:56
Simulate data from discrete power law distribution in parallel
library(xfun)
pkg_attach2("tidyverse", "poweRlaw", "furrr")
plan(multiprocess)
# Simulate from a discrete power law
# @param counter numeric, to enable mapping
# @param n integer, number of draws
# @param xmin numeric, minimum value subject to power law
# @param alpha, scaling parameter
Y X Z A
1.41349020936189 Signup 3.76363446527701 0
-1.19511457323204 No Signup -3.81402465182499 1
1.87668885146666 Signup 0.712012451131932 1
1.8385401566156 No Signup -0.774724331043617 1
3.02728268507929 Signup 3.25513469702395 1
2.88424256922274 Signup -0.863068694934284 1
-0.387965283366912 No Signup -2.59568803608236 0
0.577546833602056 Signup -0.277480406497884 0
2.72670763084368 Signup 0.181542158647839 1