Skip to content

Instantly share code, notes, and snippets.

View briatte's full-sized avatar

François Briatte briatte

View GitHub Profile
@briatte
briatte / Briatte_Kelbel-moment_du_choix-2023-03-02.do
Created May 20, 2024 18:03
Stata code for François Briatte, Camille Kelbel, ‘‘C‘est quand qu‘on va où ?’ Indécision et moment du choix de vote en 2022,” in Vincent Tiberj et al. (eds), Citoyens et partis après 2022. Éloignement, fragmentation, Paris, Presses Universitaires de France, 2024, pp.285-302.
// -----------------------------------------------------------------------------
// DEPENDENCIES
// -----------------------------------------------------------------------------
// minimum version for `import spss`
version 16
// version used for estimation
version 17
@briatte
briatte / dark_base16-tomorrow-night.rstheme
Created April 26, 2020 19:29 — forked from amandeepjutla/dark_base16-tomorrow-night.rstheme
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;
}
# triangle.py
import math
def triangle_perimeter(a, b, c):
return a + b + c
def triangle_area(a, b, c):
p = triangle_perimeter(a, b, c) / 2
x = math.sqrt(p * (p - a) * (p - b) * (p - c))
return round(x, 2)
# package dependency
library(haven)
path_in = "new_EB with more var_2004-2016.dta"
out_tsv = "new_EB.tsv"
out_sav = "new_EB.sav"
# name repair shows the issue with _merge
x = haven::read_dta(p, .name_repair = "universal")
names(x)[ names(x) == "._merge" ] = "MERGEVAR"
# change of PRNG in R >= 3.6.0
# use former PRNG
RNGkind(sample.kind = "Rounding")
# use new/fixed PRNG
RNGkind(sample.kind = "default")
# see ?Random for details
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
##
## download all `ergm` sources from v3.0-1 to today
##
library(rvest)
d <- "ergm-sources"
dir.create(d, showWarnings = FALSE)
# current source
@briatte
briatte / gist:ad24bb323d2603dbdd3af35086147c83
Created May 18, 2019 05:43 — forked from conormm/r-to-python-data-wrangling-basics.md
R to Python: Data wrangling with dplyr and pandas
R to python useful data wrangling snippets
The dplyr package in R makes data wrangling significantly easier.
The beauty of dplyr is that, by design, the options available are limited.
Specifically, a set of key verbs form the core of the package.
Using these verbs you can solve a wide range of data problems effectively in a shorter timeframe.
Whilse transitioning to Python I have greatly missed the ease with which I can think through and solve problems using dplyr in R.
The purpose of this document is to demonstrate how to execute the key dplyr verbs when manipulating data using Python (with the pandas package).
dplyr is organised around six key verbs
@briatte
briatte / pairs_plot.R
Created May 8, 2019 13:40 — forked from expersso/pairs_plot.R
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))
clear
input str500 x
"-73.974651 40.73868,-73.973391 40.73897,-73.97323 40.73955,-73.97298 40.7415304,-73.97247 40.7423406,-73.97229 40.7428504,-73.97213 40.74338,-73.971871 40.7440804,-73.97141 40.7446706,-73.97103 40.7455605,-73.970301 40.7465204,-73.96929 40.7472"
"-73.970301 40.7465204,-73.96929 40.7472"
end
// keep only the first pair of coordinates
replace x = regexr(x, ",.*", "")
// split by space, converting to numeric