Skip to content

Instantly share code, notes, and snippets.

View jackobailey's full-sized avatar

Jack Bailey jackobailey

View GitHub Profile
@jackobailey
jackobailey / gist:64a6a625b52d6f6885b112905f90037e
Last active May 12, 2022 15:15
bloc weighted cleavage salience index
#' Compute the Block-WeightedCleavage Saliance Index
#'
#'
#'
#' @param data A crosstab of cell percentages.
#' @param cleavage A character vector containing the names of the parties in the cleavage bloc.
#' @param opposition A character vector containing the names of the parties in the opposition bloc.
#' @param elec_1 The name of the column containing vote shares at time 1.
#' @param elec_2 The name of the column containing vote shares at time 2.
#' @return A numeric vector
@jackobailey
jackobailey / gist:6fdc627485e3e3b469f4aa963db1d9c2
Last active July 24, 2020 13:14
AMEs for continuous variables and categorical outcomes
bayes_dydx.default_mn <- function(model, data = NULL, variable, stepsize = 1e-7, re_formula = NULL){
# Check that everything is running properly and that the
# user has provided all of the relevant information.
if(is.null(model) == T){
stop("Please provide a model to the function using the 'model =' argument (e.g. model = m1)")
} else if(is.null(variable) == T){
@jackobailey
jackobailey / gist:0d29d2f2229cfe9d83c1672488308882
Created July 24, 2020 10:20
Predictive margins for categorical models
bayes_margin.factor_mn <- function(model, variable = NULL, data = NULL, draws = NULL, n = NULL, re_formula = NA){
# Check that everything is running properly and that the
# user has provided all of the relevant information.
if(is.null(model) == T){
stop("Please provide a model to the function using the 'model =' argument (e.g. model = m1)")
} else if(is.null(variable) == T){
@jackobailey
jackobailey / gist:758df4a4c853ab4aab2389c32c5654c7
Created May 11, 2020 09:04
AMEs for categorical brms models
bayes_dydx.factor_mn <- function(model, variable = NULL, data = NULL, draws = NULL, n = NULL, re_formula = NA){
# Check that everything is running properly and that the
# user has provided all of the relevant information.
if(is.null(model) == T){
stop("Please provide a model to the function using the 'model =' argument (e.g. model = m1)")
# Load packages using the 'librarian' package
librarian::shelf(tidyverse,
tidybayes,
brms,
here)
# Create function to simulate the outcome scale for arbitrary
# prior distributions
rlikert <- function(n = 1e3,
t1 = -0.84,
# Load packages
library(brms)
library(tidybayes)
library(tidyverse)
library(magrittr)
library(reshape2)
library(margins)
# Function 1: Compute numerical derivatives for continuous variables ----------
bayes_dydx.default <- function(model, data = NULL, variable, stepsize = 1e-7, re_formula = NULL){
@jackobailey
jackobailey / gist:e819f8b358b676acad79dee8bfde0039
Created July 25, 2019 07:58
AMEs for Continuous Variables
# Install and load packages
librarian::shelf(brms, tidybayes, tidyverse, magrittr, margins)
# Create "posterior_ame" function
posterior_ame <- function(model, variable, eps = 1e-7){
# Get model outcome
resp <- model$formula$resp
# Omit outcome variable from data
# Load packages
library(tidyverse)
library(brms)
library(broom)
# We need to simulate data from an ordinal distribution. This is hard.
# Unlike many distributions, treatment effects can affect the mean *and*
# the sd of the latent y* distribution that underlies the ordinal distribution
# (see Liddell & Kruschke (2018) and Bürkner & Vuorre (2019))
@jackobailey
jackobailey / UoM_Harvard.csl
Last active August 3, 2018 08:40 — forked from jbhaddow/mbsharvard1.1.csl
University of Manchester Harvard Reference Format
<?xml version="1.0" encoding="UTF-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="sort-only">
<info>
<title>Manchester Business School Harvard Reference format 1.1 (Author-Date)</title>
<id>http://www.zotero.org/styles/mbsharvard1.1</id>
<link href="http://www.zotero.org/styles/mbsharvard1.1" rel="self"/>
<author>
<name>James Haddow</name>
<email>jbh78@me.com</email>
</author>