Skip to content

Instantly share code, notes, and snippets.

View briatte's full-sized avatar

François Briatte briatte

View GitHub Profile
@elliottmorris
elliottmorris / election_night_live_model.R
Last active January 29, 2024 18:56
A live election-night prediction model using The Economist's pre-election forecast
#' Description
#' This file runs a live election-night forecast based on The Economist's pre-election forecasting model
#' available at projects.economist.com/us-2020-forecast/president.
#' It is resampling model based on https://pkremp.github.io/update_prob.html.
#' This script does not input any real election results! You will have to enter your picks/constraints manually (scroll to the bottom of the script).
#'
#' Licence
#' This software is published by *[The Economist](https://www.economist.com)* under the [MIT licence](https://opensource.org/licenses/MIT). The data generated by *The Economist* are available under the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
#' The licences include only the data and the software authored by *The Economist*, and do not cover any *Economist* content or third-party data or content made available using the software. More information about licensing, syndication and the copyright of *Economist* content can be fou
@joelgombin
joelgombin / valeurs_nulles.R
Last active May 6, 2020 16:36
Compter tous les nulls et autres d'un JDD
library(tidyverse)
base <- read_csv("./Documents/Base_menage-extrait.csv")
is_null_vector <- function(x) purrr::map_lgl(x, ~ is.null(.))
base %>%
summarise(across(.fns = list(NAs = ~ sum(is.na(.)),
NULLs = ~ sum(is_null_vector(.) | . %in% "NULL"),
empties = ~ sum(. %in% "")))) %>%
@chrishanretty
chrishanretty / tv2019.R
Created December 15, 2019 15:03
Tactical voting site analysis
### Load libraries
library(tidyverse)
library(rio)
library(rdrobust)
library(hrbrthemes)
## Data from https://docs.google.com/spreadsheets/d/1uNdRzf5-IqnSwNCPCD8eTxsBKBEEaTmlvq4eP_pt7JI/edit?usp=sharing
dat <- read.csv("data.csv")
### Graph things
p1 <- ggplot(dat, aes(x = LD17, y = LD,
#===============================================================================
# 2019-07-19-- ikashnitsky.github.io
# Reproduce Figure 2 from http://doi.org/10.1007/s10708-018-9953-5
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com
#===============================================================================
library(tidyverse)
library(hrbrthemes); import_roboto_condensed()
# the data as tribble
@tslumley
tslumley / svyivreg.R
Created July 15, 2019 01:04
survey-weighted two-stage least squares for instrumental variables
library(AER)
svyivreg<-function(formula, design, ...) UseMethod("svyivreg",design)
svyivreg.survey.design<-function(formula, design){
.data<-model.frame(design)
.data$.weights<-weights(design,"sampling")
model<- ivreg(formula, data=.data, weights=.weights)
library(cartography)
library(sp)
# Load data
data(nuts2006)
# Get a SpatialLinesDataFrame of countries borders
nuts0.contig.spdf <- getBorders(nuts0.spdf)
# Get the GDP per capita
nuts0.df$gdpcap <- nuts0.df$gdppps2008/nuts0.df$pop2008*1000000
@amandeepjutla
amandeepjutla / dark_base16-tomorrow-night.rstheme
Last active April 26, 2020 19:29
RStudio theme: dark UI with base16 colors.
/* rs-theme-name: Dark UI Base16 Tomorrow Night */
/* rs-theme-is-dark: TRUE */
/* Dark UI from Randy3k's Wombat (https://github.com/randy3k/dotfiles/blob/master/.R/rstudio/themes/Wombat.rstheme) */
/* "Tomorrow night" color scheme adapted from chriskempson's Base16 (https://github.com/chriskempson/base16). */
.body {
background: #ffffff;
}
@dgrtwo
dgrtwo / dice-rolls.R
Created February 25, 2019 23:56
Animation of die rolls
# Code behind this tweet: https://twitter.com/drob/status/1100182329350336513
library(tidyverse)
library(gganimate)
# Setup
options(gganimate.nframes = 200)
set.seed(2019)
simulation <- tibble(roll = 1:10000) %>%
mutate(result = sample(6, n(), replace = TRUE)) %>%
---
title: "World maps"
output:
html_document:
df_print: paged
---
```{r echo = FALSE, message = FALSE}
library(tidyverse)
library(sf)
@expersso
expersso / pairs_plot.R
Created October 29, 2018 16:26
Create scatterplot matrix using the tidyverse
library(tidyverse)
library(patchwork)
plot_pair <- function(data, x, y) {
ggplot(data, aes_string(x = x, y = y, color = "Species", shape = "Species")) +
geom_point() +
scale_color_brewer(palette = "Dark2") +
theme(legend.position = "none", plot.title = element_text(size = 7)) +
labs(x = NULL, y = NULL, title = paste0(y, " ~ ", x))