Skip to content

Instantly share code, notes, and snippets.

View christophergandrud's full-sized avatar

Christopher Gandrud christophergandrud

View GitHub Profile
@christophergandrud
christophergandrud / source_lines.R
Created December 1, 2014 14:08
Source specific lines from a file in R
#' Source specific lines in an R file
#'
#' @param file character string with the path to the file to source.
#' @param lines numeric vector of lines to source in \code{file}.
source_lines <- function(file, lines){
source(textConnection(readLines(file)[lines]))
}
@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 / FillIn.R
Last active June 20, 2022 11:59
FillIn: an R function for filling in missing values of a variable from one data frame with the values from another variable. The function also prints the correlation coefficient between the two variables. You will need to have the data.table package installed to run FillIn.
#' An R function for filling in missing values of a variable from one data frame with the values from another variable.
#'
#' \code{FillIn} uses values of a variable from one data set to fill in missing values in another.
#'
#' @param D1 the data frame with the variable you would like to fill in.
#' @param D2 the data frame with the variable you would like to use to fill in \code{D1}.
#' @param Var1 a character string of the name of the variable in \code{D1} you want to fill in.
#' @param Var2 an optional character string of variable name in \code{D2} that you would like to use to fill in.
#' @param KeyVar a character vector of variable names that are shared by \code{D1} and \code{D2} that can be used to join the data frames.
#'
@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
@christophergandrud
christophergandrud / topicmodels_json_ldavis.R
Last active March 13, 2021 19:00
Convert the output of a topicmodels Latent Dirichlet Allocation model to JSON for use with LDAvis
#' Convert the output of a topicmodels Latent Dirichlet Allocation to JSON
#' for use with LDAvis
#'
#' @param fitted Output from a topicmodels \code{LDA} model.
#' @param corpus Corpus object used to create the document term
#' matrix for the \code{LDA} model. This should have been create with
#' the tm package's \code{Corpus} function.
#' @param doc_term The document term matrix used in the \code{LDA}
#' model. This should have been created with the tm package's
#' \code{DocumentTermMatrix} function.
"""
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>
"""
"""
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 / 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,