Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
@carlislerainey
carlislerainey / comparing.R
Created February 12, 2024 21:06
Comparing OLS w/ interactions to RE w/ and w/o interactions with oversight data
# load packages
library(tidyverse)
library(marginaleffects)
library(rstanarm); options(mc.cores = parallel::detectCores())
# load data
data <- read_rds("https://www.dropbox.com/s/9grn8kkb5yzwagx/data.rds?raw=1") %>%
filter(passed_mvc1) %>%
glimpse()
@carlislerainey
carlislerainey / animated-sharing.R
Last active January 31, 2024 11:36
Code for animating the figure on data sharing in political science
# load packages
library(tidyverse)
library(ggrepel)
library(gganimate)
# set ggplot options
theme_set(theme_bw(base_family = "Gill Sans"))
update_geom_defaults("label", list(family = theme_get()$text$family))
update_geom_defaults("text", list(family = theme_get()$text$family))
@carlislerainey
carlislerainey / list-exit-pwr-analysis. R
Created November 16, 2023 13:33
Power analysis for list experiment
# load packages
library(tidyverse)
library(marginaleffects)
library(patchwork)
# clear workspace
rm(list = ls())
@carlislerainey
carlislerainey / test.R
Created November 6, 2023 16:22
{marginaleffects} Question: I feel like these two should give me the same answer...
# load packages
library(tidyverse)
library(marginaleffects)
devtools::install_github("carlislerainey/crdata")
# load data
cg <- crdata::cg2006
# fit model
@carlislerainey
carlislerainey / simulate-and-recover-zinb.R
Created October 24, 2023 18:52
Simulate and recover for ZINB
# create explanatory variables
n <- 500000
x1 <- runif(n)
x2 <- rnorm(n)
x3 <- rbinom(n, size = 1, prob = 0.5)
# negative binomial part
mu <- exp(-1 + 1*x1 - 0.5*x2 + 0.5*x3)
phi <- 3
@carlislerainey
carlislerainey / replication.R
Created October 11, 2023 20:02
Example Replication Script for Adjustment Project
# clear workspace
rm(list = ls())
# Ainsley and Elke's portion
# PART 1: Add Study Metadata
# -----------------------------
author_year <- "Noh, Grewal, and Kilavuz (2023)"
doi_url <- "http://dx.doi.org/10.1017/s000305542300059x"
@carlislerainey
carlislerainey / mean-vs-median-t3.R
Created September 17, 2023 18:18
Compare mean and median as estimators of location of t3 distribution
# parameters
n_samples <- 1000 # number of samples
sample_size <- 10 # size of each sample
# container
means <- numeric(n_samples)
medians <- numeric(n_samples)
# simulation
for (i in 1:n_samples) {
rm(list = ls())
library(tidyverse)
library(patchwork)
## -------------------------------
## don't change things in this box
# these are known quantities from Ahler and Sood
@carlislerainey
carlislerainey / refine-dois.R
Created September 8, 2023 13:40
Code to refine DOIs using title and abstract string matches
# load the required libraries
library(tidyverse)
read_and_clean <- function(path) {
read_csv(path) %>%
# throw out all dois without "experiment" in the abstract or title
filter(str_detect(abstract, "(?i)Experiment") |
str_detect(title, "(?i)Experiment") ) %>%
# flag all dois with "survey experiment" in the abstract
mutate(svy_expt_flag = str_detect(abstract, "(?i)survey experiment")) %>%
@carlislerainey
carlislerainey / get-all-dois.R
Created September 8, 2023 13:38
Code to collect DOIs from journals
library(tidyverse)
make_call <- function(eissn) {
crossref_api_call <- rcrossref::cr_journals(issn = eissn,
works = TRUE,
limit = 1000,
cursor = "*",
cursor_max = 1000000,