Skip to content

Instantly share code, notes, and snippets.

View RobertMyles's full-sized avatar
👽

Robert Myles McDonnell RobertMyles

👽
View GitHub Profile
@RobertMyles
RobertMyles / Load-Multiple-R-Packages.R
Created July 8, 2016 19:19
Load multiple packages at the same time in R
# library() or require() only load one package at a time
# but...
Packages <- c("dplyr", "ggplot2", "rstan", "readr")
lapply(Packages, library, character.only = TRUE)
# this loads as many as you put in 'Packages'. They need to be installed first, of course.
@RobertMyles
RobertMyles / gganimate_beta_simulation.R
Created August 31, 2016 14:03 — forked from mrecos/gganimate_beta_simulation.R
Code for a ggplot2/gganimate version of Cory's Bayesian updating animation: https://gist.github.com/cjbayesian/3373348
# packages
library("ggplot2")
# devtools::install_github("dgrtwo/gganimate")
library("gganimate")
library("dplyr")
# set distribution parameters for simulation
p = 0.5
N = 100
y_lim = 20
a_a = 2
@RobertMyles
RobertMyles / garden plots.R
Created June 9, 2019 10:50 — forked from rmcelreath/garden plots.R
Code for drawing the forking data gardens in Chapter 2 of "Statistical Rethinking" textbook
# functions for plotting garden of forking data plots
library(rethinking)
polar2screen <- function( dist, origin, theta ) {
## takes dist, angle and origin and returns x and y of destination point
vx <- cos(theta) * dist;
vy <- sin(theta) * dist;
c( origin[1]+vx , origin[2]+vy );
}
# extracted from http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip
# under public domain terms
country_bounding_boxes = {
'AF': ('Afghanistan', (60.5284298033, 29.318572496, 75.1580277851, 38.4862816432)),
'AO': ('Angola', (11.6400960629, -17.9306364885, 24.0799052263, -4.43802336998)),
'AL': ('Albania', (19.3044861183, 39.624997667, 21.0200403175, 42.6882473822)),
'AE': ('United Arab Emirates', (51.5795186705, 22.4969475367, 56.3968473651, 26.055464179)),
'AR': ('Argentina', (-73.4154357571, -55.25, -53.628348965, -21.8323104794)),
'AM': ('Armenia', (43.5827458026, 38.7412014837, 46.5057198423, 41.2481285671)),
@RobertMyles
RobertMyles / get_all_sentate_votes.R
Created April 25, 2017 22:00
Get all votes over a long time period from the Brazilian Federal Senate API, which only allows requests for 60 days.
library(congressbr)
Seq <- seq(as.Date("1991-02-01"), to = as.Date("2018-12-31"),
by = 60)
Seq2 <- seq(as.Date("1991-03-01"), to = as.Date("2019-01-31"),
by = 60)
seq_m <- data.frame(date = Seq, end_date = Seq2)
seq_m$date <- gsub("-", "", seq_m$date)
seq_m$end_date <- gsub("-", "", seq_m$end_date)
@RobertMyles
RobertMyles / plot.BEST_2.R
Last active February 8, 2017 14:34
Modify R package BEST's plotPost function to include other font family for Brazilian Political Science Review research note
plot.BEST_2 <-
function(x, which=c("mean", "sd", "effect", "nu"), credMass=0.95,
ROPE=NULL, compVal=0, showCurve=FALSE, ...) {
# This function plots the posterior distribution for one selected item.
# Description of arguments:
# x is mcmc.list object of the type returned by function BESTmcmc.
# which indicates which item should be displayed; possible values are "mean", "sd",
# "effect" or "nu".
# ROPE is a two element vector, such as c(-1,1), specifying the limit
# of the ROPE.
@RobertMyles
RobertMyles / plotPost_2.R
Last active February 8, 2017 14:32
Modify R package BEST's plotPost function to include other font family for Brazilian Political Science Review research note
# modified by R. McDonnell. Original at: https://github.com/cran/BEST/blob/master/R/plotPost.R
# Original code by John Kruschke, modified by Mike.
plotPost_2 <-
function( paramSampleVec, credMass=0.95, compVal=NULL, ROPE=NULL,
HDItextPlace=0.7, showMode=FALSE, showCurve=FALSE, ... ) {
# Does a plot for a single parameter. Called by plot.BEST but also exported.
# Returns a histogram object invisibly.
@RobertMyles
RobertMyles / brexit.R
Last active February 5, 2017 20:55
code for tidying up spatial UK data, for a blog post here:
library(rgdal)
library(maptools)
library(dplyr)
library(ggplot2)
library(readr)
library(httr)
library(XML)
library(ggmap)
library(scales)
@RobertMyles
RobertMyles / emojis.R
Created February 4, 2017 12:56
little script to see what emojis are available with the emojifont package in R.
library(rvest)
library(stringr)
library(emojifont)
library(tidyverse)
url <- "http://www.webpagefx.com/tools/emoji-cheat-sheet/"
page <- read_html(url)
nodes <- html_nodes(page, css = "#content")
text <- html_text(nodes)
txt <- data_frame(label = unlist(str_extract_all(text, ":[a-z_]*:"))) %>%
@RobertMyles
RobertMyles / LostPackages.R
Last active January 10, 2017 19:23
Updated R today and realized that I lost a load of packages that I had installed previously. Instead of re-installing them one by one, I just did this (it works for Mac OS, it'll be slightly different on Windows):
Old_packList <- as.list(installed.packages("/Library/Frameworks/R.framework/Versions/3.2/Resources/library"))
# here, my R version was 3.2. Change it for the R version you want the packages from!
Old <- as.character(Old_packList)
install.packages(Old)