Skip to content

Instantly share code, notes, and snippets.

@favstats
favstats / colorcoded-map-population-structures.R
Created March 26, 2018 19:36 — forked from ikashnitsky/colorcoded-map-population-structures.R
Code to reproduce the RGB map of the population structure of NUTS-3 regions of Europe -- https://ikashnitsky.github.io/2017/colorcoded-map/
################################################################################
#
# ikashnitsky.github.io 2017-06-30
# Produce an RGB coded map of pop structures at NUTS-3 level
# Ilya Kashnitsky, [email protected]
#
################################################################################
# load required packages
library(tidyverse) # data manipulation and viz
@favstats
favstats / social_blade_twitter.R
Created January 10, 2019 11:54
Twitter Followers per Day from Social Blade
## Helper function to preview ggplots
## thanks to @tjmahr for sharing!
ggpreview <- function (..., device = "png") {
fname <- tempfile(fileext = paste0(".", device))
ggplot2::ggsave(filename = fname, device = device, ...)
system2("open", fname)
invisible(NULL)
}
## install pacman if you don't have it
#' A pipable if statement
#'
#' This function allows to create an if statement that can be used within a pipable workflow
#'
#' @importFrom magrittr %>%
#' @importFrom rlang parse_expr
#' @param .data tibble
#' @param condition logical test
#' @param call a formula descibing a pipe to be evaluated if condition is \code{code}
#' @examples
library(dplyr)
do_if <- function(.data, condition, call){
if(condition){
.x <- .data
call_str <- call %>%
as.character %>%
.[2]
@favstats
favstats / intel_report.R
Last active December 4, 2019 00:09
Short code snippet that parses contact data included in the Trump-Ukraine Impeachment Inquiry Report
####-- Install/Load Packages --####
#install.packages("pacman")
pacman::p_load(tidyverse, tabulizer, lubridate)
####-- Get Intel Report and extract Tables --####
# Location of Intel Report pdf file
location <- 'https://intelligence.house.gov/uploadedfiles/20191203_-_full_report___hpsci_impeachment_inquiry_-_20191203.pdf'
# Install these packages if you don't have them yet
# if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, egg, gridExtra, grid, patchwork)
## Function to fix panel width and add "buffers" for right-alignment
## based on: https://gist.github.com/baptiste/eadeecd7d3756a7f294eb3cade8b0b17
facet_cleaner <- function(p, panel_size = 5, panel_height = 1) {
p %>%
egg::set_panel_size(
@favstats
favstats / measurement_invariance.R
Last active February 21, 2020 00:48
Function performs multiple group analysis to check for measurement invariance
library(tidyverse)
library(lavaan)
measurement_invariance <- function(model, data, group, fit_measures) {
## Configural Model
model1 <- cfa(model = model, data = data, group = group)
## Weak/metric invariance
model2 <- cfa(model = model, data = data, group = group, group.equal=c("loadings"))
## Strong/scalar invariance
library(tidyverse)
library(ggbeeswarm)
library(ggthemes)
library(glue)
elex <- read_csv("https://raw.githubusercontent.com/favstats/USElection2020-NYT-Results/master/data/latest/presidential.csv")
elex %>%
mutate(votes_perc = votes/sum(votes)) %>%
mutate(abs_margin2020 = abs(margin2020)) %>%
paadc_ids_2021_01_22 <- old_data%>%
# distinct(paadc) %>%
mutate(impressionTime = lubridate::ymd_hms(impressionTime)) %>%
group_by(paadc) %>%
filter(impressionTime == min(impressionTime)) %>%
ungroup() %>%
distinct(paadc, .keep_all = T) %>%
arrange(impressionTime) %>%
mutate(firstImpressionTime = as.character(impressionTime)) %>%
select(paadc, firstImpressionTime) %>%
mutate(type = case_when(
type == "Regio" ~ "Local Politics",
type == "Rest" ~ "Party Youth Organization/Dutch Politicians (Other)",
type == "Kandidaat" ~ "Dutch Politician",
type == "Landelijk" ~ "Dutch National Party"
))