Skip to content

Instantly share code, notes, and snippets.

@zilinskyjan
zilinskyjan / comedians-freespeech-tum-oxford.do
Created September 24, 2025 13:43
summary stats (free speech dataset)
* Binary agreement with "Comedians should be allowed to say what they want without any restrictions."
gen q25r7_bin = (q25r7 >=4) if !mi(q25r7)
* Generate party ID labels/variables
gen Democrat = (q6a <=3)
gen Republican = (q6a ==5 | q6a ==6 | q6a ==7 )
tabstat q25r7_bin [aw=WEIGHT] if Democrat==1
tabstat q25r7_bin [aw=WEIGHT] if Republican==1
@zilinskyjan
zilinskyjan / R-bar-graph-padding.R
Created August 27, 2025 14:43
Make sample bar bplots
# The expand parameter controls the padding around your data on each axis.
# By default, ggplot2 adds approximately 5% padding on each side of your data range to ensure data points don't touch the plot boundaries.
# Setting expand = c(0, 0) removes this padding entirely:
library(patchwork)
data <- data.frame(
category = c("Category A", "Category B", "Category C"),
value = c(25, 40, 30)
)
@zilinskyjan
zilinskyjan / CHES_ukr.R
Last active February 2, 2024 19:05
plot CHES party positions
library(tidyverse)
library(haven)
# Get data from https://www.chesdata.eu/ches-europe
## Look for: Special Edition Chapel Hill Expert Surveys (SPEED CHES) - 2023 SPEED CHES - Ukraine
uk <- read_dta("CHES_Ukraine_Feb2024.dta")
# Or run:
# uk <- read_csv("https://raw.githubusercontent.com/zilinskyjan/code-and-data/master/politics/CHES_Ukraine_Feb2024.csv")
@zilinskyjan
zilinskyjan / ces_gender_ideo.do
Created January 29, 2024 23:31
Ideological self-identification chart using the CES2022 data
* Get data from: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910/DVN/PR4L8P
use "CCES22_Common_OUTPUT_vv_topost.dta", clear
* net install cibar, from("http://fmwww.bc.edu/RePEc/bocode/c") replace
* ssc install schemepack, replace
gen age = 2022-birthyr
* Keeping the main categories (99.27% of observations)
my_pal <- rcartocolor::carto_pal(n = 7, name = "ag_Sunset")
col2 <- rcartocolor::carto_pal(n=7,name="Burg")
Bold <- rcartocolor::carto_pal(n=12,name="Bold")
Fall2 <- rcartocolor::carto_pal(n=2,name="Fall")
Geyser2 <- rcartocolor::carto_pal(n=2,name="Geyser")
library(showtext)
font_add_google("Outfit", "Outfit")
font_add_google("Cabin", "Cabin")
#font_add_google("Montserrat", "Montserrat")
@zilinskyjan
zilinskyjan / get_active_members_of_congress_twitter_handles.R
Created October 31, 2022 20:20
People who serve in the 117th Congress may have multiple Twitter accounts: get all of them with this script.
library(tidyverse)
allHandles <- read_csv("https://raw.githubusercontent.com/USATODAY/clustering-congress-twitter/main/data/source/cong_handles_master_and_ids08082022.csv")
cong117 <- allHandles %>% filter(c117th==1) %>%
select(
twitter_handle = twitter_lower,
state, district, party, chamber,
Official)
library(tidyverse)
library(haven)
library(labelled)
library(stargazer)
# Download data from: https://dataverse.harvard.edu/file.xhtml?fileId=4363671&version=1.0
d2 <- read_dta("Clean Data, Study 2.dta")
d2$college_grad <- ifelse(d2$edu >= 5,1,0)
* Download Nationscape data
* [either make a cumulative file, or just use Wave 47, June 2020]
* URL: https://www.voterstudygroup.org/publication/nationscape-data-set
gen Men = (gender==2) if !mi(gender)
recode age (18/29=1 "Age: 18-29 years") (30/44=2 "30-44 years") (45/64=3 "45-64 years") (65/110=4 "65+ years"), gen(age_group)
gen registered = (registration==1) if !mi(registration)
gen intendsToVote = (vote_intention==1) if !mi(vote_intention)
@zilinskyjan
zilinskyjan / cbs2016_winner.do
Created September 23, 2020 05:34
2016_winner_expectations
* Get data from Roper center
* Downloadable from: https://ropercenter.cornell.edu/ipoll/study/31114169
use "31114169.DTA"
* Table:
tab hrctrump WIN16 [aw=weight], row nof
* 89% of Clinton voters and 61% of third-party voters expected a Clinton victory
@zilinskyjan
zilinskyjan / obama_voters_CCES_panel_2010_2014.do
Last active November 5, 2019 01:54
How many 2008 Obama voters voted for a Republican congressional candidate in 2010?
* Question: how many 2008 Obama voters voted
* for a Republican congressional candidate in 2010 and then
* voted for Obama in '12?
*
* Asked by Dan Pfeiffer here https://twitter.com/danpfeiffer/status/1191492977429893120
* Step 1: Download the 2010-2014 Cooperative Congressional Election Study Panel Survey
* from https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi%3A10.7910/DVN/TOE8I1
use "CCES_Panel_Full3waves_VV_V4.dta", clear