Skip to content

Instantly share code, notes, and snippets.

@tor-gu
tor-gu / model_with_points_removed.py
Created November 7, 2023 04:57
Find optimal points to remove so that a model fits better.
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import LinearConstraint, minimize
np.random.seed(3737)
LO, HI = 0, 100
n = 100 # Number of points
k = 73 # Points to remove
@tor-gu
tor-gu / election_maps.R
Created August 25, 2022 19:04
Two party share of vote in NJ maps
library(tidyverse)
library(tigris)
library(njmunicipalities)
library(njelections)
county_names <- njmunicipalities::counties |> pull(county)
# Get the map of NJ
options(tigris_use_cache = TRUE)
nj_municipality_map <- county_names %>%
map_df( ~ county_subdivisions("NJ", county = ., class = "sf")) |>
@tor-gu
tor-gu / cf_anim.R
Created August 1, 2022 18:38
Animation of frequency of digits in continued fractions of sqrt(n)
library(tidyverse)
library(future)
library(furrr)
library(gganimate)
plan(multisession, workers = 12)
cf_init <- function(D) {
r = as.integer(floor((sqrt(D))))
list(
D = D, r = r, a = r, b = r, c = D - r ^ 2
@tor-gu
tor-gu / build_combined_table.R
Created July 22, 2022 14:29
Build nj_municipality map and combined_table for use of force data
# Tidyverse packages
library(dplyr)
library(magrittr)
library(purrr)
# Maps and census libraries
# *** Note: CENSUS API Key required ***
library(sf)
library(tidycensus)
library(tigris)
@tor-gu
tor-gu / video_footage_map.R
Created March 17, 2022 15:35
Video footage of use of force incidents in NJ counties
library(tidyverse)
library(lubridate)
library(njoaguof)
# Get number proportion of incidents with body worn camera or any video
# by county and by 4-month interval
# devtools::install_github("tor-gu/njoaguof")
video_by_county <- incident %>%
left_join(incident_video_type) %>%
select(form_id,
@tor-gu
tor-gu / force_type_map.R
Last active March 17, 2022 16:03
Per capita use of force by type in NJ counties
library(tidyverse)
# Get number of incidents of selected types of force by county
# devtools::install_github("tor-gu/njoaguof")
library(njoaguof)
force_type_by_county <- incident %>%
right_join(incident_subject_force_type) %>%
mutate(
force_type =
fct_collapse(