Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
chrishanretty / ord_dirichlet.R
Created November 16, 2021 19:45
Likely faulty Ordered Dirichlet R/Stan code
library(tidyverse)
library(cmdstanr)
library(MCMCpack)
### log PDF is based on eqn. 3 in https://www2.seas.gwu.edu/~dorpjr/Publications/Bookchapter/MarcelDekker2003.pdf
stan_code <-
"
functions {
real orddir_lpdf(vector y, real[] alpha) {
int K = size(alpha);
library(countrycode)
library(tidyverse)
library(rio)
holder <- list()
### (0) PartyFacts lookup
pf <- read.csv("data/partyfacts-external-parties.csv")
### (1) Manifesto Project
@chrishanretty
chrishanretty / dontsmoothlineardgps.R
Created March 20, 2021 11:33
Smoothing and piecewise linear data-generating processes
### Let's generate some data with a step function
library(mgcv)
set.seed(1116)
nDays <- 200
day <- 1:nDays
y <- rnorm(200, 1, 0.25) + I(day > 100) * -0.5
y <- ifelse(y < 0,
@chrishanretty
chrishanretty / nor_distance.R
Created February 3, 2021 22:21
Distances to the Storting
library(sp)
library(raster)
library(viridis)
library(rgdal)
library(rnaturalearth)
### need two things, presumably
### - a polygon mask
### - a population raster
@chrishanretty
chrishanretty / replace_radar.R
Created February 2, 2021 17:10
Radar plot alternative
library(tidyverse)
### Pivot the table so we have four columns
### L/R, GAL/TAN, issue area, and value
dat <- read.csv("surridge1.csv") %>%
pivot_longer(cols = -c(Column1, Column2),
names_to = "Area")
### Tidy up the factor levels and order
dat <- dat %>%
@chrishanretty
chrishanretty / brm_multiple.R
Created January 27, 2021 21:16
Getting brm_multiple and future to play together
library(brms)
library(mice)
library(tictoc)
### Set a nice long number of iters so we can be sure it's not the
### overhead of spinning up cores, passing data, etc.,
nIters <- 1e5
thin <- nIters / 1000
# Load data
@chrishanretty
chrishanretty / towns_fund.R
Created September 24, 2020 19:44
Towns fund analysis
### Start by plotting stuff
library(tidyverse)
library(hrbrthemes)
## Download data from https://docs.google.com/spreadsheets/d/1inaI17l9Q-XADRNE_toXLp65sKu4_x4bXUFjZ3qjB2o/edit?usp=sharing
dat <- read.csv("wherever_i_saved_the_google_docs.csv")
### Estimate a simple model which we'll use when graphing
mod <- glm(I(Funded == "Yes") ~ Rank2 + ConWinner1,
family = binomial,
library(tidyverse)
library(rdrobust)
library(hrbrthemes)
library(cowplot)
library(Cairo)
dat <- readRDS("tidy_data.rds")
dat <- dat %>%
mutate(count_satisfied.20 = round(pct_satisfied.20 * response.20),
@chrishanretty
chrishanretty / lockdown_survey.json
Created June 11, 2020 13:16
Learning under lockdown survey
{
"class": "Survey",
"id": 609340,
"internal_title": "Learning under lockdown survey",
"l10n": "en_GB",
"runs": [
{
"class": "SurveyRun",
"alert_frequency": 0,
"alert_last_update": 0,
@chrishanretty
chrishanretty / crazy_lm.R
Created May 18, 2020 12:35
Am I going crazy?
dat <- read.csv("https://www.dropbox.com/s/e3p6xvjq6desua8/am_i_going_crazy.csv?dl=1")
holder <- list()
for (i in 1:20) {
holder[[i]] <- lm(lasso ~ ols, data = dat)
}
### For me,
lapply(holder, coef)
### gives the following. Note the intercepts in runs 1, 5 and 8 are radically different