View election_night_live_model.R
#' 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 |
View valeurs_nulles.R
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% "")))) %>% |
View tv2019.R
### 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, |
View dotplot-plus-manual-legend.R
#=============================================================================== | |
# 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 |
View svyivreg.R
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) |
View disc2019.R
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 |
View dark_base16-tomorrow-night.rstheme
/* 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; | |
} |
View dice-rolls.R
# 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)) %>% |
View world_maps.Rmd
--- | |
title: "World maps" | |
output: | |
html_document: | |
df_print: paged | |
--- | |
```{r echo = FALSE, message = FALSE} | |
library(tidyverse) | |
library(sf) |
View pairs_plot.R
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)) |
NewerOlder